toolkit/javascript/d3/examples/axis/axis-multiples.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 path, .axis line {
       
    25   fill: none;
       
    26   stroke: #fff;
       
    27   shape-rendering: crispEdges;
       
    28 }
       
    29 
       
    30     </style>
       
    31   </head>
       
    32   <body>
       
    33     <script type="text/javascript">
       
    34 
       
    35 var m = [20, 20, 20, 20],
       
    36     w = 960 - m[1] - m[3],
       
    37     h = 140 - m[0] - m[2],
       
    38     parse = d3.time.format("%b %Y").parse;
       
    39 
       
    40 // Scales. Note the inverted domain for the y-scale: bigger is up!
       
    41 var x = d3.time.scale().range([0, w]),
       
    42     y = d3.scale.linear().range([h, 0]);
       
    43 
       
    44 // X-axis.
       
    45 var xAxis = d3.svg.axis()
       
    46     .scale(x)
       
    47     .tickSize(-h);
       
    48 
       
    49 // An area generator, for the light fill.
       
    50 var area = d3.svg.area()
       
    51     .interpolate("monotone")
       
    52     .x(function(d) { return x(d.date); })
       
    53     .y0(h)
       
    54     .y1(function(d) { return y(d.price); });
       
    55 
       
    56 // A line generator, for the dark stroke.
       
    57 var line = d3.svg.line()
       
    58     .interpolate("monotone")
       
    59     .x(function(d) { return x(d.date); })
       
    60     .y(function(d) { return y(d.price); });
       
    61 
       
    62 d3.csv("../data/stocks.csv", function(data) {
       
    63 
       
    64   // Nest values by symbol.
       
    65   var symbols = d3.nest()
       
    66       .key(function(d) { return d.symbol; })
       
    67       .entries(data);
       
    68 
       
    69   // Parse dates and numbers. We assume values are sorted by date.
       
    70   // Also compute the maximum price per symbol, needed for the y-domain.
       
    71   symbols.forEach(function(s) {
       
    72     s.values.forEach(function(d) { d.date = parse(d.date); d.price = +d.price; });
       
    73     s.maxPrice = d3.max(s.values, function(d) { return d.price; });
       
    74   });
       
    75 
       
    76   // Compute the minimum and maximum date across symbols.
       
    77   x.domain([
       
    78     d3.min(symbols, function(d) { return d.values[0].date; }),
       
    79     d3.max(symbols, function(d) { return d.values[d.values.length - 1].date; })
       
    80   ]);
       
    81 
       
    82   // Add an SVG element for each symbol, with the desired dimensions and margin.
       
    83   var svg = d3.select("body").selectAll("svg")
       
    84       .data(symbols)
       
    85     .enter().append("svg:svg")
       
    86       .attr("width", w + m[1] + m[3])
       
    87       .attr("height", h + m[0] + m[2])
       
    88     .append("svg:g")
       
    89       .attr("transform", "translate(" + m[3] + "," + m[0] + ")");
       
    90 
       
    91   // Add the area path elements. Note: the y-domain is set per element.
       
    92   svg.append("svg:path")
       
    93       .attr("class", "area")
       
    94       .attr("d", function(d) { y.domain([0, d.maxPrice]); return area(d.values); });
       
    95 
       
    96   // Add the x-axis.
       
    97   svg.append("svg:g")
       
    98       .attr("class", "x axis")
       
    99       .attr("transform", "translate(0," + h + ")")
       
   100       .call(xAxis);
       
   101 
       
   102   // Add the line path elements. Note: the y-domain is set per element.
       
   103   svg.append("svg:path")
       
   104       .attr("class", "line")
       
   105       .attr("d", function(d) { y.domain([0, d.maxPrice]); return line(d.values); });
       
   106 
       
   107   // Add a small label for the symbol name.
       
   108   svg.append("svg:text")
       
   109       .attr("x", w - 6)
       
   110       .attr("y", h - 6)
       
   111       .attr("text-anchor", "end")
       
   112       .text(function(d) { return d.key; });
       
   113 });
       
   114 
       
   115     </script>
       
   116   </body>
       
   117 </html>