Skip to content
This repository was archived by the owner on Nov 27, 2020. It is now read-only.

Conversation

Means88
Copy link

@Means88 Means88 commented Mar 28, 2017

其实这个应该是 g2 的问题
这里是 fix 以后的效果

问题重现:
从这里稍加修改的一个示例

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>简单线图</title>
    <script src="https://a.alipayobjects.com/jquery/jquery/1.11.1/jquery.js"></script>
    <script src="https://a.alipayobjects.com/g/datavis/g2/2.2.6/g2.js"></script>
  </head>
  <body>
    <div id="c1"></div>
    <div id="button"></div>
    <script>
      var data = [];
      // ***** 加上这里 *****
      var newData = [
        {month: 'Jan', temperature: 7.0},
        {month: 'Feb', temperature: 6.9},
        {month: 'Mar', temperature: 9.5},
        {month: 'Apr', temperature: 14.5},
        {month: 'May', temperature: 18.2},
        {month: 'Jun', temperature: 21.5},
        {month: 'Jul', temperature: 25.2},
        {month: 'Aug', temperature: 26.5},
        {month: 'Sep', temperature: 23.3},
        {month: 'Oct', temperature: 18.3},
        {month: 'Nov', temperature: 13.9},
        {month: 'Dec', temperature: 9.6}
      ];
      var chart = new G2.Chart({
        id: 'c1',
        forceFit: true,
        height: 450
      });
      chart.source(data, {
        month: {
          alias: '月份',
          range: [0, 1]
        },
        temperature: {
          alias: '平均温度(°C)'
        }
      });
      chart.line().position('month*temperature').size(2);
      chart.render();
      // ***** 改变数据 *****
      chart.changeData(newData);
    </script>
  </body>
</html>

改变数据后的效果是这样的
image

@jaredleechn
Copy link
Collaborator

👍

这个问题的确很多用户有问到,一直觉得 { this.state.data.length && <Chart /> } 这种方式非常不优雅

但是这样的话,如果中途数据从空变为有意义的数据,就会导致一次 destory,receiveProps 里面判断第一次 nextProps 是有意义的数据,然后执行 init?

cc @simaQ 怎么看

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants