chart.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. {% removeemptylines %}
  2. {% assign data = charts[include.chart-id] %}
  3. {% assign id = include.id | default: include.chart-id %}
  4. {% assign height = include.height | default: data.height | default: 10 %}
  5. {% assign class = include.class %}
  6. {% if include['size'] == 'sm' %}
  7. {% assign class = class | append: ' chart-sm' | strip %}
  8. {% assign height = 2.5 %}
  9. {% elsif include['size'] == 'lg' %}
  10. {% assign class = class | append: ' chart-lg' | strip %}
  11. {% assign height = 15 %}
  12. {% endif %}
  13. {% if data %}
  14. {% if data.extend %}
  15. {% assign data = charts[data.extend] | concat_objects: charts[include.chart-id] %}
  16. {% endif %}
  17. <div id="chart-{{ id }}" class="position-relative {% if class %} {{ class }}{% endif %}"></div>
  18. {% capture script %}
  19. {% assign chart-type = data.type | default: 'bar' -%}
  20. {% removeemptylines %}
  21. <script>
  22. document.addEventListener("DOMContentLoaded", function () {
  23. {% if environment == 'development' %}
  24. window.tabler_chart = window.tabler_chart || {};
  25. {% endif %}
  26. window.ApexCharts && ({% if environment == 'development' %}window.tabler_chart["chart-{{ include.chart-id }}"] = {% endif %}new ApexCharts(document.getElementById('chart-{{ id }}'), {
  27. chart: {
  28. type: "{{ chart-type }}",
  29. fontFamily: 'inherit',
  30. height: {{ height | times: 16 }},
  31. {% if data.sparkline %}
  32. sparkline: {
  33. enabled: true
  34. },
  35. {% else %}
  36. parentHeightOffset: 0,
  37. toolbar: {
  38. show: false,
  39. },
  40. {% endif %}
  41. {% unless data.animations %}
  42. animations: {
  43. enabled: false
  44. },
  45. {% endunless %}
  46. {% if data.stacked %}
  47. stacked: true,
  48. {% endif %}
  49. },
  50. {% if chart-type == 'bar' %}
  51. plotOptions: {
  52. bar: {
  53. {% if data.horizontal %}
  54. barHeight: '50%',
  55. horizontal: true,
  56. {% else %}
  57. columnWidth: '50%',
  58. {% endif %}
  59. }
  60. },
  61. {% endif %}
  62. {% if chart-type == 'radialBar' %}
  63. plotOptions: {
  64. radialBar: {
  65. startAngle: -120,
  66. endAngle: 120,
  67. hollow: {
  68. margin: 16,
  69. size: "50%"
  70. },
  71. dataLabels: {
  72. show: true,
  73. value: {
  74. offsetY: -8,
  75. fontSize: '24px',
  76. }
  77. },
  78. },
  79. },
  80. {% endif %}
  81. {% if chart-type == 'bar' or chart-type == 'area' %}
  82. dataLabels: {
  83. enabled: {% if data.datalabels %}true{% else %}false{% endif %},
  84. },
  85. {% endif %}
  86. {% if chart-type == 'area' %}
  87. fill: {
  88. colors: [
  89. 'color-mix(in srgb, transparent, var(--tblr-primary) {% if chart-type == 'area' %}16%{% else %}100%{% endif %})',
  90. 'color-mix(in srgb, transparent, var(--tblr-primary) {% if chart-type == 'area' %}16%{% else %}80%{% endif %})',
  91. ],
  92. type: 'solid'
  93. },
  94. {% endif %}
  95. {% if data.title %}
  96. title: {
  97. text: "{{ data.title | escape }}",
  98. margin: 0,
  99. floating: true,
  100. offsetX: 10,
  101. style: {
  102. fontSize: '18px',
  103. },
  104. },
  105. {% endif %}
  106. {% if chart-type == 'area' or chart-type == 'line' %}
  107. stroke: {
  108. width: {% if data.stroke-width %}[{{ data.stroke-width | join: ', '}}]{% else %}2{% endif %},
  109. {% if data.stroke-dash %}dashArray: [{{ data.stroke-dash | join: ', '}}],{% endif %}
  110. lineCap: "round",
  111. curve: "{{ data.stroke-curve | default: 'smooth' }}",
  112. },
  113. {% endif %}
  114. {% if data.series %}
  115. {% if chart-type == 'pie' or chart-type == 'donut' or chart-type == 'radialBar' %}
  116. series: [{% for serie in data.series %}{{ serie.data }}{% unless forloop.last %}, {% endunless %}{% endfor %}],
  117. labels: [{% for serie in data.series %}"{{ serie.name }}"{% unless forloop.last %}, {% endunless %}{% endfor %}],
  118. {% else %}series: [{% for serie in data.series %}{
  119. name: "{{ serie.name }}",
  120. data: [{{ serie.data | join: ', ' }}]
  121. }{% unless forloop.last %},{% endunless %}{% endfor %}],
  122. {% endif %}
  123. {% endif %}
  124. tooltip: {
  125. theme: 'dark'
  126. },
  127. grid: {
  128. {% unless data.sparkline %}
  129. padding: {
  130. top: -20,
  131. right: 0,
  132. left: -4,
  133. bottom: -4
  134. },
  135. {% endunless %}
  136. {% if data.hide-grid %}
  137. show: false,
  138. {% else %}
  139. strokeDashArray: 4,
  140. {% if data.show-x %}
  141. xaxis: {
  142. lines: {
  143. show: true
  144. }
  145. },
  146. {% endif %}
  147. {% endif %}
  148. },
  149. {% if data.show-data-labels %}
  150. dataLabels: {
  151. enabled: true,
  152. },
  153. {% endif %}
  154. {% if data.categories or data.datetime %}
  155. xaxis: {
  156. labels: {
  157. padding: 0,
  158. {% if data.x-formatter %}
  159. formatter: function(val) {
  160. return {{ data.x-formatter }}
  161. },
  162. {% endif %}
  163. },
  164. tooltip: {
  165. enabled: false
  166. },
  167. {% if chart-type == 'area' or chart-type == 'bar' %}
  168. axisBorder: {
  169. show: false,
  170. },
  171. {% endif %}
  172. {% if data.categories %}
  173. categories: [{% for category in data.categories %}'{{ category }}'{% unless forloop.last %}, {% endunless %}{% endfor %}],
  174. {% endif %}
  175. {% if data.datetime %}
  176. type: 'datetime',
  177. {% endif %}
  178. },
  179. {% endif %}
  180. {% unless chart-type == 'pie' or chart-type == 'donut' or chart-type == 'radialBar' %}
  181. yaxis: {
  182. labels: {
  183. padding: 4
  184. },
  185. {% if data.y-max %}
  186. max: {{ data.y-max }},
  187. {% endif %}
  188. {% if data.y-title %}
  189. title: {
  190. text: '{{ data.y-title | escape }}'
  191. }
  192. {% endif %}
  193. },
  194. {% endunless %}
  195. {% if data.datetime %}
  196. {% assign date = data.start-date | default: '2020-06-20' | date: "%s" %}
  197. {% assign days-count = data.series[0].data.size %}
  198. labels: [
  199. {% for i in (1..days-count) %}{% assign date = date | plus: 86400 %}'{{ date | timestamp_to_date }}'{% unless forloop.last %}, {% endunless %}{% endfor %}
  200. ],
  201. {% endif %}
  202. {% if data.series %}
  203. colors: [{% for serie in data.series %}{% assign color = serie.color | default: data.color | default: 'primary' %}
  204. 'color-mix(in srgb, transparent, var(--tblr-{{ color }}) {% if serie.color-opacity %}{{ serie.color-opacity }}{% else %}100%{% endif %})'{% unless forloop.last %}, {% endunless %}{% endfor %}
  205. ],
  206. {% endif %}
  207. legend: {
  208. {% if data.legend %}
  209. show: true,
  210. position: 'bottom',
  211. offsetY: 12,
  212. markers: {
  213. width: 10,
  214. height: 10,
  215. radius: 100,
  216. },
  217. itemMargin: {
  218. horizontal: 8,
  219. vertical: 8
  220. },
  221. {% else %}
  222. show: false,
  223. {% endif %}
  224. },
  225. {% if data.hide-tooltip or chart-type == 'pie' or chart-type == 'donut' %}
  226. tooltip: {
  227. {% if data.hide-tooltip %}
  228. enabled: false,
  229. {% endif %}
  230. {% if chart-type == 'pie' or chart-type == 'donut' %}
  231. fillSeriesColor: false
  232. {% endif %}
  233. },
  234. {% endif %}
  235. {% if data.hide-points %}
  236. point: {
  237. show: false
  238. },
  239. {% endif %}
  240. {% if data.show-markers %}
  241. markers: {
  242. size: 2
  243. },
  244. {% endif %}
  245. })).render();
  246. });
  247. </script>
  248. {% endremoveemptylines %}
  249. {%- endcapture %}
  250. {% capture_script %}
  251. {{ script }}
  252. {% endcapture_script %}
  253. {% endif %}
  254. {% endremoveemptylines %}