简体   繁体   English

在D3区域图中绘制线

[英]Plot lines in D3 Area Chart

I am working on creating an Area chart using d3 js library. 我正在使用d3 js库创建面积图。

Plotting the area chart without the lines has been achieved however, need inputs on how the lines can be drawn to separate the areas. 然而,已经实现了在不使用线条的情况下绘制面积图的情况,需要输入有关如何绘制线条以分隔区域的信息。

One way I can think of is to draw lines one by one with the below approach wherein x1 = x2, y1=0 and y2=xy plotted point 我能想到的一种方法是使用以下方法一一画线,其中x1 = x2,y1 = 0和y2 = xy绘制点

svg.append("line")
            .data(data)
        .style("stroke", "#e6e3e3")
        .attr("x1", 30)
        .attr("y1", 0)
        .attr("x2", 30)
        .attr("y2", 40);

Need help with below points. 需要以下几点帮助。 1. If there is a way to draw the lines in one go (using a function). 1.如果有一种方法可以一次性绘制线条(使用功能)。 2. In case the lines are drawn separately (as explained above) how to calculate the X and Y axis given the dataset below. 2.如果分别绘制线(如上所述),则在给定以下数据集的情况下如何计算X和Y轴。

var data = [
    {"quarter": "A", "totalIncome": 60 },
    {"quarter": "B", "totalIncome": 70 },
    {"quarter": "C", "totalIncome": 60 },
    {"quarter": "D", "totalIncome": 80 },
    {"quarter": "E", "totalIncome": 75 }
];

I would do something like this : 我会做这样的事情:

If you have the points to make the path at the top like so : 如果您有指向顶部的路径,例如:

var points = { x:0,y:60},{x:50,y:70},{x:100,y:60}.. and so on

And you have data like in the question for the vertical lines. 您将获得像垂直线问题一样的数据。 I would make a new array for the 5 lines you want to create like so : 我将为要创建的5行创建一个新数组,如下所示:

var newLineData = [];
for(i=0;i<data.length;i++){
var thisLine = { x1:i*step,y1:0, x2:i*step,y2:data[i].totalIncome };

newLineData.push(thisLine); //line data

}

Then use this data to create the lines like so : 然后使用此数据创建如下行:

var link = svg.selectAll(".link")
      .data([newLineData])
    .enter().append("line")
      .attr("class", "link")
      .style('stroke','black')
      .style("stroke-width", function(d) { return Math.sqrt(d.value); });
  //console.log(line);

  link.attr("x1", function(d) { console.log(d); return d.x1; })
        .attr("y1", function(d) { return d.y1; }) 
        //.attr("y1", function(d) { return x[d.y1]; }) if using axis : x is the axis
        .attr("x2", function(d) { return d.x2; })
        .attr("y2", function(d) { return d.y2; });

Here is a fiddle how it would work without axis : https://jsfiddle.net/reko91/knetxt5n/ 这是一个无用的提琴技巧: https : //jsfiddle.net/reko91/knetxt5n/

But if you pass the axis like I have shown above it should go in the correct direction for you :) 但是,如果您像上面显示的那样通过轴,则应该朝您的正确方向走:)

Code below too in case JSFiddle goes down. 如果JSFiddle发生故障,下面的代码也是如此。

 var svg = d3.select('#container').append('svg') .attr('width', 500) .attr('height', 500) //.append('g') var data = [ {"quarter": "A", "totalIncome": 60 }, {"quarter": "B", "totalIncome": 70 }, {"quarter": "C", "totalIncome": 60 }, {"quarter": "D", "totalIncome": 80 }, {"quarter": "E", "totalIncome": 75 } ]; var newLineData = []; var step = 50; for(i=1;i<data.length+1;i++){ var thisLine = { x1:i*step,y1:0, x2:i*step,y2:data[i-1].totalIncome }; newLineData.push(thisLine); //line data } var link = svg.selectAll(".link") .data(newLineData) .enter().append("line") .attr("class", "link") .style('stroke','black') .style("stroke-width", function(d) { return Math.sqrt(d.value); }); //console.log(line); link.attr("x1", function(d) { console.log(d); return d.x1; }) .attr("y1", function(d) { return d.y1; }) .attr("x2", function(d) { return d.x2; }) .attr("y2", function(d) { return d.y2; }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <div id='container'> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM