gpt4 book ai didi

javascript - JQuery Gauge,如何通过json设置值和颜色?

转载 作者:行者123 更新时间:2023-11-27 23:42:57 27 4
gpt4 key购买 nike

在仪表图中,使用 jquery 插件。
此时,我正在设置固定值,但我需要通过 json 存档设置值和颜色。我该如何制作?

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=1024, maximum-scale=1">
<link href="../jquery-gauge.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="gauge1 demo1"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"> </script>
<script type="text/javascript" src="../jquery-gauge.min.js"></script>
<script>

$('.gauge1').gauge({
values: {
0 : '0',
25: '25',
50: '50',
75: '75',
100: '100'
},
colors: {
0 : 'green',
30: 'orange',
40: 'red'
},
angles: [
180,
360
],
lineWidth: 10,
arrowWidth: 5,
arrowColor: 'black',
inset:false,

value: 50
});
</script>
</body>
</html>

最佳答案

您必须使用 Ajax 调用加载 JSON 文件的内容。

由于您已经有了 jQuery,因此您可以简单地执行以下操作:

$.getJSON( "path/to/file.json", function( options ) {
$(".gauge1").gauge(options);
});

您的 file.json 应包含以下内容(或类似内容),以免产生错误:

{
"values": {
"0": '0',
"25": '25',
"50": '50',
"75": '75',
"100": '100'
},
"colors": {
"0": 'green',
"30": 'orange',
"40": 'red'
},
"angles": [
180,
360
],
"lineWidth": 10,
"arrowWidth": 5,
"arrowColor": 'black',
"inset":false,
"value": 50
}

编辑(针对您的评论):optionsfile.json内容。您可能只有值和颜色,并固定其他参数,如下所示:

文件.json

{
"values": {
"0": '0',
"25": '25',
"50": '50',
"75": '75',
"100": '100'
},
"colors": {
"0": 'green',
"30": 'orange',
"40": 'red'
}
}

在你的脚本中:

$.getJSON( "path/to/file.json", function( options ) {
$(".gauge1").gauge({
values: options.values, // uses values from your file.json
colors: options.colors, // uses colors from your file.json
// uses fixed parameters
angles: [
180,
360
],
lineWidth: 10,
arrowWidth: 5,
arrowColor: 'black',
inset:false,
value: 50
});
});

关于javascript - JQuery Gauge,如何通过json设置值和颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33504338/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com