@operato/chart 2.0.0-beta.25 → 2.0.0-beta.27

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.
Files changed (33) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/src/chartjs/config-converter.d.ts +22 -1
  3. package/dist/src/chartjs/config-converter.js +40 -23
  4. package/dist/src/chartjs/config-converter.js.map +1 -1
  5. package/dist/src/chartjs/ox-chart-js.d.ts +11 -5
  6. package/dist/src/chartjs/ox-chart-js.js +98 -5
  7. package/dist/src/chartjs/ox-chart-js.js.map +1 -1
  8. package/dist/src/editors/input-chart-abstract.js +3 -1
  9. package/dist/src/editors/input-chart-abstract.js.map +1 -1
  10. package/dist/stories/common.d.ts +5 -0
  11. package/dist/stories/common.js +9 -0
  12. package/dist/stories/common.js.map +1 -1
  13. package/dist/stories/ox-input-chart-hbar.stories.js +6 -4
  14. package/dist/stories/ox-input-chart-hbar.stories.js.map +1 -1
  15. package/dist/stories/ox-input-chart-mixed.stories.js +7 -4
  16. package/dist/stories/ox-input-chart-mixed.stories.js.map +1 -1
  17. package/dist/stories/ox-input-chart-pie.stories.js +9 -6
  18. package/dist/stories/ox-input-chart-pie.stories.js.map +1 -1
  19. package/dist/stories/ox-input-chart-radar.stories.js +9 -6
  20. package/dist/stories/ox-input-chart-radar.stories.js.map +1 -1
  21. package/dist/tsconfig.tsbuildinfo +1 -1
  22. package/package.json +27 -19
  23. package/src/chartjs/config-converter.ts +46 -28
  24. package/src/chartjs/ox-chart-js.ts +123 -8
  25. package/src/editors/input-chart-abstract.ts +3 -1
  26. package/src/types.d.ts +2 -0
  27. package/stories/common.ts +10 -0
  28. package/stories/ox-input-chart-hbar.stories.ts +6 -4
  29. package/stories/ox-input-chart-mixed.stories.ts +7 -4
  30. package/stories/ox-input-chart-pie.stories.ts +9 -6
  31. package/stories/ox-input-chart-radar.stories.ts +9 -6
  32. package/tsconfig.json +1 -1
  33. /package/src/{globat.d.ts → global.d.ts} +0 -0
@@ -1,11 +1,17 @@
1
+ import 'chartjs-plugin-datalabels'
2
+
1
3
  import { LitElement, html, css } from 'lit'
2
- import { property, query, customElement } from 'lit/decorators.js'
4
+ import { property, query, state, customElement } from 'lit/decorators.js'
3
5
  import { Chart, ChartConfiguration } from 'chart.js/auto'
6
+ import { convertConfigure } from './config-converter'
4
7
 
5
8
  @customElement('ox-chart-js')
