plot.html 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. {% macro draw_plot_tooltips(id, graph) -%}
  2. {%- for stop in graph.timeline -%}
  3. <div id="{{ id }}-tooltip-{{ loop.index0 }}" class="peak peak-{{ id }} tooltip-{% if loop.first %}right{% elif loop.last %}left{% else %}top{% endif %}" title="{{ stop|date(graph.format) }}<br><strong>{{ graph.stat[loop.index0]|intcomma }}</strong>"></div>
  4. {%- endfor -%}
  5. {%- endmacro %}
  6. {% macro draw_plot(name, id, max, variables=[], color='#729fcf') -%}
  7. // Begin drawing graph for {{ id }}
  8. function {{ name }}() {
  9. var margin = 12;
  10. var margin_end = margin * 2;
  11. var canvas = document.getElementById('{{ id }}');
  12. var canvas_parent = $(canvas).parent();
  13. var offset = canvas_parent.offset();
  14. canvas.width = canvas_parent.width();
  15. var ctx = canvas.getContext('2d');
  16. // Guidelines
  17. ctx.beginPath();
  18. ctx.lineWidth = 1;
  19. ctx.strokeStyle = "#EEEEEE";
  20. ctx.fillStyle = "#CCC";
  21. ctx.font = "10pt Helvetica";
  22. ctx.moveTo(margin, margin + 0.5);
  23. ctx.lineTo(canvas.width - margin, margin + 0.5);
  24. {%- if max > 0 %}
  25. {%- for line in [25, 50, 75] %}
  26. {%- if max > 3 %}
  27. {% set y_scales = 4 %}
  28. {%- else -%}
  29. {% set y_scales = max %}
  30. {% endif -%}
  31. {% if loop.index < max and loop.index < 4 %}
  32. {%- set y_label = max - (loop.index / y_scales * max)|int -%}
  33. y_pos = margin + Math.round((canvas.height - margin_end) * ({{ loop.index }} / {{ max }})) + 0.5;
  34. ctx.moveTo(margin, y_pos);
  35. ctx.lineTo(canvas.width - margin, y_pos);
  36. ctx.fillText("{{ y_label }}", margin + 4, y_pos - 4.5);
  37. ctx.fillText("{{ y_label }}", margin + Math.round((canvas.width - margin_end) * 0.25), y_pos - 4.5);
  38. ctx.fillText("{{ y_label }}", margin + Math.round((canvas.width - margin_end) * 0.5), y_pos - 4.5);
  39. ctx.fillText("{{ y_label }}", margin + Math.round((canvas.width - margin_end) * 0.75), y_pos - 4.5);
  40. {%- endif %}
  41. {%- endfor %}
  42. {%- endif %}
  43. ctx.stroke();
  44. // Final guide
  45. ctx.beginPath();
  46. ctx.strokeStyle = "#999999";
  47. ctx.moveTo(margin, canvas.height - margin + 0.5);
  48. ctx.lineTo(canvas.width - margin, canvas.height - margin + 0.5);
  49. ctx.fillText("0", margin + 4, canvas.height - margin - 4);
  50. ctx.fillText("0", margin + Math.round((canvas.width - margin_end) * 0.25), canvas.height - margin - 4);
  51. ctx.fillText("0", margin + Math.round((canvas.width - margin_end) * 0.5), canvas.height - margin - 4);
  52. ctx.fillText("0", margin + Math.round((canvas.width - margin_end) * 0.75), canvas.height - margin - 4);
  53. ctx.stroke();
  54. // Graph line
  55. ctx.beginPath();
  56. ctx.lineWidth = 4;
  57. ctx.strokeStyle = "{{ color }}";
  58. step_size = (canvas.width - margin_end) / {{ variables|length - 1 }};
  59. {%- for stop in variables %}
  60. pos_x = margin + ({{ loop.index0 }} * step_size);
  61. pos_y = {% if max > 0 %}margin + Math.round((canvas.height - margin_end) * {{ ((max - stop) / max)|round(5) }}) + 0.5{% else %}canvas.height - margin{% endif %};
  62. $('#{{ id }}-tooltip-{{ loop.index0 }}').offset({ top: offset.top + pos_y - 2, left: offset.left + pos_x - 1 });
  63. ctx.{% if loop.index0 == 0 %}moveTo{% else %}lineTo{% endif -%}
  64. (pos_x, pos_y);
  65. {%- endfor %}
  66. ctx.stroke();
  67. }
  68. // Update graphs
  69. $(function() { {{ name }}(); $(".peak-{{ id }}").css('border-color', '{{ color }}') });
  70. $(window).resize(function() { {{ name }}(); });
  71. // End drawing graph for {{ id }}
  72. {%- endmacro %}