<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

        React render Chart.js based on api response(React 基于 api 响应渲染 Chart.js)
          <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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)

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

                        <legend id='H82Nh'><style id='H82Nh'><dir id='H82Nh'><q id='H82Nh'></q></dir></style></legend>
                          <tbody id='H82Nh'></tbody>
                      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>