api - 如何使用 vue-apexcharts 将数据更新为饼图
问题描述
我想用 vue-apexcharts 创建饼图。我有来自 API 的数据,但我不知道如何更新图表的数据。
mounted() {
axios
.get("/data/episodes.json")
.then(response => {
console.log(response);
});
}
我要创建的图表是这样的
解决方案
这是我对做的理解,...
首先,您必须定义图表的数据和选项值。
components: {
apexchart: VueApexCharts,
},
data: {
series: [],
chartOptions: {
chart: {
width: 380,
type: "pie",
},
labels: [],
responsive: [
{
breakpoint: 480,
options: {
chart: {
width: 200,
},
legend: {
position: "bottom",
},
},
},
],
},
},
现在在挂载时,您已经调用您的 api 来获取数据并更新图表信息。像这样......我在想你的服务器会返回一个具有值和键的对象数组。
mounted() {
axios.get("/data/episodes.json").then((response) => {
for (let i = 0; i < response.data.length; i++) {
this.data.series.push(response.data[i].value);
this.data.chartOptions.labels.push(response.data[i].key);
}
});
},
最后,您必须将图表组件添加到 vue 模板中。像这样。
<div id="chart">
<apexchart type="pie" width="380" :options="chartOptions" :series="series"></apexchart>
</div>
完毕,
问我是否不清楚。
推荐阅读
- gitlab - 如何确保使用 GitLab CI 更改了文件?
- r - 我打开我的 R 控制台并收到此消息 - '错误:找不到函数“install.packages”'。我该如何解决?
- linux - 如何让 GDB 将我手工制作的可运行 ELF 文件识别为可执行文件?
- npm - 错误:子编译失败:找不到入口模块:错误:无法解析[文件路径]
- python - 如何在不使用拆分方法的情况下找到字符串中最长的单词
- python-3.x - DRF Serializer 在执行 serializer.data 时返回空字典
- nginx - Nginx 和 certbot 的 ERR_SSL_PROTOCOL_ERROR
- postgresql - MacOS 上本地 postgres 实例中的符号链接问题
- php - 使用 $_POST 将用户输入添加到 PHP 中的 JSON 数组返回“null”数组
- google-cloud-dataflow - “ValueError:不支持将写入处置 WRITE_EMPTY 流式插入到 BigQuery”