@orbcharts/core 3.0.0-alpha.55 → 3.0.0-alpha.57
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +200 -200
- package/dist/orbcharts-core.es.js +221 -218
- package/dist/orbcharts-core.umd.js +2 -2
- package/package.json +41 -41
- package/src/AbstractChart.ts +48 -48
- package/src/GridChart.ts +20 -20
- package/src/MultiGridChart.ts +20 -20
- package/src/MultiValueChart.ts +20 -20
- package/src/RelationshipChart.ts +20 -20
- package/src/SeriesChart.ts +20 -20
- package/src/TreeChart.ts +20 -20
- package/src/base/createBaseChart.ts +369 -369
- package/src/base/createBasePlugin.ts +95 -95
- package/src/defaults.ts +226 -226
- package/src/defineGridPlugin.ts +3 -3
- package/src/defineMultiGridPlugin.ts +3 -3
- package/src/defineMultiValuePlugin.ts +3 -3
- package/src/defineNoneDataPlugin.ts +4 -4
- package/src/defineRelationshipPlugin.ts +3 -3
- package/src/defineSeriesPlugin.ts +3 -3
- package/src/defineTreePlugin.ts +3 -3
- package/src/grid/computeGridData.ts +134 -134
- package/src/grid/createGridContextObserver.ts +155 -155
- package/src/grid/gridObservables.ts +613 -607
- package/src/index.ts +21 -21
- package/src/multiGrid/computeMultiGridData.ts +130 -130
- package/src/multiGrid/createMultiGridContextObserver.ts +41 -41
- package/src/multiGrid/multiGridObservables.ts +365 -365
- package/src/multiValue/computeMultiValueData.ts +143 -143
- package/src/multiValue/createMultiValueContextObserver.ts +12 -12
- package/src/relationship/computeRelationshipData.ts +118 -118
- package/src/relationship/createRelationshipContextObserver.ts +12 -12
- package/src/series/computeSeriesData.ts +90 -90
- package/src/series/createSeriesContextObserver.ts +93 -93
- package/src/series/seriesObservables.ts +175 -175
- package/src/tree/computeTreeData.ts +132 -132
- package/src/tree/createTreeContextObserver.ts +61 -61
- package/src/tree/treeObservables.ts +94 -94
- package/src/types/Chart.ts +50 -50
- package/src/types/ChartParams.ts +51 -51
- package/src/types/ComputedData.ts +83 -83
- package/src/types/ComputedDataGrid.ts +13 -13
- package/src/types/ComputedDataMultiGrid.ts +2 -2
- package/src/types/ComputedDataMultiValue.ts +9 -9
- package/src/types/ComputedDataRelationship.ts +19 -19
- package/src/types/ComputedDataSeries.ts +7 -7
- package/src/types/ComputedDataTree.ts +19 -19
- package/src/types/ContextObserver.ts +38 -38
- package/src/types/ContextObserverGrid.ts +42 -42
- package/src/types/ContextObserverMultiGrid.ts +16 -16
- package/src/types/ContextObserverMultiValue.ts +4 -4
- package/src/types/ContextObserverRelationship.ts +4 -4
- package/src/types/ContextObserverSeries.ts +29 -29
- package/src/types/ContextObserverTree.ts +11 -11
- package/src/types/ContextSubject.ts +18 -18
- package/src/types/Data.ts +45 -45
- package/src/types/DataFormatter.ts +74 -74
- package/src/types/DataFormatterGrid.ts +67 -67
- package/src/types/DataFormatterMultiGrid.ts +44 -44
- package/src/types/DataFormatterMultiValue.ts +23 -23
- package/src/types/DataFormatterRelationship.ts +25 -25
- package/src/types/DataFormatterSeries.ts +20 -20
- package/src/types/DataFormatterTree.ts +12 -12
- package/src/types/DataGrid.ts +11 -11
- package/src/types/DataMultiGrid.ts +6 -6
- package/src/types/DataMultiValue.ts +12 -12
- package/src/types/DataRelationship.ts +27 -27
- package/src/types/DataSeries.ts +11 -11
- package/src/types/DataTree.ts +20 -20
- package/src/types/Event.ts +153 -153
- package/src/types/Layout.ts +11 -11
- package/src/types/Padding.ts +5 -5
- package/src/types/Plugin.ts +60 -60
- package/src/types/TransformData.ts +7 -7
- package/src/types/index.ts +37 -37
- package/src/utils/commonUtils.ts +50 -50
- package/src/utils/d3Utils.ts +92 -92
- package/src/utils/index.ts +4 -4
- package/src/utils/observables.ts +201 -201
- package/src/utils/orbchartsUtils.ts +349 -349
- package/tsconfig.base.json +13 -13
- package/tsconfig.json +2 -2
- package/vite.config.js +22 -22
package/src/types/Plugin.ts
CHANGED
@@ -1,60 +1,60 @@
|
|
1
|
-
import * as d3 from 'd3'
|
2
|
-
import { Subject } from 'rxjs'
|
3
|
-
import type { ChartType } from './Chart'
|
4
|
-
import type { ContextSubject } from './ContextSubject'
|
5
|
-
import type { ContextObserverTypeMap } from './ContextObserver'
|
6
|
-
|
7
|
-
// // 透過類型選擇Plugin
|
8
|
-
// export type PluginParamsMap<T extends ChartType, PluginParams> = T extends 'series' ? Plugin<DataSeries, DataFormatterSeries, ComputedDataSeries, PluginParams>
|
9
|
-
// : T extends 'grid' ? Plugin<DataGrid, DataFormatterGrid, ComputedDataGrid, PluginParams>
|
10
|
-
// : T extends 'multiGrid' ? Plugin<DataMultiGrid, DataFormatterMultiGrid, ComputedDataMultiGrid, PluginParams>
|
11
|
-
// : T extends 'multiValue' ? Plugin<DataMultiValue, DataFormatterMultiValue, ComputedDataMultiValue, PluginParams>
|
12
|
-
// : T extends 'relationship' ? Plugin<DataRelationship, DataFormatterRelationship, ComputedDataRelationship, PluginParams>
|
13
|
-
// : T extends 'tree' ? Plugin<DataTree, DataFormatterTree, ComputedDataTree, PluginParams>
|
14
|
-
// : Plugin<unknown, unknown, unknown, unknown>
|
15
|
-
|
16
|
-
export interface CreateBasePlugin {
|
17
|
-
<T extends ChartType>(): DefinePlugin<T>
|
18
|
-
}
|
19
|
-
|
20
|
-
export interface DefinePlugin<T extends ChartType> {
|
21
|
-
<PluginName, PluginParams>(name: PluginName , defaultParams: PluginParams): DefinePluginInitFn<T, PluginName, PluginParams>
|
22
|
-
}
|
23
|
-
|
24
|
-
export interface DefinePluginInitFn<T extends ChartType, PluginName, PluginParams> {
|
25
|
-
(initFn: PluginInitFn<T, PluginName, PluginParams>): PluginConstructor<T, PluginName, PluginParams>
|
26
|
-
}
|
27
|
-
|
28
|
-
// export interface CreatePlugin<T extends ChartType, PluginName, PluginParams> {
|
29
|
-
// (): PluginEntity<T, PluginName, PluginParams>
|
30
|
-
// }
|
31
|
-
|
32
|
-
export interface PluginConstructor<T extends ChartType, PluginName, PluginParams> {
|
33
|
-
new (): PluginEntity<T, PluginName, PluginParams>
|
34
|
-
}
|
35
|
-
|
36
|
-
export interface PluginEntity<T extends ChartType, PluginName, PluginParams> {
|
37
|
-
params$: Subject<Partial<PluginParams>>
|
38
|
-
name: PluginName
|
39
|
-
defaultParams: PluginParams
|
40
|
-
// presetParams: Partial<PluginParams>
|
41
|
-
init: () => void
|
42
|
-
destroy: () => void
|
43
|
-
setPresetParams: (presetParams: Partial<PluginParams>) => void
|
44
|
-
setContext: (pluginContext: PluginContext<T, PluginName, PluginParams>) => void
|
45
|
-
}
|
46
|
-
|
47
|
-
export interface PluginInitFn<T extends ChartType, PluginName, PluginParams> {
|
48
|
-
(pluginContext: PluginContext<T, PluginName, PluginParams>): () => void
|
49
|
-
}
|
50
|
-
|
51
|
-
export interface PluginContext<T extends ChartType, PluginName, PluginParams> {
|
52
|
-
selection: d3.Selection<any, unknown, any, unknown>
|
53
|
-
rootSelection: d3.Selection<any, unknown, any, unknown>
|
54
|
-
name: PluginName
|
55
|
-
chartType: ChartType, // 這邊的ChartType是由 createChart 時依 chart類別決定的,所以不能使用 pluginContext 本身的類別 T
|
56
|
-
// 原本的store
|
57
|
-
// selection: d3.Selection<SVGGElement, unknown, HTMLElement, unknown>
|
58
|
-
subject: ContextSubject<T>
|
59
|
-
observer: ContextObserverTypeMap<T, PluginParams>
|
60
|
-
}
|
1
|
+
import * as d3 from 'd3'
|
2
|
+
import { Subject } from 'rxjs'
|
3
|
+
import type { ChartType } from './Chart'
|
4
|
+
import type { ContextSubject } from './ContextSubject'
|
5
|
+
import type { ContextObserverTypeMap } from './ContextObserver'
|
6
|
+
|
7
|
+
// // 透過類型選擇Plugin
|
8
|
+
// export type PluginParamsMap<T extends ChartType, PluginParams> = T extends 'series' ? Plugin<DataSeries, DataFormatterSeries, ComputedDataSeries, PluginParams>
|
9
|
+
// : T extends 'grid' ? Plugin<DataGrid, DataFormatterGrid, ComputedDataGrid, PluginParams>
|
10
|
+
// : T extends 'multiGrid' ? Plugin<DataMultiGrid, DataFormatterMultiGrid, ComputedDataMultiGrid, PluginParams>
|
11
|
+
// : T extends 'multiValue' ? Plugin<DataMultiValue, DataFormatterMultiValue, ComputedDataMultiValue, PluginParams>
|
12
|
+
// : T extends 'relationship' ? Plugin<DataRelationship, DataFormatterRelationship, ComputedDataRelationship, PluginParams>
|
13
|
+
// : T extends 'tree' ? Plugin<DataTree, DataFormatterTree, ComputedDataTree, PluginParams>
|
14
|
+
// : Plugin<unknown, unknown, unknown, unknown>
|
15
|
+
|
16
|
+
export interface CreateBasePlugin {
|
17
|
+
<T extends ChartType>(): DefinePlugin<T>
|
18
|
+
}
|
19
|
+
|
20
|
+
export interface DefinePlugin<T extends ChartType> {
|
21
|
+
<PluginName, PluginParams>(name: PluginName , defaultParams: PluginParams): DefinePluginInitFn<T, PluginName, PluginParams>
|
22
|
+
}
|
23
|
+
|
24
|
+
export interface DefinePluginInitFn<T extends ChartType, PluginName, PluginParams> {
|
25
|
+
(initFn: PluginInitFn<T, PluginName, PluginParams>): PluginConstructor<T, PluginName, PluginParams>
|
26
|
+
}
|
27
|
+
|
28
|
+
// export interface CreatePlugin<T extends ChartType, PluginName, PluginParams> {
|
29
|
+
// (): PluginEntity<T, PluginName, PluginParams>
|
30
|
+
// }
|
31
|
+
|
32
|
+
export interface PluginConstructor<T extends ChartType, PluginName, PluginParams> {
|
33
|
+
new (): PluginEntity<T, PluginName, PluginParams>
|
34
|
+
}
|
35
|
+
|
36
|
+
export interface PluginEntity<T extends ChartType, PluginName, PluginParams> {
|
37
|
+
params$: Subject<Partial<PluginParams>>
|
38
|
+
name: PluginName
|
39
|
+
defaultParams: PluginParams
|
40
|
+
// presetParams: Partial<PluginParams>
|
41
|
+
init: () => void
|
42
|
+
destroy: () => void
|
43
|
+
setPresetParams: (presetParams: Partial<PluginParams>) => void
|
44
|
+
setContext: (pluginContext: PluginContext<T, PluginName, PluginParams>) => void
|
45
|
+
}
|
46
|
+
|
47
|
+
export interface PluginInitFn<T extends ChartType, PluginName, PluginParams> {
|
48
|
+
(pluginContext: PluginContext<T, PluginName, PluginParams>): () => void
|
49
|
+
}
|
50
|
+
|
51
|
+
export interface PluginContext<T extends ChartType, PluginName, PluginParams> {
|
52
|
+
selection: d3.Selection<any, unknown, any, unknown>
|
53
|
+
rootSelection: d3.Selection<any, unknown, any, unknown>
|
54
|
+
name: PluginName
|
55
|
+
chartType: ChartType, // 這邊的ChartType是由 createChart 時依 chart類別決定的,所以不能使用 pluginContext 本身的類別 T
|
56
|
+
// 原本的store
|
57
|
+
// selection: d3.Selection<SVGGElement, unknown, HTMLElement, unknown>
|
58
|
+
subject: ContextSubject<T>
|
59
|
+
observer: ContextObserverTypeMap<T, PluginParams>
|
60
|
+
}
|
@@ -1,8 +1,8 @@
|
|
1
|
-
export interface TransformData {
|
2
|
-
translate: [number, number]
|
3
|
-
scale: [number, number]
|
4
|
-
rotate: number
|
5
|
-
rotateX: number
|
6
|
-
rotateY: number
|
7
|
-
value: string
|
1
|
+
export interface TransformData {
|
2
|
+
translate: [number, number]
|
3
|
+
scale: [number, number]
|
4
|
+
rotate: number
|
5
|
+
rotateX: number
|
6
|
+
rotateY: number
|
7
|
+
value: string
|
8
8
|
}
|
package/src/types/index.ts
CHANGED
@@ -1,37 +1,37 @@
|
|
1
|
-
export * from './Axis'
|
2
|
-
export * from './Chart'
|
3
|
-
export * from './ChartParams'
|
4
|
-
export * from './ComputedData'
|
5
|
-
export * from './ComputedDataGrid'
|
6
|
-
export * from './ComputedDataMultiGrid'
|
7
|
-
export * from './ComputedDataMultiValue'
|
8
|
-
export * from './ComputedDataRelationship'
|
9
|
-
export * from './ComputedDataSeries'
|
10
|
-
export * from './ComputedDataTree'
|
11
|
-
export * from './ContextObserver'
|
12
|
-
export * from './ContextObserverGrid'
|
13
|
-
export * from './ContextObserverMultiGrid'
|
14
|
-
export * from './ContextObserverMultiValue'
|
15
|
-
export * from './ContextObserverRelationship'
|
16
|
-
export * from './ContextObserverSeries'
|
17
|
-
export * from './ContextObserverTree'
|
18
|
-
export * from './ContextSubject'
|
19
|
-
export * from './Data'
|
20
|
-
export * from './DataFormatter'
|
21
|
-
export * from './DataFormatterGrid'
|
22
|
-
export * from './DataFormatterMultiGrid'
|
23
|
-
export * from './DataFormatterMultiValue'
|
24
|
-
export * from './DataFormatterRelationship'
|
25
|
-
export * from './DataFormatterSeries'
|
26
|
-
export * from './DataFormatterTree'
|
27
|
-
export * from './DataGrid'
|
28
|
-
export * from './DataMultiGrid'
|
29
|
-
export * from './DataMultiValue'
|
30
|
-
export * from './DataRelationship'
|
31
|
-
export * from './DataSeries'
|
32
|
-
export * from './DataTree'
|
33
|
-
export * from './Event'
|
34
|
-
export * from './Layout'
|
35
|
-
export * from './Padding'
|
36
|
-
export * from './Plugin'
|
37
|
-
export * from './TransformData'
|
1
|
+
export * from './Axis'
|
2
|
+
export * from './Chart'
|
3
|
+
export * from './ChartParams'
|
4
|
+
export * from './ComputedData'
|
5
|
+
export * from './ComputedDataGrid'
|
6
|
+
export * from './ComputedDataMultiGrid'
|
7
|
+
export * from './ComputedDataMultiValue'
|
8
|
+
export * from './ComputedDataRelationship'
|
9
|
+
export * from './ComputedDataSeries'
|
10
|
+
export * from './ComputedDataTree'
|
11
|
+
export * from './ContextObserver'
|
12
|
+
export * from './ContextObserverGrid'
|
13
|
+
export * from './ContextObserverMultiGrid'
|
14
|
+
export * from './ContextObserverMultiValue'
|
15
|
+
export * from './ContextObserverRelationship'
|
16
|
+
export * from './ContextObserverSeries'
|
17
|
+
export * from './ContextObserverTree'
|
18
|
+
export * from './ContextSubject'
|
19
|
+
export * from './Data'
|
20
|
+
export * from './DataFormatter'
|
21
|
+
export * from './DataFormatterGrid'
|
22
|
+
export * from './DataFormatterMultiGrid'
|
23
|
+
export * from './DataFormatterMultiValue'
|
24
|
+
export * from './DataFormatterRelationship'
|
25
|
+
export * from './DataFormatterSeries'
|
26
|
+
export * from './DataFormatterTree'
|
27
|
+
export * from './DataGrid'
|
28
|
+
export * from './DataMultiGrid'
|
29
|
+
export * from './DataMultiValue'
|
30
|
+
export * from './DataRelationship'
|
31
|
+
export * from './DataSeries'
|
32
|
+
export * from './DataTree'
|
33
|
+
export * from './Event'
|
34
|
+
export * from './Layout'
|
35
|
+
export * from './Padding'
|
36
|
+
export * from './Plugin'
|
37
|
+
export * from './TransformData'
|
package/src/utils/commonUtils.ts
CHANGED
@@ -1,50 +1,50 @@
|
|
1
|
-
// import * as d3 from 'd3'
|
2
|
-
|
3
|
-
// 是否為原始物件
|
4
|
-
export function isPlainObject(variable: any) {
|
5
|
-
return Object.prototype.toString.call(variable) === "[object Object]";
|
6
|
-
}
|
7
|
-
|
8
|
-
// 是否為function
|
9
|
-
export function isFunction(fn: any) {
|
10
|
-
return !!fn && !fn.nodename && fn.constructor != String && fn.constructor != RegExp && fn.constructor != Array && /function/i.test(fn + "");
|
11
|
-
}
|
12
|
-
|
13
|
-
// 將可選的參數和預設值合併
|
14
|
-
export function mergeOptionsWithDefault<Options extends { [key: string]: any; }> (options: {[key: string]: any}, defaultOptions: Options): Options {
|
15
|
-
if (isPlainObject(options) === false || isPlainObject(defaultOptions) === false) {
|
16
|
-
return Object.assign({}, defaultOptions)
|
17
|
-
}
|
18
|
-
const mergeObjColumns = (_options: {[key: string]: any}, _defaultOptions: {[key: string]: any}) => {
|
19
|
-
const obj: Options = (Object.assign({}, _defaultOptions) as any)
|
20
|
-
for (let key of Object.keys(_options)) {
|
21
|
-
if ((key in _defaultOptions) == false) {
|
22
|
-
continue
|
23
|
-
}
|
24
|
-
let objValue: any = undefined
|
25
|
-
// 下一層的plain object
|
26
|
-
if (isPlainObject(_options[key]) && isPlainObject(_defaultOptions[key])) {
|
27
|
-
objValue = mergeObjColumns(_options[key], _defaultOptions[key])
|
28
|
-
obj[key as keyof Options] = objValue
|
29
|
-
}
|
30
|
-
// 不是plain object直接賦值
|
31
|
-
else {
|
32
|
-
obj[key as keyof Options] = _options[key]
|
33
|
-
}
|
34
|
-
}
|
35
|
-
return obj
|
36
|
-
}
|
37
|
-
|
38
|
-
return mergeObjColumns(options, defaultOptions)
|
39
|
-
}
|
40
|
-
|
41
|
-
// 加上千分位 ,
|
42
|
-
export function formatCommaNumber (num = 0): string {
|
43
|
-
try {
|
44
|
-
let parts = num.toString().split('.');
|
45
|
-
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
46
|
-
return parts.join('.');
|
47
|
-
} catch (e: any) {
|
48
|
-
console.error(e)
|
49
|
-
}
|
50
|
-
}
|
1
|
+
// import * as d3 from 'd3'
|
2
|
+
|
3
|
+
// 是否為原始物件
|
4
|
+
export function isPlainObject(variable: any) {
|
5
|
+
return Object.prototype.toString.call(variable) === "[object Object]";
|
6
|
+
}
|
7
|
+
|
8
|
+
// 是否為function
|
9
|
+
export function isFunction(fn: any) {
|
10
|
+
return !!fn && !fn.nodename && fn.constructor != String && fn.constructor != RegExp && fn.constructor != Array && /function/i.test(fn + "");
|
11
|
+
}
|
12
|
+
|
13
|
+
// 將可選的參數和預設值合併
|
14
|
+
export function mergeOptionsWithDefault<Options extends { [key: string]: any; }> (options: {[key: string]: any}, defaultOptions: Options): Options {
|
15
|
+
if (isPlainObject(options) === false || isPlainObject(defaultOptions) === false) {
|
16
|
+
return Object.assign({}, defaultOptions)
|
17
|
+
}
|
18
|
+
const mergeObjColumns = (_options: {[key: string]: any}, _defaultOptions: {[key: string]: any}) => {
|
19
|
+
const obj: Options = (Object.assign({}, _defaultOptions) as any)
|
20
|
+
for (let key of Object.keys(_options)) {
|
21
|
+
if ((key in _defaultOptions) == false) {
|
22
|
+
continue
|
23
|
+
}
|
24
|
+
let objValue: any = undefined
|
25
|
+
// 下一層的plain object
|
26
|
+
if (isPlainObject(_options[key]) && isPlainObject(_defaultOptions[key])) {
|
27
|
+
objValue = mergeObjColumns(_options[key], _defaultOptions[key])
|
28
|
+
obj[key as keyof Options] = objValue
|
29
|
+
}
|
30
|
+
// 不是plain object直接賦值
|
31
|
+
else {
|
32
|
+
obj[key as keyof Options] = _options[key]
|
33
|
+
}
|
34
|
+
}
|
35
|
+
return obj
|
36
|
+
}
|
37
|
+
|
38
|
+
return mergeObjColumns(options, defaultOptions)
|
39
|
+
}
|
40
|
+
|
41
|
+
// 加上千分位 ,
|
42
|
+
export function formatCommaNumber (num = 0): string {
|
43
|
+
try {
|
44
|
+
let parts = num.toString().split('.');
|
45
|
+
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
46
|
+
return parts.join('.');
|
47
|
+
} catch (e: any) {
|
48
|
+
console.error(e)
|
49
|
+
}
|
50
|
+
}
|
package/src/utils/d3Utils.ts
CHANGED
@@ -1,92 +1,92 @@
|
|
1
|
-
import * as d3 from 'd3'
|
2
|
-
import { DATA_FORMATTER_VALUE_AXIS_DEFAULT } from '../defaults'
|
3
|
-
|
4
|
-
// scaleLinear - 連續資料對應到比例尺座標上
|
5
|
-
export const createAxisLinearScale = ({
|
6
|
-
maxValue = 1,
|
7
|
-
minValue = 0,
|
8
|
-
axisWidth,
|
9
|
-
scaleDomain = DATA_FORMATTER_VALUE_AXIS_DEFAULT.scaleDomain,
|
10
|
-
scaleRange = DATA_FORMATTER_VALUE_AXIS_DEFAULT.scaleRange,
|
11
|
-
}: {
|
12
|
-
maxValue: number
|
13
|
-
minValue: number
|
14
|
-
axisWidth: number
|
15
|
-
scaleDomain: [number | 'auto', number | 'auto']
|
16
|
-
scaleRange: [number, number] // 0-1
|
17
|
-
}) => {
|
18
|
-
// -- 無值補上預設值 --
|
19
|
-
const domainMin: number | 'auto' = scaleDomain[0] ?? DATA_FORMATTER_VALUE_AXIS_DEFAULT.scaleDomain[0]
|
20
|
-
const domainMax: number | 'auto' = scaleDomain[1] ?? DATA_FORMATTER_VALUE_AXIS_DEFAULT.scaleDomain[1]
|
21
|
-
const rangeMin: number = scaleRange[0] ?? DATA_FORMATTER_VALUE_AXIS_DEFAULT.scaleRange[0]
|
22
|
-
const rangeMax: number = scaleRange[1] ?? DATA_FORMATTER_VALUE_AXIS_DEFAULT.scaleRange[1]
|
23
|
-
|
24
|
-
// -- 'auto'提換成實際值 --
|
25
|
-
const domainMinValue = domainMin === 'auto' ? minValue : domainMin
|
26
|
-
const domainMaxValue = domainMax === 'auto' ? maxValue : domainMax
|
27
|
-
|
28
|
-
// let rangeMinValue = axisWidth * rangeMin
|
29
|
-
// let rangeMaxValue = axisWidth * rangeMax
|
30
|
-
|
31
|
-
// -- 計算padding --
|
32
|
-
// if (padding > 0) {
|
33
|
-
// const stepAmount = maxValue - minValue + (padding * 2)
|
34
|
-
// const eachStepWidth = axisWidth / stepAmount
|
35
|
-
// const paddingWidth = eachStepWidth * padding
|
36
|
-
// rangeMinValue += paddingWidth
|
37
|
-
// rangeMaxValue -= paddingWidth
|
38
|
-
// }
|
39
|
-
|
40
|
-
// -- 依場景大小換算 --
|
41
|
-
const axisDomainMinValue = maxValue - (maxValue - domainMinValue) / (1 - rangeMin)
|
42
|
-
const axisDomainMaxValue = domainMaxValue / rangeMax
|
43
|
-
|
44
|
-
// return d3.scaleLinear()
|
45
|
-
// .domain([domainMinValue, domainMaxValue])
|
46
|
-
// .range([rangeMinValue, rangeMaxValue])
|
47
|
-
return d3.scaleLinear()
|
48
|
-
.domain([axisDomainMinValue, axisDomainMaxValue])
|
49
|
-
.range([0, axisWidth])
|
50
|
-
}
|
51
|
-
|
52
|
-
// scalePoint - 非連續資料對應到比例尺座標上
|
53
|
-
export const createAxisPointScale = ({ axisLabels, axisWidth, padding = 0.5 }: {
|
54
|
-
axisLabels: string[]
|
55
|
-
axisWidth: number
|
56
|
-
padding?: number
|
57
|
-
// reverse?: boolean
|
58
|
-
}) => {
|
59
|
-
let range: [d3.NumberValue, d3.NumberValue] = [0, axisWidth]
|
60
|
-
|
61
|
-
return d3.scalePoint()
|
62
|
-
.domain(axisLabels)
|
63
|
-
.range(range)
|
64
|
-
.padding(padding)
|
65
|
-
}
|
66
|
-
|
67
|
-
// scaleQuantize - 比例尺座標對應非連續資料索引
|
68
|
-
export const createAxisQuantizeScale = ({ axisLabels, axisWidth, padding = 0, reverse = false }:{
|
69
|
-
axisLabels: string[] | Date[],
|
70
|
-
axisWidth: number
|
71
|
-
padding?: number
|
72
|
-
reverse?: boolean
|
73
|
-
}) => {
|
74
|
-
|
75
|
-
let range: number[] = axisLabels.map((d: string | Date, i: number) => i)
|
76
|
-
if (reverse) {
|
77
|
-
range.reverse()
|
78
|
-
}
|
79
|
-
// if (reverse) {
|
80
|
-
// range = axisLabels.map((d: string | Date, i: number) => axisLabels.length - 1 - i)
|
81
|
-
// } else {
|
82
|
-
// range = axisLabels.map((d: string | Date, i: number) => i)
|
83
|
-
// }
|
84
|
-
const step = range.length - 1 + (padding * 2) // 圖軸刻度分段數量
|
85
|
-
const stepWidth = axisWidth / step
|
86
|
-
const rangePadding = stepWidth * padding - (stepWidth * 0.5) // 實際要計算的範圍是圖軸左右那邊增加0.5
|
87
|
-
|
88
|
-
// console.log('rangePadding', rangePadding)
|
89
|
-
return d3.scaleQuantize<number>()
|
90
|
-
.domain([rangePadding, axisWidth - rangePadding])
|
91
|
-
.range(range)
|
92
|
-
}
|
1
|
+
import * as d3 from 'd3'
|
2
|
+
import { DATA_FORMATTER_VALUE_AXIS_DEFAULT } from '../defaults'
|
3
|
+
|
4
|
+
// scaleLinear - 連續資料對應到比例尺座標上
|
5
|
+
export const createAxisLinearScale = ({
|
6
|
+
maxValue = 1,
|
7
|
+
minValue = 0,
|
8
|
+
axisWidth,
|
9
|
+
scaleDomain = DATA_FORMATTER_VALUE_AXIS_DEFAULT.scaleDomain,
|
10
|
+
scaleRange = DATA_FORMATTER_VALUE_AXIS_DEFAULT.scaleRange,
|
11
|
+
}: {
|
12
|
+
maxValue: number
|
13
|
+
minValue: number
|
14
|
+
axisWidth: number
|
15
|
+
scaleDomain: [number | 'auto', number | 'auto']
|
16
|
+
scaleRange: [number, number] // 0-1
|
17
|
+
}) => {
|
18
|
+
// -- 無值補上預設值 --
|
19
|
+
const domainMin: number | 'auto' = scaleDomain[0] ?? DATA_FORMATTER_VALUE_AXIS_DEFAULT.scaleDomain[0]
|
20
|
+
const domainMax: number | 'auto' = scaleDomain[1] ?? DATA_FORMATTER_VALUE_AXIS_DEFAULT.scaleDomain[1]
|
21
|
+
const rangeMin: number = scaleRange[0] ?? DATA_FORMATTER_VALUE_AXIS_DEFAULT.scaleRange[0]
|
22
|
+
const rangeMax: number = scaleRange[1] ?? DATA_FORMATTER_VALUE_AXIS_DEFAULT.scaleRange[1]
|
23
|
+
|
24
|
+
// -- 'auto'提換成實際值 --
|
25
|
+
const domainMinValue = domainMin === 'auto' ? minValue : domainMin
|
26
|
+
const domainMaxValue = domainMax === 'auto' ? maxValue : domainMax
|
27
|
+
|
28
|
+
// let rangeMinValue = axisWidth * rangeMin
|
29
|
+
// let rangeMaxValue = axisWidth * rangeMax
|
30
|
+
|
31
|
+
// -- 計算padding --
|
32
|
+
// if (padding > 0) {
|
33
|
+
// const stepAmount = maxValue - minValue + (padding * 2)
|
34
|
+
// const eachStepWidth = axisWidth / stepAmount
|
35
|
+
// const paddingWidth = eachStepWidth * padding
|
36
|
+
// rangeMinValue += paddingWidth
|
37
|
+
// rangeMaxValue -= paddingWidth
|
38
|
+
// }
|
39
|
+
|
40
|
+
// -- 依場景大小換算 --
|
41
|
+
const axisDomainMinValue = maxValue - (maxValue - domainMinValue) / (1 - rangeMin)
|
42
|
+
const axisDomainMaxValue = domainMaxValue / rangeMax
|
43
|
+
|
44
|
+
// return d3.scaleLinear()
|
45
|
+
// .domain([domainMinValue, domainMaxValue])
|
46
|
+
// .range([rangeMinValue, rangeMaxValue])
|
47
|
+
return d3.scaleLinear()
|
48
|
+
.domain([axisDomainMinValue, axisDomainMaxValue])
|
49
|
+
.range([0, axisWidth])
|
50
|
+
}
|
51
|
+
|
52
|
+
// scalePoint - 非連續資料對應到比例尺座標上
|
53
|
+
export const createAxisPointScale = ({ axisLabels, axisWidth, padding = 0.5 }: {
|
54
|
+
axisLabels: string[]
|
55
|
+
axisWidth: number
|
56
|
+
padding?: number
|
57
|
+
// reverse?: boolean
|
58
|
+
}) => {
|
59
|
+
let range: [d3.NumberValue, d3.NumberValue] = [0, axisWidth]
|
60
|
+
|
61
|
+
return d3.scalePoint()
|
62
|
+
.domain(axisLabels)
|
63
|
+
.range(range)
|
64
|
+
.padding(padding)
|
65
|
+
}
|
66
|
+
|
67
|
+
// scaleQuantize - 比例尺座標對應非連續資料索引
|
68
|
+
export const createAxisQuantizeScale = ({ axisLabels, axisWidth, padding = 0, reverse = false }:{
|
69
|
+
axisLabels: string[] | Date[],
|
70
|
+
axisWidth: number
|
71
|
+
padding?: number
|
72
|
+
reverse?: boolean
|
73
|
+
}) => {
|
74
|
+
|
75
|
+
let range: number[] = axisLabels.map((d: string | Date, i: number) => i)
|
76
|
+
if (reverse) {
|
77
|
+
range.reverse()
|
78
|
+
}
|
79
|
+
// if (reverse) {
|
80
|
+
// range = axisLabels.map((d: string | Date, i: number) => axisLabels.length - 1 - i)
|
81
|
+
// } else {
|
82
|
+
// range = axisLabels.map((d: string | Date, i: number) => i)
|
83
|
+
// }
|
84
|
+
const step = range.length - 1 + (padding * 2) // 圖軸刻度分段數量
|
85
|
+
const stepWidth = axisWidth / step
|
86
|
+
const rangePadding = stepWidth * padding - (stepWidth * 0.5) // 實際要計算的範圍是圖軸左右那邊增加0.5
|
87
|
+
|
88
|
+
// console.log('rangePadding', rangePadding)
|
89
|
+
return d3.scaleQuantize<number>()
|
90
|
+
.domain([rangePadding, axisWidth - rangePadding])
|
91
|
+
.range(range)
|
92
|
+
}
|
package/src/utils/index.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
export * from './orbchartsUtils'
|
2
|
-
export * from './observables'
|
3
|
-
export * from './d3Utils'
|
4
|
-
export * from './commonUtils'
|
1
|
+
export * from './orbchartsUtils'
|
2
|
+
export * from './observables'
|
3
|
+
export * from './d3Utils'
|
4
|
+
export * from './commonUtils'
|