gpt4 book ai didi

javascript - 在 highcharts 中切换饼图和条形图

转载 作者:行者123 更新时间:2023-11-30 19:50:36 25 4
gpt4 key购买 nike

我正在寻找一种功能,允许我使用 highcharts 提供的上下文菜单从饼图切换到条形图(然后再切换回来)。下图显示了我希望放置图表开关的位置。

enter image description here

这是我到目前为止所做的:

Highcharts.chart('container', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'Browser market shares in January, 2018'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
}
},
series: [{
name: 'Brands',
colorByPoint: true,
data: [{
name: 'Chrome',
y: 61.41,
sliced: true,
selected: true
}, {
name: 'Internet Explorer',
y: 11.84
}, {
name: 'Firefox',
y: 10.85
}, {
name: 'Edge',
y: 4.67
}, {
name: 'Safari',
y: 4.18
}, {
name: 'Sogou Explorer',
y: 1.64
}, {
name: 'Opera',
y: 1.6
}, {
name: 'QQ',
y: 1.2
}, {
name: 'Other',
y: 2.61
}]
}]
});
<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
</head>

<body>
<div id="container" style="height: 400px; width: 800px"></div>

</body>

</html>

最佳答案

您可以将自定义项添加到导出菜单,并在 click 事件中使用 update 方法为图表添加新选项:

exporting: {
menuItemDefinitions: {
// Custom definition
switchChart: {
onclick: function() {
var chartType = this.options.chart.type;

this.update({
chart: {
type: chartType === 'bar' ? 'pie' : 'bar'
}
})
},
text: 'Switch chart'
}
},
buttons: {
contextButton: {
menuItems: ["switchChart", "separator", "printChart", "separator", "downloadPNG", "downloadJPEG", "downloadPDF", "downloadSVG"]
}
}
},

现场演示:http://jsfiddle.net/BlackLabel/xdsgL6rm/

应用程序接口(interface):

https://api.highcharts.com/highcharts/exporting.buttons.contextButton.menuItems

https://api.highcharts.com/class-reference/Highcharts.Chart#update

关于javascript - 在 highcharts 中切换饼图和条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54513904/

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