我想将拖放功能添加到沿 yAxis 的绘图线,因为我的图表是倒置的。
我发现了几个似乎让我非常接近解决方案的问题,但我无法使用 React-Highcharts 包巧妙地实施解决方案。
- https://stackoverflow.com/a/18484071/4567096。(http://jsfiddle.net/VXTeR/1/ - 来自答案的链接)
- 具有对数轴刻度的 HighCharts 可拖动绘图线(JSFiddle 链接:http: //jsfiddle.net/48awM/30/)
这两种解决方案都说明了一个非常可行的解决方案。我的问题是将解决方案翻译成React-Highcharts组件。
这是一个JSFiddle,它是我的图表的精简版。
以下是我当前的 yAxis 配置
config.yaxis = {
title: 'Times',
descrition: 'Times within a 24 hour day.',
endOnTick: false,
opposite: true,
min: Date.UTC(0,0,0,0,0,0),
max: Date.UTC(0,0,0,24,30,0),
plotLines: [{
value: Date.UTC(0,0,0,6,0,0),
id: 'plotLineId',
color: colorObj.open,
dashStyle: 'shortdash',
width: 2,
zIndex: 4,
onDragStart: function (new_value) {
$("#x_value").text(new_value + ' (Not changed yet)');
},
onDragChange: function (new_value) {
$("#x_value").text(new_value + ' (Dragging)');
},
onDragFinish: function (new_value) {
$("#x_value").text(new_value);
}
}, {
value: Date.UTC(0,0,0,21,0,0),
id: 'plotLineId',
color: colorObj.close,
dashStyle: 'shortdash',
width: 2,
zIndex: 4,
onDragStart: function (new_value) {
$("#x_value").text(new_value + ' (Not changed yet)');
},
onDragChange: function (new_value) {
$("#x_value").text(new_value + ' (Dragging)');
},
onDragFinish: function (new_value) {
$("#x_value").text(new_value);
}
}],
type: 'datetime',
dateTimeLabelFormats: {
minute: '%l:%M%p',
hour: '%l %p',
day: '%l %p'
},
events: {
mouseOver: function (e) {
console.log('mouseOver - ', e);
}
}
}
以及我在 ReactHighcharts 组件文件中的内容
import React, { Component } from 'react';
import ReactHighcharts from 'react-highcharts'
import { allViewConfig } from './graphConfigs/graphConfigs';
class Graph extends Component {
afterRender = (chart) => {
function draggablePlotLine(axis, plotLineId) {
var clickX, clickY;
var getPlotLine = function () {
for (var i = 0; i < axis.plotLinesAndBands.length; i++) {
if (axis.plotLinesAndBands[i].id === plotLineId) {
return axis.plotLinesAndBands[i];
}
}
};
var getValue = function() {
var plotLine = getPlotLine();
var translation = axis.horiz ? plotLine.svgElem.translateX : plotLine.svgElem.translateY;
var new_value = axis.toValue(translation) - axis.toValue(0) + plotLine.options.value;
new_value = Math.max(axis.min, Math.min(axis.max, new_value));
return new_value;
};
var drag_start = function (e) {
$(document).bind({
'mousemove.line': drag_step,
'mouseup.line': drag_stop
});
var plotLine = getPlotLine();
clickX = e.pageX - plotLine.svgElem.translateX;
clickY = e.pageY - plotLine.svgElem.translateY;
if (plotLine.options.onDragStart) {
plotLine.options.onDragStart(getValue());
}
};
var drag_step = function (e) {
var plotLine = getPlotLine();
var new_translation = axis.horiz ? e.pageX - clickX : e.pageY - clickY;
var new_value = axis.toValue(new_translation) - axis.toValue(0) + plotLine.options.value;
new_value = Math.max(axis.min, Math.min(axis.max, new_value));
new_translation = axis.toPixels(new_value + axis.toValue(0) - plotLine.options.value);
plotLine.svgElem.translate(
axis.horiz ? new_translation : 0,
axis.horiz ? 0 : new_translation);
if (plotLine.options.onDragChange) {
plotLine.options.onDragChange(new_value);
}
};
var drag_stop = function () {
$(document).unbind('.line');
var plotLine = getPlotLine();
var plotLineOptions = plotLine.options;
//Remove + Re-insert plot line
//Otherwise it gets messed up when chart is resized
if (plotLine.svgElem.hasOwnProperty('translateX')) {
plotLineOptions.value = getValue()
axis.removePlotLine(plotLineOptions.id);
axis.addPlotLine(plotLineOptions);
if (plotLineOptions.onDragFinish) {
plotLineOptions.onDragFinish(plotLineOptions.value);
}
}
getPlotLine().svgElem
.css({'cursor': 'pointer'})
.translate(0, 0)
.on('mousedown', drag_start);
};
drag_stop();
};
}
render(){
return (
<div>
<ReactHighcharts
config={allViewConfig(this.props.configArr, this.props.bandRow, this.props.configVars)}
callback = {this.afterRender}
/>
</div>
);
}
};
export default Graph;