简体   繁体   English

如何使用d3.js绘制折线图?

[英]How to draw a line graph using d3.js?

I am new to d3.js and am trying to graph three lines on the same plot. 我是d3.js的新手,正在尝试在同一图上绘制三条线。 I'm reading from a tsv file with four columns: time, x, y, and z. 我正在读取包含以下四列的tsv文件:时间,x,y和z。 This is accelerometer data. 这是加速度计数据。 I want to plot the x,y,z columns vs time but can't seem to get it. 我想绘制x,y,z列与时间的关系,但似乎无法理解。 Any suggestions? 有什么建议么?

    function graph(){

// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
    width = 600 - margin.left - margin.right,
    height = 270 - margin.top - margin.bottom;

// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

// Define the axes
var xAxis = d3.svg.axis().scale(x)
    .orient("bottom").ticks(5);

var yAxis = d3.svg.axis().scale(y)
    .orient("left").ticks(5);

// Define the line
var valueline = d3.svg.line()
    .x(function(d) { return x(d.time); })
    .y(function(d) { return y(d.x); });

var     valueline2 = d3.svg.line()
        .x(function(d) { return x(d.time); })
        .y(function(d) { return y(d.y); })

var     valueline3 = d3.svg.line()
        .x(function(d) { return x(d.time); })
        .y(function(d) { return y(d.z); });

// Adds the svg canvas
var svg = d3.select("body")
    .append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
    .append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

// Get the data
d3.tsv("data/data2.tsv", function(error, data) {
    data.forEach(function(d) {
        d.time = +d.time;
        d.x = +d.x;
                d.y = +d.y;
                d.z = +d.z;
    });

    // Scale the range of the data
    x.domain(d3.extent(data, function(d) { return d.time; }));
    y.domain([0, d3.max(data, function(d) { return Math.max(d.x, d.y, d.z); })]);

    // Add the valueline path.
    svg.append("path")      // Add the valueline path.
        .attr("class", "line")
        .attr("d", valueline(data));

        svg.append("path")      // Add the valueline path.
        .attr("class", "line")
                .style("stroke", "red")
        .attr("d", valueline2(data));

        svg.append("path")      // Add the valueline path.
        .attr("class", "line")
                .style("stroke", "green")
        .attr("d", valueline3(data));

    // Add the X Axis
    svg.append("g")         // Add the X Axis
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);

    // Add the Y Axis
    svg.append("g")         // Add the Y Axis
        .attr("class", "y axis")
        .call(yAxis);

});}

You can add each line one at a time to the canvas but it's not very d3 or efficient. 您可以一次将每一行添加到画布,但这不是d3或效率很高。 It's much better to organise your data in the appropriate way. 最好以适当的方式组织数据。 So the the main issue is the way that the data's been prepared. 因此,主要问题是准备数据的方式。 In the example that Lar's pointed to the data is nested using this block of code: 在Lar指向数据的示例中,使用以下代码块嵌套:

var series = color.domain().map(function(name) {
    return {
        name: name,
        values: data.map(function(d) {
            return {
                time: d.time,
                score: +d[name]
            };
        })
    };
});

And what this does is to create an array of 3 objects. 这样做是创建一个由3个对象组成的数组。 Each object has a key value pair: name and an array. 每个对象都有一个键值对:名称和一个数组。 In each array is another object which contains the time and score information for plotting. 在每个数组中是另一个对象,其中包含用于绘制的时间和分数信息。 It is this last array of object that is passed to the line generator. 这是最后一个对象数组,传递给线生成器。

I've created a fiddle, here , that's heavily based on the example that Lar's has pointed to and it's commented throughout. 我在这里创建了一个小提琴,它很大程度上是基于Lar's所指的示例,并在全文中进行了评论。 The trick at this stage is to inspect the elements and use console.log 此阶段的技巧是检查元素并使用console.log

Best of luck. 祝你好运。

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

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