area-stacked.html 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <html>
  2. <head>
  3. <title>Stacked Area Chart</title>
  4. <script type="text/javascript" src="../protovis-d3.1.js"></script>
  5. </head>
  6. <body>
  7. <script type="text/javascript+protovis">
  8. var data = pv.range(4).map(function()
  9. pv.range(0, 10, .1).map(function(x) {
  10. return {x: x, y: Math.sin(x) + Math.random() * .5 + 2};
  11. })),
  12. w = 400,
  13. h = 200,
  14. x = pv.Scale.linear(0, 9.9).range(0, w),
  15. y = pv.Scale.linear(0, 14).range(0, h);
  16. var vis = new pv.Panel()
  17. .width(w)
  18. .height(h)
  19. .bottom(20)
  20. .left(20)
  21. .right(10)
  22. .top(5)
  23. .strokeStyle("#ccc");
  24. vis.add(pv.Panel)
  25. .data(data)
  26. .add(pv.Area)
  27. .data(function(a) a)
  28. .left(function(d) x(d.x))
  29. .bottom(pv.Layout.stack())
  30. .height(function(d) y(d.y));
  31. vis.add(pv.Rule)
  32. .data(x.ticks())
  33. .visible(function(d) d > 0)
  34. .left(function(d) Math.round(x(d)) - .5)
  35. .strokeStyle("rgba(128,128,128,.1)")
  36. .anchor("bottom").add(pv.Label)
  37. .text(function(d) d.toFixed());
  38. vis.add(pv.Rule)
  39. .data(y.ticks())
  40. .visible(function() !(this.index % 2))
  41. .bottom(function(d) Math.round(y(d)) - .5)
  42. .strokeStyle("rgba(128,128,128,.2)")
  43. .anchor("left").add(pv.Label)
  44. .text(function(d) d.toFixed());
  45. vis.render();
  46. </script>
  47. </body>
  48. </html>