高图中的两条同步垂直线 [英] Two synchronized vertical lines in highcharts

查看:60
本文介绍了高图中的两条同步垂直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一个非常好的示例: https://jsfiddle.net/BlackLabel/7t59w4po/

I recently came across this really nice example: https://jsfiddle.net/BlackLabel/7t59w4po/

基本上,它的作用是使所有其他图形中的一个图形中的一条线的拖动同步.

Basically, what it does is that it synchronizes the drag of a line in one graph in all the other graphs.

我想知道是否有人可以帮助我重现相同的示例,但是我希望有两条垂直线而不是一条垂直线.这可能吗?

I was wondering if someone could help me out to reproduce the same example, but instead of one vertical line, I would like to have two. Is this possible?

谢谢!

JS代码:

/*
The purpose of this demo is to demonstrate how multiple charts on the same page
can be linked through DOM and Highcharts events and API methods. It takes a
standard Highcharts config with a small variation for each data set, and a
mouse/touch event handler to bind the charts together.
*/


/**
 * In order to synchronize tooltips and crosshairs, override the
 * built-in events with handlers defined on the parent element.
 */
['mousemove', 'touchmove', 'touchstart'].forEach(function(eventType) {
  document.getElementById('container').addEventListener(
    eventType,
    function(e) {
      var chart,
        point,
        i,
        event;

      for (i = 0; i < Highcharts.charts.length; i = i + 1) {
        chart = Highcharts.charts[i];
        // Find coordinates within the chart
        event = chart.pointer.normalize(e);
        // Get the hovered point
        point = chart.series[0].searchPoint(event, true);

        if (point) {
          point.highlight(e);
        }
      }
    }
  );
});

/**
 * Override the reset function, we don't need to hide the tooltips and
 * crosshairs.
 */
Highcharts.Pointer.prototype.reset = function() {
  return undefined;
};

/**
 * Highlight a point by showing tooltip, setting hover state and draw crosshair
 */
Highcharts.Point.prototype.highlight = function(event) {
  event = this.series.chart.pointer.normalize(event);
  this.onMouseOver(); // Show the hover marker
  this.series.chart.tooltip.refresh(this); // Show the tooltip
  this.series.chart.xAxis[0].drawCrosshair(event, this); // Show the crosshair
};

/**
 * Synchronize zooming through the setExtremes event handler.
 */
function syncExtremes(e) {
  var thisChart = this.chart;

  if (e.trigger !== 'syncExtremes') { // Prevent feedback loop
    Highcharts.each(Highcharts.charts, function(chart) {
      if (chart !== thisChart) {
        if (chart.xAxis[0].setExtremes) { // It is null while updating
          chart.xAxis[0].setExtremes(
            e.min,
            e.max,
            undefined,
            false, {
              trigger: 'syncExtremes'
            }
          );
        }
      }
    });
  }
}

/**
 * Synchronize annotations drag&drop
 */
function syncAnnotations(e) {
  var thisChart = this.chart;
  var newX = this.options.shapes[0].points[0].x

  if (e.type !== 'afterUpdate') {
    Highcharts.each(Highcharts.charts, function(chart) {
      if (chart !== thisChart) {
        chart.annotations[0].update({
          labels: [{
            point: {
              x: newX
            }
          }],
          shapes: [{
            points: [{
              x: newX,
              xAxis: 0,
              y: 0
            }, {
              x: newX,
              xAxis: 0,
              y: 1000
            }]
          }]
        });
      }
    });
  }
}

// Get the data. The contents of the data file can be viewed at
Highcharts.ajax({
  url: 'https://cdn.jsdelivr.net/gh/highcharts/highcharts@v7.0.0/samples/data/activity.json',
  dataType: 'text',
  success: function(activity) {

    activity = JSON.parse(activity);
    activity.datasets.forEach(function(dataset, i) {

      // Add X values
      dataset.data = Highcharts.map(dataset.data, function(val, j) {
        return [activity.xData[j], val];
      });

      var chartDiv = document.createElement('div');
      chartDiv.className = 'chart';
      document.getElementById('container').appendChild(chartDiv);

      Highcharts.chart(chartDiv, {
        chart: {
          marginLeft: 40, // Keep all charts left aligned
          spacingTop: 20,
          spacingBottom: 20
        },
        title: {
          text: dataset.name,
          align: 'left',
          margin: 0,
          x: 30
        },
        credits: {
          enabled: false
        },
        legend: {
          enabled: false
        },
        xAxis: {
          crosshair: true,
          events: {
            setExtremes: syncExtremes
          },
          labels: {
            format: '{value} km'
          }
        },
        yAxis: {
          title: {
            text: null
          }
        },


        annotations: [{
          draggable: 'x',
          animation: {
            defer: false
          },
          events: {
            drag: syncAnnotations,
            afterUpdate: syncAnnotations
          },
          shapes: [{
            strokeWidth: 3,
            type: 'path',
            points: [{
              x: 3,
              y: 0,
              xAxis: 0
            }, {
              x: 3,
              y: 1000,
              xAxis: 0
            }]
          }],
          labels: [{
            point: {
              x: 3,
              y: 30,
              xAxis: 0
            },
            shape: 'rect',
            formatter: function(e) {
              // Use shape options because value is available there. Label use translation only
              return this.target.annotation.shapes[0].options.points[0].x.toFixed(3);
            }
          }]

        }],
        tooltip: {
          positioner: function() {
            return {
              // right aligned
              x: this.chart.chartWidth - this.label.width,
              y: 10 // align to title
            };
          },
          borderWidth: 0,
          backgroundColor: 'none',
          pointFormat: '{point.y}',
          headerFormat: '',
          shadow: false,
          style: {
            fontSize: '18px'
          },
          valueDecimals: dataset.valueDecimals
        },
        series: [{
          data: dataset.data,
          name: dataset.name,
          type: dataset.type,
          color: Highcharts.getOptions().colors[i],
          fillOpacity: 0.3,
          tooltip: {
            valueSuffix: ' ' + dataset.unit
          }
        }]
      });
    });
  }
});

推荐答案

您只需要添加另一个注释:

You only need to add another annotation:

    annotations: [{
        ..., 
    {
        ...
    }],

并稍微改进 syncAnnotations 函数:

    function syncAnnotations(e) {
        var thisChart = this.chart;
        var newX = this.options.shapes[0].points[0].x
        var index = this.chart.annotations.indexOf(this);

        if (e.type !== 'afterUpdate') {
            Highcharts.each(Highcharts.charts, function(chart) {
                if (chart !== thisChart) {
                    chart.annotations[index].update({
                        ...
                    });
                }
            });
        }
    }


实时演示: https://jsfiddle.net/BlackLabel/jwtLc379/

API参考: https://api.highcharts.com/highcharts/annotations

这篇关于高图中的两条同步垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