咨询Highcharts多图表双同步可拖拽竖线的复现方案
关联图表双竖线拖拽同步实现说明
我近期发现了一个功能实现效果良好的示例,核心能力为:单个图表内的竖线拖拽操作,可同步映射到所有其他关联图表上。
我希望复现该示例的功能,但需要将原本的单条竖线调整为双竖线,该需求完全可行,无需重构整套多图同步机制,仅需在现有代码基础上做小幅调整即可实现。
具体调整方向
- 把原本annotation配置里的单竖线shape,调整为两个独立的竖线路径配置,分别设置左右两条竖线的初始x坐标
- 修改
syncAnnotations同步函数的取值逻辑,同时读取两条竖线拖拽后的最新x值,同步到其他关联图表时,逐次更新两个shape的坐标,替换原有仅读取单条竖线坐标的逻辑 - 为两条竖线分别配置独立的显示标签,实时展示对应竖线的坐标值
- 若需要实现两条竖线之间的选中区域填充效果,可在shape配置中新增跨两个竖线的面状元素,同步更新时自动适配两个竖线的位置,不影响现有拖拽同步逻辑
参考原始实现代码
/* * 多图表联动示例:通过DOM事件和Highcharts API实现跨图表的tooltip、十字线、拖拽标注的同步 * 基础配置为通用Highcharts图表配置,通过鼠标/触摸事件绑定实现多图联动 */ // 覆盖原生指针事件,实现tooltip和十字线的跨图同步 ['mousemove', 'touchmove', 'touchstart'].forEach(function(eventType) { document.getElementById('container').addEventListener( eventType, function(e) { let chart, point, i, event; for (i = 0; i < Highcharts.charts.length; i++) { chart = Highcharts.charts[i]; // 计算当前事件在当前图表内的相对坐标 event = chart.pointer.normalize(e); // 获取当前鼠标位置下的最近数据点 point = chart.series[0].searchPoint(event, true); if (point) { point.highlight(e); } } } ); }); // 覆盖原生指针重置方法,避免跨图同步时tooltip和十字线被隐藏 Highcharts.Pointer.prototype.reset = function() { return undefined; }; // 自定义点高亮方法:实现tooltip展示、十字线绘制的统一逻辑 Highcharts.Point.prototype.highlight = function(event) { event = this.series.chart.pointer.normalize(event); this.onMouseOver(); // 触发数据点hover状态 this.series.chart.tooltip.refresh(this); // 刷新tooltip位置和内容 this.series.chart.xAxis[0].drawCrosshair(event, this); // 绘制x轴十字线 }; // 同步x轴缩放范围,避免事件循环 function syncExtremes(e) { const thisChart = this.chart; if (e.trigger !== 'syncExtremes') { Highcharts.each(Highcharts.charts, function(chart) { if (chart !== thisChart && chart.xAxis[0].setExtremes) { chart.xAxis[0].setExtremes( e.min, e.max, undefined, false, { trigger: 'syncExtremes' } ); } }); } } // 同步标注元素的拖拽位置 function syncAnnotations(e) { const thisChart = this.chart; const 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 } ] }] }); } }); } } // 拉取示例数据初始化图表 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) { // 为每个数据集补全x轴坐标 dataset.data = Highcharts.map(dataset.data, function(val, j) { return [activity.xData[j], val]; }); // 创建图表容器 const chartDiv = document.createElement('div'); chartDiv.className = 'chart'; document.getElementById('container').appendChild(chartDiv); // 初始化图表实例 Highcharts.chart(chartDiv, { chart: { marginLeft: 40, // 统一所有图表左对齐边距 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() { // 从shape配置中获取坐标值做显示 return this.target.annotation.shapes[0].options.points[0].x.toFixed(3); } }] }], tooltip: { positioner: function() { return { // tooltip右对齐到图表右上角 x: this.chart.chartWidth - this.label.width, y: 10 }; }, 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 } }] }); }); } });
内容的提问来源于stack exchange,提问作者FMM
相关产品推荐
相关产品推荐

