因此,如果值在某个范围内,我想更改单个条的颜色。我如何在Javascript中执行此操作?我真的不想弄乱我的JSON数据。
var chart = AmCharts.makeChart( "chartdiv", {
"type": "serial",
"theme": "none",
"dataProvider": [ {
"country": "USA",
"visits": 2025
}, {
"country": "China",
"visits": 1882
}, {
"country": "Japan",
"visits": 1809
}, {
"country": "Germany",
"visits": 1322
}, {
"country": "UK",
"visits": 1122
}, {
"country": "France",
"visits": 1114
}, {
"country": "India",
"visits": 984
}, {
"country": "Spain",
"visits": 711
}, {
"country": "Netherlands",
"visits": 665
}, {
"country": "Russia",
"visits": 580
}, {
"country": "South Korea",
"visits": 443
}, {
"country": "Canada",
"visits": 441
}, {
"country": "Brazil",
"visits": 395
} ],
"valueAxes": [ {
"gridColor": "#FFFFFF",
"gridAlpha": 0.2,
"dashLength": 0
} ],
"gridAboveGraphs": true,
"startDuration": 1,
"graphs": [ {
"balloonText": "[[category]]: <b>[[value]]</b>",
"fillAlphas": 0.8,
"lineAlpha": 0.2,
"fillColors": changeColour,
"type": "column",
"valueField": "visits"
} ],
"chartCursor": {
"categoryBalloonEnabled": false,
"cursorAlpha": 0,
"zoomable": false
},
"categoryField": "country",
"categoryAxis": {
"gridPosition": "start",
"gridAlpha": 0,
"tickPosition": "start",
"tickLength": 20
},
"export": {
"enabled": true
}
} );
function changeColour(value){
/*var colour;
if (0 <= value && value <= 40){
colour = "#ff3333";
} else if (41 <= value && value <= 66){
colour = "#ff751a";
} else if (value > 66){
colour = "#009933";
}*/
//return colour;
return "#ff3333";
}
我创建了一个函数,如果值在某个数字范围内,我试图返回颜色,但这不起作用。
这是一个我正在弄清楚如何做到这一点的例子。 https://jsfiddle.net/ckylec4/8to7hghj/2/
答案 0 :(得分:0)
图表不支持自定义函数作为颜色的格式化程序。
可能唯一的解决方案是预处理您的数据,并为每个数据点设置fillColorsField
。
AmCharts.addInitHandler()
似乎是所有预处理的完美工具。
让我们“发明”我们将使用的新参数fillColorsFunction
。我们可以重用您的函数changeColour
:
"graphs": [{
"balloonText": "[[category]]: <b>[[value]]</b>",
"fillAlphas": 0.8,
"lineAlpha": 0.2,
"fillColorsFunction": changeColour,
"type": "column",
"valueField": "visits"
}],
现在,让我们创建一个将使用它的“插件”:
/**
* Plugin: checks for graphs with "fillColorsFunction"
*/
AmCharts.addInitHandler(function(chart) {
// iterate thorugh all graphs and check if they have
// fillColorsFunction set
for (i = 0; i < chart.graphs.length; i++) {
var graph = chart.graphs[i];
if (graph.fillColorsFunction !== undefined) {
// iterate thorugh all of the data and add color
graph.fillColorsField = graph.valueField + "FillColor";
for (var x = 0; x < chart.dataProvider.length; x++) {
var dp = chart.dataProvider[x];
dp[graph.fillColorsField] = graph.fillColorsFunction.call(this, dp[graph.valueField]);
}
}
}
}, ["serial"]);
这是整个工作的东西: http://codepen.io/team/amcharts/pen/963bb83fa1d3c146cd337b981e6c9a11
如果您使用的是Data Loader插件,则可以将相同的代码移入其complete
处理程序中:
"dataLoader": {
"url": "data.php",
"complete": function( chart ) {
// iterate thorugh all graphs and check if they have
// fillColorsFunction set
for ( i = 0; i < chart.graphs.length; i++ ) {
var graph = chart.graphs[ i ];
if ( graph.fillColorsFunction !== undefined ) {
// iterate thorugh all of the data and add color
graph.fillColorsField = graph.valueField + "FillColor";
for ( var x = 0; x < chart.dataProvider.length; x++ ) {
var dp = chart.dataProvider[ x ];
dp[ graph.fillColorsField ] = graph.fillColorsFunction.call( this, dp[ graph.valueField ] );
}
}
}
}
}