3

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

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

我将此添加到我的代码中,但我什么也没看到。

但是,如果我检查的话,我确实会看到一个SVG标签,里面有一些东西。

在此处输入图像描述

是否有一些元素被展平,或者您是否需要设置一些东西以在浏览器中显示 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 元素的添加神奇地启动了图表的渲染过程。

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

4

1 回答 1

8

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

但是我注意到的是,在他们从gitHub列出的示例代码中, 他们没有将 LineChart 嵌套在 Chart 中(就像他们在说明中所做的那样)。一旦我删除了包含的“图表”,我的图表就会出现。

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

于 2017-07-17T19:23:10.043 回答