简体   繁体   English

d3中json数据的折线图

[英]line chart of json data in d3

I am learning d3. 我正在学习d3。 I want to make a line chart using json data. 我想用json数据制作折线图。 The data I am using is: 我使用的数据是:

var data = [
    { "at": "2014-11-18T07:29:03.859Z", "value": 0.553292},
    { "at": "2014-11-18T07:28:53.859Z", "value": 0.563292},
    { "at": "2014-11-18T07:28:43.859Z", "value": 0.573292},
    { "at": "2014-11-18T07:28:33.859Z", "value": 0.583292},
    { "at": "2014-11-18T07:28:13.859Z", "value": 0.553292},
    { "at": "2014-11-18T07:28:03.859Z", "value": 0.563292}]; 

I want "at" on x axis and "value" on y axis. 我希望x轴上的“at”和y轴上的“value”。 Also I need to parse the "at" as time only. 另外,我需要解析“at”只作为时间。 Please provide me pointers how i will proceed further. 请指点我将如何进一步。 The code till now I have implemented is below. 到目前为止我已经实现的代码如下。 I tried to look for documentation for this but found none. 我试图为此寻找文档,但没有找到。

<html>
    <head>
        <title>line chart on json</title>
        <script src="http://d3js.org/d3.v2.js"></script>
        <style>
            /* tell the SVG path to be a thin blue line without any area fill */
            path {
                stroke: steelblue;
                stroke-width: 1;
                fill: none;
            }
        </style>
    </head>
    <body>

    <script>

    var width = 400;
    var height = 150;

    var data = [
    { "at": "2014-11-18T07:29:03.859Z", "value": 0.553292},
    { "at": "2014-11-18T07:28:53.859Z", "value": 0.563292},
    { "at": "2014-11-18T07:28:43.859Z", "value": 0.573292},
    { "at": "2014-11-18T07:28:33.859Z", "value": 0.583292},
    { "at": "2014-11-18T07:28:13.859Z", "value": 0.553292},
    { "at": "2014-11-18T07:28:03.859Z", "value": 0.563292}]; 

    var x = d3.scale.ordinal(); 

    var y = d3.scale.linear().range([height, 0]);
    var xAxis = d3.svg.axis().scale(x).orient("bottom"); 
    var line = d3.svg.line()
        .x(function(d) { return x(d.at);})
        .y(function(d) { return y(d.value); })
        .interpolate("linear")
    var graph = d3.select(graph1).append("svg:svg").attr("width", "300").  attr("height", "150");
    function make_y_axis() {
        return d3.svg.axis().scale(y).orient("left").ticks(5)
    }

    </script>

    </body>
</html>

Look at this example http://bl.ocks.org/crayzeewulf/9719255 请看这个例子http://bl.ocks.org/crayzeewulf/9719255

There used 2 graphs, but you can use just one and place data as you want. 使用了2个图表,但您可以只使用一个图表并根据需要放置数据。

As for date you can look at this example http://jsfiddle.net/robdodson/KWRxW/ , xAxis in particular. 至于日期,您可以查看此示例http://jsfiddle.net/robdodson/KWRxW/ ,特别是xAxis。

var xAxis = d3.svg.axis()
    .scale(x)
    .orient('bottom')
    .ticks(d3.time.days, 1)
    .tickFormat(d3.time.format('%a %d'))
    .tickSize(0)
    .tickPadding(8);

What is graph1 in the below line: 什么是以下行中的graph1:

var graph = d3.select(graph1).append("svg:svg").attr("width", "300"). var graph = d3.select(graph1).append(“svg:svg”)。attr(“width”,“300”)。 attr("height", "150"); attr(“身高”,“150”);

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

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