From c57d78c1610926b5fd4f8eed700c2a7b246f4b2d Mon Sep 17 00:00:00 2001 From: Arman Date: Thu, 23 Jun 2022 13:43:03 +0200 Subject: [PATCH 1/4] feat: initialize & start to implement charts --- package-lock.json | 61 ++- package.json | 3 +- src/lib/charts/echartDark.json | 354 ++++++++++++++++++ src/lib/charts/echartLight.json | 348 +++++++++++++++++ src/lib/charts/echarts.svelte | 96 +++++ src/lib/charts/index.ts | 2 +- src/lib/charts/line.svelte | 0 src/routes/console/[project]/index.svelte | 4 +- .../console/[project]/users/usage.svelte | 94 ++++- tsconfig.json | 3 +- 10 files changed, 955 insertions(+), 10 deletions(-) create mode 100644 src/lib/charts/echartDark.json create mode 100644 src/lib/charts/echartLight.json create mode 100644 src/lib/charts/echarts.svelte delete mode 100644 src/lib/charts/line.svelte diff --git a/package-lock.json b/package-lock.json index c791fb5a7..d1812d28d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,8 @@ "version": "0.0.1", "dependencies": { "@aw-labs/icons": "^0.0.0-15", - "@aw-labs/ui": "^0.0.0-15" + "@aw-labs/ui": "^0.0.0-15", + "echarts": "^5.3.3" }, "devDependencies": { "@playwright/test": "^1.22.2", @@ -2700,6 +2701,20 @@ "node": ">=12" } }, + "node_modules/echarts": { + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/echarts/-/echarts-5.3.3.tgz", + "integrity": "sha512-BRw2serInRwO5SIwRviZ6Xgm5Lb7irgz+sLiFMmy/HOaf4SQ+7oYqxKzRHAKp4xHQ05AuHw1xvoQWJjDQq/FGw==", + "dependencies": { + "tslib": "2.3.0", + "zrender": "5.3.2" + } + }, + "node_modules/echarts/node_modules/tslib": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.0.tgz", + "integrity": "sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==" + }, "node_modules/electron-to-chromium": { "version": "1.4.71", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.71.tgz", @@ -7766,6 +7781,19 @@ "engines": { "node": ">=12" } + }, + "node_modules/zrender": { + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/zrender/-/zrender-5.3.2.tgz", + "integrity": "sha512-8IiYdfwHj2rx0UeIGZGGU4WEVSDEdeVCaIg/fomejg1Xu6OifAL1GVzIPHg2D+MyUkbNgPWji90t0a8IDk+39w==", + "dependencies": { + "tslib": "2.3.0" + } + }, + "node_modules/zrender/node_modules/tslib": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.0.tgz", + "integrity": "sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==" } }, "dependencies": { @@ -9830,6 +9858,22 @@ "webidl-conversions": "^7.0.0" } }, + "echarts": { + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/echarts/-/echarts-5.3.3.tgz", + "integrity": "sha512-BRw2serInRwO5SIwRviZ6Xgm5Lb7irgz+sLiFMmy/HOaf4SQ+7oYqxKzRHAKp4xHQ05AuHw1xvoQWJjDQq/FGw==", + "requires": { + "tslib": "2.3.0", + "zrender": "5.3.2" + }, + "dependencies": { + "tslib": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.0.tgz", + "integrity": "sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==" + } + } + }, "electron-to-chromium": { "version": "1.4.71", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.71.tgz", @@ -13527,6 +13571,21 @@ "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.9.tgz", "integrity": "sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==", "dev": true + }, + "zrender": { + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/zrender/-/zrender-5.3.2.tgz", + "integrity": "sha512-8IiYdfwHj2rx0UeIGZGGU4WEVSDEdeVCaIg/fomejg1Xu6OifAL1GVzIPHg2D+MyUkbNgPWji90t0a8IDk+39w==", + "requires": { + "tslib": "2.3.0" + }, + "dependencies": { + "tslib": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.0.tgz", + "integrity": "sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==" + } + } } } } diff --git a/package.json b/package.json index e23fd0f0a..5e8b0624a 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,9 @@ "e2e": "playwright test tests/e2e" }, "dependencies": { + "@aw-labs/icons": "^0.0.0-15", "@aw-labs/ui": "^0.0.0-15", - "@aw-labs/icons": "^0.0.0-15" + "echarts": "^5.3.3" }, "devDependencies": { "@playwright/test": "^1.22.2", diff --git a/src/lib/charts/echartDark.json b/src/lib/charts/echartDark.json new file mode 100644 index 000000000..a0c9bd8f3 --- /dev/null +++ b/src/lib/charts/echartDark.json @@ -0,0 +1,354 @@ +{ + "color": ["#00a7c3", "#F02E65", "#00BC5D", "#F38500", "#FF453A"], + "backgroundColor": "rgba(0,0,0,0)", + "textStyle": {}, + "title": { + "textStyle": { + "color": "#eeeeee" + }, + "subtextStyle": { + "color": "#aaaaaa" + } + }, + "line": { + "itemStyle": { + "borderWidth": 1 + }, + "lineStyle": { + "width": 2 + }, + "symbolSize": 4, + "symbol": "circle", + "smooth": true + }, + "radar": { + "itemStyle": { + "borderWidth": 1 + }, + "lineStyle": { + "width": 2 + }, + "symbolSize": 4, + "symbol": "circle", + "smooth": true + }, + "bar": { + "itemStyle": { + "barBorderWidth": "0", + "barBorderColor": "#444444" + } + }, + "pie": { + "itemStyle": { + "borderWidth": "0", + "borderColor": "#444444" + } + }, + "scatter": { + "itemStyle": { + "borderWidth": "0", + "borderColor": "#444444" + } + }, + "boxplot": { + "itemStyle": { + "borderWidth": "0", + "borderColor": "#444444" + } + }, + "parallel": { + "itemStyle": { + "borderWidth": "0", + "borderColor": "#444444" + } + }, + "sankey": { + "itemStyle": { + "borderWidth": "0", + "borderColor": "#444444" + } + }, + "funnel": { + "itemStyle": { + "borderWidth": "0", + "borderColor": "#444444" + } + }, + "gauge": { + "itemStyle": { + "borderWidth": "0", + "borderColor": "#444444" + } + }, + "candlestick": { + "itemStyle": { + "color": "#fd1050", + "color0": "#0cf49b", + "borderColor": "#fd1050", + "borderColor0": "#0cf49b", + "borderWidth": 1 + } + }, + "graph": { + "itemStyle": { + "borderWidth": "0", + "borderColor": "#444444" + }, + "lineStyle": { + "width": 1, + "color": "#aaaaaa" + }, + "symbolSize": 4, + "symbol": "circle", + "smooth": true, + "color": ["#dd6b66", "#759aa0", "#e69d87", "#f38500", "#ff453a"], + "label": { + "color": "#444444" + } + }, + "map": { + "itemStyle": { + "areaColor": "#eee", + "borderColor": "#444", + "borderWidth": 0.5 + }, + "label": { + "color": "#000" + }, + "emphasis": { + "itemStyle": { + "areaColor": "rgba(255,215,0,0.8)", + "borderColor": "#444", + "borderWidth": 1 + }, + "label": { + "color": "rgb(100,0,0)" + } + } + }, + "geo": { + "itemStyle": { + "areaColor": "#eee", + "borderColor": "#444", + "borderWidth": 0.5 + }, + "label": { + "color": "#000" + }, + "emphasis": { + "itemStyle": { + "areaColor": "rgba(255,215,0,0.8)", + "borderColor": "#444", + "borderWidth": 1 + }, + "label": { + "color": "rgb(100,0,0)" + } + } + }, + "categoryAxis": { + "axisLine": { + "show": true, + "lineStyle": { + "color": "#333333" + } + }, + "axisTick": { + "show": true, + "lineStyle": { + "color": "#333333" + } + }, + "axisLabel": { + "show": true, + "color": "#444444" + }, + "splitLine": { + "show": true, + "lineStyle": { + "color": ["#444444"] + } + }, + "splitArea": { + "show": false, + "areaStyle": { + "color": ["#eeeeee"] + } + } + }, + "valueAxis": { + "axisLine": { + "show": true, + "lineStyle": { + "color": "#333333" + } + }, + "axisTick": { + "show": true, + "lineStyle": { + "color": "#333333" + } + }, + "axisLabel": { + "show": true, + "color": "#666666" + }, + "splitLine": { + "show": true, + "lineStyle": { + "color": ["#333333"] + } + }, + "splitArea": { + "show": false, + "areaStyle": { + "color": ["#666666"] + } + } + }, + "logAxis": { + "axisLine": { + "show": true, + "lineStyle": { + "color": "#333333" + } + }, + "axisTick": { + "show": true, + "lineStyle": { + "color": "#333333" + } + }, + "axisLabel": { + "show": true, + "color": "#666666" + }, + "splitLine": { + "show": true, + "lineStyle": { + "color": ["#333333"] + } + }, + "splitArea": { + "show": false, + "areaStyle": { + "color": ["#666666"] + } + } + }, + "timeAxis": { + "axisLine": { + "show": true, + "lineStyle": { + "color": "#333333" + } + }, + "axisTick": { + "show": true, + "lineStyle": { + "color": "#333333" + } + }, + "axisLabel": { + "show": true, + "color": "#666666" + }, + "splitLine": { + "show": true, + "lineStyle": { + "color": ["#333333"] + } + }, + "splitArea": { + "show": false, + "areaStyle": { + "color": ["#666666"] + } + } + }, + "toolbox": { + "iconStyle": { + "borderColor": "#999999" + }, + "emphasis": { + "iconStyle": { + "borderColor": "#444444" + } + } + }, + "legend": { + "textStyle": { + "color": "#eeeeee" + } + }, + "tooltip": { + "axisPointer": { + "lineStyle": { + "color": "#444444", + "width": "1" + }, + "crossStyle": { + "color": "#444444", + "width": "1" + } + } + }, + "timeline": { + "lineStyle": { + "color": "#444444", + "width": 1 + }, + "itemStyle": { + "color": "#dd6b66", + "borderWidth": 1 + }, + "controlStyle": { + "color": "#444444", + "borderColor": "#444444", + "borderWidth": 0.5 + }, + "checkpointStyle": { + "color": "#e43c59", + "borderColor": "#c23531" + }, + "label": { + "color": "#eeeeee" + }, + "emphasis": { + "itemStyle": { + "color": "#a9334c" + }, + "controlStyle": { + "color": "#444444", + "borderColor": "#444444", + "borderWidth": 0.5 + }, + "label": { + "color": "#eeeeee" + } + } + }, + "visualMap": { + "color": ["#bf444c", "#d88273", "#f6efa6"] + }, + "dataZoom": { + "backgroundColor": "rgba(47,69,84,0)", + "dataBackgroundColor": "rgba(255,255,255,0.3)", + "fillerColor": "rgba(167,183,204,0.4)", + "handleColor": "#a7b7cc", + "handleSize": "100%", + "textStyle": { + "color": "#eeeeee" + } + }, + "markPoint": { + "label": { + "color": "#444444" + }, + "emphasis": { + "label": { + "color": "#444444" + } + } + } +} diff --git a/src/lib/charts/echartLight.json b/src/lib/charts/echartLight.json new file mode 100644 index 000000000..e334832c4 --- /dev/null +++ b/src/lib/charts/echartLight.json @@ -0,0 +1,348 @@ +{ + "color": ["#00a7c3", "#F02E65", "#00BC5D", "#F38500", "#FF453A"], + "backgroundColor": "rgba(255,255,255,0)", + "textStyle": {}, + "title": { + "textStyle": { + "color": "#373b4d" + }, + "subtextStyle": { + "color": "#616b7c" + } + }, + "line": { + "itemStyle": { + "borderWidth": 1 + }, + "lineStyle": { + "width": 2 + }, + "symbolSize": 4, + "symbol": "emptyCircle", + "smooth": false + }, + "radar": { + "itemStyle": { + "borderWidth": 1 + }, + "lineStyle": { + "width": 2 + }, + "symbolSize": 4, + "symbol": "emptyCircle", + "smooth": false + }, + "bar": { + "itemStyle": { + "barBorderWidth": 0, + "barBorderColor": "#ccc" + } + }, + "pie": { + "itemStyle": { + "borderWidth": 0, + "borderColor": "#ccc" + } + }, + "scatter": { + "itemStyle": { + "borderWidth": 0, + "borderColor": "#ccc" + } + }, + "boxplot": { + "itemStyle": { + "borderWidth": 0, + "borderColor": "#ccc" + } + }, + "parallel": { + "itemStyle": { + "borderWidth": 0, + "borderColor": "#ccc" + } + }, + "sankey": { + "itemStyle": { + "borderWidth": 0, + "borderColor": "#ccc" + } + }, + "funnel": { + "itemStyle": { + "borderWidth": 0, + "borderColor": "#ccc" + } + }, + "gauge": { + "itemStyle": { + "borderWidth": 0, + "borderColor": "#ccc" + } + }, + "candlestick": { + "itemStyle": { + "color": "#eb5454", + "color0": "#47b262", + "borderColor": "#eb5454", + "borderColor0": "#47b262", + "borderWidth": 1 + } + }, + "graph": { + "itemStyle": { + "borderWidth": 0, + "borderColor": "#ccc" + }, + "lineStyle": { + "width": 1, + "color": "#aaa" + }, + "symbolSize": 4, + "symbol": "emptyCircle", + "smooth": false, + "color": ["#007187", "#da1a5b", "#fac858", "#ee6666", "#73c0de"], + "label": { + "color": "#eee" + } + }, + "map": { + "itemStyle": { + "areaColor": "#eee", + "borderColor": "#444", + "borderWidth": 0.5 + }, + "label": { + "color": "#000" + }, + "emphasis": { + "itemStyle": { + "areaColor": "rgba(255,215,0,0.8)", + "borderColor": "#444", + "borderWidth": 1 + }, + "label": { + "color": "rgb(100,0,0)" + } + } + }, + "geo": { + "itemStyle": { + "areaColor": "#eee", + "borderColor": "#444", + "borderWidth": 0.5 + }, + "label": { + "color": "#000" + }, + "emphasis": { + "itemStyle": { + "areaColor": "rgba(255,215,0,0.8)", + "borderColor": "#444", + "borderWidth": 1 + }, + "label": { + "color": "rgb(100,0,0)" + } + } + }, + "categoryAxis": { + "axisLine": { + "show": true, + "lineStyle": { + "color": "#6E7079" + } + }, + "axisTick": { + "show": true, + "lineStyle": { + "color": "#6E7079" + } + }, + "axisLabel": { + "show": true, + "color": "#6E7079" + }, + "splitLine": { + "show": false, + "lineStyle": { + "color": ["#E0E6F1"] + } + }, + "splitArea": { + "show": false, + "areaStyle": { + "color": ["rgba(250,250,250,0.2)", "rgba(210,219,238,0.2)"] + } + } + }, + "valueAxis": { + "axisLine": { + "show": false, + "lineStyle": { + "color": "#6E7079" + } + }, + "axisTick": { + "show": false, + "lineStyle": { + "color": "#6E7079" + } + }, + "axisLabel": { + "show": true, + "color": "#6E7079" + }, + "splitLine": { + "show": true, + "lineStyle": { + "color": ["#E0E6F1"] + } + }, + "splitArea": { + "show": false, + "areaStyle": { + "color": ["rgba(250,250,250,0.2)", "rgba(210,219,238,0.2)"] + } + } + }, + "logAxis": { + "axisLine": { + "show": false, + "lineStyle": { + "color": "#6E7079" + } + }, + "axisTick": { + "show": false, + "lineStyle": { + "color": "#6E7079" + } + }, + "axisLabel": { + "show": true, + "color": "#6E7079" + }, + "splitLine": { + "show": true, + "lineStyle": { + "color": ["#E0E6F1"] + } + }, + "splitArea": { + "show": false, + "areaStyle": { + "color": ["rgba(250,250,250,0.2)", "rgba(210,219,238,0.2)"] + } + } + }, + "timeAxis": { + "axisLine": { + "show": true, + "lineStyle": { + "color": "#6E7079" + } + }, + "axisTick": { + "show": true, + "lineStyle": { + "color": "#6E7079" + } + }, + "axisLabel": { + "show": true, + "color": "#6E7079" + }, + "splitLine": { + "show": false, + "lineStyle": { + "color": ["#E0E6F1"] + } + }, + "splitArea": { + "show": false, + "areaStyle": { + "color": ["rgba(250,250,250,0.2)", "rgba(210,219,238,0.2)"] + } + } + }, + "toolbox": { + "iconStyle": { + "borderColor": "#999" + }, + "emphasis": { + "iconStyle": { + "borderColor": "#666" + } + } + }, + "legend": { + "textStyle": { + "color": "#333" + } + }, + "tooltip": { + "axisPointer": { + "lineStyle": { + "color": "#ccc", + "width": 1 + }, + "crossStyle": { + "color": "#ccc", + "width": 1 + } + } + }, + "timeline": { + "lineStyle": { + "color": "#DAE1F5", + "width": 2 + }, + "itemStyle": { + "color": "#A4B1D7", + "borderWidth": 1 + }, + "controlStyle": { + "color": "#A4B1D7", + "borderColor": "#A4B1D7", + "borderWidth": 1 + }, + "checkpointStyle": { + "color": "#316bf3", + "borderColor": "fff" + }, + "label": { + "color": "#A4B1D7" + }, + "emphasis": { + "itemStyle": { + "color": "#FFF" + }, + "controlStyle": { + "color": "#A4B1D7", + "borderColor": "#A4B1D7", + "borderWidth": 1 + }, + "label": { + "color": "#A4B1D7" + } + } + }, + "visualMap": { + "color": ["#bf444c", "#d88273", "#f6efa6"] + }, + "dataZoom": { + "handleSize": "undefined%", + "textStyle": {} + }, + "markPoint": { + "label": { + "color": "#eee" + }, + "emphasis": { + "label": { + "color": "#eee" + } + } + } +} diff --git a/src/lib/charts/echarts.svelte b/src/lib/charts/echarts.svelte new file mode 100644 index 000000000..1780d2641 --- /dev/null +++ b/src/lib/charts/echarts.svelte @@ -0,0 +1,96 @@ + + +
+ + diff --git a/src/lib/charts/index.ts b/src/lib/charts/index.ts index b5ece6e20..50fb2499f 100644 --- a/src/lib/charts/index.ts +++ b/src/lib/charts/index.ts @@ -1 +1 @@ -export { default as Line } from './line.svelte'; +export { default as Chart } from './echarts.svelte'; diff --git a/src/lib/charts/line.svelte b/src/lib/charts/line.svelte deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/routes/console/[project]/index.svelte b/src/routes/console/[project]/index.svelte index 8cdc0cd74..10eec2fe8 100644 --- a/src/routes/console/[project]/index.svelte +++ b/src/routes/console/[project]/index.svelte @@ -5,7 +5,7 @@ import { project } from './store'; import { Card, List, ListItem } from '$lib/components'; import { Cover, Container } from '$lib/layout'; - import { Line } from '$lib/charts'; + // import { Line } from '$lib/charts'; // import { page } from '$app/stores'; import { base } from '$app/paths'; import CreatePlatform from './_createPlatform.svelte'; @@ -54,7 +54,7 @@ - +

Platforms

diff --git a/src/routes/console/[project]/users/usage.svelte b/src/routes/console/[project]/users/usage.svelte index aa43e6741..708310ec4 100644 --- a/src/routes/console/[project]/users/usage.svelte +++ b/src/routes/console/[project]/users/usage.svelte @@ -1,10 +1,96 @@ -

Usage

- - + {#await getUsage(currentTimeframe)} + Loading... + {:then res} + +
    +
  • + +
  • +
  • + +
  • +
  • + +
  • +
+ +
Users
+

Count of users over time

+ [e.date * 1000, e.value])], + type: 'line', + smooth: true, + areaStyle: {} + } + ]} /> +
+ +
Operations
+

