React-d3图表未显示

时间:2017-06-02 14:39:55

标签: reactjs svg react-d3

我已经按照 react-d3 获取图表的分步说明执行了:

http://www.reactd3.org/get_start/

我将此添加到我的代码中,我看到没有

但是,如果我进行检查,那么我会看到 SVG 标记,其中包含一些内容。

enter image description here

是否某些元素已被展平,或者您是否需要设置一些内容以在浏览器中显示SVG?

我的确切代码如下:

import React from 'react';

var Chart = require('react-d3-core').Chart;
var LineChart = require('react-d3-basic').LineChart;

class WelcomeView extends React.Component {

  render() {

    var chartData = [
      {"name":"Darron Weissnat IV","BMI":20.72,"age":39,"birthday":"2005-01-03T00:00:00.000Z","city":"East Russel","married":false,"index":0},
      {"name":"Guido Conroy","BMI":25.17,"age":39,"birthday":"1977-04-20T00:00:00.000Z","city":"Scarlettland","married":true,"index":20},
      {"name":"Miss Demond Weissnat V","BMI":21.44,"age":19,"birthday":"2007-06-09T00:00:00.000Z","city":"Savionberg","married":false,"index":21},
      {"name":"Easton Mante","BMI":20.61,"age":43,"birthday":"2007-01-29T00:00:00.000Z","city":"Kutchberg","married":false,"index":22},
      {"name":"Dayton Ebert","BMI":29.88,"age":20,"birthday":"1978-04-27T00:00:00.000Z","city":"West Wiley","married":true,"index":23}
    ]

    var width = 700,
      height = 300,
      margins = {left: 100, right: 100, top: 50, bottom: 50},
      title = "User sample",
      chartSeries = [
        {
          field: 'BMI',
          name: 'BMI',
          color: '#ff7f0e'
        }
      ],
      // your x accessor
      x = function(d) {
        return d.index;
      }

    return (
      <div>
          <div>
            <Chart
              title={title}
              width={width}
              height={height}
              margins= {margins}
            >
              <LineChart
                margins= {margins}
                title={title}
                data={chartData}
                width={width}
                height={height}
                chartSeries={chartSeries}
                x={x}
              />
            </Chart>
          </div>
      </div>
    );
  }
}

export default WelcomeView;

我认为我正确地遵循了一切,所以我不确定我做错了什么。

更新:

如果我在图表元素上方动态添加另一个SVG元素,例如圆圈:

<svg height="100" width="100">
  <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>

然后出现圆圈,神奇地添加了SVG元素,启动了图表的渲染过程。

非常困惑为什么会发生这种情况:(

1 个答案:

答案 0 :(得分:8)

我遵循了相同的步骤,最初无法显示它。

然而,我注意到的是,他们在gitHub列出的示例代码中 他们没有在Chart中嵌套LineChart(正如他们在说明中所做的那样)。一旦我删除了包含的图表&#39;我的图表出现了。

&#13;
&#13;
return (
      <LineChart
        //showXGrid={false}
        //showYGrid={false}
        margins= {margins}
        title={title}
        data={chartData}
        width={width}
        height={height}
        chartSeries={chartSeries}
        x={x}
      />
    );
&#13;
&#13;
&#13;