ReactJS D3图表不显示从上下文映射的数据,但对静态变量有效。

huangapple go评论60阅读模式
英文:

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 &quot;react&quot;
import * as d3 from &quot;d3&quot;
import logo from &#39;../../assets/images//charts/360Chart.svg&#39;
import CohortContext from &quot;../../utility/context/cohort/cohort-context&quot;
import Saboteur from &#39;../../assets/images/charts/chartBubbleKey.svg&#39;
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(() =&gt; {
//Retrieve the Goal data from the useContext
const fetchData = async () =&gt; {
const goalCohorts = cohorts
.filter(records =&gt; records.goalId === goal.goalId)
.map((records) =&gt; {
return {
chartAsset: Saboteur,
xCoordinates: records.xcoordinates,
yCoordinates: records.ycoordinates
}
})
console.log(&#39;goal cohorts for Chart :&#39;, goalCohorts)
const svg = d3
.select(&quot;#svgcontainer&quot;)
.append(&quot;svg&quot;)
.attr(&quot;width&quot;, width)
.attr(&quot;height&quot;, height)
//background Chart
svg
.append(&quot;svg:image&quot;)
.attr(&quot;xlink:href&quot;, () =&gt; logo)
.attr(&quot;width&quot;, 580)
.attr(&quot;height&quot;, 580)
.attr(&quot;x&quot;, 110)
.attr(&quot;y&quot;, 20)
// Add in the data for the chart data
const g = svg
.selectAll(&quot;g&quot;)
.append(&quot;g&quot;)
.data(goalCohorts)  // works if I use .data(gcs)
.enter()
g.append(&quot;svg:image&quot;)
.attr(&quot;xlink:href&quot;, (data) =&gt; data.chartAsset)
.attr(&quot;width&quot;, 50)
.attr(&quot;height&quot;, 50)
.attr(&quot;x&quot;, (data) =&gt; data.xCoordinates)
.attr(&quot;y&quot;, (data) =&gt; data.yCoordinates)
}
fetchData()
}, [])
return (
&lt; div className=&quot;App&quot; &gt;
&lt;div id=&quot;svgcontainer&quot;&gt;&lt;/div&gt;
&lt;/div &gt;
)
}
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(() =&gt; {
//Retrieve the Goal data from the useContext
const fetchData = async () =&gt; {
const goalCohorts = cohorts
.....

, [cohorts]);

Please replace [cohorts] instead of [].

huangapple
  • 本文由 发表于 2023年2月10日 15:15:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定