英文:
ReactJS D3 chart not showing data mapped from Context, but works with static variable
问题
以下是代码的翻译部分:
新手使用D3和React。
我尝试在网格上(x/y轴)显示每个记录的数据,以背景图像(标志)为背景,显示小图像。
如果我使用静态数据(在下面的示例代码中为gcs),则D3图表可以正常工作。但是,如果我尝试从CohortContext中过滤/映射数据,元素将不会显示在图表上。
我认为这可能是一个定时问题,D3在.map返回数据之前呈现,但我可能是错误的。我尝试在useEffect中包装async,但现在我已经束手无策,而且无论如何,根据所有代码都在async中,都没有解决问题。
代码:
import React, { useEffect, useContext } from "react";
import * as d3 from "d3";
import logo from '../../assets/images//charts/360Chart.svg';
import CohortContext from "../../utility/context/cohort/cohort-context";
import Saboteur from '../../assets/images/charts/chartBubbleKey.svg';
function ChartData({ goal }) {
const gcs = [
{
chartAsset: Saboteur,
xCoordinates: 200,
yCoordinates: 300
},
{
chartAsset: Saboteur,
xCoordinates: 150,
yCoordinates: 150
}
];
console.log(gcs);
const width = 800;
const height = 800;
const { cohorts } = useContext(CohortContext);
useEffect(() => {
// 从useContext中检索Goal数据
const fetchData = async () => {
const goalCohorts = cohorts
.filter(records => records.goalId === goal.goalId)
.map(records => {
return {
chartAsset: Saboteur,
xCoordinates: records.xcoordinates,
yCoordinates: records.ycoordinates
};
});
console.log('用于图表的目标队:', goalCohorts);
const svg = d3
.select("#svgcontainer")
.append("svg")
.attr("width", width)
.attr("height", height);
// 背景图表
svg
.append("svg:image")
.attr("xlink:href", () => logo)
.attr("width", 580)
.attr("height", 580)
.attr("x", 110)
.attr("y", 20);
// 添加图表数据
const g = svg
.selectAll("g")
.append("g")
.data(goalCohorts) // 如果我使用.data(gcs),则正常工作
.enter();
g.append("svg:image")
.attr("xlink:href", data => data.chartAsset)
.attr("width", 50)
.attr("height", 50)
.attr("x", data => data.xCoordinates)
.attr("y", data => data.yCoordinates);
};
fetchData();
}, []);
return (
<div className="App">
<div id="svgcontainer"></div>
</div>
);
}
export default ChartData;
注意:代码部分未翻译,只返回翻译好的内容。如果有其他问题,请随时提出。
英文:
New to D3 and React.
I'm trying to show data on a grid (x/y axis) for each record against a background image (logo), showing a small images.
I've got the D3 graph working if I use static data (gcs in my example code below). But, if I try to filter/map the data from CohortContext, the elements are not being shown on the graph.
I think it's a timing issue that the D3 is rendering before the .map returns with the data, but I may be wrong. I tried wrapping the useEffect in an async, but I'm grasping at straws now and it didn't resolve the issue anyway, based on all the code being within the async.
The code:
import React, { useEffect, useContext } from "react"
import * as d3 from "d3"
import logo from '../../assets/images//charts/360Chart.svg'
import CohortContext from "../../utility/context/cohort/cohort-context"
import Saboteur from '../../assets/images/charts/chartBubbleKey.svg'
function ChartData({ goal }) {
const gcs = [
{
chartAsset: Saboteur,
xCoordinates: 200,
yCoordinates: 300
},
{
chartAsset: Saboteur,
xCoordinates: 150,
yCoordinates: 150
}
]
console.log(gcs)
const width = 800
const height = 800
const { cohorts } = useContext(CohortContext)
useEffect(() => {
//Retrieve the Goal data from the useContext
const fetchData = async () => {
const goalCohorts = cohorts
.filter(records => records.goalId === goal.goalId)
.map((records) => {
return {
chartAsset: Saboteur,
xCoordinates: records.xcoordinates,
yCoordinates: records.ycoordinates
}
})
console.log('goal cohorts for Chart :', goalCohorts)
const svg = d3
.select("#svgcontainer")
.append("svg")
.attr("width", width)
.attr("height", height)
//background Chart
svg
.append("svg:image")
.attr("xlink:href", () => logo)
.attr("width", 580)
.attr("height", 580)
.attr("x", 110)
.attr("y", 20)
// Add in the data for the chart data
const g = svg
.selectAll("g")
.append("g")
.data(goalCohorts) // works if I use .data(gcs)
.enter()
g.append("svg:image")
.attr("xlink:href", (data) => data.chartAsset)
.attr("width", 50)
.attr("height", 50)
.attr("x", (data) => data.xCoordinates)
.attr("y", (data) => data.yCoordinates)
}
fetchData()
}, [])
return (
< div className="App" >
<div id="svgcontainer"></div>
</div >
)
}
export default ChartData
If I look at the console.logs, the data is the same - so assuming a timing issue.
Can anyone see anything obvious on why data(goalCohorts) is not showing the data?
I'm using React v18.1, D3 v7.8
答案1
得分: 0
useEffect(() => {
// 从useContext中获取目标数据
const fetchData = async () => {
const goalCohorts = cohorts;
// 其他操作...
}, [cohorts]);
请将 [cohorts]
替换为 [cohorts]
。
英文:
useEffect(() => {
//Retrieve the Goal data from the useContext
const fetchData = async () => {
const goalCohorts = cohorts
.....
, [cohorts]);
Please replace [cohorts] instead of [].
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论