简体   繁体   English

highcharts:柱形图基于值的颜色变化

[英]highcharts: column chart color change based on value

I am creating a column type chart using highcharts. 我正在使用highcharts创建列类型图表。 My data has two dimensions, the second dimension shows the severity of the first dimension and range from 0 to 100. 我的数据有两个维度,第二个维度显示第一个维度的严重性,范围从0到100。

data:{(234,45),(55645,7),(964,97),...}

i need my column height to show the first dimension of data, and dinamically change the color of each column based on the severity(second dimension).something like heatmap color but the color change based on another sets of data. 我需要我的列高度来显示数据的第一维,并根据严重性(第二维)来动态更改每列的颜色。类似热图颜色,但是颜色会基于另一组数据而变化。

像这样的东西

Any suggestions how i can do this? 任何建议我该怎么做?

Thanks in Advance 提前致谢

I'll show an approach I think you can adapt. 我将展示一种我认为您可以适应的方法。 Lets create a series and set a default color for it (which I assume to be the color for zero severity): 让我们创建一个系列并为其设置默认颜色(我认为这是严重性为零的颜色):

series: [{
    color: '#ffff00', // yellow
    data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1]
}]

To color depending on severity, we need some values and a function to calculate the new color. 要根据严重程度进行着色,我们需要一些值和一个函数来计算新颜色。 I'm going to assume you'll have an array of the severity values outside the chart, which correspond to the series point indexes. 我将假设您在图表外部将具有一系列严重性值,这些严重性值与序列点索引相对应。 For example: 例如:

severity = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110];
min = Math.min(...severity);
max = Math.max(...severity);
maxColor = new Highcharts.Color('#ff0000'); // red

calculateColor = function(inputColor, min, max, value) {
    color = new Highcharts.Color(inputColor);
    interval = max - min;
    adjustedValue = value - min;
    alpha = adjustedValue / interval;
    return color.tweenTo(maxColor, alpha);
}

Using the calculateColor function with an input color, the min and max of the severity along with the specific severity value should now return a severity-adjusted color. 现在,通过对输入颜色使用calculateColor函数,严重性的最小值和最大值以及特定的严重性值现在应返回经过严重性调整的颜色。

To demonstrate, we update the series colors using this function: 为了演示,我们使用以下功能更新系列颜色:

for(i = 0; i < this.series[0].data.length; i++) {
    this.series[0].data[i].update({
        color: calculateColor(this.series[0].color, min, max, severity[i])
    }, false);
};

this.redraw();

See this complete JSFiddle demonstration of it in action. 观看此完整的JSFiddle演示实际操作。

In a simple way, you can use colorAxis from 'heatmap.js' module: 您可以通过一种简单的方式从'heatmap.js'模块使用colorAxis

var columnProto = Highcharts.seriesTypes.column.prototype;

columnProto.axisTypes = ['xAxis', 'yAxis', 'colorAxis'];
columnProto.optionalAxis = 'colorAxis';
columnProto.colorKey = 'y';

Highcharts.wrap(columnProto, 'translate', function(proceed) {
    proceed.apply(this, Array.prototype.slice.call(arguments, 1));

    Highcharts.seriesTypes.heatmap.prototype.translateColors.call(this);
});

Highcharts.chart('container', {
    series: [{
        data: [1, 2, 3, 4, 5, 6],
        type: 'column'
    }],
    colorAxis: {
        minColor: '#c6e48b',
        maxColor: '#196127',
        min: 1,
        max: 6,
        gridLineWidth: 0
    }
});

Live demo: http://jsfiddle.net/BlackLabel/szv2g3u7/ 现场演示: http : //jsfiddle.net/BlackLabel/szv2g3u7/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM