@orbcharts/core 3.0.0-alpha.26 → 3.0.0-alpha.28

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orbcharts/core",
3
- "version": "3.0.0-alpha.26",
3
+ "version": "3.0.0-alpha.28",
4
4
  "description": "OrbCharts is an open source chart library based on d3.js and rx.js",
5
5
  "author": "Blue Planet Inc.",
6
6
  "license": "Apache-2.0",
package/src/defaults.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // import { ChartRenderOptions } from './types/Chart'
3
3
  import type { ChartType, ChartOptionsPartial } from './types/Chart'
4
4
  import type { DataSeries } from './types/DataSeries'
5
- import type { DataGrid } from './types/DataGrid'
5
+ import type { DataGrid, DataGridDatum } from './types/DataGrid'
6
6
  import type { DataMultiGrid } from './types/DataMultiGrid'
7
7
  import type { DataMultiValue } from './types/DataMultiValue'
8
8
  import type { DataTree } from './types/DataTree'
@@ -197,7 +197,20 @@ export const DATA_FORMATTER_GRID_DEFAULT: DataFormatterGrid = {
197
197
  export const DATA_FORMATTER_MULTI_GRID_DEFAULT: DataFormatterMultiGrid = {
198
198
  ...DATA_FORMATTER,
199
199
  type: 'multiGrid',
200
- multiGrid: [], // 預設無資料,由各plugin定義預設值
200
+ multiGrid: [
201
+ {
202
+ ...DATA_FORMATTER_GRID_DEFAULT
203
+ },
204
+ // @Q@ 暫時性的邏輯,之後colorsPredicate移除掉後,這邊也要移除(但colors使用的seriesIndex要接續前一組grid)
205
+ {
206
+ ...DATA_FORMATTER_GRID_DEFAULT,
207
+ colorsPredicate: (datum, rowIndex, columnIndex, { chartParams, dataFormatter }) => {
208
+ const seriesIndex = dataFormatter.grid.seriesType === 'row' ? rowIndex : columnIndex
209
+ const reverseIndex = chartParams.colors[chartParams.colorScheme].series.length - 1 - seriesIndex
210
+ return chartParams.colors[chartParams.colorScheme].series[reverseIndex]
211
+ },
212
+ }
213
+ ],
201
214
  // visibleGroupRange: null,
202
215
  }
203
216
 
@@ -205,7 +218,7 @@ export const DATA_FORMATTER_MULTI_VALUE_DEFAULT: DataFormatterMultiValue = {
205
218
  ...DATA_FORMATTER,
206
219
  type: 'multiValue',
207
220
  // labelFormat: (datum: any) => (datum && datum.label) ?? '',
208
- multiValue: [], // 預設無資料,由各plugin定義預設值
221
+ multiValue: [],
209
222
  xAxis: { ...DATA_FORMATTER_VALUE_AXIS },
210
223
  yAxis: { ...DATA_FORMATTER_VALUE_AXIS },
211
224
  }
@@ -127,7 +127,8 @@ export const gridAxesTransformObservable = ({ fullDataFormatter$, layout$ }: {
127
127
  fullDataFormatter: fullDataFormatter$,
128
128
  layout: layout$
129
129
  }).pipe(
130
- takeUntil(destroy$)
130
+ takeUntil(destroy$),
131
+ switchMap(async (d) => d),
131
132
  ).subscribe(data => {
132
133
  const axesTransformData = calcAxesTransform({
133
134
  xAxis: data.fullDataFormatter.groupAxis,
@@ -238,7 +239,8 @@ export const gridGraphicTransformObservable = ({ computedData$, fullDataFormatte
238
239
  fullDataFormatter: fullDataFormatter$,
239
240
  layout: layout$
240
241
  }).pipe(
241
- takeUntil(destroy$)
242
+ takeUntil(destroy$),
243
+ switchMap(async (d) => d),
242
244
  ).subscribe(data => {
243
245
  const dataAreaTransformData = calcGridDataAreaTransform ({
244
246
  data: data.computedData,
@@ -311,7 +313,6 @@ export const gridAxesSizeObservable = ({ fullDataFormatter$, layout$ }: {
311
313
  layout: layout$
312
314
  }).pipe(
313
315
  takeUntil(destroy$),
314
- // 轉換後會退訂前一個未完成的訂閱事件,因此可以取到「同時間」最後一次的訂閱事件
315
316
  switchMap(async (d) => d),
316
317
  ).subscribe(data => {
317
318
 
@@ -1,12 +1,26 @@
1
+ import {
2
+ shareReplay } from 'rxjs'
1
3
  import type { ContextObserverFn } from '../types'
4
+ import { multiGridObservable } from './multiGridObservables'
2
5
 
3
6
  export const createMultiGridContextObserver: ContextObserverFn<'multiGrid'> = ({ subject, observer }) => {
4
7
 
8
+ const multiGrid$ = multiGridObservable({
9
+ fullDataFormatter$: observer.fullDataFormatter$,
10
+ computedData$: observer.computedData$,
11
+ layout$: observer.layout$,
12
+ fullChartParams$: observer.fullChartParams$,
13
+ event$: subject.event$
14
+ }).pipe(
15
+ shareReplay(1)
16
+ )
17
+
5
18
  return {
6
19
  fullParams$: observer.fullParams$,
7
20
  fullChartParams$: observer.fullChartParams$,
8
21
  fullDataFormatter$: observer.fullDataFormatter$,
9
22
  computedData$: observer.computedData$,
10
23
  layout$: observer.layout$,
24
+ multiGrid$
11
25
  }
12
26
  }
@@ -0,0 +1,154 @@
1
+ import {
2
+ combineLatest,
3
+ distinctUntilChanged,
4
+ filter,
5
+ of,
6
+ map,
7
+ merge,
8
+ takeUntil,
9
+ shareReplay,
10
+ switchMap,
11
+ Subject,
12
+ Observable,
13
+ combineLatestAll} from 'rxjs'
14
+ import type {
15
+ AxisPosition,
16
+ ChartType,
17
+ ChartParams,
18
+ ComputedDataTypeMap,
19
+ ComputedDatumTypeMap,
20
+ ContextObserverFn,
21
+ DataTypeMap,
22
+ DataFormatterTypeMap,
23
+ DataFormatterGrid,
24
+ DataFormatterContext,
25
+ DataFormatterValueAxis,
26
+ DataFormatterGroupAxis,
27
+ EventMultiGrid,
28
+ HighlightTarget,
29
+ Layout,
30
+ TransformData } from '../types'
31
+ import { getMinAndMaxGrid, transposeData } from '../utils/orbchartsUtils'
32
+ import { createAxisLinearScale, createAxisPointScale, createAxisQuantizeScale } from '../utils/d3Utils'
33
+ import {
34
+ highlightObservable,
35
+ seriesDataMapObservable,
36
+ groupDataMapObservable } from '../utils/observables'
37
+ import {
38
+ gridAxesTransformObservable,
39
+ gridGraphicTransformObservable,
40
+ gridAxesOppositeTransformObservable,
41
+ gridAxesSizeObservable,
42
+ gridVisibleComputedDataObservable } from '../grid/gridObservables'
43
+
44
+ export const multiGridObservable = ({ fullDataFormatter$, computedData$, layout$, fullChartParams$, event$ }: {
45
+ fullDataFormatter$: Observable<DataFormatterTypeMap<'multiGrid'>>
46
+ computedData$: Observable<ComputedDataTypeMap<'multiGrid'>>
47
+ layout$: Observable<Layout>
48
+ fullChartParams$: Observable<ChartParams>
49
+ event$: Subject<EventMultiGrid>
50
+ }) => {
51
+ const destroy$ = new Subject()
52
+
53
+ return combineLatest({
54
+ fullDataFormatter: fullDataFormatter$,
55
+ computedData: computedData$,
56
+ }).pipe(
57
+ switchMap(async (d) => d),
58
+ distinctUntilChanged((a, b) => {
59
+ // 只有當computedData的長度改變時,才重新計算
60
+ return a.computedData.length === b.computedData.length
61
+ }),
62
+ map(data => {
63
+ // 每次重新計算時,清除之前的訂閱
64
+ destroy$.next(undefined)
65
+
66
+ return data.computedData.map((gridComputedData, gridIndex) => {
67
+
68
+ // -- 取得該grid的data和dataFormatter
69
+ const gridDataFormatter = data.fullDataFormatter.multiGrid[gridIndex]
70
+ ?? data.fullDataFormatter.multiGrid[0] // 預設使用第0筆資料
71
+ const gridDataFormatter$ = of(gridDataFormatter).pipe(
72
+ takeUntil(destroy$),
73
+ shareReplay(1)
74
+ )
75
+ const gridComputedData$ = of(gridComputedData).pipe(
76
+ takeUntil(destroy$),
77
+ shareReplay(1)
78
+ )
79
+
80
+ // -- 建立Observables --
81
+ const gridAxesTransform$ = gridAxesTransformObservable({
82
+ fullDataFormatter$: gridDataFormatter$,
83
+ layout$: layout$
84
+ }).pipe(
85
+ shareReplay(1)
86
+ )
87
+
88
+ const gridGraphicTransform$ = gridGraphicTransformObservable({
89
+ computedData$: gridComputedData$,
90
+ fullDataFormatter$: gridDataFormatter$,
91
+ layout$: layout$
92
+ }).pipe(
93
+ shareReplay(1)
94
+ )
95
+
96
+ const gridAxesOppositeTransform$ = gridAxesOppositeTransformObservable({
97
+ gridAxesTransform$
98
+ }).pipe(
99
+ shareReplay(1)
100
+ )
101
+
102
+ const gridAxesSize$ = gridAxesSizeObservable({
103
+ fullDataFormatter$: gridDataFormatter$,
104
+ layout$: layout$
105
+ }).pipe(
106
+ shareReplay(1)
107
+ )
108
+
109
+ const datumList$ = gridComputedData$.pipe(
110
+ map(d => d.flat())
111
+ ).pipe(
112
+ shareReplay(1)
113
+ )
114
+
115
+ const gridHighlight$ = highlightObservable({
116
+ datumList$,
117
+ fullChartParams$: fullChartParams$,
118
+ event$: event$
119
+ }).pipe(
120
+ shareReplay(1)
121
+ )
122
+
123
+ const SeriesDataMap$ = seriesDataMapObservable({
124
+ datumList$: datumList$
125
+ }).pipe(
126
+ shareReplay(1)
127
+ )
128
+
129
+ const GroupDataMap$ = groupDataMapObservable({
130
+ datumList$: datumList$
131
+ }).pipe(
132
+ shareReplay(1)
133
+ )
134
+
135
+ const visibleComputedData$ = gridVisibleComputedDataObservable({
136
+ computedData$: gridComputedData$,
137
+ }).pipe(
138
+ shareReplay(1)
139
+ )
140
+
141
+ return {
142
+ gridAxesTransform$,
143
+ gridGraphicTransform$,
144
+ gridAxesOppositeTransform$,
145
+ gridAxesSize$,
146
+ gridHighlight$,
147
+ SeriesDataMap$,
148
+ GroupDataMap$,
149
+ visibleComputedData$
150
+ }
151
+ })
152
+ })
153
+ )
154
+ }
@@ -1,5 +1,19 @@
1
+ import { Observable } from 'rxjs'
1
2
  import type { ContextObserverBase } from './ContextObserver'
3
+ import type { ComputedDataGrid, ComputedDatumGrid } from './ComputedDataGrid'
4
+ import type { TransformData } from './TransformData'
2
5
 
3
6
  export interface ContextObserverMultiGrid<PluginParams> extends ContextObserverBase<'multiGrid', PluginParams> {
7
+ multiGrid$: Observable<MultiGridObservableAll[]>
8
+ }
4
9
 
10
+ export interface MultiGridObservableAll {
11
+ gridAxesTransform$: Observable<TransformData>
12
+ gridGraphicTransform$: Observable<TransformData>
13
+ gridAxesOppositeTransform$: Observable<TransformData>
14
+ gridAxesSize$: Observable<{ width: number; height: number; }>
15
+ gridHighlight$: Observable<string[]>
16
+ SeriesDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
17
+ GroupDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
18
+ visibleComputedData$: Observable<ComputedDataGrid>
5
19
  }
@@ -1,5 +1,5 @@
1
1
  import { DataGridDatum, DataGridValue } from './DataGrid'
2
- import { DataFormatterGridGrid } from './DataFormatterGrid'
2
+ import { DataFormatterGrid, DataFormatterGridGrid } from './DataFormatterGrid'
3
3
  import {
4
4
  DataFormatterBase,
5
5
  DataFormatterBasePartial,
@@ -9,19 +9,19 @@ import {
9
9
  import { AxisPosition } from './Axis'
10
10
 
11
11
  export interface DataFormatterMultiGrid extends DataFormatterBase<'multiGrid'> {
12
- multiGrid: Array<DataFormatterMultiGridMultiGrid>
12
+ multiGrid: Array<DataFormatterGrid>
13
13
  // visibleGroupRange: [number, number] | null
14
14
  }
15
15
 
16
16
  export interface DataFormatterMultiGridPartial extends DataFormatterBasePartial<'multiGrid'> {
17
- multiGrid?: Array<Partial<DataFormatterMultiGridMultiGrid>>
17
+ multiGrid?: Array<Partial<DataFormatterGrid>>
18
18
  }
19
19
 
20
- // multiGrid欄位
21
- export interface DataFormatterMultiGridMultiGrid {
22
- grid: DataFormatterGridGrid
23
- valueAxis: DataFormatterValueAxis // default: 'left'
24
- groupAxis: DataFormatterGroupAxis // default: 'bottom'
25
- colorsPredicate: (datum: DataGridDatum | DataGridValue, rowIndex: number, columnIndex: number, context: DataFormatterContext<'grid'>) => string
26
- // colors: Colors
27
- }
20
+ // // multiGrid欄位
21
+ // export interface DataFormatterMultiGridMultiGrid {
22
+ // grid: DataFormatterGridGrid
23
+ // valueAxis: DataFormatterValueAxis // default: 'left'
24
+ // groupAxis: DataFormatterGroupAxis // default: 'bottom'
25
+ // colorsPredicate: (datum: DataGridDatum | DataGridValue, rowIndex: number, columnIndex: number, context: DataFormatterContext<'grid'>) => string
26
+ // // colors: Colors
27
+ // }