简体   繁体   English

如何在 Highcharts 的图例中显示绘图线?

[英]How to show plot lines in the legend in Highcharts?

I not only want my legend to contain series' names, I also want it to contain name of my other items like my plotline.我不仅希望我的图例包含系列的名称,我还希望它包含我的其他项目的名称,例如我的情节线。 Here is my code:这是我的代码:

plotLines: [
    {
        color: 'red',
        dashStyle: 'shortdash',
        value: response.AverageTime,
        width: 2,
        label: {
            text: response.PlotLineName, 
            x: +10, 
            y: +30,
            rotation: 360,
            color: 'red'
        }
    }
]

So I want my plotline's name which is "Average Velocity Deviation" looks like in the legend box like below.所以我希望我的情节线的名称是“平均速度偏差”,在下面的图例框中看起来像。 How can I achieve that?我怎样才能做到这一点?

在此处输入图片说明

You can create an empty series which mimics the characteristics of the plot line (color, dash style...).您可以创建一个模仿情节线特征(颜色、虚线样式...)的空系列。 You can then optionally use the legendItemClick event to "link it up" to the plot line.然后,您可以选择使用legendItemClick事件将其“链接”到绘图线。

For example, you predefine these variables for ID and plot line options:例如,您为 ID 和绘图线选项预定义这些变量:

plotLineId = 'myPlotLine'; // To identify for removal

// Plot line options for adding
plotLineOptions = {
    color: '#FF0000',
    id: plotLineId, 
    width: 2,
    value: 5.5,
    dashStyle: 'shortdash'
};

Your axis plotline:你的轴情节线:

xAxis: {
    plotLines: [ plotLineOptions ]
}

Your mimic series:你的模仿系列:

series: [
    // ...
    {
        // Series that mimics the plot line
        color: '#FF0000',
        name: 'My plotline',
        dashStyle: 'shortdash',
        marker: {
            enabled: false
        },
        events: {
            // Event for showing/hiding plot line
            legendItemClick: function(e) {
                if(this.visible) {
                    this.chart.xAxis[0].removePlotLine(plotLineId);
                }
                else {
                    this.chart.xAxis[0].addPlotLine(plotLineOptions);
                }
            }
        }
    }
]

See this JSFiddle demonstration of how it works (or this minimal example , without events).请参阅此 JSFiddle 演示,了解其工作原理(或此最小示例,没有事件)。

Just as an alternative, if you're going to go so far as to make a fake series that mimics all of the aspects of your plot line...作为替代方案,如果您打算制作一个模仿情节线所有方面的假系列......

You can just use that series as your plotLine , and skip all of the extra work of tying everything together...您可以将该系列用作您的plotLine ,并跳过将所有内容捆绑在一起的所有额外工作......

like:喜欢:

{  
  name: 'Plot Line',
  color: 'red',
  type:'line',
  dashStyle: 'shortdash',
  lineWidth: 2,
  data: [[minXvalue, plotLineValue], {x:maxXvalue, y:plotLineValue, dataLabels: { enabled: true }}]
}

Example:例子:

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

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