简体   繁体   English

断开d3折线图?

[英]Disconnect d3 line chart?

I have this d3 line chart: 我有这个d3折线图:

http://jovansfreelance.com/bikestats/d3/tipsy.php http://jovansfreelance.com/bikestats/d3/tipsy.php

You can see the JS source there, it's located at http://jovansfreelance.com/bikestats/d3/js/d3LineChart.js . 您可以在http://jovansfreelance.com/bikestats/d3/js/d3LineChart.js上找到JS源。

I added an alert to the page so it will alert the data object when the page loads. 我向页面添加了警报,因此它将在页面加载时向数据对象发出警报。 As you see, I have multiple values for each year. 如您所见,我每年都有多个值。 That's fine and the graph looks like it should, except the values from 2012 shouldn't connect back to the values from 2006. 很好,该图看起来应该是这样,除了2012年的值不应与2006年的值连接起来。

Basically, my question is, how do I disconnect the graph at certain places? 基本上,我的问题是,如何在某些地方断开图形? Every time the x-axis value is 2012, I need to disconnect the graph (not show the line from 2012 back to 2006). 每次x轴值为2012时,我都需要断开图形连接(不显示从2012年到2006年的直线)。

You need to change the structure of your data. 您需要更改数据的结构。 As written now, the generateData() function takes the 20 values and pushes them onto the same array. 就像现在所写的那样, generateData()函数采用20个值并将它们推入同一数组中。 This results in the data variable being an array of objects with the following structure: 这导致数据变量是具有以下结构的对象数组:

data = [
  { value: "253.65", date: "2006"},
  { value: "269.67", date: 2007 },
...
];

This data structure is causing d3 to draw a single, connected line from 2006 to 2012 then back to 2006 again two times. 这种数据结构使d3从2006年到2012年绘制一条连接的线,然后再绘制两次到2006年。

To get three different lines, you need to break the data up so that d3 recognizes each series as a distinct object. 要获得三行不同的内容,您需要分解数据,以便d3将每个系列识别为一个不同的对象。 Consider getting your data into a strucuture like this: 考虑将数据放入这样的结构中:

data = [
{
    name: "series01",
    points: [ 
        { value: "253.65", date: "2006"},
        { value: "269.67", date: 2007 },
        ...
    ]
},
{
    name: "series02",
    points: [ 
        { value: "253.65", date: "2006"},
        { value: "269.67", date: 2007 },
        ...
    ]
},
{
    name: "series01",
    points: [ 
        { value: "253.65", date: "2006"},
        { value: "269.67", date: 2007 },
        ...
    ]
}

]; ];

Of course if you do this, you will need to rewrite the other d3 components do deal with the fact that the x and y values for your circles are nested within the points property of each series object. 当然,如果这样做,您将需要重写其他d3组件,以处理圆圈的x和y值嵌套在每个系列对象的points属性内的事实。 See multiple lines d3 for more about drawing multiple lines. 有关绘制多条线的更多信息,请参见多条线d3

Added in reply to a comment: 在回复评论时添加:

The size loop that assigns rawdata elements to data one at a time is the problem. 问题是,一次将原始数据元素分配给数据的大小循环。 It works against the way d3 handles data. 它与d3处理数据的方式相反。 In the first loop, d3 enters 7 paths and circles for the 7 elements in the data array. 在第一个循环中,d3为数据数组中的7个元素输入7条路径和圆。

.enter() only adds new svg elements based on changes in the data array. .enter()仅基于数据数组中的更改添加新的svg元素。 If no key is used, changes are driven by array length. 如果不使用任何键,则更改由阵列长度决定。 Since the length of the data array does not change on the second and third times through the loop, no new paths are added. 由于数据数组的长度在循环中第二次和第三次不变,因此不会添加任何新路径。

To get all three lines, remove the loop, use .data(rawdata) for the data and change the line 'd' attribute to .attr('d', line(d.points)) . 要获得所有三行,请删除循环,对数据使用.data(rawdata)并将行'd'属性更改为.attr('d', line(d.points))

Unfortunately, this will mess up the circles. 不幸的是,这会搞乱圈子。 For help with drawing circles and lines on the same data take a look at Mike Bostocks svg:g element suggestion in this google groups discussion https://groups.google.com/forum/?fromgroups=#!topic/d3-js/8XLzUYLoFnY 如需在同一数据上绘制圆和线的帮助,请参阅此Google组讨论中的Mike Bostocks svg:g元素建议https://groups.google.com/forum/?fromgroups=#!topic/d3-js/ 8XLzUYLoFnY

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

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