6
- class OxChartJs extends LitElement {
7
- @property({ type: Object }) config: ChartConfiguration | null = null
9
+ export class OxChartJs extends LitElement {
10
+ @property({ type: Object }) config: OperatoChart.ChartConfig | null = null
11
+ @property({ type: Array }) data: { [key: string]: any }[] = []
12
+
8
13
  private chart: Chart | null = null
14
+ private chartjsConfig: ChartConfiguration | null = null
9
15
 
10
16
  @query('canvas') canvas!: HTMLCanvasElement
11
17
 
@@ -20,23 +26,132 @@ class OxChartJs extends LitElement {
20
26
  }
21
27
  `
22
28
 
23
- firstUpdated() {
24
- if (this.config) {
25
- this.chart = new Chart(this.canvas, this.config)
29
+ disconnectedCallback(): void {
30
+ super.disconnectedCallback()
31
+ if (this.chart) {
32
+ this.chart.destroy()
26
33
  }
27
34
  }
28
35
 
29
36
  updated(changedProperties: Map<string | number | symbol, unknown>) {
37
+ var needUpdateData = false
38
+
30
39
  if (changedProperties.has('config') && this.config) {
40
+ this.chartjsConfig = convertConfigure(this.config, {}) as any
41
+
31
42
  this.chart && this.chart.destroy()
32
- this.chart = new Chart(this.canvas, this.config)
33
- console.log('new chart', this.chart)
43
+ this.chart = new Chart(this.canvas, this.chartjsConfig!)
44
+
45
+ needUpdateData = true
46
+ }
47
+
48
+ if (changedProperties.has('data')) {
49
+ needUpdateData = true
50
+ }
51
+
52
+ if (needUpdateData) {
53
+ this.updateData()
34
54
  }
35
55
  }
36
56
 
57
+ updateData() {
58
+ if (!this.chart) {
59
+ return
60
+ }
61
+
62
+ var data = this.data
63
+
64
+ if (this.data[0]?.hasOwnProperty('__field1')) {
65
+ /* DEPRECATED 이 케이스는 앞으로 지원하지 않는 것이 좋다. 하위 호환성 때문에 제공함. 사용빈도 낮음. */
66
+ data = toObjectArrayValue(this.data) as { [key: string]: any }[]
67
+ }
68
+
69
+ const labelDataKey = this.config!.data.labelDataKey || ''
70
+
71
+ labelDataKey && (this.chart!.config.data!.labels = data.map((data: any) => data[labelDataKey]))
72
+
73
+ const datasets = this.chart!.config.data.datasets
74
+ for (let key in datasets) {
75
+ const dataset = datasets[Number(key)]
76
+ const dataKey = (dataset as any).dataKey
77
+ dataKey && (dataset.data = data.map(d => d[dataKey]) as any)
78
+ }
79
+
80
+ this.chart.update()
81
+ }
82
+
37
83
  render() {
38
84
  return html`<canvas></canvas>`
39
85
  }
86
+
87
+ attachPluginOptions(options: OperatoChart.ChartOptions) {
88
+ if (!options.plugins) {
89
+ options.plugins = {}
90
+ }
91
+
92
+ this.attachDatalabelPluginOptions(options.plugins)
93
+ }
94
+
95
+ attachDatalabelPluginOptions(pluginOptions: any) {
96
+ pluginOptions.datalabels = {
97
+ ...pluginOptions.datalabels,
98
+ display: function (context: any) {
99
+ return !!context.dataset.displayValue
100
+ },
101
+ anchor: function (context: any) {
102
+ return context.dataset.dataLabelAnchor || 'center'
103
+ },
104
+ color: function (context: any) {
105
+ return context.dataset?.defaultFontColor || '#000'
106
+ },
107
+ font: function (context: any) {
108
+ return {
109
+ size: context.dataset?.defaultFontSize,
110
+ family: context.chart.options?.defaultFontFamily
111
+ }
112
+ },
113
+ clamp: true,
114
+ formatter: function (value: string, context: any) {
115
+ var prefix = context.dataset.valuePrefix || ''
116
+ var suffix = context.dataset.valueSuffix || ''
117
+
118
+ if (value === undefined) {
119
+ return value
120
+ }
121
+
122
+ var stringValue = Number(value).toLocaleString()
123
+ return prefix + stringValue + suffix
124
+ }
125
+ }
126
+ }
127
+ }
128
+
129
+ function toObjectArrayValue(array: any[]): any[] | null {
130
+ if (!array || array.length === 0) {
131
+ return null
132
+ }
133
+
134
+ let indexKeyMap: any = {}
135
+ let value = []
136
+
137
+ for (let key in array[0]) {
138
+ indexKeyMap[key] = array[0][key]
139
+ }
140
+
141
+ for (var i = 1; i < array.length; i++) {
142
+ let object: any = {}
143
+ let thisObject = array[i]
144
+
145
+ for (let key in indexKeyMap) {
146
+ let k = indexKeyMap[key]
147
+ let v = thisObject[key]
148
+ object[k] = v
149
+ }
150
+
151
+ value.push(object)
152
+ }
153
+
154
+ return value
40
155
  }
41
156
 
42
157
  declare global {
@@ -139,7 +139,9 @@ export class InputChartAbstract extends OxFormField {
139
139
  borderWidth: 1,
140
140
  dataKey: '',
141
141
  yAxisID: 'left',
142
- color: '#000',
142
+ color: randomColor({
143
+ hue: new TinyColor('#fff').clone().toString()
144
+ }).toRgbString(),
143
145
  backgroundColor: '#FFFFFF',
144
146
  type: seriesType,
145
147
  stack: '',
package/src/types.d.ts CHANGED
@@ -42,6 +42,8 @@ declare namespace OperatoChart {
42
42
  yGridLine?: boolean
43
43
  y2ndGridLine?: boolean
44
44
  multiAxis?: boolean
45
+ maintainAspectRatio?: boolean
46
+ plugins?: { [plugin: string]: any }
45
47
  }
46
48
 
47
49
  export interface LegendOptions {
package/stories/common.ts CHANGED
@@ -45,3 +45,13 @@ export function getDefaultAxisOptions(): OperatoChart.AxisOptions {
45
45
  }
46
46
  }
47
47
  }
48
+
49
+ export const data = [
50
+ { year: 2010, count: 10, average: 120 },
51
+ { year: 2011, count: 20, average: 110 },
52
+ { year: 2012, count: 15, average: 80 },
53
+ { year: 2013, count: 25, average: 130 },
54
+ { year: 2014, count: 22, average: 120 },
55
+ { year: 2015, count: 30, average: 60 },
56
+ { year: 2016, count: 28, average: 90 }
57
+ ]
@@ -3,9 +3,8 @@ import { html, TemplateResult } from 'lit'
3
3
  import '../src/editors/ox-input-chart-hbar.js'
4
4
  import '../src/chartjs/ox-chart-js.js'
5
5
  import '../src/scichart/ox-scichart.js'
6
- import { convertConfigure } from '../src/chartjs/config-converter.js'
7
6
 
8
- import { getDefaultChartConfig } from './common.js'
7
+ import { data, getDefaultChartConfig } from './common.js'
9
8
 
10
9
  export default {
11
10
  title: 'ox-input-chart-hbar',
@@ -79,6 +78,9 @@ const Template: Story<ArgTypes> = ({ value }: ArgTypes) => html`
79
78
  }
80
79
  </style>
81
80
 
81
+ <div style="background-color: black; color: white;">
82
+ Type 'count' or 'average' for series dataKey, type 'year' for axis's dataKey
83
+ </div>
82
84
  <div class="container light">
83
85
  <ox-input-chart-hbar
84
86
  id="editor"
@@ -88,7 +90,7 @@ const Template: Story<ArgTypes> = ({ value }: ArgTypes) => html`
88
90
  const config = target.value
89
91
  const chartjs = document.querySelector('#chartjs') as HTMLElement
90
92
  if (chartjs) {
91
- ;(chartjs as any).config = convertConfigure(config, {})
93
+ ;(chartjs as any).config = { ...config }
92
94
  }
93
95
  const scichart = document.querySelector('#scichart') as HTMLElement
94
96
  if (scichart) {
@@ -109,7 +111,7 @@ const Template: Story<ArgTypes> = ({ value }: ArgTypes) => html`
109
111
  }}
