gpt4 book ai didi

javascript - 如何根据 Highcharts 中的值更改仪表颜色

转载 作者:行者123 更新时间:2023-11-28 14:38:43 25 4
gpt4 key购买 nike

我在 highcharts 中有一个仪表图表,在这里我想根据旋转时的值更改针的颜色。

下面是我的代码:

$(function() {

$('#container').highcharts({
chart: {
type: 'gauge',
},
title: {
text: 'PV Generation'
},
tooltip: {
enabled: false
},
pane: {
center: ['50%', '55%'],
size: '75%',
startAngle: -100,
endAngle: 100,
background: {
backgroundColor: '#aaaaaa',
innerRadius: '95%',
outerRadius: '100%',
shape: 'arc',
},
},

yAxis: [{
lineWidth: 0,
min: 0,
max: 900,
tickInterval: 50,
tickPosition: 'outside',
minorTickPosition: 'outside',
tickLength: 15,
minorTickLength: 5,
labels: {
distance: 25,
},
offset: 5,
endOnTick: false,
title: {
y: -70
},

plotOptions: {
solidgauge: {
dataLabels: {
y: 5,
borderWidth: 0,
useHTML: true
}
}
},
plotBands: [{
from: 0,
to: 250,
color: '#55BF3B' // green
}, {
from: 250,
to: 700,
color: '#DDDF0D', // yellow
series: [{
data: [{
id: 'hour',
y: 400,
yAxis: 0,
dial: {
backgroundColor: '#000000',
color: 'red',
radius: '100%',
baseWidth: 10,
baseLength: '5%',
baseWidth: 15,
rearLength: '0%',
}
}]
}]
}, {
from: 700,
to: 900,
color: '#DF5353' // red
}]

}],
series: [{
name: 'Speed',
data: [{
y: 450
}]
}]
},
// Add some life
function(chart) {
setInterval(function() {
var point = chart.series[0].points[0],
newVal,
inc = Math.round((Math.random() - 0.5) * 20);

newVal = point.y + 4 * inc;
if (newVal < 0 || newVal > 900) {
newVal = point.y - 4 * inc;
}

chart.yAxis[0].removePlotBand('plot-band-1');
chart.yAxis[0].addPlotBand({
from: 0,
to: newVal,
// color: '#000000',
thickness: '10%',
id: 'plot-band-1'
});
point.update(newVal);
}, 4000);

});


$("#container").find("circle").attr("r", 15);
$("#container").find("circle").attr("fill", "red");
// $("#container").find("series").data("fill", "black");
});
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

最佳答案

您可以通过更改更新代码的某些部分来实现此目的。我已完成以下操作:

删除point.update(newVal)

添加一个更新,根据值是增加还是减少来更新针、枢轴和值:

if (inc > 0) {
chart.series[0].update({
dial: {
backgroundColor: 'green',
},
pivot: {
backgroundColor: 'green'
},
data: [{
y: newVal
}]
}, true);
} else {
chart.series[0].update({
dial: {
backgroundColor: 'red',
},
pivot: {
backgroundColor: 'red'
},
data: [{
y: newVal
}]
}, true);
}

此外,我删除了这个:

$("#container").find("circle").attr("r", 15);
$("#container").find("circle").attr("fill", "red");

而是将其放入系列定义中:

series: [{
name: 'Speed',
data: [{
y: 450
}],
pivot: {
radius: 15,
backgroundColor: 'red'
}
}]

这样它就是一个 highchart 属性,并且当我们更新系列时不会被覆盖。

API 引用: series.gaugeHighcharts.update

<小时/>

完整工作示例:

