簡體   English   中英

如何使用 json 響應數據創建簡單的折線圖

[英]How to create simple line chart with json response data

我正在從 API 端點獲取價格變化數據,並從 axios 收到此響應

0: (2) [1639382650242, 48759.49757943229]
1: (2) [1639386250855, 49131.24712464529]
2: (2) [1639389730718, 48952.65930253478]
3: (2) [1639393442318, 48821.67463307133]
4: (2) [1639396908986, 48835.214009032316]
5: (2) [1639400493105, 48618.25814914513]
6: (2) [1639404186695, 48572.06354948456]
7: (2) [1639407821756, 47974.48879991822]
8: (2) [1639411648037, 47105.42054562465]
9: (2) [1639414998352, 47176.56337927206]

我嘗試使用文檔中的這個示例代碼來呈現一個簡單的折線圖,但它說我必須至少有 2 列數據:

                         <Chart
                            width={'600px'}
                            height={'400px'}
                            chartType="Line"
                            loader={<div>Loading Chart</div>}
                            data={
                                historicData.prices
                            }
                          />

有沒有辦法讓它與 google-react-charts 一起使用?

React Google 折線圖示例

請根據上圖檢查您是否錯過了數據部分中的此標簽?

timeprice作為標簽添加到第一個條目。 您可以將時間戳轉換為實際的日期對象。

export default function App() {
  const data = [
    [1639382650242, 48759.49757943229],
    [1639386250855, 49131.24712464529],
    [1639389730718, 48952.65930253478],
    [1639393442318, 48821.67463307133],
    [1639396908986, 48835.214009032316],
    [1639400493105, 48618.25814914513],
    [1639404186695, 48572.06354948456],
    [1639407821756, 47974.48879991822],
    [1639411648037, 47105.42054562465],
    [1639414998352, 47176.56337927206]
  ];

  const processedData = [[{ type: "date", label: "time" }, "price"]].concat(
    data.map(([timestamp, price]) => {
      return [new Date(timestamp), price];
    })
  );

  return (
    <div className="App">
      <Chart
        width={"600px"}
        height={"400px"}
        chartType="Line"
        loader={<div>Loading Chart</div>}
        data={processedData}
      />
    </div>
  );
}

代碼沙箱 => https://codesandbox.io/s/busy-johnson-1xc0w?file=/src/App.js

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM