如何为实体仪表图定义 colorAxis 数据类?
How to define colorAxis dataClasses for solid gauge chart?
我正在尝试使用 colorAxis dataClasses 将实心仪表图设置为对范围使用定义的颜色,但它不起作用。不确定我做错了什么。
我应该如何定义 colorAxis 和 dataClasses?
请参阅 http://jsfiddle.net/edob/27oc38L1 更新!!! 我修改了 fiddle 以使用止损,它适用于定义为红色、绿色和不适用于十六进制颜色!试试 51、65 和 81
这样的值
$(function() {
Highcharts.chart('container-cpu', {
chart: {
type: 'solidgauge'
},
title: null,
pane: {
center: ['50%', '85%'],
size: '140%',
startAngle: -90,
endAngle: 90,
background: {
backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || '#EEE',
innerRadius: '60%',
outerRadius: '100%',
shape: 'arc'
}
},
tooltip: {
enabled: false
},
yAxis: {
min: 0,
max: 100,
title: {
text: 'CPU'
}
},
colorAxis: {
dataClasses: [{
from: 0,
to: 50,
color: 'red'
}, {
from: 50,
to: 90,
color: 'green'
}, {
from: 90,
color: 'yellow'
}]
},
series: [{
name: 'HDD',
data: [70]
}]
});
});
我看到任何值的默认蓝色,我希望看到值 70 的绿色。
要为 solidgauge 的值着色,您应该使用止损,如下所示:
yAxis: {
stops: [
[0.5, 'red'],
[0.9, 'green'],
[1, 'yellow']
],
min: 0,
max: 100,
title: {
text: 'CPU'
}
},
Solid gauge series only. Color stops for the solid gauge. Use this in cases where a linear gradient between a minColor and maxColor is not sufficient. The stops is an array of tuples, where the first item is a float between 0 and 1 assigning the relative position in the gradient, and the second item is the color.
工作 JSFiddle 示例: http://jsfiddle.net/ewolden/c8qy4x5o/1/
API 停靠点: https://api.highcharts.com/highcharts/yAxis.stops
既然你在评论中说你想使用纯色,而不是渐变;以下是您可以实现的一种方式:
chart: {
type: 'solidgauge',
events: {
load: function() {
var currentValue = this.series[0].points[0].y;
var newColor = '';
console.log(currentValue)
if(currentValue < 50) {
newColor = 'red'
} else if(currentValue < 90) {
newColor = 'green'
} else {
newColor = 'yellow'
}
this.series[0].points[0].update({color: newColor}, true)
}
}
},
工作示例: http://jsfiddle.net/ewolden/s9qrhtmb/
API 在 point.update: https://api.highcharts.com/class-reference/Highcharts.Point#update
您需要在 yAxis
中添加具有适当值的 stops
而不是 colorAxis
。我相信这就是您要找的 working example。正如我检查的那样,它没有创建渐变并且颜色是纯色的。
$(function() {
Highcharts.chart('container-cpu', {
chart: {
type: 'solidgauge'
},
title: null,
pane: {
center: ['50%', '85%'],
size: '140%',
startAngle: -90,
endAngle: 90,
background: {
backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || '#EEE',
innerRadius: '60%',
outerRadius: '100%',
shape: 'arc'
}
},
tooltip: {
enabled: false
},
yAxis: {
stops: [
[0.5, '#DF5353'], // red
[0.9, '#55BF3B'], // green
[1, '#DDDF0D'] // yellow
],
min: 0,
max: 100,
title: {
text: 'CPU'
}
},
series: [{
name: 'HDD',
data: [49]
}]
});
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>
<script src="https://code.highcharts.com/modules/solid-gauge.js"></script>
<div id="container-cpu" class="chart-container"></div>
我正在尝试使用 colorAxis dataClasses 将实心仪表图设置为对范围使用定义的颜色,但它不起作用。不确定我做错了什么。
我应该如何定义 colorAxis 和 dataClasses?
请参阅 http://jsfiddle.net/edob/27oc38L1 更新!!! 我修改了 fiddle 以使用止损,它适用于定义为红色、绿色和不适用于十六进制颜色!试试 51、65 和 81
这样的值 $(function() {
Highcharts.chart('container-cpu', {
chart: {
type: 'solidgauge'
},
title: null,
pane: {
center: ['50%', '85%'],
size: '140%',
startAngle: -90,
endAngle: 90,
background: {
backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || '#EEE',
innerRadius: '60%',
outerRadius: '100%',
shape: 'arc'
}
},
tooltip: {
enabled: false
},
yAxis: {
min: 0,
max: 100,
title: {
text: 'CPU'
}
},
colorAxis: {
dataClasses: [{
from: 0,
to: 50,
color: 'red'
}, {
from: 50,
to: 90,
color: 'green'
}, {
from: 90,
color: 'yellow'
}]
},
series: [{
name: 'HDD',
data: [70]
}]
});
});
我看到任何值的默认蓝色,我希望看到值 70 的绿色。
要为 solidgauge 的值着色,您应该使用止损,如下所示:
yAxis: {
stops: [
[0.5, 'red'],
[0.9, 'green'],
[1, 'yellow']
],
min: 0,
max: 100,
title: {
text: 'CPU'
}
},
Solid gauge series only. Color stops for the solid gauge. Use this in cases where a linear gradient between a minColor and maxColor is not sufficient. The stops is an array of tuples, where the first item is a float between 0 and 1 assigning the relative position in the gradient, and the second item is the color.
工作 JSFiddle 示例: http://jsfiddle.net/ewolden/c8qy4x5o/1/
API 停靠点: https://api.highcharts.com/highcharts/yAxis.stops
既然你在评论中说你想使用纯色,而不是渐变;以下是您可以实现的一种方式:
chart: {
type: 'solidgauge',
events: {
load: function() {
var currentValue = this.series[0].points[0].y;
var newColor = '';
console.log(currentValue)
if(currentValue < 50) {
newColor = 'red'
} else if(currentValue < 90) {
newColor = 'green'
} else {
newColor = 'yellow'
}
this.series[0].points[0].update({color: newColor}, true)
}
}
},
工作示例: http://jsfiddle.net/ewolden/s9qrhtmb/
API 在 point.update: https://api.highcharts.com/class-reference/Highcharts.Point#update
您需要在 yAxis
中添加具有适当值的 stops
而不是 colorAxis
。我相信这就是您要找的 working example。正如我检查的那样,它没有创建渐变并且颜色是纯色的。
$(function() {
Highcharts.chart('container-cpu', {
chart: {
type: 'solidgauge'
},
title: null,
pane: {
center: ['50%', '85%'],
size: '140%',
startAngle: -90,
endAngle: 90,
background: {
backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || '#EEE',
innerRadius: '60%',
outerRadius: '100%',
shape: 'arc'
}
},
tooltip: {
enabled: false
},
yAxis: {
stops: [
[0.5, '#DF5353'], // red
[0.9, '#55BF3B'], // green
[1, '#DDDF0D'] // yellow
],
min: 0,
max: 100,
title: {
text: 'CPU'
}
},
series: [{
name: 'HDD',
data: [49]
}]
});
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>
<script src="https://code.highcharts.com/modules/solid-gauge.js"></script>
<div id="container-cpu" class="chart-container"></div>