graphing.js.coffee 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #= require d3
  2. #= require rickshaw
  3. #= require_self
  4. # This is not included in the core application.js bundle.
  5. window.renderGraph = ($chart, data, peaks, name) ->
  6. graph = new Rickshaw.Graph
  7. element: $chart.find(".chart").get(0)
  8. width: 700
  9. height: 240
  10. series: [
  11. data: data
  12. name: name
  13. color: 'steelblue'
  14. ]
  15. x_axis = new Rickshaw.Graph.Axis.Time(graph: graph)
  16. annotator = new Rickshaw.Graph.Annotate
  17. graph: graph
  18. element: $chart.find(".timeline").get(0)
  19. $.each peaks, ->
  20. annotator.add this, "Peak"
  21. y_axis = new Rickshaw.Graph.Axis.Y
  22. graph: graph
  23. orientation: 'left'
  24. tickFormat: Rickshaw.Fixtures.Number.formatKMBT
  25. element: $chart.find(".y-axis").get(0)
  26. graph.onUpdate ->
  27. mean = d3.mean data, (i) -> i.y
  28. standard_deviation = Math.sqrt(d3.mean(data.map((i) -> Math.pow(i.y - mean, 2))))
  29. minX = d3.min data, (i) -> i.x
  30. maxX = d3.max data, (i) -> i.x
  31. graph.vis.append("svg:line")
  32. .attr('x1', graph.x(minX))
  33. .attr('x2', graph.x(maxX))
  34. .attr('y1', graph.y(mean))
  35. .attr('y2', graph.y(mean))
  36. .attr 'class', 'summary-statistic mean'
  37. graph.vis.append("svg:line")
  38. .attr('x1', graph.x(minX))
  39. .attr('x2', graph.x(maxX))
  40. .attr('y1', graph.y(mean + standard_deviation))
  41. .attr('y2', graph.y(mean + standard_deviation))
  42. .attr 'class', 'summary-statistic one-std'
  43. graph.vis.append("svg:line")
  44. .attr('x1', graph.x(minX))
  45. .attr('x2', graph.x(maxX))
  46. .attr('y1', graph.y(mean + 2 * standard_deviation))
  47. .attr('y2', graph.y(mean + 2 * standard_deviation))
  48. .attr 'class', 'summary-statistic two-std'
  49. graph.vis.append("svg:line")
  50. .attr('x1', graph.x(minX))
  51. .attr('x2', graph.x(maxX))
  52. .attr('y1', graph.y(mean + 3 * standard_deviation))
  53. .attr('y2', graph.y(mean + 3 * standard_deviation))
  54. .attr 'class', 'summary-statistic three-std'
  55. graph.render()