与色的backgroundcolumns的Highcharts

时间:2014-07-06 08:26:36

标签: javascript highcharts

是否可以获得如图所示的图表。主要目标是为每个时间单元提供不同的背景颜色,例如为图表中的每一天提供一种颜色....

Graph background color example

3 个答案:

答案 0 :(得分:1)

使用xAxis.plotbands完成垂直背景颜色(或垂直yAxis.plotbands),如下所示:

xAxis: {
    plotBands: [
        {
            from: ${x1}, 
            to: ${x2}, 
            color: ${color}
        },
    ],
    // more configuration...
}



$(function () {
    $('#container').highcharts({
        title: {
            text: 'Monthly Average Temperature',
        },
        subtitle: {
            text: 'Source: WorldClimate.com',
        },
        xAxis: {
            type: 'linear',
            tickPositions: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ],
            plotBands: [
                { from: 1, to: 2, color: 'rgba(127, 255, 127, 0.5)' },
                { from: 2, to: 3, color: 'rgba(255, 255, 63, 0.5)' },
                { from: 3, to: 4, color: 'rgba(255, 127, 127, 0.5)' },
                { from: 4, to: 6, color: 'rgba(255, 255, 63, 0.5)' },
                { from: 6, to: 7, color: 'rgba(255, 127, 127, 0.5)' },
                { from: 7, to: 8, color: 'rgba(127, 255, 127, 0.5)' },
                { from: 8, to: 9, color: 'rgba(255, 255, 63, 0.5)' },
                { from: 9, to: 10, color: 'rgba(255, 127, 127, 0.5)' },
                { from: 10, to: 12, color: 'rgba(127, 255, 127, 0.5)' },
            ],
            labels: {
                formatter: function() {
                    switch(parseInt(this.value)) {
                        case 1: return 'Jan';
                        case 2: return 'Feb';
                        case 3: return 'Mar';
                        case 4: return 'Apr';
                        case 5: return 'May';
                        case 6: return 'Jun';
                        case 7: return 'Jul';
                        case 8: return 'Aug';
                        case 9: return 'Sep';
                        case 10: return 'Oct';
                        case 11: return 'Nov';
                        case 12: return 'Dec';
                        default: return 'NaM(' + this.value + ')';
                    }
                }
            }
        },
        yAxis: {
            title: {
                text: 'Temperature (°C)'
            }
        },
        series: [
            {
                type: 'line', // or 'spline'
                name: 'Air Temperature',
                data: [
                    { x: 1,  y: 7.0 },
                    { x: 2,  y: 6.9 },
                    { x: 3,  y: 9.5 },
                    { x: 4,  y: 14.5 },
                    { x: 5,  y: 18.4 },
                    { x: 6,  y: 21.5 },
                    { x: 7,  y: 25.2 },
                    { x: 8,  y: 26.5 },
                    { x: 9,  y: 23.3 },
                    { x: 10, y: 18.3 },
                    { x: 11, y: 13.9 },
                    { x: 12, y: 9.6 }
                ]
            }, 
            {
                name: 'Water Temperature',
                data: [
                    { x: 1,  y: 3.9 },
                    { x: 2,  y: 4.2 },
                    { x: 3,  y: 5.7 },
                    { x: 4,  y: 8.5 },
                    { x: 5,  y: 11.9 },
                    { x: 6,  y: 15.2 },
                    { x: 7,  y: 17.0 },
                    { x: 8,  y: 16.6 },
                    { x: 9,  y: 14.2 },
                    { x: 10, y: 10.3 },
                    { x: 11, y: 6.6 },
                    { x: 12, y: 4.8 }
                ]
        	}
        ],
        plotOptions: {
            series: {
                dataLabels: {
                    enabled: true
                },
                enableMouseTracking: false
            }
        },
    });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<div id="container" style="height: 450px"></div>
&#13;
&#13;
&#13;

enter image description here

只要您使用线性值,就可以设置任何分辨率。 (出于格式化的目的,我建议使用线性整数值而不是日期,以便更容易地定义/生成服务器端格式化。

答案 1 :(得分:0)

在highcharts中,您可以使用Renderer来添加自定义形状。

答案 2 :(得分:-1)

这在Highcharts中没有直接的支持,据我所知,时间单位列不容易通过JS或CSS获得。

但是,您可以访问情节和plotBackgroundColor。如果您事先知道时间单位列的数量,这可能是您的解决方案。将plotBackgroundColor设置为使用linearGradient可能会让人觉得每列都有不同的颜色。

演示:JFiddle

正如您所看到的,您必须为每列使用两个stops并计算出每个列所需的百分比宽度。