<legend id='XHmGU'><style id='XHmGU'><dir id='XHmGU'><q id='XHmGU'></q></dir></style></legend>

<tfoot id='XHmGU'></tfoot>

        <i id='XHmGU'><tr id='XHmGU'><dt id='XHmGU'><q id='XHmGU'><span id='XHmGU'><b id='XHmGU'><form id='XHmGU'><ins id='XHmGU'></ins><ul id='XHmGU'></ul><sub id='XHmGU'></sub></form><legend id='XHmGU'></legend><bdo id='XHmGU'><pre id='XHmGU'><center id='XHmGU'></center></pre></bdo></b><th id='XHmGU'></th></span></q></dt></tr></i><div id='XHmGU'><tfoot id='XHmGU'></tfoot><dl id='XHmGU'><fieldset id='XHmGU'></fieldset></dl></div>

        <small id='XHmGU'></small><noframes id='XHmGU'>

        • <bdo id='XHmGU'></bdo><ul id='XHmGU'></ul>

      1. React 基于 api 响应渲染 Chart.js

        时间:2023-11-01
          <i id='KVeUW'><tr id='KVeUW'><dt id='KVeUW'><q id='KVeUW'><span id='KVeUW'><b id='KVeUW'><form id='KVeUW'><ins id='KVeUW'></ins><ul id='KVeUW'></ul><sub id='KVeUW'></sub></form><legend id='KVeUW'></legend><bdo id='KVeUW'><pre id='KVeUW'><center id='KVeUW'></center></pre></bdo></b><th id='KVeUW'></th></span></q></dt></tr></i><div id='KVeUW'><tfoot id='KVeUW'></tfoot><dl id='KVeUW'><fieldset id='KVeUW'></fieldset></dl></div>

          <small id='KVeUW'></small><noframes id='KVeUW'>

            <tbody id='KVeUW'></tbody>
          <legend id='KVeUW'><style id='KVeUW'><dir id='KVeUW'><q id='KVeUW'></q></dir></style></legend>
            • <bdo id='KVeUW'></bdo><ul id='KVeUW'></ul>

                <tfoot id='KVeUW'></tfoot>

                1. 本文介绍了React 基于 api 响应渲染 Chart.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 reacts 和 chart.js,以及 chart-js/react 包装器.我从 API 动态提取数据,然后根据返回的数据绘制图表.问题是它只是 API 调用中显示在图表中的一个名称.应该是三个名字.

                  我认为问题在于 Graph 组件的渲染是在 API 返回所有数据之前渲染的,因此该图仅包含名字.至少这是我的想法.我曾尝试使用 componentWillMount 函数,但无法使其正常工作.

                  谁能发现我下面的代码有什么问题?

                  var App = React.createClass({获取初始状态:函数(){返回 {名称:[]}},组件DidMount:函数(){$.get(url, 函数(数据) {this.setState({ 名称:数据 })}.bind(this));},渲染:函数(){返回(

                  <图表名称={this.state.names}/></div>)}});var Graph = React.createClass({渲染:函数(){for(var i = 0; i < this.props.names.length; i++) {names.push(this.props.names[i].name)}var 图表数据 = {标签:名字,数据集:[{数据:目标},{数据:预测}]};返回 <LineChart 数据={chartData} width="600" height="250"/>}});

                  解决方案

                  你必须像下面这样控制你的状态:

                  var App = React.createClass({获取初始状态:函数(){返回 {名称:[],已加载:假}},组件DidMount:函数(){$.get(url, 函数(数据) {这个.setState({名称:数据,已加载:真})}.bind(this));},渲染:函数(){返回(

                  {this.state.isLoaded ?<图表名称={this.state.names}/>: <div>Still Loading... </div>}</div>)}});

                  因此,一旦您获得所有数据,您就可以渲染图表,否则会显示加载屏幕.看看这个 链接 可能对你有帮助.希望它对你有意义.

                  I am using reacts and chart.js, and the chart-js/react wrapper. I am dynamically pulling the data from an API and then basing the chart on the data returned. The issue is that it is only one name from the API call that is being displayed in the graph. There should be three names.

                  I think the problem is that the rendering of the Graph component is being rendered before all the data has been returned by the API, so the graph only contains the first name. At least this is what i think. I have tried working with the componentWillMount function but cannot get it working.

                  can anyone spot anything wrong with my code below?

                  var App = React.createClass({
                  
                      getInitialState: function(){
                          return {
                              names: []
                          }
                      },
                  
                      componentDidMount: function() {
                          $.get(url, function (data) {
                              this.setState({ names: data })
                          }.bind(this));
                      },
                  
                      render: function() {
                          return(
                              <div>
                                  <Graph names={this.state.names}/>               
                              </div>
                          )
                      }
                  });
                  
                  var Graph = React.createClass({
                  
                      render: function() {
                          for(var i = 0; i < this.props.names.length; i++) {
                              names.push(this.props.names[i].name)
                          }
                          var chartData =  {
                              labels: names,
                              datasets: [{
                                     data: goals
                                  },
                                  {
                                      data: predictions
                                  }]
                          };
                          return <LineChart data={chartData} width="600" height="250"/>
                      }
                  });
                  

                  解决方案

                  You have to control your state like below:

                  var App = React.createClass({
                      getInitialState: function(){
                          return {
                              names: [],
                              isLoaded: false
                          }
                      },
                      componentDidMount: function() {
                          $.get(url, function (data) {
                              this.setState({ 
                                  names: data,
                                  isLoaded: true
                              })
                          }.bind(this));
                      },
                      render: function() {
                          return(
                              <div>
                                  {this.state.isLoaded ? <Graph names={this.state.names}/> : <div>Still Loading... </div> }
                              </div>
                          )
                      }
                  });
                  

                  So once you get all your data you can render your chart otherwise show the loading screen. Take a look at this Link probably it will be helpfull for you. Hope it makes sense for you.

                  这篇关于React 基于 api 响应渲染 Chart.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:更改图表的标签 下一篇:Chart.js - 在条形图中绘制水平线(类型栏)

                  相关文章

                    <bdo id='H82Nh'></bdo><ul id='H82Nh'></ul>

                    <legend id='H82Nh'><style id='H82Nh'><dir id='H82Nh'><q id='H82Nh'></q></dir></style></legend>
                  1. <small id='H82Nh'></small><noframes id='H82Nh'>

                    <i id='H82Nh'><tr id='H82Nh'><dt id='H82Nh'><q id='H82Nh'><span id='H82Nh'><b id='H82Nh'><form id='H82Nh'><ins id='H82Nh'></ins><ul id='H82Nh'></ul><sub id='H82Nh'></sub></form><legend id='H82Nh'></legend><bdo id='H82Nh'><pre id='H82Nh'><center id='H82Nh'></center></pre></bdo></b><th id='H82Nh'></th></span></q></dt></tr></i><div id='H82Nh'><tfoot id='H82Nh'></tfoot><dl id='H82Nh'><fieldset id='H82Nh'></fieldset></dl></div>
                  2. <tfoot id='H82Nh'></tfoot>