gpt4 book ai didi

javascript - Highcharts 单选按钮在图表之间切换

转载 作者:行者123 更新时间:2023-12-03 04:50:53 24 4
gpt4 key购买 nike

我正在尝试使用单选按钮来使用 div 在两个 Highchart 图表(不同的数据系列)之间切换显示。我本质上需要这里接受的解决方案:Mootools Highcharts radio button toggle between charts, working jQuery version...但我无法弄清楚如何从现有代码中指定每个图表的变量。上面的解决方案使用了这种格式:

var chart1 = new Highcharts.Chart({
chart: {
renderTo: 'divID-1',
height: 400,
...

var chart2 = new Highcharts.Chart({
chart: {
renderTo: 'divID-2',
height: 400,
...

而我的每个图表都按以下格式指定,例如图一:

Highcharts.setOptions({
lang: {
decimalPoint: '.',
thousandsSep: ','
}
});

$.get('data_stackedarea_value.csv', function(csv) {
$('#divID-1').highcharts({

chart: {type: 'area'},
data: {csv: csv},
...

如何将它们转换为可由初始切换函数调用的变量?

window.addEvent('domready', function() {
document.getElements("[name=toggler]").addEvent('click', function(){
$$('.toHide').setStyle('top', '-9999em');
$$('.toHide').setStyle('opacity', '0');
$("divID-"+this.get('value')).setStyle('top', '0').fade(0,1);
});

非常感谢

最佳答案

这是我采用 23817700 中的解决方案的示例使用将变量传递到图表的方式。

我忍不住还要调整切换代码以使用 jQuery,因为它已经是必需的了。您可以尝试在 @RachelGallen 提供的 fiddle 中执行此操作:https://jsfiddle.net/ezc7oghm/1/

<!doctype html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<body>
<div id="graphwrap" style="height: 410px;">
<div id="divID-1" class="toHide" style="position:relative;margin-bottom:-400px;"></div>
<div id="divID-2" class="toHide" style="position:relative;top:-9999em;opacity:0;"></div>
</div>

<div id="viewSelectWrap">
<h4>View Select</h4>
<label><input id="rdb1" type="radio" name="toggler" value="divID-1" style="cursor:pointer;" checked/>1</label>
<label><input id="rdb2" type="radio" name="toggler" value="divID-2" style="cursor:pointer;" />2</label>
</div>

<script>
$(function() {
$('[name=toggler]').click(function () {
$('.toHide').css({
top: '-9999em',
opacity: 0
});
var chartToShow = $(this).val();
$('#' + chartToShow).css({
top: 0,
opacity: 1
});
});

$('#divID-1').highcharts({
chart: {
type: 'bar'
},
title: {
text: 'Fruit Consumption'
},
xAxis: {
categories: ['Apples', 'Bananas', 'Oranges']
},
yAxis: {
title: {
text: 'Fruit eaten'
}
},
series: [{
name: 'Jane',
data: [1, 0, 4]
}, {
name: 'John',
data: [5, 7, 3]
}]
});
$('#divID-2').highcharts({
chart: {
type: 'bar'
},
title: {
text: 'Time spent on hobbies'
},
xAxis: {
categories: ['Skiing', 'Bicycling', 'Swimming']
},
yAxis: {
title: {
text: 'Time spent on hobbies'
}
},
series: [{
name: 'Jane',
data: [1, 0, 4]
}, {
name: 'John',
data: [5, 7, 3]
}]
});
});
</script>

</body>
</html>

关于javascript - Highcharts 单选按钮在图表之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42661010/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com