簡體   English   中英

React-d3圖表未顯示

[英]React-d3 Chart Not Showing

我已經執行了在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元素啟動了圖表的渲染過程。

很困惑為什么會發生這種情況:(

我按照相同的步驟,最初無法顯示它。

但是我注意到的是,在他們從gitHub列出的示例代碼中,他們沒有將LineChart嵌套在Chart中(正如他們在指令中所做的那樣)。 一旦我刪除了包含的“圖表”,我的圖表就出現了。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM