简体   繁体   English

高亮区域范围图表,其后跟随线条的渐变

[英]Highchart Area Range chart with gradient that follows the line

The following is an Area Range chart which has a gradient. 以下是具有渐变的区域范围图表。

http://jsfiddle.net/gXpHH/1/ http://jsfiddle.net/gXpHH/1/

What I want to do is have the gradient follow the curve, so that the color at any point along the bottom line is the same, and it ends at the same color when it reaches the top line. 我想要做的是让渐变跟随曲线,使得沿底线的任何点的颜色都是相同的,并且当它到达顶线时它以相同的颜色结束。 I am pretty sure that this is not currently an option in highcharts, but I just wanted to see if anyone has run into this before. 我很确定这在高级图表中目前不是一个选项,但我只是想知道是否有人之前遇到过此问题。 Here is the code that currently generates the gradient: 以下是当前生成渐变的代码:

series: [{
            name: "Shade",
            fillColor: {
                linearGradient: [0, 0, 0, 300],
                stops: [
                    [0, "#4572A7"],
                    [1, "rgba(2,0,0,0)"]
                ]
            },
            data: [
                [0, 14733, 18890],
                [1, 16583, 21642],
                //... rest here
                [10, 42417, 61955]
            ]
    }]

Thanks 谢谢

Note : this is not the same as Gradient Fill on Line Chart (Highcharts) since I need the gradient to follow the curve 注意 :这与线图(Highcharts)上的渐变填充不同,因为我需要跟随曲线的渐变

The difficulty is the granularity: Highcharts draws one single SVG path for the series' points, and associates that one path with the gradient. 难点在于粒度:Highcharts为系列的点绘制一条SVG路径,并将一条路径与渐变相关联。 If your series data is relatively linear however, the following approximation might be useful. 但是,如果您的系列数据是相对线性的,则以下近似可能很有用。 See the jsfiddle that I've created : 看看我创建jsfiddle

Assuming that your series data is not static, my demo includes two series and a function that, for each series, attempts to dynamically create the linearGradient that comes closest to your requirement: 假设您的系列数据不是静态的,我的演示包括两个系列和一个函数,对于每个系列,它会尝试动态创建最接近您要求的linearGradient:

function getLinearGradient(series) {
    var lastY0=null, lastY1=null, maxY0=null, maxY1=null;
    $.each(series.data, function(key,value) {
        maxY0 = Math.max(value[1],maxY0);
        maxY1 = Math.max(value[2],maxY1);
        lastY0 = value[1];
        lastY1 = value[2];
    });
    var firstY0 = series.data[0][2],
        firstY1 = series.data[0][2]
    ;
    var minY0=maxY0, minY1=maxY1;
    $.each(series.data, function(key,value) {
        minY0 = Math.min(value[1],minY0);
        minY1 = Math.min(value[2],minY1);
    });
    var minY = minY0,
        maxY = maxY1,
        heightY = maxY - minY
    ;        
    var gradient = {
            x1: 10 + ( ((firstY0-minY) / heightY) * 80 ) + "%",
            y1: "10%",
            x2: 10 + ( ((lastY1-minY) / heightY) * 80 ) + "%",
            y2: "90%"
    };
    return gradient;
};

Of course, this approach is not a full-blown solution and only useful if you're dealing with data that approximately follows a linear curve. 当然,这种方法并不是一个完整的解决方案,只有在处理大致遵循线性曲线的数据时才有用。 Here's the jsfiddle 这是jsfiddle

I don't have any example, nor do I know for sure if it is already in the specifications, but I am pretty sure the only way to accomplish this effect would be using svg mash gradients. 我没有任何示例,也不确定它是否已经在规范中,但我很确定实现此效果的唯一方法是使用svg mash渐变。

You could use the path points as boundary points for the mash matrix and get the effect you want. 您可以使用路径点作为mash矩阵的边界点并获得所需的效果。

See here for further information. 请参阅此处获取更多信息。

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

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