$(function() {

$('#container').highcharts({
chart: {
type: 'gauge',
},
title: {
text: 'PV Generation'
},
tooltip: {
enabled: false
},
pane: {
center: ['50%', '55%'],
size: '75%',
startAngle: -100,
endAngle: 100,
background: {
backgroundColor: '#aaaaaa',
innerRadius: '95%',
outerRadius: '100%',
shape: 'arc',
},
},
yAxis: [{
lineWidth: 0,
min: 0,
max: 900,
tickInterval: 50,
tickPosition: 'outside',
minorTickPosition: 'outside',
tickLength: 15,
minorTickLength: 5,
labels: {
distance: 25,
},
offset: 5,
endOnTick: false,
title: {
y: -70
},
plotOptions: {
solidgauge: {
dataLabels: {
y: 5,
borderWidth: 0,
useHTML: true
}
}
},
plotBands: [{
from: 0,
to: 250,
color: '#55BF3B' // green
}, {
from: 250,
to: 700,
color: '#DDDF0D', // yellow
series: [{
data: [{
id: 'hour',
y: 400,
yAxis: 0,
dial: {
backgroundColor: '#000000',
color: 'red',
radius: '100%',
baseWidth: 10,
baseLength: '5%',
baseWidth: 15,
rearLength: '0%',
}
}]
}]
}, {
from: 700,
to: 900,
color: '#DF5353' // red
}]
}],
series: [{
name: 'Speed',
data: [{
y: 450
}],
pivot: {
radius: 15,
backgroundColor: 'red'
}
}]
},
// Add some life
function(chart) {
setInterval(function() {
var point = chart.series[0].points[0],
newVal,
inc = Math.round((Math.random() - 0.5) * 20);
newVal = point.y + 4 * inc;
if (newVal < 0 || newVal > 900) {
newVal = point.y - 4 * inc;
}
chart.yAxis[0].removePlotBand('plot-band-1');
chart.yAxis[0].addPlotBand({
from: 0,
to: newVal,
// color: '#000000',
thickness: '10%',
id: 'plot-band-1'
});
if (inc > 0) {
chart.series[0].update({
dial: {
backgroundColor: 'green',
},
pivot: {
backgroundColor: 'green'
},
data: [{
y: newVal
}]
}, true);
} else {
chart.series[0].update({
dial: {
backgroundColor: 'red',
},
pivot: {
backgroundColor: 'red'
},

data: [{
y: newVal
}]
}, true);
}
}, 4000);

});


//$("#container").find("circle").attr("r", 15);
//$("#container").find("circle").attr("fill", "red");
// $("#container").find("series").data("fill", "black");
});
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

<小时/>

编辑以处理每种颜色的范围:

0 - 250:绿色250-700:黄色700-900:红色

$(function() {

$('#container').highcharts({
chart: {
type: 'gauge',
},
title: {
text: 'PV Generation'
},
tooltip: {
enabled: false
},
pane: {
center: ['50%', '55%'],
size: '75%',
startAngle: -100,
endAngle: 100,
background: {
backgroundColor: '#aaaaaa',
innerRadius: '95%',
outerRadius: '100%',
shape: 'arc',
},
},
yAxis: [{
lineWidth: 0,
min: 0,
max: 900,
tickInterval: 50,
tickPosition: 'outside',
minorTickPosition: 'outside',
tickLength: 15,
minorTickLength: 5,
labels: {
distance: 25,
},
offset: 5,
endOnTick: false,
title: {
y: -70
},
plotOptions: {
solidgauge: {
dataLabels: {
y: 5,
borderWidth: 0,
useHTML: true
}
}
},
plotBands: [{
from: 0,
to: 250,
color: '#55BF3B' // green
}, {
from: 250,
to: 700,
color: '#DDDF0D', // yellow
series: [{
data: [{
id: 'hour',
y: 400,
yAxis: 0,
dial: {
backgroundColor: '#000000',
color: 'red',
radius: '100%',
baseWidth: 10,
baseLength: '5%',
baseWidth: 15,
rearLength: '0%',
}
}]
}]
}, {
from: 700,
to: 900,
color: '#DF5353' // red
}]
}],
series: [{
name: 'Speed',
data: [{
y: 450
}],
dial: {
backgroundColor: '#DDDF0D',
},
pivot: {
radius: 15,
backgroundColor: '#DDDF0D'
}
}]
},
// Add some life
function(chart) {
setInterval(function() {
var point = chart.series[0].points[0],
newVal,
inc = Math.round((Math.random() - 0.5) * 20);
newVal = point.y + 4 * inc;
if (newVal < 0 || newVal > 900) {
newVal = point.y - 4 * inc;
}
chart.yAxis[0].removePlotBand('plot-band-1');
chart.yAxis[0].addPlotBand({
from: 0,
to: newVal,
// color: '#000000',
thickness: '10%',
id: 'plot-band-1'
});
if (newVal < 250) {
chart.series[0].update({
dial: {
backgroundColor: 'green',
},
pivot: {
backgroundColor: 'green'
},
data: [{
y: newVal
}]
}, true);
} else if(newVal > 250 && newVal < 700){
chart.series[0].update({
dial: {
backgroundColor: '#DDDF0D',
},
pivot: {
backgroundColor: '#DDDF0D'
},
data: [{
y: newVal
}]
}, true);
} else {
chart.series[0].update({
dial: {
backgroundColor: 'red',
},
pivot: {
backgroundColor: 'red'
},

data: [{
y: newVal
}]
}, true);
}
}, 4000);

});


//$("#container").find("circle").attr("r", 15);
//$("#container").find("circle").attr("fill", "red");
// $("#container").find("series").data("fill", "black");
});
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

关于javascript - 如何根据 Highcharts 中的值更改仪表颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48887510/

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