简体   繁体   English

如何用数据在两个轴之间画一条线? (d3.js)

[英]how to draw a line between two axes with a data? (d3.js)

I am trying to create a parallel coordinates with my own code, so that i can learn d3.js very well. 我正在尝试使用自己的代码创建一个平行坐标,以便可以很好地学习d3.js。 Now i am stuck in a situation. 现在我陷入了困境。 I have two axis with some data in it 我有两个轴,里面有一些数据 在此处输入图片说明

and i want to connect the data with a line. 我想用一条线连接数据。 I tried of getting the position of the two data in the axes but its not working for me and its got complicated 我试图获取两个数据在轴上的位置,但是它对我不起作用,并且变得复杂 在此处输入图片说明

Is there any way to connect the axes like this? 有没有办法像这样连接轴? 在此处输入图片说明

The jsfiddle link is commented below. jsfiddle链接在下面进行了注释。 Please find it 请找到它

Thanks for the help 谢谢您的帮助

Appending an svg:line between those ticks is the way to go but the hard part is finding the proper positioning within the overall SVG document. 在这些刻度之间添加svg:line是可行的方法,但困难的是要在整个SVG文档中找到合适的位置。 Since things are being transitioned twice (once for the axis g and once for the tick g ), you have two options, sum up all the positions by using d3.tranform on the elements, or use something like getBoundingClientRect on the node. 由于事物被转换了两次(一次用于轴g ,一次用于刻度g ),因此您有两个选择,可以通过在元素上使用d3.tranform汇总所有位置,或者在节点上使用诸如getBoundingClientRect之类的东西。

In the below code I've chosen the later. 在下面的代码中,我选择了后者。 This quick function will take the text value of any two ticks and draw a line. 此快速功能将获取任意两个刻度的text值并画一条线。 Note, those text values have to be unique: 注意,这些文本值必须是唯一的:

function addLine(t1, t2){
    var ticks = {};
    d3.selectAll('.tick text').each(function(d) { 
        ticks[d3.select(this).text()] = this;
    });
    var pos1 = ticks[t1].getBoundingClientRect();
    var pos2 = ticks[t2].getBoundingClientRect();

    svg.append('line')
      .attr('x1', pos1.left)
      .attr('y1', pos1.top + 5)
      .attr('x2', pos2.left - 5)
      .attr('y2', pos2.top + 5)
      .style('stroke','black');
}

addLine('a', 'ab');
addLine('a', 'bb');

Full working example: 完整的工作示例:

 var w = 200; var h = 400; var padding = 100; var x = ["a","b"]; var z = ["aa","ab","ba","bb"]; var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); for(var i=1;i<3;i++){ var yScale = d3.scale.linear() .domain([0, i === 1 ? x.length : z.length]) .range([h - padding, padding]); //Define Y axis var yAxis = d3.svg.axis() .scale(yScale) .ticks(i === 1 ? x.length : z.length) .orient("left") .tickSize(1) .tickFormat(function(d){ return i === 1 ? x[d] : z[d]; }) // .style("text-anchor", "middle"); //Create SVG element svg.append("g") .attr("class", "axis" + i) .attr("transform", "translate("+(i*padding)+",0)") .call(yAxis) .attr("fill","red"); } function addLine(t1, t2){ var ticks = {}; d3.selectAll('.tick text').each(function(d) { ticks[d3.select(this).text()] = this; }); var pos1 = ticks[t1].getBoundingClientRect(); var pos2 = ticks[t2].getBoundingClientRect(); svg.append('line') .attr('x1', pos1.left) .attr('y1', pos1.top + 5) .attr('x2', pos2.left - 5) .attr('y2', pos2.top + 5) .style('stroke','black'); } addLine('a', 'ab'); addLine('a', 'bb'); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 

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

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