【发布时间】:2021-03-21 15:44:16
【问题描述】:
我正在编写一个使用 Firebase 作为数据库的 React 文章应用程序。 我想创建一个图表,其中 X 轴为星期几,Y 轴为 textarea 中写入的字数。除了弄清楚如何将这些数据正确地放入图表之外,我什么都能做。
import React from "react";
import Chartist from "react-chartist";
import ChartistTooltip from 'chartist-plugin-tooltips-updated';
import {useEffect, useState} from 'react'
import { db } from "../index";
export const SalesValueChart = () => {
const [articles, setArticles] = useState([]);
const reducer = (accumulator, currentValue) => accumulator + currentValue;
useEffect(() => {
getArticles();
console.log(articles.reduce(reducer, 0))
}, []); // blank to run only on first launch
function getArticles() {
db.collection("notes").onSnapshot(function (querySnapshot) {
setArticles(
querySnapshot.docs.map((doc) => doc.data().wordsCounted)
);
});
}
const data = {
labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
series: [articles.reduce(reducer, 0)]
};
const options = {
low: 0,
showArea: true,
fullWidth: true,
axisX: {
position: 'end',
showGrid: true
},
axisY: {
// On the y-axis start means left and end means right
showGrid: false,
showLabel: false,
labelInterpolationFnc: value => `$${value / 1}k`
}
};
const plugins = [
ChartistTooltip()
]
return (
<Chartist data={data} options={{...options, plugins}} type="Line" className="ct-series-g ct-double-octave" />
);
};
export const SalesValueChartphone = () => {
const data = {
labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
series: [[1, 2, 2, 3, 3, 4, 3]]
};
const options = {
low: 0,
showArea: true,
fullWidth: false,
axisX: {
position: 'end',
showGrid: true
},
axisY: {
// On the y-axis start means left and end means right
showGrid: false,
showLabel: false,
labelInterpolationFnc: value => `$${value / 1}k`
}
};
const plugins = [
ChartistTooltip()
]
return (
<Chartist data={data} options={{...options, plugins}} type="Line" className="ct-series-g ct-major-tenth" />
);
};
....................... ....................................
【问题讨论】:
-
在调用
getArticles()之后,您能否提供一个articles数组的示例? propseries应该是一个长度为 7 的二维数组? -
我按预期得到了数组形状的文章。但问题是如何使用保存在 Firebase 中的文章的单词和 Timestamp 属性更改标签和系列。你能帮我做这件事吗?
-
您是否使用外部库来显示图表?
-
是的,我正在使用 Chartist。我已经编辑了帖子,现在您可以查看完整代码
标签: reactjs firebase google-cloud-firestore textarea