我需要设计与以下类似的高库存,并为Rangeselctor下拉菜单:
我遵循了jsfiddle代码,但是并不能完全满足我的需求。同样,当用户选择一个范围时说“ 1M”,它应显示为选中状态。 目前,图形会调整到所选范围,但下拉菜单不会显示当前所选范围。
exporting: {
buttons: {
contextButton: {
enabled: false
},
toggle: {
text: 'Select range',
align: 'left',
height: 20,
y: -3,
theme: {
'stroke-width': 0.5,
stroke: '#000000',
r: 2
},
menuItems: [{
text: '1M',
onclick: function() {
this.rangeSelector.clickButton(0, true);
}
}, {
text: '3M',
onclick: function() {
this.rangeSelector.clickButton(1, true);
}
}]
}
}
},
答案 0 :(得分:1)
包装器可用于实现您所追求的功能,例如:
(function(H) {
H.wrap(H.Chart.prototype, 'contextMenu', function(proceed) {
if(this.rangeSelector.selected >= 0) {
$('.highcharts-button text tspan').text(this.rangeSelector.buttonOptions[this.rangeSelector.selected].text.toUpperCase())
}
proceed.apply(this, Array.prototype.slice.call(arguments, 1));
});
}(Highcharts));
这将设置单击按钮时的文本
$('.highcharts-button text tspan').text(
反映当前选择的内容
this.rangeSelector.buttonOptions[this.rangeSelector.selected].text
此文本将保留在那里,直到做出下一个选择为止,因此我们必须添加一个函数以在做出选择后将其重置:
function resetSelector() {
$('.highcharts-button text tspan').text('Select range')
}
选择后将执行以下操作:
menuItems: [{
text: '1M',
onclick: function() {
this.rangeSelector.clickButton(0, true);
resetSelector();
}
}, {
text: '3M',
onclick: function() {
this.rangeSelector.clickButton(1, true);
resetSelector();
}
},
function resetSelector() {
$('.highcharts-button text tspan').text('Select range')
}
(function(H) {
H.wrap(H.Chart.prototype, 'contextMenu', function(proceed) {
if(this.rangeSelector.selected >= 0) {
$('.highcharts-button text tspan').text(this.rangeSelector.buttonOptions[this.rangeSelector.selected].text.toUpperCase())
} else {
$('.highcharts-button text tspan').text('ALL')
}
proceed.apply(this, Array.prototype.slice.call(arguments, 1));
});
}(Highcharts));
$.getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function(data) {
Highcharts.stockChart('container', {
exporting: {
buttons: {
contextButton: {
enabled: false
},
toggle: {
text: 'Select range',
align: 'left',
height: 20,
y: -3,
theme: {
'stroke-width': 0.5,
stroke: '#000000',
r: 2
},
menuItems: [{
text: '1M',
onclick: function() {
this.rangeSelector.clickButton(0, true);
resetSelector();
}
}, {
text: '3M',
onclick: function() {
this.rangeSelector.clickButton(1, true);
resetSelector();
}
}, {
text: '6M',
onclick: function() {
this.rangeSelector.clickButton(2, true);
resetSelector();
}
}, {
text: 'YTD',
onclick: function() {
this.rangeSelector.clickButton(3, true);
resetSelector();
}
}, {
text: '1Y',
onclick: function() {
this.rangeSelector.clickButton(4, true);
resetSelector();
}
}, {
text: 'All',
onclick: function() {
this.rangeSelector.clickButton(5, true);
resetSelector();
}
}]
}
}
},
series: [{
data: data
}]
});
});
.highcharts-range-selector-buttons {
visibility: hidden;
}
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<div id="container" style="height: 400px; min-width: 310px"></div>
工作示例: http://jsfiddle.net/ewolden/973ve1sx/1/
如果您只想始终显示所选内容,则只需执行以下操作:
设置文本的功能:
function setSelector(text) {
$('.highcharts-button text tspan').text(text)
}
菜单选项:
menuItems: [{
text: '1M',
onclick: function() {
this.rangeSelector.clickButton(0, true);
setSelector('1M');
}
}, {
text: '3M',
onclick: function() {
this.rangeSelector.clickButton(1, true);
setSelector('3M');
}
}