高图未渲染:React + Typescript + Highcharts [英] Highcharts not rendering : React+Typescript+Highcharts

查看:72
本文介绍了高图未渲染:React + Typescript + Highcharts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用react调高图表.我有多个提取api调用(为说明起见,我仅添加了2个),这些数据将用于在UI中呈现某些内容.

Trying to bring up a highchart using react. I am having multiple fetch api calls(for illustration, I have added only 2) whose data I will be using to render something in the UI.

在此示例中,data1用于呈现表,data2用于呈现高图.

In this example data1 is used to render a table, data2 is used to render a highchart.

我将这些调用的输出存储在状态对象中.当我调用这些API时,我正在获取数据,但无法将其设置为highcharts的"series"属性以进行渲染,结果什么也没有得到渲染.

I am storing the outputs of these calls in a state object. When I am calling these API's, I am getting the data but unable to set it to "series" property of highcharts for rendering, as a result nothing is getting rendered.

"api2":[ { "name":"Test1", 值":12 }, { "name":"Test2", 值":9 } ]

"api2" : [ { "name" : "Test1", "value" : 12 }, { "name" : "Test2", "value" : 9 } ]

有人可以帮我吗?我要去哪里错了?

Can someone help me with this? Where am I going wrong?

我正在为此使用highcharts-react-official

I am using highcharts-react-official for this

import * as React from 'react';
import Highcharts from 'highcharts'
import HighchartsReact from 'highcharts-react-official';

interface IState {
  data1: [];
  data2: [];
}

interface IProps {}

class Example extends React.Component<IProps,IState> {
  constructor(props:any)
  {
    super(props);
    this.state = {
       data1: [],
       data2: []
    }
  }

  componentDidMount()
  {
      Promise.all([
        fetch('http://localhost:3001/api1'),
        fetch('http://localhost:3001/api2')
      ])
      .then(([res1, res2]) => Promise.all([res1.json(), res2.json()]))
       .then(([data1, data2]) => this.setState({
         data1: data1, 
         data2: data2
      }));
  }

  render() {    
    let options:any;
    options = {
         chart: {
            type: 'column'
         },
      credits: false,
      exporting: {enabled: false},
      title: {
        text: ''
      },
      legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'bottom'
            },
      xAxis: {
        visible:false
      },
      yAxis: {
        visible:false
      },
      plotOptions: {
        column: {
          dataLabels: {
                        enabled: true                         }
        }
      },
      series: this.state.data2
     };

    return(
      <div className="an-content">
          //some table rendering will happen here
          <HighchartsReact
                 highcharts={Highcharts}
                 options={options} 
          />
      </div>
    )
  }
} 
export default Example;

推荐答案

您需要提供Highcharts所需的数据格式:

You need to provide the data format required by Highcharts:

  this.setState({
      data2: data2.map(x => ({ name: x.name, data: [x.value] }))
  });

实时演示: https://codesandbox.io/s/7w2pw4p900

API参考: https://api.highcharts.com/highcharts/series. column.data

这篇关于高图未渲染:React + Typescript + Highcharts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