chart.html 6.0 KB
Newer Older
C
chomik 已提交
1
{% assign data = site.data.charts[include.chart-id] %}
C
codecalm 已提交
2
{% assign id = include.id | default: include.chart-id %}
C
codecalm 已提交
3
{% assign height = include.height | default: 10 %}
C
codecalm 已提交
4
{% assign class = include.class %}
C
codecalm 已提交
5 6

{% if include['size'] == 'sm' %}
C
codecalm 已提交
7 8 9 10 11
	{% assign class = class | append: ' chart-sm' | strip %}
	{% assign height = 2.5 %}
{% elsif include['size'] == 'lg' %}
	{% assign class = class | append: ' chart-lg' | strip %}
	{% assign height = 15 %}
C
codecalm 已提交
12
{% endif %}
C
chomik 已提交
13

C
chomik 已提交
14
{% if data %}
C
codecalm 已提交
15 16 17 18
{% if data.extend %}
{% assign data = site.data.charts[data.extend] | concat_objects: site.data.charts[include.chart-id] %}
{% endif %}

C
codecalm 已提交
19
<div id="chart-{{ id }}" {% if class %} class="{{ class }}" {% endif %}{% if data.debug and jekyll.environment == 'development' %} style="outline: 1px solid red" {% endif %}></div>
C
codecalm 已提交
20

C
codecalm 已提交
21
{% append_lib apexcharts %}
C
codecalm 已提交
22
{% capture script %}
C
codecalm 已提交
23
{% assign chart-type = data.type | default: 'bar' %}
C
chomik 已提交
24
<script>
C
codecalm 已提交
25
	// @formatter:off
