简体   繁体   English

高图中的动态图线

[英]Dynamic PlotLine in Highcharts

I have a scatter plot with a horizontal PlotLine. 我有一个带有水平PlotLine的散点图。 I would like people to be able to drag the PlotLine. 我希望人们能够拖动PlotLine。 The y axis value is used as a threshold value to generate a column chart (the column chart is initially generated with a default value for the threshold). y轴值用作生成柱形图的阈值(柱形图最初使用阈值的默认值生成)。 So far I have the draggable plotline working using code that I found on the Highcharts forum: 到目前为止,我已经使用在Highcharts论坛上找到的代码来工作可拖动的情节线:

var line,
    clickX,
    clickY;

var start= function (e) {
    $(document).bind({
        'mousemove.line': step,
        'mouseup.line': stop
    });
    clickY=e.pageY - line.translateY;
}

var step = function (e) {
    line.translate(e.pageX - clickX, e.pageY - clickY)
}

var stop = function (e) {
    var chart = $('#container').highcharts();
    var max_y_axis = chart.yAxis[0].max;
    var newVal = chart.yAxis[0].toValue(e.pageY - clickY + chart.plotTop) + chart.yAxis[0].plotLinesAndBands[0].options.value)- max_y_axis;
    $('#report').text('Value: ' + newVal);
    $(document).unbind('.line');
}

This works fine and I can see that the threshold value is being produced correctly. 这可以正常工作,我可以看到正确生成了阈值。 However, I would like to access the value newVal (the threshold) elsewhere in my code, ie to create the 2nd chart. 但是,我想在代码的其他位置访问值newVal(阈值),即创建第二个图表。 Does anyone have an idea of how I can get newVal outside of the function? 有谁知道如何在函数之外获取newVal?

Just increase its scope: 只需增加其范围即可:

var line,
    clickX,
    clickY,
    newVal;

var start= function (e) {
    $(document).bind({
        'mousemove.line': step,
        'mouseup.line': stop
    });
    clickY=e.pageY - line.translateY;
}

var step = function (e) {
    line.translate(e.pageX - clickX, e.pageY - clickY)
}

var stop = function (e) {
    var chart = $('#container').highcharts();
    var max_y_axis = chart.yAxis[0].max;
    newVal = chart.yAxis[0].toValue(e.pageY - clickY + chart.plotTop) + chart.yAxis[0].plotLinesAndBands[0].options.value)- max_y_axis;
    $('#report').text('Value: ' + newVal);
    $(document).unbind('.line');
}

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

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