@kubex/zinc 1.0.106 → 1.0.107

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,204 +1,96 @@
1
- import { property } from 'lit/decorators.js';
1
+ import * as echarts from 'echarts/core';
2
+ import { BarChart } from 'echarts/charts';
3
+ import { CanvasRenderer } from 'echarts/renderers';
2
4
  import { type CSSResultGroup, html, unsafeCSS } from 'lit';
3
- import { Chart, ChartConfiguration, registerables } from "chart.js";
5
+ import { GridComponent, TooltipComponent } from 'echarts/components';
6
+ import { property } from 'lit/decorators.js';
7
+ import styles from './simple-chart.scss';
4
8
  import ZincElement from '../../internal/zinc-element';
9
+ import type { ECharts } from 'echarts/core';
5
10
 
6
- import styles from './simple-chart.scss';
11
+ echarts.use([BarChart, GridComponent, TooltipComponent, CanvasRenderer]);
12
+
13
+ const DEFAULT_LABELS = [
14
+ 'Jun 2016', 'Jul 2016', 'Aug 2016', 'Sep 2016', 'Oct 2016', 'Nov 2016',
15
+ 'Dec 2016', 'Jan 2017', 'Feb 2017', 'Mar 2017', 'Apr 2017', 'May 2017',
16
+ ];
17
+ const DEFAULT_DATA = [56.4, 39.8, 66.8, 66.4, 40.6, 55.2, 77.4, 69.8, 57.8, 76, 110.8, 142.6];
7
18
 
8
19
  /**
9
- * @summary Short summary of the component's intended use.
20
+ * @summary A simple, pre-styled bar chart.
10
21
  * @documentation https://zinc.style/components/simple-chart
11
22
  * @status experimental
12
23
  * @since 1.0
13
- *
14
- * @dependency zn-example
15
- *
16
- * @event zn-event-name - Emitted as an example.
17
- *
18
- * @slot - The default slot.
19
- * @slot example - An example slot.
20
- *
21
- * @csspart base - The component's base wrapper.
22
- *
23
- * @cssproperty --example - An example CSS custom property.
24
24
  */
