【发布时间】:2021-12-23 15:33:40
【问题描述】:
作为标题,我正在尝试使用 vue2 在 Vue apexcharts(折线图)中加载 Axios 数据。 我已经阅读了 apexcharts 的文档,但仍然无法弄清楚如何将数据传递到图表中。我想将日期作为 x 轴,将其他日期作为折线图中的值。
还有,我在vue devtools里查过了,好像数据传成功了?
我的数据格式是这样的:
follower = [
{
date: '2021-11-10',
follower: 2000,
following: 500,
media: 150
}
{
date: '2021-11-11',
follower: 2000,
following: 500,
media: 150
}
]
我的图表初始化:
<template>
<apexchart
ref="sample"
width="500"
type="area"
:options="chartOptions"
:series="series"
>
</apexchart>
</template>
export default {
name: "FollowerLineApex",
components: {
apexcharts: VueApexCharts,
},
data: function() {
return {
series: [{
name: 'test',
data: []
}],
chartOptions: {
chart: {
height: 350,
type: 'area',
},
dataLabels: {
enabled: true
},
title: {
text: 'Example-1',
},
xaxis: {
type: "datetime"
},
}
}
},
下面是我在 Axios 部分的代码:
created: function () {
this.getData()
},
methods: {
getData() {
this.$axios.get('/api/')
.then(res => {
this.series = [{
data: res.data["follower"]
}]
console.log(this.series)
this.$refs.sample.updateOptions({
series: [{
data: this.series
}]
})
})
.catch(err => {
console.log(err);
})
}
},
【问题讨论】:
-
这是 vue 2 还是 vue 3?
-
我正在使用 vue2。我会在文章中补充,谢谢提醒!
-
在 3 个数据点(
follower、following和media)中,您希望它们如何绘制图表?它们是分开的系列吗? -
我想让每个日期一起显示这 3 个数据点。喜欢链接中的折线图:apexcharts.com/javascript-chart-demos/line-charts/data-labels
-
再过一遍文档就好了。是的,我认为这 3 个数据点应该是单独的系列。
标签: javascript vue.js axios apexcharts