【发布时间】:2015-05-27 16:13:57
【问题描述】:
请你帮忙.. 我目前正在制作一个图表,其中有两条线,一条总需要线(黑色)和一条总收集线(红色或绿色)。我想在代码中添加一个 if 语句,用于识别该行是绿色还是红色。当它高于所需的总黑线时它应该是绿色的,当它低于它时应该是红色的。在我的代码中,我已经为给定场景的情况做出了规定。
$(function () {
$('#container').highcharts({
title: {
text: 'Total Contribution'
},
xAxis: {
categories: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
},
labels: {
items: [{
html: 'Student VS Total contribution',
style: {
left: '-10px',
top: '0px',
color: (Highcharts.theme && Highcharts.theme.textColor) || 'black'
}
}]
},
series: [{
type: 'column',
name: 'John',
tooltip: {
pointFormat: '<b>{series.name}</b>: R{point.y:.0f}'
},
data: [3000, 2000, 1000, 3000, 4000, 6000, 7000, 8000, 7000, 9000, 6000, 9000]
},
{
type: 'line',
name: 'Total Needed',
lineColor: Highcharts.getOptions().colors[1],
tooltip: {
pointFormat: '<b>{series.name}</b>: R{point.y:.0f}'
},
data: [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 11000, 12000],
marker: {
lineWidth: 2,
lineColor: Highcharts.getOptions().colors[1],
fillColor: 'white'
}
}, {
type: 'line',
name: 'Total Collected - Good example',
lineColor: Highcharts.getOptions().colors[6],
tooltip: {
pointFormat: '<b>{series.name}</b>: R{point.y:.0f}'
},
data: [3000, 5000, 7000, 7000, 9000, 10500, 11000, 12000, 12000, 13000, 14000, 14000],
marker: {
lineWidth: 2,
lineColor: Highcharts.getOptions().colors[6],
fillColor: 'white'
}
}, {
type: 'line',
name: 'Total Collected - BAD example', // The data in the columns does not match - it is an example
lineColor: Highcharts.getOptions().colors[8],
tooltip: {
pointFormat: '<b>{series.name}</b>: R{point.y:.0f}'
},
data: [1000, 1000, 2000, 2000, 3000, 3000, 4500, 4500, 6000, 7000, 7000, 8000],
marker: {
lineWidth: 2,
lineColor: Highcharts.getOptions().colors[8],
fillColor: 'white'
}
},
{
type: 'pie',
name: 'Contribution',
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.2f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.0f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
}
},
data: [{
name: 'John',
y: 25,
color: Highcharts.getOptions().colors[0] // John's color
}, {
name: 'Total',
y: 75,
color: Highcharts.getOptions().colors[7] // Other's color
}],
center: [30, 45],
size: 100,
showInLegend: false,
dataLabels: {
enabled: false,
format: '<b>{point.name}</b>: {point.percentage:.0f} %'
}
}]
});
});
【问题讨论】:
-
这取决于 - 您的意思是在每种情况下都希望整条线为红色/绿色,还是希望线在高于/低于所需值时改变颜色?如果你想改变它,它会变得更加困难,你需要查看多个系列,并决定对于低于阈值的数据点实际改变颜色的线的哪一部分。如果红线始终是红色的,只需对数据进行预处理并预先设置颜色即可。
-
图表将用作预算工具...我认为只要其中一条线的整体数据处于高于或低于总需要线的状态,它应该反映一种颜色?
-
您需要确定如何定义“整体数据”高于或低于。完成后,首先对数据集进行计算,并将颜色定义为一个变量,然后您可以在图表代码中调用该变量。
标签: javascript if-statement colors highcharts