为了账号安全,请及时绑定邮箱和手机立即绑定

将数据放入 d3 反应

将数据放入 d3 反应

守着星空守着你 2023-07-20 16:24:44
你好,我有一个折线图,我应该在其中显示动态数据,如果他们不向我传递数据,那只是一条直线。我遇到的问题是我不知道如何将道具传递给折线图var data `[{    "name": "Samantha White",    "id": "484567489sda",    "address": "4116 Barton Creek Boulevard Austin, TX 78735 USA",    "logo": "https://sssssss.s3.amazonaws.com/ssss/express.png",    "occupancy": {      "capacity": 150,      "occupancy": 0,      "percentage": 0    },    "alerts": {      "conglomerations": 0,      "occupancy": 0    },    "visits": 2721  "plot": []  },  {    "name": "Jacqueline Wells",    "id": "sdasdx45616a4dsa5",    "address": "s15035 Highview Road Grand Junction, CO 81504 USA",    "store_logo": "ssssss.s3.amazonaws.com/ssss/lider.png",    "occupancy": {      "capacity": 150,      "occupancy": 0,      "percentage": 0    },    "alerts": {      "conglomerations": 0,      "occupancy": 0    },    "visits": 2069,    "plot": [9, 29, 48, 52, 88, 107, 130, 127, 122, 157, 165, 172, 211, 214, 267, 296, 331, 353, 371, 381]  }]`;function App() {  const [tiendas, setTiendas] = useState(JSON.parse(data));  const [showTiendas, setShowTiendas] = useState(false)  useEffect(() => {    setTiendas(JSON.parse(data));  }, []);  return (    <div className="w-full ">      <Menu  showTiendas={setShowTiendas} menuData={tiendas} tiendasSet={setTiendas} />      {showTiendas && tiendas.map((tienda) => (        <Card key={Math.random()} {...tienda}></Card>      ))}    </div>  );}export default App;图表的数据将是plot,在卡组件中我命令调用图表,这将是图表的代码,我不知道如何从图表发送调用plot,现在图表有随机数import React, { useState } from "react";import { scaleLinear, scaleBand } from "d3-scale";import Line from "./Line";import { line, curveMonotoneX } from "d3-shape";import { extent } from "d3-array";function Chart(props) {  const [data, setData] = useState(    [    Math.random()*30,    Math.random()*30,    Math.random()*30,    Math.random()*30,    Math.random()*30,    Math.random()*30,    Math.random()*30,  ]  );  const parentWidth = 125;  const margins = {    top: 20,    right: 20,    bottom: 20,    left: 20  };
查看完整描述

2 回答

?
慕的地8271018

TA贡献1796条经验 获得超4个赞

我不太理解您的代码片段,但您可以按照与菜单相同的方式将数据传递到图表。

<Chart data={data} />

然后在您的图表组件中,您将能够通过 props.data 访问该数据


查看完整回答
反对 回复 2023-07-20
?
GCT1015

TA贡献1827条经验 获得超4个赞

我假设您的组件 Chart 位于 Card 组件内。首先,你正在这样做:


{showTiendas && tiendas.map((tienda) => (

    <Card key={Math.random()} {...tienda}></Card> 

))}

然后,在 Card 组件中你应该有这样的内容:


function Card(props) {

    const { plot } = props;


    ...


    return (

        <Chart plot={plot}/> // you should pass the plot data to the Chart component like this

    )

    ...

最后,在图表组件中,您应该能够从中获取数据,props如下所示:


function Chart(props) {

    const { plot } = props;

    

此外,您在第一个数组对象(属性之前)中缺少逗号plot。


查看完整回答
反对 回复 2023-07-20
  • 2 回答
  • 0 关注
  • 84 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信