[英]How to show monthly data in Recharts stacked bar chart?
I have some data which looks like so:我有一些数据看起来像这样:
[ { "CarbonAmount": 120, "CarbonDescription": null, "Date": "2022-03-14" }, { "CarbonAmount": 140, "CarbonDescription": "Electricity", "Date": "2022-04-11" } ]
What I am trying to do is get it in the format so I can visualize it with recharts.我想要做的是以格式获取它,以便我可以使用重新图表将其可视化。 There may be a lot of data and What I want is to show exactly show 12 graphs (each graph for one month) and I want exactly like the picture attached
可能有很多数据,我想要的是准确显示 12 个图表(每个图表一个月),我想要完全像附图
I did try to prepare data for the chart but still not sure why I am not able to show monthly data.我确实尝试为图表准备数据,但仍然不确定为什么我无法显示月度数据。 example code is available in this link
此链接中提供了示例代码
any help will be greatly appreciated任何帮助将不胜感激
because you only focusing on the month, i don't think it is necessary to include day in the Date
key.因为您只关注月份,所以我认为没有必要在
Date
键中包含日期。
const data = [ { "CarbonAmount": 120, "CarbonDescription": null, "Date": "2022-03-14" }, { "CarbonAmount": 140, "CarbonDescription": "Electricity", "Date": "2022-04-11" }, { "CarbonAmount": 150, "CarbonDescription": null, "Date": "2022-05-17" }, { "CarbonAmount": 120, "CarbonDescription": "Electricity", "Date": "2022-06-14" }, { "CarbonAmount": 150, "CarbonDescription": "Electricity", "Date": "2022-07-18" }, { "CarbonAmount": 180, "CarbonDescription": "Electricity", "Date": "2022-08-16" }, { "CarbonAmount": 190, "CarbonDescription": "Electricity", "Date": "2022-09-21" }, { "CarbonAmount": 210, "CarbonDescription": "Electricity", "Date": "2022-10-18" }, { "CarbonAmount": 220, "CarbonDescription": "Electricity", "Date": "2022-11-15" }, { "CarbonAmount": 260, "CarbonDescription": "Electricity", "Date": "2022-12-12" }, { "CarbonAmount": -120, "CarbonDescription": "Carbon Credit", "Date": "2022-01-11" }, { "CarbonAmount": 20, "CarbonDescription": "Solar Panel", "Date": "2022-02-08" }, { "CarbonAmount": -30, "CarbonDescription": "Carbon Credit", "Date": "2022-03-22" }, { "CarbonAmount": -20, "CarbonDescription": "Carbon Credit", "Date": "2022-12-12" }, { "CarbonAmount": 120, "CarbonDescription": null, "Date": "2022-01-14" }, { "CarbonAmount": 140, "CarbonDescription": "Electricity", "Date": "2022-02-11" }, { "CarbonAmount": 150, "CarbonDescription": null, "Date": "2022-03-17" }, { "CarbonAmount": 120, "CarbonDescription": "Electricity", "Date": "2022-04-14" }, { "CarbonAmount": 150, "CarbonDescription": "Electricity", "Date": "2022-05-18" }, { "CarbonAmount": 80, "CarbonDescription": "Electricity", "Date": "2022-06-16" }, { "CarbonAmount": 190, "CarbonDescription": "Electricity", "Date": "2022-07-21" }, { "CarbonAmount": 66, "CarbonDescription": "Electricity", "Date": "2022-08-18" }, { "CarbonAmount": 99, "CarbonDescription": "Electricity", "Date": "2022-09-15" }, { "CarbonAmount": 160, "CarbonDescription": "Electricity", "Date": "2022-10-12" }, { "CarbonAmount": -120, "CarbonDescription": "Carbon Credit", "Date": "2022-11-11" }, { "CarbonAmount": 90, "CarbonDescription": "Solar Panel", "Date": "2022-12-08" }, { "CarbonAmount": -90, "CarbonDescription": "Carbon Credit", "Date": "2022-03-22" }, { "CarbonAmount": -60, "CarbonDescription": "Carbon Credit", "Date": "2022-12-12" } ] const result = data.sort((a, b) => Number(a.Date.split("-")[1] - Number(b.Date.split("-")[1]))).reduce((p, { CarbonDescription, CarbonAmount, Date }) => { Date = Date.slice(0, Date.lastIndexOf("-")) const found = p.findIndex(p => p.Date === Date); CarbonDescription && (CarbonDescription = CarbonDescription.replace(" ", "_")) found?== -1: p[found][CarbonDescription] = CarbonAmount. p,push({ Date: [CarbonDescription]; CarbonAmount }) return p, }; []). console;log(result);
your fetchData
function should look something like this.你的
fetchData
函数应该看起来像这样。
const fetchData = async () => {
const result = jsonData.sort((a, b) => Number(a.Date.split("-")[1] - Number(b.Date.split("-")[1]))).reduce((p, { CarbonDescription, CarbonAmount, Date }) => {
Date = Date.slice(0, Date.lastIndexOf("-"))
const found = p.findIndex(p => p.Date === Date);
CarbonDescription && (CarbonDescription = CarbonDescription.replace(" ", "_"))
found !== -1 ? p[found][CarbonDescription] = CarbonAmount : p.push({ Date, [CarbonDescription]: CarbonAmount })
return p;
}, []);
setData(result);
};
i did try this in your code sandbox and i think it's working like you're expected.我确实在您的代码沙箱中尝试过这个,我认为它的工作方式与您预期的一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.