toolkit/javascript/d3/examples/axis/axis-transition.html
changeset 47 c0b4a8b5a012
equal deleted inserted replaced
46:efd9c589177a 47:c0b4a8b5a012
       
     1 <!DOCTYPE html>
       
     2 <html>
       
     3   <head>
       
     4     <script type="text/javascript" src="../../d3.js"></script>
       
     5     <script type="text/javascript" src="../../d3.csv.js"></script>
       
     6     <script type="text/javascript" src="../../d3.time.js"></script>
       
     7     <style type="text/css">
       
     8 
       
     9 body {
       
    10   font: 10px sans-serif;
       
    11   margin: 0;
       
    12 }
       
    13 
       
    14 path.line {
       
    15   fill: none;
       
    16   stroke: #666;
       
    17   stroke-width: 1.5px;
       
    18 }
       
    19 
       
    20 path.area {
       
    21   fill: #e7e7e7;
       
    22 }
       
    23 
       
    24 .axis {
       
    25   shape-rendering: crispEdges;
       
    26 }
       
    27 
       
    28 .x.axis line {
       
    29   stroke: #fff;
       
    30 }
       
    31 
       
    32 .x.axis .minor {
       
    33   stroke-opacity: .5;
       
    34 }
       
    35 
       
    36 .x.axis path {
       
    37   display: none;
       
    38 }
       
    39 
       
    40 .y.axis line, .y.axis path {
       
    41   fill: none;
       
    42   stroke: #000;
       
    43 }
       
    44 
       
    45     </style>
       
    46   </head>
       
    47   <body>
       
    48     <script type="text/javascript">
       
    49 
       
    50 var m = [20, 40, 20, 20],
       
    51     w = 960 - m[1] - m[3],
       
    52     h = 140 - m[0] - m[2],
       
    53     parse = d3.time.format("%b %Y").parse;
       
    54 
       
    55 // Scales. Note the inverted domain for the y-scale: bigger is up!
       
    56 var x = d3.time.scale().range([0, w]),
       
    57     y = d3.scale.linear().range([h, 0]);
       
    58 
       
    59 // Axes.
       
    60 var xAxis = d3.svg.axis().scale(x).tickSize(-h).tickSubdivide(true),
       
    61     yAxis = d3.svg.axis().scale(y).ticks(4).orient("right");
       
    62 
       
    63 // An area generator, for the light fill.
       
    64 var area = d3.svg.area()
       
    65     .interpolate("monotone")
       
    66     .x(function(d) { return x(d.date); })
       
    67     .y0(h)
       
    68     .y1(function(d) { return y(d.price); });
       
    69 
       
    70 // A line generator, for the dark stroke.
       
    71 var line = d3.svg.line()
       
    72     .interpolate("monotone")
       
    73     .x(function(d) { return x(d.date); })
       
    74     .y(function(d) { return y(d.price); });
       
    75 
       
    76 d3.csv("../data/stocks.csv", function(data) {
       
    77 
       
    78   // Filter to one symbol; the S&P 500.
       
    79   var values = data.filter(function(d) {
       
    80     return d.symbol == "S&P 500";
       
    81   });
       
    82 
       
    83   // Parse dates and numbers. We assume values are sorted by date.
       
    84   values.forEach(function(d) {
       
    85     d.date = parse(d.date);
       
    86     d.price = +d.price;
       
    87   });
       
    88 
       
    89   // Compute the minimum and maximum date, and the maximum price.
       
    90   x.domain([values[0].date, values[values.length - 1].date]);
       
    91   y.domain([0, d3.max(values, function(d) { return d.price; })]);
       
    92 
       
    93   // Add an SVG element with the desired dimensions and margin.
       
    94   var svg = d3.select("body").append("svg:svg")
       
    95       .attr("width", w + m[1] + m[3])
       
    96       .attr("height", h + m[0] + m[2])
       
    97     .append("svg:g")
       
    98       .attr("transform", "translate(" + m[3] + "," + m[0] + ")");
       
    99 
       
   100   // Add the clip path.
       
   101   svg.append("svg:clipPath")
       
   102       .attr("id", "clip")
       
   103     .append("svg:rect")
       
   104       .attr("width", w)
       
   105       .attr("height", h);
       
   106 
       
   107   // Add the area path.
       
   108   svg.append("svg:path")
       
   109       .attr("class", "area")
       
   110       .attr("clip-path", "url(#clip)")
       
   111       .attr("d", area(values));
       
   112 
       
   113   // Add the x-axis.
       
   114   svg.append("svg:g")
       
   115       .attr("class", "x axis")
       
   116       .attr("transform", "translate(0," + h + ")")
       
   117       .call(xAxis);
       
   118 
       
   119   // Add the y-axis.
       
   120   svg.append("svg:g")
       
   121       .attr("class", "y axis")
       
   122       .attr("transform", "translate(" + w + ",0)")
       
   123       .call(yAxis);
       
   124 
       
   125   // Add the line path.
       
   126   svg.append("svg:path")
       
   127       .attr("class", "line")
       
   128       .attr("clip-path", "url(#clip)")
       
   129       .attr("d", line(values));
       
   130 
       
   131   // Add a small label for the symbol name.
       
   132   svg.append("svg:text")
       
   133       .attr("x", w - 6)
       
   134       .attr("y", h - 6)
       
   135       .attr("text-anchor", "end")
       
   136       .text(values[0].symbol);
       
   137 
       
   138   // On click, update the x-axis.
       
   139   svg.on("click", function() {
       
   140     var n = values.length - 1,
       
   141         i = Math.floor(Math.random() * n / 2),
       
   142         j = i + Math.floor(Math.random() * n / 2) + 1;
       
   143     x.domain([values[i].date, values[j].date]);
       
   144     var t = svg.transition().duration(750);
       
   145     t.select(".x.axis").call(xAxis);
       
   146     t.select(".area").attr("d", area(values));
       
   147     t.select(".line").attr("d", line(values));
       
   148   });
       
   149 });
       
   150 
       
   151     </script>
       
   152   </body>
       
   153 </html>