toolkit/javascript/d3/examples/line/line.js
changeset 47 c0b4a8b5a012
equal deleted inserted replaced
46:efd9c589177a 47:c0b4a8b5a012
       
     1 var data = d3.range(20).map(function(i) {
       
     2   return {x: i / 19, y: (Math.sin(i / 3) + 1) / 2};
       
     3 });
       
     4 
       
     5 var w = 450,
       
     6     h = 275,
       
     7     p = 20,
       
     8     x = d3.scale.linear().domain([0, 1]).range([0, w]),
       
     9     y = d3.scale.linear().domain([0, 1]).range([h, 0]);
       
    10 
       
    11 var vis = d3.select("body")
       
    12     .data([data])
       
    13   .append("svg:svg")
       
    14     .attr("width", w + p * 2)
       
    15     .attr("height", h + p * 2)
       
    16   .append("svg:g")
       
    17     .attr("transform", "translate(" + p + "," + p + ")");
       
    18 
       
    19 var rules = vis.selectAll("g.rule")
       
    20     .data(x.ticks(10))
       
    21   .enter().append("svg:g")
       
    22     .attr("class", "rule");
       
    23 
       
    24 rules.append("svg:line")
       
    25     .attr("x1", x)
       
    26     .attr("x2", x)
       
    27     .attr("y1", 0)
       
    28     .attr("y2", h - 1);
       
    29 
       
    30 rules.append("svg:line")
       
    31     .attr("class", function(d) { return d ? null : "axis"; })
       
    32     .attr("y1", y)
       
    33     .attr("y2", y)
       
    34     .attr("x1", 0)
       
    35     .attr("x2", w + 1);
       
    36 
       
    37 rules.append("svg:text")
       
    38     .attr("x", x)
       
    39     .attr("y", h + 3)
       
    40     .attr("dy", ".71em")
       
    41     .attr("text-anchor", "middle")
       
    42     .text(x.tickFormat(10));
       
    43 
       
    44 rules.append("svg:text")
       
    45     .attr("y", y)
       
    46     .attr("x", -3)
       
    47     .attr("dy", ".35em")
       
    48     .attr("text-anchor", "end")
       
    49     .text(y.tickFormat(10));
       
    50 
       
    51 vis.append("svg:path")
       
    52     .attr("class", "line")
       
    53     .attr("d", d3.svg.line()
       
    54     .x(function(d) { return x(d.x); })
       
    55     .y(function(d) { return y(d.y); }));
       
    56 
       
    57 vis.selectAll("circle.line")
       
    58     .data(data)
       
    59   .enter().append("svg:circle")
       
    60     .attr("class", "line")
       
    61     .attr("cx", function(d) { return x(d.x); })
       
    62     .attr("cy", function(d) { return y(d.y); })
       
    63     .attr("r", 3.5);