110
112
  ></ox-input-chart-hbar>
111
113
  <div id="charts">
112
- <ox-chart-js id="chartjs"></ox-chart-js>
114
+ <ox-chart-js id="chartjs" .data=${data}></ox-chart-js>
113
115
  <ox-scichart id="scichart"></ox-scichart>
114
116
  </div>
115
117
  </div>
@@ -3,9 +3,8 @@ import { html, TemplateResult } from 'lit'
3
3
  import '../src/editors/ox-input-chart-mixed.js'
4
4
  import '../src/chartjs/ox-chart-js.js'
5
5
  import '../src/scichart/ox-scichart.js'
6
- import { convertConfigure } from '../src/chartjs/config-converter.js'
7
6
 
8
- import { getDefaultChartConfig } from './common.js'
7
+ import { data, getDefaultChartConfig } from './common.js'
9
8
 
10
9
  export default {
11
10
  title: 'ox-input-chart-mixed',
@@ -79,6 +78,10 @@ const Template: Story<ArgTypes> = ({ value }: ArgTypes) => html`
79
78
  }
80
79
  </style>
81
80
 
81
+ <div style="background-color: black; color: white;">
82
+ Type 'count' or 'average' for series dataKey, type 'year' for axis's dataKey
83
+ </div>
84
+
82
85
  <div class="container light">
83
86
  <ox-input-chart-mixed
84
87
  id="editor"
@@ -88,7 +91,7 @@ const Template: Story<ArgTypes> = ({ value }: ArgTypes) => html`
88
91
  const config = target.value
89
92
  const chartjs = document.querySelector('#chartjs') as HTMLElement
90
93
  if (chartjs) {
91
- ;(chartjs as any).config = convertConfigure(config, {})
94
+ ;(chartjs as any).config = { ...config }
92
95
  }
93
96
  const scichart = document.querySelector('#scichart') as HTMLElement
94
97
  if (scichart) {
@@ -109,7 +112,7 @@ const Template: Story<ArgTypes> = ({ value }: ArgTypes) => html`
109
112
  }}
110
113
  ></ox-input-chart-mixed>
111
114
  <div id="charts">
112
- <ox-chart-js id="chartjs"></ox-chart-js>
115
+ <ox-chart-js id="chartjs" .data=${data}></ox-chart-js>
113
116
  <ox-scichart id="scichart"></ox-scichart>
114
117
  </div>
115
118
  </div>
@@ -2,9 +2,8 @@ import { html, TemplateResult } from 'lit'
2
2
 
3
3
  import '../src/editors/ox-input-chart-pie.js'
4
4
  import '../src/chartjs/ox-chart-js.js'
5
- import { convertConfigure } from '../src/chartjs/config-converter.js'
6
5
 
7
- import { getDefaultChartConfig } from './common.js'
6
+ import { data, getDefaultChartConfig } from './common.js'
8
7
 
9
8
  export default {
10
9
  title: 'ox-input-chart-pie',
@@ -68,6 +67,10 @@ const Template: Story<ArgTypes> = ({ value, currentSeriesIndex }: ArgTypes) => h
68
67
  }
69
68
  </style>
70
69
 
70
+ <div style="background-color: black; color: white;">
71
+ Type 'count' or 'average' for series dataKey, type 'year' for axis's dataKey
72
+ </div>
73
+
71
74
  <div class="container light">
72
75
  <ox-input-chart-pie
73
76
  id="editor"
@@ -75,9 +78,9 @@ const Template: Story<ArgTypes> = ({ value, currentSeriesIndex }: ArgTypes) => h
75
78
  @change=${(e: CustomEvent) => {
76
79
  const target = e.currentTarget as any
77
80
  const config = target.value
78
- const chart = document.querySelector('#chart') as HTMLElement
79
- if (chart) {
80
- ;(chart as any).config = convertConfigure(config, {})
81
+ const chartjs = document.querySelector('#chartjs') as HTMLElement
82
+ if (chartjs) {
83
+ ;(chartjs as any).config = { ...config }
81
84
  }
82
85
 
83
86
  const container = document.querySelector('.container') as HTMLElement
@@ -93,7 +96,7 @@ const Template: Story<ArgTypes> = ({ value, currentSeriesIndex }: ArgTypes) => h
93
96
  }
94
97
  }}
95
98
  ></ox-input-chart-pie>
96
- <ox-chart-js id="chart"></ox-chart-js>
99
+ <ox-chart-js id="chartjs" .data=${data}></ox-chart-js>
97
100
  </div>
98
101
  `
99
102
 
@@ -2,9 +2,8 @@ import { html, TemplateResult } from 'lit'
2
2
 
3
3
  import '../src/editors/ox-input-chart-radar.js'
4
4
  import '../src/chartjs/ox-chart-js.js'
5
- import { convertConfigure } from '../src/chartjs/config-converter.js'
6
5
 
7
- import { getDefaultChartConfig } from './common.js'
6
+ import { data, getDefaultChartConfig } from './common.js'
8
7
 
9
8
  export default {
10
9
  title: 'ox-input-chart-radar',
@@ -68,6 +67,10 @@ const Template: Story<ArgTypes> = ({ value, currentSeriesIndex }: ArgTypes) => h
68
67
  }
69
68
  </style>
70
69
 
70
+ <div style="background-color: black; color: white;">
71
+ Type 'count' or 'average' for series dataKey, type 'year' for axis's dataKey
72
+ </div>
73
+
71
74
  <div class="container light">
72
75
  <ox-input-chart-radar
73
76
  id="editor"
@@ -75,9 +78,9 @@ const Template: Story<ArgTypes> = ({ value, currentSeriesIndex }: ArgTypes) => h
75
78
  @change=${(e: CustomEvent) => {
76
79
  const target = e.currentTarget as any
77
80
  const config = target.value
78
- const chart = document.querySelector('#chart') as HTMLElement
79
- if (chart) {
80
- ;(chart as any).config = convertConfigure(config, {})
81
+ const chartjs = document.querySelector('#chartjs') as HTMLElement
82
+ if (chartjs) {
83
+ ;(chartjs as any).config = { ...config }
81
84
  }
82
85
 
83
86
  const container = document.querySelector('.container') as HTMLElement
@@ -93,7 +96,7 @@ const Template: Story<ArgTypes> = ({ value, currentSeriesIndex }: ArgTypes) => h
93
96
  }
94
97
  }}
95
98
  ></ox-input-chart-radar>
96
- <ox-chart-js id="chart"></ox-chart-js>
99
+ <ox-chart-js id="chartjs" .data=${data}></ox-chart-js>
97
100
  </div>
98
101
  `
99
102
 
package/tsconfig.json CHANGED
@@ -20,5 +20,5 @@
20
20
  "incremental": true,
21
21
  "types": ["node", "mocha"]
22
22
  },
23
- "include": ["**/*.ts"]
23
+ "include": ["**/*.ts", "*.d.ts"]
24
24
  }
File without changes