react-google-charts react,如何使用grapql中的json?

问题描述

我正在尝试使用 graphql 和 google-react-charts 打印价格图表,我可以成功检索数据,但无法处理它。我不知道如何在 google-react-charts 中使用来自 graphql 的 json 响应。到目前为止我找到的最佳答案是 this

代码

function ExchangeRates() {
  const { loading,error,data } = useQuery(gql`
  {
    ethereum(network: bsc) {
      dexTrades(
        baseCurrency: {is: "address"}
        quoteCurrency: {is: "address"}
        options: {desc: ["block.height","transaction.index"],limit: 10}
      ) {
        block {
          height
          timestamp {
            time(format: "%Y-%m-%d %H:%M:%s")
          }
        }
        transaction {
          index
        }
        baseCurrency {
          symbol
        }
        quoteCurrency {
          symbol
        }
        quotePrice
      }
    }
  }
  `);

  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error :(</p>;
  return (
<Chart
  width='500px'
  height='300px'
  chartType="AreaChart"
  loader={<div>Loading Chart</div>}
  data={[ 
    ["Date","Price"],data.ethereum.dexTrades.map(d => [ d.block.timestamp.time,d.quotePrice ],)

  ]}
  options={{
    height: 300,legend: { position: 'top',maxLines: 3 },vAxis: {
      minValue: 0,},}}
/>
  )}

来自 GraphQL 的 JSON 响应

  "ethereum": {
    "dexTrades": [
      {
        "block": {
          "height": 6161087,"timestamp": {
            "time": "2021-03-31 13:14:08"
          }
        },"transaction": {
          "index": 63
        },"baseCurrency": {
          "symbol": ""
        },"quoteCurrency": {
          "symbol": ""
        },"quotePrice": 1.1840567672583981
      },{
        "block": {
          "height": 6160908,"timestamp": {
            "time": "2021-03-31 13:04:17"
          }
        },"transaction": {
          "index": 102
        },"quotePrice": 1.1939250721714323
      },...
...
...

错误

Uncaught (in promise) Error: Row 1 has 10 columns,but must have 2

问题是我如何仅使用时间戳和报价单以及打印图表来迭代 json。

  data={[ 
    ["Date",)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...