Highchart重绘功能中的Vue数据对象

时间:2019-11-20 15:58:12

标签: vue.js highcharts data-objects

我在Highchart内部有一个按钮作为Vue对象。调整图表大小时,无法访问图表重绘功能内的Vue数据对象。

使用Javascript,我已经完成了(附加代码)。调整大小时,按钮也会移到新位置。但是,如果我使用Vue,则在调整大小时,Button会保持在相同位置。就像我创建的这个jsfiddle中的button属性一样,我有一个按钮的Vue数据对象,在redraw函数中无法访问它。

  1. Y轴调整大小回调应更改其他html元素,并同时渲染按钮的位置
  2. 在图表的事件重绘函数中访问Vue数据属性。现在,在函数内部只能访问图表对象或事件本身。
  3. 拖动Y轴时,是否有任何方法可以知道正在调整哪个轴的大小。

Highcharts.getJSON('https://www.highcharts.com/samples/data/aapl-ohlcv.json', function(data) {

    // split the data set into ohlc and volume
    var ohlc = [],
        volume = [],
        dataLength = data.length,
        // set the allowed units for data grouping
        groupingUnits = [
            [
                'week', // unit name
                [1] // allowed multiples
            ],
            [
                'month',
                [1, 2, 3, 4, 6]
            ]
        ],

        i = 0;

    for (i; i < dataLength; i += 1) {
        ohlc.push([
            data[i][0], // the date
            data[i][1], // open
            data[i][2], // high
            data[i][3], // low
            data[i][4] // close
        ]);

        volume.push([
            data[i][0], // the date
            data[i][5] // the volume
        ]);
    }

    var btn = {};


    // create the chart
    var chart = Highcharts.stockChart('container', {

        chart: {
            events: {
                render: function() {
                    var chart = this;

                    if (btn.customBtn) {
                        btn.customBtn.attr({
                            y: chart.yAxis[1].top,
                        });

                        btn.customBtn2.attr({
                            y: chart.yAxis[2].top,
                        });

                    } else {
                        btn.customBtn = chart.renderer.button(
                            'first',
                            5,
                            chart.yAxis[1].top + 2,
                            function() {
                                console.log('some task')
                            }).add()

                        btn.customBtn2 = chart.renderer.button(
                            'second',
                            5,
                            chart.yAxis[2].top + 2,
                            function() {
                                console.log('some task')
                            }).add()
                    }
                }
            }
        },

        rangeSelector: {
            selected: 1
        },

        title: {
            text: 'AAPL Historical'
        },

        yAxis: [{
            labels: {
                align: 'right',
                x: -3
            },
            title: {
                text: 'OHLC'
            },
            height: '40%',
            lineWidth: 2,
            resize: {
                enabled: true
            }
        }, {
            labels: {
                align: 'right',
                x: -3
            },
            title: {
                text: 'Volume'
            },
            top: '40%',
            height: '30%',
            offset: 0,
            lineWidth: 2,
            resize: {
                enabled: true
            }
        }, {
            labels: {
                align: 'right',
                x: -3
            },
            top: '70%',
            height: '30%',
            offset: 0,
            lineWidth: 2
        }],

        tooltip: {
            split: true
        },

        series: [{
            type: 'candlestick',
            id: 'candlestick',
            name: 'AAPL',
            data: ohlc,
            dataGrouping: {
                units: groupingUnits
            }
        }, {
            type: 'column',
            name: 'Volume',
            data: volume,
            yAxis: 1,
            dataGrouping: {
                units: groupingUnits
            }
        }, {
            type: 'sma',
            name: 'rsi',
            linkedTo: 'candlestick',
            yAxis: 2
        }]
    });
});
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/data.js"></script>
<script src="https://code.highcharts.com/stock/modules/drag-panes.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<script src="https://code.highcharts.com/stock/indicators/indicators.js"></script>
<script src="https://code.highcharts.com/stock/indicators/volume-by-price.js"></script>


<div id="container" style="height: 400px; min-width: 310px"></div>

0 个答案:

没有答案