Count of users create, read, update and delete operations over time

+ [e.date * 1000, e.value])], + type: 'line', + smooth: true, + areaStyle: {} + }, + { + name: 'Read', + data: [...res.usersRead.map((e) => [e.date * 1000, e.value])], + type: 'line', + smooth: true, + areaStyle: {} + }, + { + name: 'Update', + data: [...res.usersUpdate.map((e) => [e.date * 1000, e.value])], + type: 'line', + smooth: true, + areaStyle: {} + }, + { + name: 'Delete', + data: [...res.usersDelete.map((e) => [e.date * 1000, e.value])], + type: 'line', + smooth: true, + areaStyle: {} + } + ]} /> +
+ {/await}
diff --git a/tsconfig.json b/tsconfig.json index 578f70be8..d164d251e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,6 +8,7 @@ "noUncheckedIndexedAccess": true, "noUnusedLocals": true, "noUnusedParameters": true, - "esModuleInterop": true + "esModuleInterop": true, + "resolveJsonModule": true } } From d21444e9e3a27853b60ed1550f7f9ccf2976a011 Mon Sep 17 00:00:00 2001 From: Arman Date: Thu, 23 Jun 2022 15:11:00 +0200 Subject: [PATCH 2/4] feat: refactor charts to be dynamic & fixes --- src/lib/charts/echarts.svelte | 99 +++++++++++++------ src/routes/console/[project]/users/store.ts | 26 +++++ .../console/[project]/users/usage.svelte | 37 +++---- 3 files changed, 115 insertions(+), 47 deletions(-) diff --git a/src/lib/charts/echarts.svelte b/src/lib/charts/echarts.svelte index 1780d2641..4c75fc695 100644 --- a/src/lib/charts/echarts.svelte +++ b/src/lib/charts/echarts.svelte @@ -15,7 +15,7 @@ // Import the Canvas renderer // Note that introducing the CanvasRenderer or SVGRenderer is a required step import { CanvasRenderer } from 'echarts/renderers'; - import { onMount } from 'svelte'; + import { onDestroy, onMount } from 'svelte'; import { app } from '$lib/stores/app'; import light from './echartLight.json'; import dark from './echartDark.json'; @@ -32,42 +32,67 @@ UniversalTransition, CanvasRenderer ]); + echarts.registerTheme('light', light); + echarts.registerTheme('dark', dark); export let series: LineSeriesOption[]; export let title: string; - onMount(() => { - echarts.registerTheme('light', light); - echarts.registerTheme('dark', dark); + let myChart: echarts.ECharts; + $: option = { + animationDuration: 400, + title: { + text: '' + }, + legend: { + orient: 'vertical', + right: 10, + top: 'center' + }, + tooltip: { trigger: 'axis' }, + xAxis: { + type: 'time', + show: true + }, + yAxis: { type: 'value' }, + series + }; + let notMerge = false; + let lazyUpdate = false; + let timeoutId: any; - let myChart = echarts.init(document.getElementById(`echart-${title}`), $app.theme); + $: option && setOption(); + $: if (myChart && $app.theme) { + makeChart(); + setOption(); + } + + onMount(() => { + makeChart(); + }); + + onDestroy(() => { + destroyChart(); + }); + const setOption = () => { + if (myChart && !myChart.isDisposed()) { + myChart.setOption(option, notMerge, lazyUpdate); + } + }; + + const destroyChart = () => { + if (myChart && !myChart.isDisposed()) { + myChart.dispose(); + } + }; + + const makeChart = () => { + destroyChart(); + myChart = echarts.init(document.getElementById(`echart-${title}`), $app.theme); series.forEach((s: LineSeriesOption, i: number) => { s.areaStyle = colorToGradient(myChart['_theme'].color[i]); }); - - myChart.setOption({ - animationDuration: 400, - title: { - text: '' - }, - legend: { - orient: 'vertical', - right: 10, - top: 'center' - }, - tooltip: { trigger: 'axis' }, - xAxis: { - type: 'time', - show: true - }, - yAxis: { type: 'value' }, - series - }); - - window.onresize = function () { - myChart.resize(); - }; - }); + }; function colorToGradient(color: string) { return { @@ -84,6 +109,22 @@ ]) }; } + window.onresize = () => { + if (myChart && !myChart.isDisposed()) { + handleResize(); + } + }; + const handleResize = () => { + console.log('test'); + if (timeoutId == undefined) { + timeoutId = setTimeout(() => { + if (myChart && !myChart.isDisposed()) { + myChart.resize(); + } + timeoutId = undefined; + }, 500); + } + };
diff --git a/src/routes/console/[project]/users/store.ts b/src/routes/console/[project]/users/store.ts index b36e79077..b1d840ea1 100644 --- a/src/routes/console/[project]/users/store.ts +++ b/src/routes/console/[project]/users/store.ts @@ -72,11 +72,37 @@ function createTeamsListStore() { } }; } +function createUsersUsageStore() { + const { subscribe, set } = writable({ + loading: true, + response: browser ? JSON.parse(sessionStorage.getItem('usersUsage')) : null + }); + + return { + subscribe, + set, + load: async (range: string) => { + try { + const response = await sdkForProject.users.getUsage(range); + set({ + loading: false, + response + }); + } catch (error) { + //TODO: take care what happens here + } + } + }; +} export const usersList = createUsersListStore(); export const teamsList = createTeamsListStore(); +export const usersUsage = createUsersUsageStore(); if (browser) { usersList.subscribe((n) => sessionStorage?.setItem('users', JSON.stringify(n.response ?? ''))); teamsList.subscribe((n) => sessionStorage?.setItem('teams', JSON.stringify(n.response ?? ''))); + usersUsage.subscribe((n) => + sessionStorage?.setItem('usersUsage', JSON.stringify(n.response ?? '')) + ); } diff --git a/src/routes/console/[project]/users/usage.svelte b/src/routes/console/[project]/users/usage.svelte index 708310ec4..fbcd6dd12 100644 --- a/src/routes/console/[project]/users/usage.svelte +++ b/src/routes/console/[project]/users/usage.svelte @@ -3,40 +3,41 @@ import { Chart } from '$lib/charts'; import { sdkForProject } from '$lib/stores/sdk'; import { Card } from '$lib/components'; + import { usersUsage } from './store'; - let currentTimeframe = '30d'; + let currentRange = '30d'; + + $: usersUsage.load(currentRange); + $: data = $usersUsage.response; - const getUsage = async (timeFrame) => await sdkForProject.users.getUsage(timeFrame); //TODO: add types once they are fixed - {#await getUsage(currentTimeframe)} - Loading... - {:then res} - + {#if data} +
  • @@ -49,7 +50,7 @@ series={[ { name: 'User', - data: [...res.usersCount.map((e) => [e.date * 1000, e.value])], + data: [...data.usersCount.map((e) => [e.date * 1000, e.value])], type: 'line', smooth: true, areaStyle: {} @@ -64,33 +65,33 @@ series={[ { name: 'Create', - data: [...res.usersCreate.map((e) => [e.date * 1000, e.value])], + data: [...data.usersCreate.map((e) => [e.date * 1000, e.value])], type: 'line', smooth: true, areaStyle: {} }, { name: 'Read', - data: [...res.usersRead.map((e) => [e.date * 1000, e.value])], + data: [...data.usersRead.map((e) => [e.date * 1000, e.value])], type: 'line', smooth: true, areaStyle: {} }, { name: 'Update', - data: [...res.usersUpdate.map((e) => [e.date * 1000, e.value])], + data: [...data.usersUpdate.map((e) => [e.date * 1000, e.value])], type: 'line', smooth: true, areaStyle: {} }, { name: 'Delete', - data: [...res.usersDelete.map((e) => [e.date * 1000, e.value])], + data: [...data.usersDelete.map((e) => [e.date * 1000, e.value])], type: 'line', smooth: true, areaStyle: {} } ]} /> - {/await} + {/if} From ddc70c231a92cec8cd6197642077b439fbc9ddb8 Mon Sep 17 00:00:00 2001 From: Arman Date: Thu, 23 Jun 2022 15:22:41 +0200 Subject: [PATCH 3/4] chore: removed comments --- src/lib/charts/echarts.svelte | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/lib/charts/echarts.svelte b/src/lib/charts/echarts.svelte index 4c75fc695..deeec5c7b 100644 --- a/src/lib/charts/echarts.svelte +++ b/src/lib/charts/echarts.svelte @@ -10,10 +10,7 @@ TransformComponent, LegendComponent } from 'echarts/components'; - // Features like Universal Transition and Label Layout import { LabelLayout, UniversalTransition } from 'echarts/features'; - // Import the Canvas renderer - // Note that introducing the CanvasRenderer or SVGRenderer is a required step import { CanvasRenderer } from 'echarts/renderers'; import { onDestroy, onMount } from 'svelte'; import { app } from '$lib/stores/app'; From 86488f67f4be58ff65882fbff51b3815f9e91177 Mon Sep 17 00:00:00 2001 From: Arman Date: Thu, 23 Jun 2022 15:29:49 +0200 Subject: [PATCH 4/4] fix: remove testing button --- src/routes/console/[project]/users/usage.svelte | 1 - 1 file changed, 1 deletion(-) diff --git a/src/routes/console/[project]/users/usage.svelte b/src/routes/console/[project]/users/usage.svelte index fbcd6dd12..427480295 100644 --- a/src/routes/console/[project]/users/usage.svelte +++ b/src/routes/console/[project]/users/usage.svelte @@ -15,7 +15,6 @@ {#if data} -