graphing.js 2.1 KB

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