javascript - Highcharts.js 设置更改列颜色的阈值

标签 javascript jquery svg highcharts

我想知道是否有人可以帮助我调整这个 Highcharts 折线图:http://jsfiddle.net/highcharts/PMyHQ/柱形图同时仍保持阈值着色(红色表示值>0;蓝色表示值<0)。

问题是,如果您只是更改图形类型,type:'column',那么基于阈值的着色就会丢失,所有列都会变成蓝色。

进行颜色变化的函数是applyGraphGradient()。 我不知道如何更改此函数以保留基于阈值的着色。

/**
 * Event handler for applying different colors above and below a threshold value. 
 * Currently this only works in SVG capable browsers. A full solution is scheduled
 * for Highcharts 3.0. In the current example the data is static, so we don't need to
 * recompute after altering the data. In dynamic series, the same event handler 
 * should be added to yAxis.events.setExtremes and possibly other events, like
 * chart.events.resize.
 */
function applyGraphGradient() {

    // Options
    var threshold = 0,
        colorAbove = '#EE4643',
        colorBelow = '#4572EE';

    // internal
    var series = this.series[0], 
        i,
        point;

    if (this.renderer.box.tagName === 'svg') {

        var translatedThreshold = series.yAxis.translate(threshold),
            y1 = Math.round(this.plotHeight - translatedThreshold),
            y2 = y1 + 2; // 0.01 would be fine, but IE9 requires 2

        // Apply gradient to the path
        series.graph.attr({
            stroke: {
                linearGradient: [0, y1, 0, y2],
                stops: [
                    [0, colorAbove],
                    [1, colorBelow]
                ]
            }
         });


    }

    // Apply colors to the markers
    for (i = 0; i < series.data.length; i++) {
        point = series.data[i];
        point.color = point.y < threshold ? colorBelow : colorAbove;
        if (point.graphic) {
            point.graphic.attr({
                fill: point.color
            });
        }
    }

    // prevent the old color from coming back after hover
    delete series.pointAttr.hover.fill;
    delete series.pointAttr[''].fill;

}

// Initiate the chart
var chart = new Highcharts.Chart({

    chart: {
        renderTo: 'container',
        events: {
            load: applyGraphGradient
        },
        defaultSeriesType : 'column'
    },

    title: {
        text: 'Average monthly temperature'
    },

    yAxis: {
        plotLines: [{
            value: 0,
            color: 'silver',
            width: 2,
            zIndex: 2
        }],
        title: {
            text: 'Temperature (°C)'
        }
    },

    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },

    series: [{
        name: 'Temperature (°C)',
        data: [-2, -3, -2, 2, 5, 9, 11, 11, 10, 8, 4, -1]
    }]

});
​

最佳答案

使用 Highcharts 绘图 API,它也可以在 VML 浏览器中工作:http://jsfiddle.net/highcharts/XndxH/

关于javascript - Highcharts.js 设置更改列颜色的阈值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12180773/

相关文章:

javascript - 有没有办法检测何时通过 jquery 设置值?

jquery - 如何从 DIV 中删除特定标签?

javascript - 更改图像/svg 以在网页上正确显示横向或纵向

reactjs - 如何在material ui SVG ICON中使用自定义SVG文件

php - EventSource 与轮询 ajax 的效率/开销?

javascript - UAT 环境中 url 未定义的奇怪问题。在本地它正在工作 :

javascript - 将mysql数据库的php查询搜索限制为非空行

php - 添加更多(组)字段以通过 jQuery 形成

javascript - 使用 jQuery 上传文件在 IE 中不起作用

javascript - 如果另一个实例被隐藏,SVG 将变成黑框