我在Highchart内部有一个按钮作为Vue对象。调整图表大小时,无法访问图表重绘功能内的Vue数据对象。
使用Javascript,我已经完成了(附加代码)。调整大小时,按钮也会移到新位置。但是,如果我使用Vue,则在调整大小时,Button会保持在相同位置。就像我创建的这个jsfiddle中的button属性一样,我有一个按钮的Vue数据对象,在redraw函数中无法访问它。
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>