@fluid-topics/ft-chart 0.0.88

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.
package/README.md ADDED
@@ -0,0 +1,29 @@
1
+ Display charts from datas
2
+
3
+ ## Install
4
+
5
+ ```shell
6
+ npm install @fluid-topics/ft-chart
7
+ yarn add @fluid-topics/ft-chart
8
+ ```
9
+
10
+ ## Usage
11
+
12
+ ```typescript
13
+ import { html } from "lit"
14
+ import "@fluid-topics/ft-chart"
15
+
16
+ function render() {
17
+ return html` <ft-chart
18
+ xAxisName="date"
19
+ yAxisName="items"
20
+ valueLabels="['items', 'errors']",
21
+ valueSingular="['item', 'error']",
22
+ barChart
23
+ period="DAY"
24
+ data="[[1643673600, 120, 1], [1643760000, 1, 32], [1643846400, 150, 42],
25
+ [1643932800, 80, 61], [1644019200, 70, 69], [1644105600, 110, 98],
26
+ [1644192000, 130, 123]]" >
27
+ </ft-chart> `
28
+ }
29
+ ```
@@ -0,0 +1,45 @@
1
+ import { PropertyValues } from 'lit';
2
+ import { FtLitElement } from '@fluid-topics/ft-wc-utils';
3
+ import { EChartsType } from "echarts/core";
4
+ declare global {
5
+ interface Window {
6
+ echarts?: {
7
+ init: (dom: HTMLElement, theme?: string | object, opts?: any) => EChartsType;
8
+ };
9
+ }
10
+ }
11
+ export interface FtChartProperties {
12
+ xAxisName: string;
13
+ yAxisName: string;
14
+ valueLabels?: string[];
15
+ valueLabelsSingular?: string[];
16
+ barChart: boolean;
17
+ period: Period;
18
+ data: number[][];
19
+ }
20
+ export declare enum Period {
21
+ DAY = "DAY",
22
+ WEEK = "WEEK",
23
+ MONTH = "MONTH"
24
+ }
25
+ export declare class FtChart extends FtLitElement implements FtChartProperties {
26
+ getStyles(): import("lit").CSSResult;
27
+ xAxisName: string;
28
+ yAxisName: string;
29
+ valueLabels?: string[];
30
+ valueLabelsSingular?: string[];
31
+ barChart: boolean;
32
+ period: Period;
33
+ data: number[][];
34
+ private container;
35
+ private myChart?;
36
+ getTemplate(): import("lit-html").TemplateResult<1>;
37
+ protected firstUpdated(_changedProperties: PropertyValues): void;
38
+ protected updated(props: PropertyValues): void;
39
+ private getOptions;
40
+ private getTooltip;
41
+ private isThisPeriod;
42
+ private getSeries;
43
+ private getData;
44
+ }
45
+ //# sourceMappingURL=ft-chart.d.ts.map
@@ -0,0 +1,199 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { css, html } from 'lit';
8
+ import { property, query } from 'lit/decorators.js';
9
+ import { customElement, FtLitElement, jsonProperty } from '@fluid-topics/ft-wc-utils';
10
+ import * as moment from 'moment';
11
+ if (window.echarts == null) {
12
+ console.warn("Echarts is not available");
13
+ }
14
+ export var Period;
15
+ (function (Period) {
16
+ Period["DAY"] = "DAY";
17
+ Period["WEEK"] = "WEEK";
18
+ Period["MONTH"] = "MONTH";
19
+ })(Period || (Period = {}));
20
+ let FtChart = class FtChart extends FtLitElement {
21
+ constructor() {
22
+ super(...arguments);
23
+ this.xAxisName = 'date';
24
+ this.yAxisName = '';
25
+ this.valueLabels = undefined;
26
+ this.valueLabelsSingular = undefined;
27
+ this.barChart = false;
28
+ this.period = Period.DAY;
29
+ this.data = [];
30
+ }
31
+ getStyles() {
32
+ // language=CSS
33
+ return css `
34
+ .sessions-chart {
35
+ height: 100%;
36
+ width: 100%;
37
+ }
38
+ `;
39
+ }
40
+ getTemplate() {
41
+ return html `
42
+ <div class="sessions-chart"></div>
43
+ `;
44
+ }
45
+ firstUpdated(_changedProperties) {
46
+ var _a;
47
+ super.firstUpdated(_changedProperties);
48
+ this.myChart = (_a = window.echarts) === null || _a === void 0 ? void 0 : _a.init(this.container, undefined, { renderer: 'svg' });
49
+ }
50
+ updated(props) {
51
+ super.updated(props);
52
+ const option = this.getOptions();
53
+ option && this.myChart && this.myChart.setOption(option);
54
+ }
55
+ getOptions() {
56
+ const self = this;
57
+ return {
58
+ tooltip: {
59
+ trigger: 'axis',
60
+ formatter(params) {
61
+ return self.getTooltip(params);
62
+ }
63
+ },
64
+ grid: {
65
+ left: '80px',
66
+ right: '100px',
67
+ top: '50px',
68
+ bottom: '20px',
69
+ },
70
+ xAxis: {
71
+ type: 'category',
72
+ name: this.xAxisName.toUpperCase(),
73
+ axisTick: {
74
+ alignWithLabel: true
75
+ },
76
+ axisLabel: {
77
+ formatter(value) {
78
+ if (self.period === Period.DAY || self.period === Period.WEEK) {
79
+ return moment.utc(value * 1000).format('DD MMM YYYY');
80
+ }
81
+ if (self.period === Period.MONTH) {
82
+ return moment.utc(value * 1000).format('MMMM YYYY');
83
+ }
84
+ // Defaults to month
85
+ return moment.utc(value * 1000).format('MMMM YYYY');
86
+ }
87
+ }
88
+ },
89
+ yAxis: {
90
+ type: 'value',
91
+ name: this.yAxisName.toUpperCase()
92
+ },
93
+ series: this.getSeries()
94
+ };
95
+ }
96
+ getTooltip(params) {
97
+ const date = moment.utc(params[0].data.value[0] * 1000);
98
+ const numbers = params.map(param => param.data.value[1]);
99
+ let dateStr;
100
+ let prefix;
101
+ let suffix = '';
102
+ switch (this.period) {
103
+ case Period.DAY: {
104
+ dateStr = date.format('DD MMM YYYY');
105
+ prefix = 'on';
106
+ if (this.isThisPeriod(date, Period.DAY)) {
107
+ suffix = ' so far';
108
+ }
109
+ break;
110
+ }
111
+ case Period.WEEK: {
112
+ dateStr = date.format('DD MMM YYYY');
113
+ prefix = 'during the week of';
114
+ if (this.isThisPeriod(date, Period.WEEK)) {
115
+ suffix = ' so far';
116
+ }
117
+ break;
118
+ }
119
+ case Period.MONTH: {
120
+ prefix = 'in';
121
+ dateStr = date.format('MMMM YYYY');
122
+ if (this.isThisPeriod(date, Period.MONTH)) {
123
+ suffix = ' so far';
124
+ }
125
+ break;
126
+ }
127
+ }
128
+ let result = '';
129
+ for (let _i = 0; _i < numbers.length; _i++) {
130
+ let label = this.valueLabels ? this.valueLabels[_i] : this.yAxisName;
131
+ let singularLabel = this.valueLabelsSingular ? this.valueLabelsSingular[_i] : label;
132
+ const itemStr = numbers[_i] === 1 ? singularLabel : label;
133
+ result += `${numbers[_i]} ${itemStr} ${prefix} ${dateStr}${suffix}</br>`;
134
+ }
135
+ return result.substring(0, result.length - 5);
136
+ }
137
+ isThisPeriod(date, period, now = moment.utc()) {
138
+ switch (period) {
139
+ case Period.DAY:
140
+ return date.format('YYYY-MM-DD') === now.clone().startOf('day').format('YYYY-MM-DD');
141
+ case Period.WEEK:
142
+ return date.clone().startOf('isoWeek').format('YYYY-MM-DD') === now.clone().startOf('isoWeek').format('YYYY-MM-DD');
143
+ case Period.MONTH:
144
+ return date.month() === now.month() && date.year() === now.year();
145
+ }
146
+ return false;
147
+ }
148
+ getSeries() {
149
+ if (this.data.length == 0) {
150
+ return [];
151
+ }
152
+ const nbSeries = this.data[0].length - 1;
153
+ if (nbSeries == 0) {
154
+ return [];
155
+ }
156
+ let series = [];
157
+ for (let _i = 0; _i < nbSeries; _i++) {
158
+ series[_i] = {
159
+ type: this.barChart ? 'bar' : 'line',
160
+ data: this.getData(_i)
161
+ };
162
+ }
163
+ return series;
164
+ }
165
+ getData(valueIndex) {
166
+ return this.data.map(v => {
167
+ return { value: [v[0], v[valueIndex + 1]] };
168
+ });
169
+ }
170
+ };
171
+ __decorate([
172
+ property({ type: String })
173
+ ], FtChart.prototype, "xAxisName", void 0);
174
+ __decorate([
175
+ property({ type: String })
176
+ ], FtChart.prototype, "yAxisName", void 0);
177
+ __decorate([
178
+ jsonProperty(undefined)
179
+ ], FtChart.prototype, "valueLabels", void 0);
180
+ __decorate([
181
+ jsonProperty(undefined)
182
+ ], FtChart.prototype, "valueLabelsSingular", void 0);
183
+ __decorate([
184
+ property({ type: Boolean })
185
+ ], FtChart.prototype, "barChart", void 0);
186
+ __decorate([
187
+ property({ type: String })
188
+ ], FtChart.prototype, "period", void 0);
189
+ __decorate([
190
+ jsonProperty([])
191
+ ], FtChart.prototype, "data", void 0);
192
+ __decorate([
193
+ query('.sessions-chart')
194
+ ], FtChart.prototype, "container", void 0);
195
+ FtChart = __decorate([
196
+ customElement('ft-chart')
197
+ ], FtChart);
198
+ export { FtChart };
199
+ //# sourceMappingURL=ft-chart.js.map
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@fluid-topics/ft-chart",
3
+ "version": "0.0.88",
4
+ "description": "Display charts from data",
5
+ "keywords": [
6
+ "Lit"
7
+ ],
8
+ "author": "Fluid Topics <devtopics@antidot.net>",
9
+ "license": "ISC",
10
+ "main": "build/ft-chart.js",
11
+ "web": "build/ft-chart.min.js",
12
+ "typings": "build/ft-chart",
13
+ "files": [
14
+ "build/*.ts",
15
+ "build/*.js"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "ssh://git@scm.mrs.antidot.net:2222/fluidtopics/ft-web-components.git"
20
+ },
21
+ "dependencies": {
22
+ "@fluid-topics/ft-wc-utils": "^0.0.88",
23
+ "lit": "^2.0.2",
24
+ "moment": "^2.25.3"
25
+ },
26
+ "devDependencies": {
27
+ "echarts": "^5.3.0"
28
+ },
29
+ "peerDependencies": {
30
+ "echarts": "^5.3.0"
31
+ },
32
+ "gitHead": "220e53dba55dfa1de1560abbc30067555f72198c"
33
+ }