【问题标题】:Rendering multiple charts with React使用 React 渲染多个图表
【发布时间】:2017-09-19 20:58:48
【问题描述】:

我是 React、Highcharts 和 UI 开发的新手。 我想从一组数据中呈现多个图表。目前,该页面仅显示最后一个图表 - 来自数组中的最后一个数据。

function chartToRender(seriesArr){
//console.log(JSON.stringify(application));
 var Chart = React.createClass({
     // When the DOM is ready, create the chart.
     componentDidMount: function() {
       // Extend Highcharts with modules
       if (this.props.modules) {
         this.props.modules.forEach(function(module) {
           module(Highcharts);
         });
       }
       // Set container which the chart should render to.
       this.chart = new Highcharts[this.props.type || "Chart"](
         this.props.container,
         this.props.options
       );
     },
     //Destroy chart before unmount.
     componentWillUnmount: function() {
       this.chart.destroy();
     },
     //Create the div which the chart will be rendered to.
     render: function() {
       return React.createElement('div', {
         id: this.props.container
       });
     }
   }), element2;



return seriesArr.map(application => 
 element2 = React.createElement(Chart, {
     container: 'stockChart',
     type: 'stockChart',
     options: {
       rangeSelector: {
         selected: 0
       },
       title: {
         text: application.app_name + ' application Free Memory'
       },
       tooltip: {
         style: {
           width: '200px'
         },
         valueDecimals: 4,
         shared: true
       },
       xAxis:{
         type:'datetime',
         //categories : timeSeries,
         dateTimeLabelFormats : {
             millisecond: '%Y-%m-%dT%H:%M:%S.%L'
         }
       },
       yAxis: {
         title: {
           text: 'Free Memory'
         }
       },
       series: application.series
     }
   })

)

 }

目前我是通过类上的render函数调用这个函数

render() {

    var seriesArr = getSeriesArr(this.props)


    return(

    <div>
        <div className="row">
            <ul>            
                {chartToRender(seriesArr)}
            </ul>
        </div>
    </div>                   

    )
}

如何在一个页面的下方显示所有图表? “seriesArr”变量具有渲染图表所需的所有数据。

【问题讨论】:

  • 看起来您错过了一些代码 - 任何地方都没有 chartToRender() 函数。可能是Chart 组件末尾的问题?
  • 您好,邓肯,感谢您抽出宝贵时间。我已经编辑了问题以显示我使用的功能。该函数只是定义了 Chart 类并使用它来创建要渲染的元素
  • Stil 看起来有点奇怪 - }), element2; 后面的 createClass() 行是什么意思。那是交付吗?
  • Duncan,我只是声明了 2 个变量 - Chart 和 element2。

标签: javascript reactjs highcharts


【解决方案1】:

尝试将您动态创建的图表组件推入一个数组,然后渲染该数组。这应该让你朝着正确的方向前进。

 let charts =[];
    charts =   seriesArr.map(application => 
    element2 = React.createElement(Chart, {
     container: 'stockChart',
     type: 'stockChart',
     options: {
       rangeSelector: {
         selected: 0
       },
       title: {
         text: application.app_name + ' application Free Memory'
       },
       tooltip: {
         style: {
           width: '200px'
         },
         valueDecimals: 4,
         shared: true
       },
       xAxis:{
         type:'datetime',
         //categories : timeSeries,
         dateTimeLabelFormats : {
             millisecond: '%Y-%m-%dT%H:%M:%S.%L'
         }
       },
       yAxis: {
         title: {
           text: 'Free Memory'
         }
       },
       series: application.series
     }
   })    
    )

render() {   

     return(

        <div>
            <div className="row">
                <ul>            
                    {charts}
                </ul>
            </div>
        </div>                   

        )
    }

【讨论】:

  • 我创建了图表数组——正如你所说,但我仍然显示最后一张图表。
  • 我什至检查了数组有 9 个图表元素,但页面只显示最后一个。
  • 尝试检查 html。还要检查 element2 的范围,这在您的代码示例中对我来说并不清楚。
  • 啊问题是每次在图表组件中的键都不是唯一的。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2021-09-12
  • 2019-11-21
  • 1970-01-01
  • 2022-09-27
  • 2021-07-22
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
相关资源
最近更新 更多