javascript - 无法使用 Vue.js 在 Highchart 中动态设置工具提示和图例
问题描述
我已经在 Vue.js 中创建了这段代码(感谢这个答案),在其中我用我的 get 请求中的数据创建了一个饼图,这里是代码。
HTML
<template v-if="this.challenge_categories.length>0">
<template v-if="setOccurrences()">
<div id="pie-container" style="min-width: 200px; height: 300px; margin: 0 auto"></div>
</template>
</template>
<template v-else>
<div id="pie-container" v-bind="setData([0])" style="min-width: 200px; height: 300px; margin: 0 auto"></div>
</template>
脚本 VUE.JS
var app= new Vue({
el:'#root',
data(){
return{
challenge_categories:[],
chartData: [],
categories:[],
teams:[],
}
},
mounted(){
this.fetchCategories();
this.chart = this.createChart()
},
methods:{
fetchCategories(){
axios.get("/getallcategories").then(function(response){
this.challenge_categories= response.data;
}.bind(this));
},
setData(data){
this.chartData = data
},
setCategories(categories){
this.categories = categories
},
setOccurrences(){
//console.log(this.challenge_categories);
var categories_occurrences=[];
var categories_labels=[];
for(i=0; i<this.challenge_categories.length;i++){
categories_occurrences[i]= this.challenge_categories[i].occurrences;
categories_labels[i]= this.challenge_categories[i].category_name;
}
//console.log(categories_occurrences);
//console.log(categories_labels);
this.setData(categories_occurrences);
this.setCategories(categories_labels);
return true;
},
createChart() {
return Highcharts.chart('pie-container', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: ''
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.categories}</b>: {point.percentage:.1f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
}
},
xAxis:{
categories:[]
},
series: [{
data: []
}]
})
}
},
watch: {
chartData(data) {
this.chart.series[0].setData(data);
console.log(this.chart.series[0].name);
this.chart.redraw()
},
categories(categories ) {
this.chart.axes[0].setCategories(categories);
this.chart.redraw()
}
}
});
这可行,但我无法使用我添加的类别设置图例和工具提示。我可以看到这张图表(不要介意 50/50,这只是一个测试)我在上一个函数中尝试了更新,但这没有用,知道吗?谢谢。
解决方案
您的所有问题都是由于您的代码执行不正确而发生的。首先,设置图表类别根本不会影响图表。默认情况下,每个切片的适当“类别”取自每个点name
属性,因此您的实现方式将不起作用。您需要更改point
值而不是类别。
实现它的最简单方法看起来(逐步)如下: -challenge_categories
通过axios获取您的。- 在您的函数中使用新数据创建新series
数组。setOccurence()
- 获取每个接收到的类别对象,并将其解析为新系列中的新点。之前定义的对象。- 查看组件中字段的所有更改,并在更改时 - 使用新对象series
更新图表。series
我准备了简化的例子来展示它应该如何实现。这是链接:http: //jsfiddle.net/b2yLc1m3/1/
PS 我们有 Highcharts 的官方 Vue 包装器,这使得图表实现过程比使用原生方法更容易。我建议你看看那个。我希望它会对你有很大帮助。这是npm包的链接:https ://www.npmjs.com/package/highcharts-vue
推荐阅读
- css - 打印页眉和页脚的媒体查询
- html - 如何在兄弟 div 留下的剩余空间中居中 div
- c++ - 谷歌 C++ 风格指南包括订单
- javascript - 关于 async/await 与 Promises 和事件循环的性能问题
- reactjs - ReactJs Material UI 随机问题
- javascript - 是否可以在组件文件中重新创建 ngIf 和异步管道行为?
- c# - 数据包未被读取 TCP
- python - 从 Bearer 类型令牌获取 Google OAuth2 凭据或刷新令牌?
- android - 谷歌地图给了我空指针异常,而它不应该
- php - 在 Woocommerce 中更新状态之前获取最后一个旧订单状态