簡體   English   中英

d3.js中的折線圖使用d3.chart.js

[英]Line charts in d3.js using d3.chart.js

我想將d3.chart()用於我已經編寫過的圖表。 我找到了circlebarchartsd3.chart()示例,但沒有找到line charts示例。 我的圖表是折線圖,我需要在d3.charts()使用以下代碼

svg.append("path")
      .datum(data)
      .attr("class", "line")
      .attr("d", line);

但嘗試使用這樣的時候我遇到了問題

<!DOCTYPE html>
<html>
 <head>
    <script type="text/javascript" src="d3.v3.min.js"></script>
    <script type="text/javascript" src="d3.chart.min.js"></script>

 </head>
 <body>
 <div id="vis"></div>
<script type="text/javascript">


d3.chart("linechart", {

  initialize: function() {
    // create a base scale we will use later.
   var chart = this;
    chart.w = chart.base.attr('width') || 200;
    chart.h = chart.base.attr('height') || 150;
    chart.w = +chart.w;
    chart.h = +chart.h;

   chart.x = d3.scale.linear()
      .range([0, chart.w]);

  chart.y = d3.scale.linear()
      .range([chart.h,0]);

  chart.base.classed('line', true);

  this.areas = {};
      chart.areas.lines = chart.base.append('g')
              .classed('lines', true)
              .attr('width', chart.w)
              .attr('height', chart.h)

       chart.line = d3.svg.line()
                  .x(function(d) { return chart.x(d.x);})   
                  .y(function(d) { return chart.y(d.y);});


    this.layer("lines", chart.areas.lines, {
      dataBind: function(data) {
        // update the domain of the xScale since it depends on the data
           chart.y.domain([d3.min(data,function(d){return d.y}),d3.max(data,function(d){return d.y})])
            chart.x.domain(d3.extent(data, function(d) { return d.x; }));

        // return a data bound selection for the passed in data.
                                    return this.append("path")
                                              .datum(data)
                                              .attr("d", chart.line)
                                              .attr('stroke','#1ABC9C')
                                              .attr('stroke-width','2')
                                              .attr('fill','none');
      },
      insert: function() {

        return null;

      },

    });
  },

  // configures the width of the chart.
  // when called without arguments, returns the
  // current width.
  width: function(newWidth) {
    if (arguments.length === 0) {
      return this.w;
    }
    this.w = newWidth;
    return this;
  },

  // configures the height of the chart.
  // when called without arguments, returns the
  // current height.
  height: function(newHeight) {
    if (arguments.length === 0) {
      return this.h;
    }
    this.h = newHeight;
    return this;
  },

});

var data = [
{x: 0,y:190},
  {x: 1,y:10},{x: 2,y:40},{x: 3,y:90},
  {x: 4,y:30},{x: 5,y:20},{x: 6,y:10}
];

var chart1 = d3.select("#vis")
  .append("svg")
  .chart("linechart")
  .width(720)
  .height(320)

chart1.draw(data);
</script>
</body>
</html>

錯誤:

未捕獲錯誤:[d3.chart]圖層選擇未正確綁定。

我也得到了線和錯誤。

注:獲得d3.chart.min.js這個鏈接獲取d3.v3.min.js這個鏈接

更新:我從@LarsKotthoff回答得到答案,但圖像有所不同。 檢查此鏈接在申請D3之前申請D3之后

看起來您已經混淆了insertdataBind操作 - 在前者中,您應該追加新元素,而后者僅綁定數據。 通過以下修改,您的代碼適合我。

dataBind: function(data) {
    // update the domain of the xScale since it depends on the data
       chart.y.domain([d3.min(data,function(d){return d.y}),d3.max(data,function(d){return d.y})])
        chart.x.domain(d3.extent(data, function(d) { return d.x; }));

    // return a data bound selection for the passed in data.
    return this.selectAll("path").data([data]);
  },
 insert: function() {
    return this.append("path")
              .attr("d", chart.line)
              .attr('stroke','#1ABC9C')
              .attr('stroke-width','2')
              .attr('fill','none');

  }

請注意,這不適用於多行 - 為此,將.data([data])更改為.data(data)並使用嵌套數組,例如[[{x:0,y:0},...], [{x:1,y:1},...], ...]

暫無
暫無

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

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