高图:添加点而不连接系列

Highcharts: Add point without connecting series

本文关键字:不连接 系列 添加 高图      更新时间:2023-09-26

All, 我试图允许用户单击两个数据点之间的高图表并画一条线。 生成的行将计算渲染行上方的最大值-最小值。

我正在尝试使用此示例(http://jsfiddle.net/2MdEN/1/)。

$(function () {
    var chart;
    $(document).ready(function() {
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'scatter',
                margin: [70, 50, 60, 80],
                events: {
                    click: function(e) {
                        // find the clicked values and the series
                        var x = e.xAxis[0].value,
                            y = e.yAxis[0].value,
                            series = this.series[0];
                        // Add it
                        series.addPoint([x, y]);
                    }
                }
            },
            title: {
                text: 'User supplied data'
            },
            subtitle: {
                text: 'Click the plot area to add a point. Click a point to remove it.'
            },
            xAxis: {
                minPadding: 0.2,
                maxPadding: 0.2,
                maxZoom: 60
            },
            yAxis: {
                title: {
                    text: 'Value'
                },
                minPadding: 0.2,
                maxPadding: 0.2,
                maxZoom: 60,
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            plotOptions: {
                series: {
                    lineWidth: 1,
                    point: {
                        events: {
                            'click': function() {
                                if (this.series.data.length > 1) this.remove();
                            }
                        }
                    }
                }
            },
            series: [{
                data: [[20, 20], [80, 80], null, [60, 40], [85, 60]]
            }]
        });
    });
});

问题是将系列中的最后一个数据点连接到新添加的点。 我希望将点从序列中分离出来,以允许在生成的图表上方绘制线条。

有什么办法可以做到这一点吗?

谢谢

您可以在图表配置中包含第二个空序列,并更改要向其添加点的序列:

    events: {
      click: function(e) {
        // find the clicked values and the series
        var x = e.xAxis[0].value,
          y = e.yAxis[0].value,
          series = this.series[1]; <------------
        // Add it
        series.addPoint([x, y]);
      }
    }
然后,如果您

不希望用户能够从原始数据中删除点,也可以将点单击事件移动到第二个系列中:

  series: [{
    data: [
      [20, 20],
      [80, 80], null, [60, 40],
      [85, 60]
    ]
  }, {
    id: 'dummy',
    color: 'rgba(204,0,0,0.9)',
    point: {
      events: {
        'click': function() {
          this.remove();
        }
      }
    }
  }]

更新的示例:

  • http://jsfiddle.net/2MdEN/107/

我使用了以下内容

$(function () {
    $('#container').highcharts({
        chart: {
            type: 'scatter',
            margin: [70, 50, 60, 80],
            events: {
                click: function (e) {
                  var c = this.series[0].chart;
                  var s = this.series.length;
                  var x = e.xAxis[0].value;
                  var y = e.yAxis[0].value;
                  if(s == 1) {
                    c.addSeries('newSeries' + new Date());
                    c.series[1].addPoint([x, y]);
                  }else{
                    if(this.series[this.series.length-1].data.length%2 == 1) {
                        c.series[this.series.length-1].addPoint([x, y]);
                    }else{
                        c.addSeries('newSeries' + new Date());
                      c.series[this.series.length-1].addPoint([x, y]);
                    }
                  }
                }
            }
        },
        title: {
            text: 'User supplied data!!!!'
        },
        subtitle: {
            text: 'Click the plot area to add a point. Click a point to remove it.'
        },
        xAxis: {
            gridLineWidth: 1,
            minPadding: 0.2,
            maxPadding: 0.2,
            maxZoom: 60
        },
        yAxis: {
            title: {
                text: 'Value'
            },
            minPadding: 0.2,
            maxPadding: 0.2,
            maxZoom: 60,
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        legend: {
            enabled: false
        },
        exporting: {
            enabled: false
        },
        plotOptions: {
            series: {
                lineWidth: 1,
                point: {
                    events: {
                        'click': function () {
                            if (this.series.data.length > 1) {
                                this.remove();
                            }
                        }
                    }
                }
            }
        },
        series: [{
            data: [[20, 20], [80, 80]]
        }]
    });
});