vue.js - Apex 线面积图未显示在 Vuejs 的页面上
问题描述
我被困在无法在页面上显示图表的页面上。
为了简化我所做的事情,这里是代码沙箱:我在控制台中看到有关数据的错误,我不确定。
https://codesandbox.io/s/compassionate-snyder-bckoq
我想显示这样的图表(作为示例),但我无法在代码沙箱上显示
请帮忙。
解决方案
系列的格式与 ApexCharts 不一致。您需要转换数据以匹配 ApexChart 格式。
请查看代码和框中的更改。 https://codesandbox.io/s/small-dew-eztod?file=/src/components/HelloWorld.vue
options: {
// X axis labels
xaxis: {
type: 'date',
categories: ["2021-05-04", "2021-05-05", "2021-05-07"]
},
},
series: [
{
name: "total",
data: [2, 2, 1],
},
{
name: "pending",
data: [0, 1, 0],
},
{
name: "approved",
data: [2, 1, 1],
},
{
name: "rejected",
data: [0, 0, 0],
},
],
转换数据以适应 ApexChart
const data = {
"2021-05-04": {
total: 2,
pending: 0,
approved: 2,
rejected: 0,
},
"2021-05-05": {
total: 2,
pending: 1,
approved: 1,
rejected: 0,
},
"2021-05-07": {
total: 1,
pending: 0,
approved: 1,
rejected: 0,
},
};
const xaxis = {
type: "date",
categories: Object.keys(data).map((key) => key), // ['2021-05-04', '2021-05-05', '2021-05-07']
};
let statusObj = [];
for (const dataValue of Object.values(data)) { // get the values from keys '2021-05-04', '2021-05-05' ...
// loop the values, e.g. 1st loop: { total: 2, pending: 0, approved: 2, rejected: 0, }
for (const [key, value] of Object.entries(dataValue)) {
// take 'total' as example, find if statusObj already has { name: 'total', data: [x] }, e.g. statusObj = { name: 'total', data: [1] }
const existingStatusIndex = Object.keys(statusObj).find(
(sKey) => statusObj[sKey].name === key
);
// if yes, return the index of it
if (existingStatusIndex) {
// add new data value to existing data object. e.g. { name: 'total', data: [1, 2] }
statusObj[existingStatusIndex].data.push(value);
continue;
}
// if no, create a new object and add it to statusObj
statusObj.push({
name: key,
data: [value],
});
}
}
输出:
xaxis {
type: 'date',
categories: [ '2021-05-04', '2021-05-05', '2021-05-07' ]
}
statusObj [
{ name: 'total', data: [ 2, 2, 1 ] },
{ name: 'pending', data: [ 0, 1, 0 ] },
{ name: 'approved', data: [ 2, 1, 1 ] },
{ name: 'rejected', data: [ 0, 0, 0 ] }
]
推荐阅读
- javascript - 在位于 SRC 文件夹的 index.html 文件中注册一个服务工作者 - ReactJS
- c - 将数据从 SVGimage 格式转换为 xmlDocPtr 共振峰的函数错误
- java - 在 Jtree 中转换字符串
- react-navigation - 使用 React Navigation 5 在选项卡双击时重置选项卡内的堆栈
- python - 类型错误'numpy.float64'没有我无法解决的len(),说服Python它正在处理一个数组?
- excel - 如何加快在共享文件中运行的 excel 宏?
- swift - SwiftUI - 在 AppDelegate 的当前视图之上呈现一个视图
- asp.net-core - 挑战在 Asp .Net Core 3 中究竟意味着什么?
- lotus-notes - 如何使用 lotusscript 在电子邮件中嵌入图像?
- android - 将导航按钮转换为后退按钮?