26
	document.addEventListener("DOMContentLoaded", function () {
C
codecalm 已提交
27 28 29
		{% if jekyll.environment == 'development' %}
		window.tabler_chart = window.tabler_chart || {};
		{% endif %}
C
codecalm 已提交
30
		
C
codecalm 已提交
31
		window.ApexCharts && ({% if jekyll.environment == 'development' %}window.tabler_chart["chart-{{ include.chart-id }}"] = {% endif %}new ApexCharts(document.getElementById('chart-{{ id }}'), {
C
codecalm 已提交
32
			chart: {
C
codecalm 已提交
33
				type: "{{ chart-type }}",
C
codecalm 已提交
34 35 36 37 38 39 40 41
				fontFamily: 'inherit',
				height: {{ height | times: 16 }},

				{% if data.sparkline %}
				sparkline: {
					enabled: true
				},
				{% else %}
C
codecalm 已提交
42
				parentHeightOffset: 0,
C
codecalm 已提交
43
				toolbar: {
C
codecalm 已提交
44
					show: false,
C
codecalm 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58
				},
				{% endif %}

				{% unless data.animations %}
				animations: {
					enabled: false
				},
				{% endunless %}

				{% if data.stacked %}
				stacked: true,
				{% endif %}
			},

C
codecalm 已提交
59
			{% if chart-type == 'bar' %}
C
codecalm 已提交
60 61
			plotOptions: {
				bar: {
C
codecalm 已提交
62 63
					{% if data.horizontal %}
					barHeight: '50%',
C
codecalm 已提交
64
			 		horizontal: true,
C
codecalm 已提交
65 66 67
					{% else %}
					columnWidth: '50%',
					{% endif %}
C
codecalm 已提交
68 69 70 71
				}
			},
			{% endif %}

C
codecalm 已提交
72
			{% if chart-type == 'bar' or chart-type == 'area' %}
C
codecalm 已提交
73
			dataLabels: {
C
codecalm 已提交
74
				enabled: {% if data.datalabels %}true{% else %}false{% endif %},
C
codecalm 已提交
75 76 77
			},
			{% endif %}

C
codecalm 已提交
78
			fill: {
C
codecalm 已提交
79 80
				opacity: {% if chart-type == 'area' %}.16{% else %}1{% endif %},
				{% if chart-type == 'area' %}type: 'solid'{% endif %}
C
codecalm 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94
			},

			{% if data.title %}
			title: {
				text: "{{ data.title | escape }}",
				margin: 0,
				floating: true,
				offsetX: 10,
				style: {
					fontSize: '18px',
				},
			},
			{% endif %}

C
codecalm 已提交
95
			{% if chart-type == 'area' or chart-type == 'line' %}
C
codecalm 已提交
96 97 98 99 100 101 102 103 104
			stroke: {
				width: {% if data.stroke-width %}[{{ data.stroke-width | join: ', '}}]{% else %}2{% endif %},
				{% if data.stroke-dash %}dashArray: [{{ data.stroke-dash | join: ', '}}],{% endif %}
				lineCap: "round",
				curve: "{{ data.stroke-curve | default: 'smooth' }}",
			},
			{% endif %}

			{% if data.series %}
C
codecalm 已提交
105
			{% if chart-type == 'pie' or chart-type == 'donut' or chart-type == 'radialBar' %}
C
codecalm 已提交
106 107 108 109 110 111 112 113 114 115 116
			series: [{% for serie in data.series %}{{ serie.data }}{% unless forloop.last %}, {% endunless %}{% endfor %}],
			labels: [{% for serie in data.series %}"{{ serie.name }}"{% unless forloop.last %}, {% endunless %}{% endfor %}],

			{% else %}series: [{% for serie in data.series %}{
				name: "{{ serie.name }}",
				data: [{{ serie.data | join: ', ' }}]
			}{% unless forloop.last %},{% endunless %}{% endfor %}],
			{% endif %}
			{% endif %}

			grid: {
C
codecalm 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
				{% unless data.sparkline %}
				padding: {
					top: -20,
					right: 0,
					left: -4,
					bottom: -8
				},
				{% endunless %}
				{% if data.hide-grid %}
				show: false,
				{% else %}
				strokeDashArray: 4,
				{% if data.show-x %}
				xaxis: {
					lines: {
						show: true
					}
				},
				{% endif %}
				{% endif %}
C
codecalm 已提交
137 138 139 140 141 142 143 144
			},

			{% if data.show-data-labels %}
			dataLabels: {
				enabled: true,
			},
			{% endif %}

C
codecalm 已提交
145
			{% if data.categories or data.datetime %}
C
codecalm 已提交
146
			xaxis: {
C
codecalm 已提交
147 148 149 150 151 152 153 154 155 156 157
				labels: {
					padding: 0
				},
				tooltip: {
					enabled: false
				},
				{% if chart-type == 'area' or chart-type == 'bar' %}
				axisBorder: {
					show: false,
				},
				{% endif %}
C
codecalm 已提交
158 159
				{% if data.categories %}
				categories: [{% for category in data.categories %}"{{ category }}"{% unless forloop.last %}, {% endunless %}{% endfor %}],
C
codecalm 已提交
160
				{% endif %}
C
codecalm 已提交
161

C
codecalm 已提交
162 163 164
				{% if data.datetime %}
				type: 'datetime',
				{% endif %}
C
codecalm 已提交
165 166 167
			},
			{% endif %}

C
codecalm 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
			{% unless chart-type == 'pie' or chart-type == 'donut' or chart-type == 'radialBar' %}
			yaxis: {
				labels: {
					padding: 4
				},
				{% if data.y-title %}
				title: {
					text: '{{ data.y-title | escape }}'
				}
				{% endif %}
			},
			{% endunless %}

			{% if data.datetime %}
			{% assign date = data.start-date | default: '2020-06-20' | date: "%s" %}
			{% assign days-count = data.series[0].data.size %}

			labels: [
				{% for i in (1..days-count) %}{% assign date = date | plus: 86400 %}'{{ date | timestamp_to_date }}'{% unless forloop.last %}, {% endunless %}{% endfor %}
			],
C
codecalm 已提交
188 189 190 191 192 193 194
			{% endif %}

			{% if data.series %}
			colors: [{% for serie in data.series %}{% assign color = serie.color | default: data.color | default: 'blue' %}"{{ color | tabler_color }}"{% unless forloop.last %}, {% endunless %}{% endfor %}],
			{% endif %}

			legend: {
C
codecalm 已提交
195 196 197 198
				{% if include.legend %}
				show: true,
				position: 'bottom',
				height: 32,
C
codecalm 已提交
199
				offsetY: 8,
C
codecalm 已提交
200 201 202 203 204 205 206 207 208
				markers: {
					width: 8,
					height: 8,
					radius: 100,
				},
				itemMargin: {
					horizontal: 8,
				},
				{% else %}
C
codecalm 已提交
209
				show: false,
C
codecalm 已提交
210
				{% endif %}
C
codecalm 已提交
211 212
			},

C
codecalm 已提交
213
			{% if data.hide-tooltip or chart-type == 'pie' or chart-type == 'donut' %}
C
codecalm 已提交
214
			tooltip: {
C
codecalm 已提交
215 216 217 218 219 220 221
				{% if data.hide-tooltip %}
				enabled: false,
				{% endif %}

				{% if chart-type == 'pie' or chart-type == 'donut' %}
				fillSeriesColor: false
				{% endif %}
C
codecalm 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
			},
			{% endif %}

			{% if data.hide-points %}
			point: {
				show: false
			},
			{% endif %}

			{% if data.show-markers %}
			markers: {
				size: 2
			},
			{% endif %}
		})).render();
	});
	// @formatter:on
C
chomik 已提交
239
</script>
C
codecalm 已提交
240 241 242
{% endcapture %}

{% if include.show-scripts %}
C
codecalm 已提交
243
{{ script }}
C
codecalm 已提交
244
{% else %}
C
codecalm 已提交
245 246 247
{% capture_global scripts %}
{{ script }}
{% endcapture_global %}
C
codecalm 已提交
248 249
{% endif %}

C
codecalm 已提交
250
{% endif %}