25
25
  export default class ZnSimpleChart extends ZincElement {
26
26
  static styles: CSSResultGroup = unsafeCSS(styles);
27
27
 
28
- @property({ attribute: 'datasets', type: Array }) datasets: any[];
29
- @property({ attribute: 'labels', type: Array }) labels: any[];
30
-
31
- myChart: Chart;
28
+ @property({ attribute: 'datasets', type: Array }) datasets?: { data: number[] }[];
29
+ @property({ attribute: 'labels', type: Array }) labels?: string[];
30
+ @property({
31
+ attribute: 'enable-animations',
32
+ converter: {
33
+ fromAttribute: (value: string | null) => {
34
+ if (value === null) return false;
35
+ if (value === '' || value === 'true') return true;
36
+ const num = parseFloat(value);
37
+ return Number.isNaN(num) ? true : num;
38
+ },
39
+ },
40
+ }) enableAnimations: boolean | number = false;
32
41
 
33
- constructor() {
34
- super();
35
- Chart.register(...registerables);
36
- }
42
+ private chart?: ECharts;
43
+ private resizeObserver?: ResizeObserver;
37
44
 
38
45
  firstUpdated() {
39
- const ctx = (this.renderRoot.querySelector('#myChart') as HTMLCanvasElement).getContext('2d');
40
-
41
- const config = {
42
- type: 'bar',
43
- data: {
44
- labels: [
45
- "Jun 2016",
46
- "Jul 2016",
47
- "Aug 2016",
48
- "Sep 2016",
49
- "Oct 2016",
50
- "Nov 2016",
51
- "Dec 2016",
52
- "Jan 2017",
53
- "Feb 2017",
54
- "Mar 2017",
55
- "Apr 2017",
56
- "May 2017"
57
- ],
58
- datasets: [
59
- {
60
- data: [
61
- 56.4,
62
- 39.8,
63
- 66.8,
64
- 66.4,
65
- 40.6,
66
- 55.2,
67
- 77.4,
68
- 69.8,
69
- 57.8,
70
- 76,
71
- 110.8,
72
- 142.6
73
- ],
74
- }
75
- ]
46
+ const host = this.shadowRoot?.getElementById('chart') as HTMLElement | null;
47
+ if (!host) return;
48
+ this.chart = echarts.init(host);
49
+ const data = this.datasets?.[0]?.data ?? DEFAULT_DATA;
50
+ const labels = this.labels ?? DEFAULT_LABELS;
51
+ const animEnabled = this.enableAnimations !== false && this.enableAnimations !== 0;
52
+ const animDuration = typeof this.enableAnimations === 'number' ? this.enableAnimations : 1500;
53
+ this.chart.setOption({
54
+ animation: animEnabled,
55
+ animationDuration: animDuration,
56
+ animationEasing: 'cubicOut',
57
+ tooltip: {
58
+ trigger: 'axis',
59
+ backgroundColor: 'rgba(0,0,0,0.7)',
60
+ borderWidth: 0,
61
+ textStyle: { color: '#fff' },
76
62
  },
77
- options: {
78
- responsive: true,
79
- maintainAspectRatio: false,
80
- plugins: {
81
- legend: {
82
- display: false
83
- },
84
- tooltip: {
85
- enabled: false,
86
- external: function (context: any) {
87
- // Tooltip Element
88
- let tooltipEl = document.getElementById("chartjs-tooltip");
89
-
90
- // Create element on first render
91
- if (!tooltipEl) {
92
- tooltipEl = document.createElement("div");
93
- tooltipEl.id = "chartjs-tooltip";
94
- tooltipEl.innerHTML = "<table></table>";
95
- document.body.appendChild(tooltipEl);
96
- }
97
-
98
- // Hide if no tooltip
99
- const tooltipModel = context.tooltip;
100
- if (tooltipModel.opacity === 0) {
101
- tooltipEl.style.opacity = "0";
102
- return;
103
- }
104
-
105
- // Set caret Position
106
- tooltipEl.classList.remove("above", "below", "no-transform");
107
- if (tooltipModel.yAlign) {
108
- tooltipEl.classList.add(tooltipModel.yAlign);
109
- } else {
110
- tooltipEl.classList.add("no-transform");
111
- }
112
-
113
- function getBody(bodyItem: any) {
114
- return bodyItem.lines;
115
- }
116
-
117
- // Set Text
118
- if (tooltipModel.body) {
119
- const titleLines = tooltipModel.title || [];
120
- const bodyLines = tooltipModel.body.map(getBody);
121
-
122
- let innerHtml = "<thead>";
123
-
124
- titleLines.forEach(function (title: any) {
125
- innerHtml += "<tr><th>" + title + "</th></tr>";
126
- });
127
- innerHtml += "</thead><tbody>";
128
-
129
- bodyLines.forEach(function (body: any, i: any) {
130
- const colors = tooltipModel.labelColors[i];
131
- let style = "background:" + colors.backgroundColor;
132
- style += "; border-color:" + colors.borderColor;
133
- style += "; border-width: 2px";
134
- const span = '<span style="' + style + '"></span>';
135
- innerHtml += "<tr><td>" + span + body + "</td></tr>";
136
- });
137
- innerHtml += "</tbody>";
138
-
139
- const tableRoot = tooltipEl.querySelector("table");
140
- if (tableRoot) {
141
- tableRoot.innerHTML = innerHtml;
142
- }
143
- }
144
-
145
- const position = context.chart.canvas.getBoundingClientRect();
146
- const bodyFont = Chart.defaults.font;
147
-
148
- tooltipModel.padding = 3;
149
-
150
- // Display, position, and set styles for font
151
- tooltipEl.style.opacity = "1";
152
- tooltipEl.style.position = "absolute";
153
- tooltipEl.style.left =
154
- position.left +
155
- window.pageXOffset +
156
- tooltipModel.caretX +
157
- 5 +
158
- "px";
159
- tooltipEl.style.top =
160
- position.top +
161
- window.pageYOffset +
162
- tooltipModel.caretY +
163
- 10 +
164
- "px";
165
- tooltipEl.style.font = bodyFont.family ?? "Helvetica Neue";
166
- tooltipEl.style.color = "white";
167
- tooltipEl.style.padding = tooltipModel.padding + "px " + tooltipModel.padding + "px";
168
- tooltipEl.style.pointerEvents = "none";
169
- tooltipEl.style.background = "rgba(0, 0, 0, 0.7)";
170
- tooltipEl.style.borderRadius = "10px";
171
- }
172
- }
63
+ xAxis: { type: 'category', data: labels, show: false },
64
+ yAxis: { type: 'value', show: false },
65
+ grid: { left: 0, right: 0, top: 0, bottom: 0 },
66
+ series: [{
67
+ type: 'bar',
68
+ data,
69
+ barWidth: 7,
70
+ itemStyle: {
71
+ color: '#29C1BC',
72
+ borderRadius: 10,
173
73
  },
174
- elements: {
175
- bar: {
176
- borderRadius: 10,
177
- barThickness: 2,
178
- maxBarThickness: 2,
179
- backgroundColor: "#29C1BC",
180
- hoverBackgroundColor: "#19837f",
181
- }
74
+ emphasis: {
75
+ itemStyle: { color: '#19837f' },
182
76
  },
183
- scales: {
184
- x: {
185
- display: false
186
- },
187
- y: {
188
- display: false
189
- }
190
- }
191
- }
192
- };
77
+ }],
78
+ });
79
+ let firstResize = true;
80
+ this.resizeObserver = new ResizeObserver(() => {
81
+ if (firstResize) { firstResize = false; return; }
82
+ this.chart?.resize();
83
+ });
84
+ this.resizeObserver.observe(host);
85
+ }
193
86
 
194
- this.myChart = new Chart(ctx as CanvasRenderingContext2D, config as ChartConfiguration);
87
+ disconnectedCallback() {
88
+ super.disconnectedCallback();
89
+ this.resizeObserver?.disconnect();
90
+ this.chart?.dispose();
195
91
  }
196
92
 
197
93
  render() {
198
- return html`
199
- <div>
200
- <canvas id="myChart"></canvas>
201
- </div>
202
- `;
94
+ return html`<div id="chart"></div>`;
203
95
  }
204
96
  }