简体   繁体   English

用点创建D3折线图

[英]Creating a D3 line graph with points

I am trying to create a line graph with points using D3. 我正在尝试使用D3创建带有点的折线图。 My data exists in an XML file that I am referencing. 我的数据存在于我正在引用的XML文件中。 Currently, I am able to display the graph with points, but I would like to add lines between them. 目前,我可以显示带有点的图形,但是我想在它们之间添加线。 Here is a picture of my current graph. 这是我当前图表的图片。 图形

I have tried using the following code to create the lines, but I have not been able to display any lines on the graph. 我尝试使用以下代码创建线条,但无法在图形上显示任何线条。

.line {
    fill: none;
    stroke: steelblue;
    stroke-width: 1.5px;
}

//Define line
var line = d3.svg.line()
    .x(function(d) { return d[0]; })
    .y(function(d) { return d[1]; });

//Draw line
svg.append("path")
    .datum(dataset)
    .attr("class", "line")
    .attr("d", line);

I am using these lines of code to pass in my data, which has worked with the way I have created the circles. 我正在使用这些代码行来传递我的数据,这与我创建圆的方式一样。

//Create dataset
for(var i=0;i<record.length;i++)
{
    x = record[i].getElementsByTagName("date")[0].childNodes[0].nodeValue;
    y = y + 1;
    var newArray = [x, y];
    dataset.push(newArray);
}

I am looking for a solution that will work with the way I am accessing my data. 我正在寻找一种可以与我访问数据的方式一起使用的解决方案。 I would appreciate any suggestions. 我将不胜感激任何建议。 Thanks! 谢谢!

you are almost there to be honest! 你几乎要诚实了! You just missed out your lineFunction. 您只是错过了lineFunction。 Here is an example on jsfiddle for you: http://jsfiddle.net/wd6r5/ 这是为您提供的jsfiddle示例: http : //jsfiddle.net/wd6r5/

//Create dataset
var dataset = [];
for(var i = 0; i < 7; i++) {
    x = 50 * i; // record[i].getElementsByTagName("date")[0].childNodes[0].nodeValue;
    y = 50 * i + 10; //y + 1;
    var newArray = [x, y];
    dataset.push(newArray);
}

var lineFunction = d3.svg.line()
.x(function(d) { return d[0]; })
.y(function(d) { return d[1]; })
.interpolate("linear");

d3.select("svg")
.append("path")
.attr("stroke", "#333")
.attr("stroke-width", 1.2)
.attr("d", lineFunction(dataset));

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

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