@orbcharts/core 3.0.0-alpha.61 → 3.0.0-alpha.62
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +200 -200
- package/dist/lib/core-types.d.ts +1 -0
- package/dist/orbcharts-core.es.js +3054 -2609
- package/dist/orbcharts-core.umd.js +6 -2
- package/dist/src/AbstractChart.d.ts +5 -3
- package/dist/src/GridChart.d.ts +1 -1
- package/dist/src/MultiGridChart.d.ts +1 -1
- package/dist/src/MultiValueChart.d.ts +1 -1
- package/dist/src/RelationshipChart.d.ts +1 -1
- package/dist/src/SeriesChart.d.ts +1 -1
- package/dist/src/TreeChart.d.ts +1 -1
- package/dist/src/base/createBaseChart.d.ts +1 -1
- package/dist/src/base/createBasePlugin.d.ts +1 -1
- package/dist/src/base/validators/chartOptionsValidator.d.ts +3 -0
- package/dist/src/base/validators/chartParamsValidator.d.ts +3 -0
- package/dist/src/base/validators/elementValidator.d.ts +3 -0
- package/dist/src/base/validators/pluginsValidator.d.ts +3 -0
- package/dist/src/defaults.d.ts +1 -16
- package/dist/src/defineGridPlugin.d.ts +1 -1
- package/dist/src/defineMultiGridPlugin.d.ts +1 -1
- package/dist/src/defineMultiValuePlugin.d.ts +1 -1
- package/dist/src/defineNoneDataPlugin.d.ts +1 -1
- package/dist/src/defineRelationshipPlugin.d.ts +1 -1
- package/dist/src/defineSeriesPlugin.d.ts +1 -1
- package/dist/src/defineTreePlugin.d.ts +1 -1
- package/dist/src/grid/computedDataFn.d.ts +4 -0
- package/dist/src/grid/contextObserverCallback.d.ts +3 -0
- package/dist/src/grid/dataFormatterValidator.d.ts +3 -0
- package/dist/src/grid/dataValidator.d.ts +3 -0
- package/dist/src/index.d.ts +1 -1
- package/dist/src/multiGrid/computedDataFn.d.ts +3 -0
- package/dist/src/multiGrid/contextObserverCallback.d.ts +3 -0
- package/dist/src/multiGrid/dataFormatterValidator.d.ts +3 -0
- package/dist/src/multiGrid/dataValidator.d.ts +3 -0
- package/dist/src/multiValue/computedDataFn.d.ts +3 -0
- package/dist/src/multiValue/contextObserverCallback.d.ts +3 -0
- package/dist/src/multiValue/dataFormatterValidator.d.ts +3 -0
- package/dist/src/multiValue/dataValidator.d.ts +3 -0
- package/dist/src/relationship/computedDataFn.d.ts +3 -0
- package/dist/src/relationship/contextObserverCallback.d.ts +3 -0
- package/dist/src/relationship/dataFormatterValidator.d.ts +3 -0
- package/dist/src/relationship/dataValidator.d.ts +3 -0
- package/dist/src/series/computedDataFn.d.ts +3 -0
- package/dist/src/series/contextObserverCallback.d.ts +3 -0
- package/dist/src/series/dataFormatterValidator.d.ts +3 -0
- package/dist/src/series/dataValidator.d.ts +3 -0
- package/dist/src/tree/computedDataFn.d.ts +3 -0
- package/dist/src/tree/contextObserverCallback.d.ts +3 -0
- package/dist/src/tree/dataFormatterValidator.d.ts +3 -0
- package/dist/src/tree/dataValidator.d.ts +3 -0
- package/dist/src/utils/commonUtils.d.ts +1 -0
- package/dist/src/utils/errorMessage.d.ts +14 -0
- package/dist/src/{grid → utils}/gridObservables.d.ts +2 -2
- package/dist/src/utils/index.d.ts +7 -3
- package/dist/src/{multiGrid → utils}/multiGridObservables.d.ts +1 -1
- package/dist/src/utils/observables.d.ts +2 -1
- package/dist/src/utils/orbchartsUtils.d.ts +1 -12
- package/dist/src/{series → utils}/seriesObservables.d.ts +3 -3
- package/dist/src/{tree → utils}/treeObservables.d.ts +1 -1
- package/dist/src/utils/validator.d.ts +3 -0
- package/lib/core-types.ts +7 -0
- package/package.json +42 -41
- package/src/AbstractChart.ts +57 -48
- package/src/GridChart.ts +24 -20
- package/src/MultiGridChart.ts +24 -20
- package/src/MultiValueChart.ts +24 -20
- package/src/RelationshipChart.ts +24 -20
- package/src/SeriesChart.ts +24 -20
- package/src/TreeChart.ts +24 -20
- package/src/base/createBaseChart.ts +500 -388
- package/src/base/createBasePlugin.ts +152 -95
- package/src/base/validators/chartOptionsValidator.ts +24 -0
- package/src/base/validators/chartParamsValidator.ts +134 -0
- package/src/base/validators/elementValidator.ts +14 -0
- package/src/base/validators/pluginsValidator.ts +15 -0
- package/src/defaults.ts +232 -228
- 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 → computedDataFn.ts} +129 -134
- package/src/grid/{createGridContextObserver.ts → contextObserverCallback.ts} +155 -155
- package/src/grid/dataFormatterValidator.ts +9 -0
- package/src/grid/dataValidator.ts +9 -0
- package/src/index.ts +20 -21
- package/src/multiGrid/{computeMultiGridData.ts → computedDataFn.ts} +123 -130
- package/src/multiGrid/{createMultiGridContextObserver.ts → contextObserverCallback.ts} +41 -41
- package/src/multiGrid/dataFormatterValidator.ts +9 -0
- package/src/multiGrid/dataValidator.ts +9 -0
- package/src/multiValue/{computeMultiValueData.ts → computedDataFn.ts} +176 -179
- package/src/multiValue/{createMultiValueContextObserver.ts → contextObserverCallback.ts} +12 -12
- package/src/multiValue/dataFormatterValidator.ts +9 -0
- package/src/multiValue/dataValidator.ts +9 -0
- package/src/relationship/{computeRelationshipData.ts → computedDataFn.ts} +125 -118
- package/src/relationship/{createRelationshipContextObserver.ts → contextObserverCallback.ts} +12 -12
- package/src/relationship/dataFormatterValidator.ts +9 -0
- package/src/relationship/dataValidator.ts +9 -0
- package/src/series/{computeSeriesData.ts → computedDataFn.ts} +88 -90
- package/src/series/{createSeriesContextObserver.ts → contextObserverCallback.ts} +100 -93
- package/src/series/dataFormatterValidator.ts +9 -0
- package/src/series/dataValidator.ts +9 -0
- package/src/tree/{computeTreeData.ts → computedDataFn.ts} +130 -132
- package/src/tree/{createTreeContextObserver.ts → contextObserverCallback.ts} +61 -61
- package/src/tree/dataFormatterValidator.ts +9 -0
- package/src/tree/dataValidator.ts +9 -0
- package/src/utils/commonUtils.ts +54 -50
- package/src/utils/d3Utils.ts +108 -108
- package/src/utils/errorMessage.ts +43 -0
- package/src/{grid → utils}/gridObservables.ts +611 -614
- package/src/utils/index.ts +10 -4
- package/src/{multiGrid → utils}/multiGridObservables.ts +366 -365
- package/src/utils/observables.ts +218 -202
- package/src/utils/orbchartsUtils.ts +352 -349
- package/src/{series → utils}/seriesObservables.ts +175 -175
- package/src/{tree → utils}/treeObservables.ts +94 -94
- package/src/utils/validator.ts +126 -0
- package/tsconfig.base.json +13 -13
- package/tsconfig.json +2 -2
- package/vite-env.d.ts +7 -0
- package/vite.config.js +22 -22
- package/dist/src/grid/computeGridData.d.ts +0 -6
- package/dist/src/grid/createGridContextObserver.d.ts +0 -3
- package/dist/src/multiGrid/computeMultiGridData.d.ts +0 -3
- package/dist/src/multiGrid/createMultiGridContextObserver.d.ts +0 -3
- package/dist/src/multiValue/computeMultiValueData.d.ts +0 -3
- package/dist/src/multiValue/createMultiValueContextObserver.d.ts +0 -3
- package/dist/src/relationship/computeRelationshipData.d.ts +0 -3
- package/dist/src/relationship/createRelationshipContextObserver.d.ts +0 -3
- package/dist/src/series/computeSeriesData.d.ts +0 -3
- package/dist/src/series/createSeriesContextObserver.d.ts +0 -3
- package/dist/src/tree/computeTreeData.d.ts +0 -3
- package/dist/src/tree/createTreeContextObserver.d.ts +0 -3
- package/dist/src/types/Axis.d.ts +0 -1
- package/dist/src/types/Chart.d.ts +0 -45
- package/dist/src/types/ChartParams.d.ts +0 -36
- package/dist/src/types/ComputedData.d.ts +0 -42
- package/dist/src/types/ComputedDataGrid.d.ts +0 -5
- package/dist/src/types/ComputedDataMultiGrid.d.ts +0 -3
- package/dist/src/types/ComputedDataMultiValue.d.ts +0 -6
- package/dist/src/types/ComputedDataRelationship.d.ts +0 -18
- package/dist/src/types/ComputedDataSeries.d.ts +0 -5
- package/dist/src/types/ComputedDataTree.d.ts +0 -7
- package/dist/src/types/ContextObserver.d.ts +0 -28
- package/dist/src/types/ContextObserverGrid.d.ts +0 -41
- package/dist/src/types/ContextObserverMultiGrid.d.ts +0 -15
- package/dist/src/types/ContextObserverMultiValue.d.ts +0 -4
- package/dist/src/types/ContextObserverRelationship.d.ts +0 -4
- package/dist/src/types/ContextObserverSeries.d.ts +0 -27
- package/dist/src/types/ContextObserverTree.d.ts +0 -11
- package/dist/src/types/ContextSubject.d.ts +0 -15
- package/dist/src/types/Data.d.ts +0 -19
- package/dist/src/types/DataFormatter.d.ts +0 -41
- package/dist/src/types/DataFormatterGrid.d.ts +0 -34
- package/dist/src/types/DataFormatterMultiGrid.d.ts +0 -20
- package/dist/src/types/DataFormatterMultiValue.d.ts +0 -18
- package/dist/src/types/DataFormatterRelationship.d.ts +0 -10
- package/dist/src/types/DataFormatterSeries.d.ts +0 -19
- package/dist/src/types/DataFormatterTree.d.ts +0 -7
- package/dist/src/types/DataGrid.d.ts +0 -6
- package/dist/src/types/DataMultiGrid.d.ts +0 -6
- package/dist/src/types/DataMultiValue.d.ts +0 -7
- package/dist/src/types/DataRelationship.d.ts +0 -21
- package/dist/src/types/DataSeries.d.ts +0 -6
- package/dist/src/types/DataTree.d.ts +0 -15
- package/dist/src/types/Event.d.ts +0 -56
- package/dist/src/types/Layout.d.ts +0 -8
- package/dist/src/types/Padding.d.ts +0 -6
- package/dist/src/types/Plugin.d.ts +0 -37
- package/dist/src/types/TransformData.d.ts +0 -8
- package/dist/src/types/index.d.ts +0 -37
- package/src/types/Axis.ts +0 -1
- package/src/types/Chart.ts +0 -54
- package/src/types/ChartParams.ts +0 -51
- package/src/types/ComputedData.ts +0 -84
- package/src/types/ComputedDataGrid.ts +0 -14
- package/src/types/ComputedDataMultiGrid.ts +0 -3
- package/src/types/ComputedDataMultiValue.ts +0 -9
- package/src/types/ComputedDataRelationship.ts +0 -20
- package/src/types/ComputedDataSeries.ts +0 -8
- package/src/types/ComputedDataTree.ts +0 -20
- package/src/types/ContextObserver.ts +0 -38
- package/src/types/ContextObserverGrid.ts +0 -43
- package/src/types/ContextObserverMultiGrid.ts +0 -17
- package/src/types/ContextObserverMultiValue.ts +0 -5
- package/src/types/ContextObserverRelationship.ts +0 -5
- package/src/types/ContextObserverSeries.ts +0 -29
- package/src/types/ContextObserverTree.ts +0 -11
- package/src/types/ContextSubject.ts +0 -18
- package/src/types/Data.ts +0 -45
- package/src/types/DataFormatter.ts +0 -74
- package/src/types/DataFormatterGrid.ts +0 -68
- package/src/types/DataFormatterMultiGrid.ts +0 -45
- package/src/types/DataFormatterMultiValue.ts +0 -24
- package/src/types/DataFormatterRelationship.ts +0 -26
- package/src/types/DataFormatterSeries.ts +0 -20
- package/src/types/DataFormatterTree.ts +0 -12
- package/src/types/DataGrid.ts +0 -11
- package/src/types/DataMultiGrid.ts +0 -7
- package/src/types/DataMultiValue.ts +0 -12
- package/src/types/DataRelationship.ts +0 -28
- package/src/types/DataSeries.ts +0 -11
- package/src/types/DataTree.ts +0 -20
- package/src/types/Event.ts +0 -153
- package/src/types/Layout.ts +0 -12
- package/src/types/Padding.ts +0 -6
- package/src/types/Plugin.ts +0 -60
- package/src/types/TransformData.ts +0 -8
- package/src/types/index.ts +0 -37
- /package/dist/src/{multiValue → utils}/multiValueObservables.d.ts +0 -0
- /package/dist/src/{relationship → utils}/relationshipObservables.d.ts +0 -0
- /package/src/{multiValue → utils}/multiValueObservables.ts +0 -0
- /package/src/{relationship → utils}/relationshipObservables.ts +0 -0
package/src/utils/commonUtils.ts
CHANGED
@@ -1,50 +1,54 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
obj[key as keyof Options] =
|
33
|
-
}
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
1
|
+
|
2
|
+
// 是否為原始物件
|
3
|
+
export function isPlainObject(variable: any) {
|
4
|
+
return Object.prototype.toString.call(variable) === "[object Object]";
|
5
|
+
}
|
6
|
+
|
7
|
+
// 是否為function
|
8
|
+
export function isFunction(fn: any) {
|
9
|
+
return !!fn && !fn.nodename && fn.constructor != String && fn.constructor != RegExp && fn.constructor != Array && /function/i.test(fn + "");
|
10
|
+
}
|
11
|
+
|
12
|
+
// 是否為dom
|
13
|
+
export function isDom(obj: any) {
|
14
|
+
return !!(obj && obj.nodeType);
|
15
|
+
}
|
16
|
+
|
17
|
+
// 將可選的參數和預設值合併
|
18
|
+
export function mergeOptionsWithDefault<Options extends { [key: string]: any; }> (options: {[key: string]: any}, defaultOptions: Options): Options {
|
19
|
+
if (isPlainObject(options) === false || isPlainObject(defaultOptions) === false) {
|
20
|
+
return Object.assign({}, defaultOptions)
|
21
|
+
}
|
22
|
+
const mergeObjColumns = (_options: {[key: string]: any}, _defaultOptions: {[key: string]: any}) => {
|
23
|
+
const obj: Options = (Object.assign({}, _defaultOptions) as any)
|
24
|
+
for (let key of Object.keys(_options)) {
|
25
|
+
if ((key in _defaultOptions) == false) {
|
26
|
+
continue
|
27
|
+
}
|
28
|
+
let objValue: any = undefined
|
29
|
+
// 下一層的plain object
|
30
|
+
if (isPlainObject(_options[key]) && isPlainObject(_defaultOptions[key])) {
|
31
|
+
objValue = mergeObjColumns(_options[key], _defaultOptions[key])
|
32
|
+
obj[key as keyof Options] = objValue
|
33
|
+
}
|
34
|
+
// 不是plain object直接賦值
|
35
|
+
else {
|
36
|
+
obj[key as keyof Options] = _options[key]
|
37
|
+
}
|
38
|
+
}
|
39
|
+
return obj
|
40
|
+
}
|
41
|
+
|
42
|
+
return mergeObjColumns(options, defaultOptions)
|
43
|
+
}
|
44
|
+
|
45
|
+
// 加上千分位 ,
|
46
|
+
export function formatCommaNumber (num = 0): string {
|
47
|
+
try {
|
48
|
+
let parts = num.toString().split('.');
|
49
|
+
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
50
|
+
return parts.join('.');
|
51
|
+
} catch (e: any) {
|
52
|
+
console.error(e)
|
53
|
+
}
|
54
|
+
}
|
package/src/utils/d3Utils.ts
CHANGED
@@ -1,108 +1,108 @@
|
|
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 | 'min' | 'auto', number | 'max' | 'auto']
|
16
|
-
scaleRange: [number, number] // 0-1
|
17
|
-
}) => {
|
18
|
-
// -- 無值補上預設值 --
|
19
|
-
const domainMin: number | 'min' | 'auto' = scaleDomain[0] ?? DATA_FORMATTER_VALUE_AXIS_DEFAULT.scaleDomain[0]
|
20
|
-
const domainMax: number | 'max' | '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' | 'max' | 'min' 替換成實際值 --
|
25
|
-
let domainMinValue: number = (() => {
|
26
|
-
if (domainMin === 'auto') {
|
27
|
-
return minValue < 0 ? minValue : 0
|
28
|
-
} else if (domainMin === 'min') {
|
29
|
-
return minValue
|
30
|
-
} else {
|
31
|
-
return domainMin
|
32
|
-
}
|
33
|
-
})()
|
34
|
-
|
35
|
-
let domainMaxValue: number = (() => {
|
36
|
-
if (domainMax === 'auto') {
|
37
|
-
return maxValue >= 0 ? maxValue : 0
|
38
|
-
} else if (domainMax === 'max') {
|
39
|
-
return maxValue
|
40
|
-
} else {
|
41
|
-
return domainMax
|
42
|
-
}
|
43
|
-
})()
|
44
|
-
// let rangeMinValue = axisWidth * rangeMin
|
45
|
-
// let rangeMaxValue = axisWidth * rangeMax
|
46
|
-
|
47
|
-
// -- 計算padding --
|
48
|
-
// if (padding > 0) {
|
49
|
-
// const stepAmount = maxValue - minValue + (padding * 2)
|
50
|
-
// const eachStepWidth = axisWidth / stepAmount
|
51
|
-
// const paddingWidth = eachStepWidth * padding
|
52
|
-
// rangeMinValue += paddingWidth
|
53
|
-
// rangeMaxValue -= paddingWidth
|
54
|
-
// }
|
55
|
-
|
56
|
-
// -- 依場景大小換算 --
|
57
|
-
const axisDomainMinValue = maxValue - (maxValue - domainMinValue) / (1 - rangeMin)
|
58
|
-
const axisDomainMaxValue = domainMaxValue / rangeMax
|
59
|
-
|
60
|
-
// return d3.scaleLinear()
|
61
|
-
// .domain([domainMinValue, domainMaxValue])
|
62
|
-
// .range([rangeMinValue, rangeMaxValue])
|
63
|
-
return d3.scaleLinear()
|
64
|
-
.domain([axisDomainMinValue, axisDomainMaxValue])
|
65
|
-
.range([0, axisWidth])
|
66
|
-
}
|
67
|
-
|
68
|
-
// scalePoint - 非連續資料對應到比例尺座標上
|
69
|
-
export const createAxisPointScale = ({ axisLabels, axisWidth, padding = 0.5 }: {
|
70
|
-
axisLabels: string[]
|
71
|
-
axisWidth: number
|
72
|
-
padding?: number
|
73
|
-
// reverse?: boolean
|
74
|
-
}) => {
|
75
|
-
let range: [d3.NumberValue, d3.NumberValue] = [0, axisWidth]
|
76
|
-
|
77
|
-
return d3.scalePoint()
|
78
|
-
.domain(axisLabels)
|
79
|
-
.range(range)
|
80
|
-
.padding(padding)
|
81
|
-
}
|
82
|
-
|
83
|
-
// scaleQuantize - 比例尺座標對應非連續資料索引
|
84
|
-
export const createAxisQuantizeScale = ({ axisLabels, axisWidth, padding = 0, reverse = false }:{
|
85
|
-
axisLabels: string[] | Date[],
|
86
|
-
axisWidth: number
|
87
|
-
padding?: number
|
88
|
-
reverse?: boolean
|
89
|
-
}) => {
|
90
|
-
|
91
|
-
let range: number[] = axisLabels.map((d: string | Date, i: number) => i)
|
92
|
-
if (reverse) {
|
93
|
-
range.reverse()
|
94
|
-
}
|
95
|
-
// if (reverse) {
|
96
|
-
// range = axisLabels.map((d: string | Date, i: number) => axisLabels.length - 1 - i)
|
97
|
-
// } else {
|
98
|
-
// range = axisLabels.map((d: string | Date, i: number) => i)
|
99
|
-
// }
|
100
|
-
const step = range.length - 1 + (padding * 2) // 圖軸刻度分段數量
|
101
|
-
const stepWidth = axisWidth / step
|
102
|
-
const rangePadding = stepWidth * padding - (stepWidth * 0.5) // 實際要計算的範圍是圖軸左右那邊增加0.5
|
103
|
-
|
104
|
-
// console.log('rangePadding', rangePadding)
|
105
|
-
return d3.scaleQuantize<number>()
|
106
|
-
.domain([rangePadding, axisWidth - rangePadding])
|
107
|
-
.range(range)
|
108
|
-
}
|
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 | 'min' | 'auto', number | 'max' | 'auto']
|
16
|
+
scaleRange: [number, number] // 0-1
|
17
|
+
}) => {
|
18
|
+
// -- 無值補上預設值 --
|
19
|
+
const domainMin: number | 'min' | 'auto' = scaleDomain[0] ?? DATA_FORMATTER_VALUE_AXIS_DEFAULT.scaleDomain[0]
|
20
|
+
const domainMax: number | 'max' | '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' | 'max' | 'min' 替換成實際值 --
|
25
|
+
let domainMinValue: number = (() => {
|
26
|
+
if (domainMin === 'auto') {
|
27
|
+
return minValue < 0 ? minValue : 0
|
28
|
+
} else if (domainMin === 'min') {
|
29
|
+
return minValue
|
30
|
+
} else {
|
31
|
+
return domainMin
|
32
|
+
}
|
33
|
+
})()
|
34
|
+
|
35
|
+
let domainMaxValue: number = (() => {
|
36
|
+
if (domainMax === 'auto') {
|
37
|
+
return maxValue >= 0 ? maxValue : 0
|
38
|
+
} else if (domainMax === 'max') {
|
39
|
+
return maxValue
|
40
|
+
} else {
|
41
|
+
return domainMax
|
42
|
+
}
|
43
|
+
})()
|
44
|
+
// let rangeMinValue = axisWidth * rangeMin
|
45
|
+
// let rangeMaxValue = axisWidth * rangeMax
|
46
|
+
|
47
|
+
// -- 計算padding --
|
48
|
+
// if (padding > 0) {
|
49
|
+
// const stepAmount = maxValue - minValue + (padding * 2)
|
50
|
+
// const eachStepWidth = axisWidth / stepAmount
|
51
|
+
// const paddingWidth = eachStepWidth * padding
|
52
|
+
// rangeMinValue += paddingWidth
|
53
|
+
// rangeMaxValue -= paddingWidth
|
54
|
+
// }
|
55
|
+
|
56
|
+
// -- 依場景大小換算 --
|
57
|
+
const axisDomainMinValue = maxValue - (maxValue - domainMinValue) / (1 - rangeMin)
|
58
|
+
const axisDomainMaxValue = domainMaxValue / rangeMax
|
59
|
+
|
60
|
+
// return d3.scaleLinear()
|
61
|
+
// .domain([domainMinValue, domainMaxValue])
|
62
|
+
// .range([rangeMinValue, rangeMaxValue])
|
63
|
+
return d3.scaleLinear()
|
64
|
+
.domain([axisDomainMinValue, axisDomainMaxValue])
|
65
|
+
.range([0, axisWidth])
|
66
|
+
}
|
67
|
+
|
68
|
+
// scalePoint - 非連續資料對應到比例尺座標上
|
69
|
+
export const createAxisPointScale = ({ axisLabels, axisWidth, padding = 0.5 }: {
|
70
|
+
axisLabels: string[]
|
71
|
+
axisWidth: number
|
72
|
+
padding?: number
|
73
|
+
// reverse?: boolean
|
74
|
+
}) => {
|
75
|
+
let range: [d3.NumberValue, d3.NumberValue] = [0, axisWidth]
|
76
|
+
|
77
|
+
return d3.scalePoint()
|
78
|
+
.domain(axisLabels)
|
79
|
+
.range(range)
|
80
|
+
.padding(padding)
|
81
|
+
}
|
82
|
+
|
83
|
+
// scaleQuantize - 比例尺座標對應非連續資料索引
|
84
|
+
export const createAxisQuantizeScale = ({ axisLabels, axisWidth, padding = 0, reverse = false }:{
|
85
|
+
axisLabels: string[] | Date[],
|
86
|
+
axisWidth: number
|
87
|
+
padding?: number
|
88
|
+
reverse?: boolean
|
89
|
+
}) => {
|
90
|
+
|
91
|
+
let range: number[] = axisLabels.map((d: string | Date, i: number) => i)
|
92
|
+
if (reverse) {
|
93
|
+
range.reverse()
|
94
|
+
}
|
95
|
+
// if (reverse) {
|
96
|
+
// range = axisLabels.map((d: string | Date, i: number) => axisLabels.length - 1 - i)
|
97
|
+
// } else {
|
98
|
+
// range = axisLabels.map((d: string | Date, i: number) => i)
|
99
|
+
// }
|
100
|
+
const step = range.length - 1 + (padding * 2) // 圖軸刻度分段數量
|
101
|
+
const stepWidth = axisWidth / step
|
102
|
+
const rangePadding = stepWidth * padding - (stepWidth * 0.5) // 實際要計算的範圍是圖軸左右那邊增加0.5
|
103
|
+
|
104
|
+
// console.log('rangePadding', rangePadding)
|
105
|
+
return d3.scaleQuantize<number>()
|
106
|
+
.domain([rangePadding, axisWidth - rangePadding])
|
107
|
+
.range(range)
|
108
|
+
}
|
@@ -0,0 +1,43 @@
|
|
1
|
+
|
2
|
+
// export function createMessagePrefix (status: 'warning' | 'error'): string {
|
3
|
+
// return `[OrbCharts ${status}]:`
|
4
|
+
// }
|
5
|
+
|
6
|
+
export function createOrbChartsErrorMessage (e: {
|
7
|
+
message: string // e.message
|
8
|
+
stack: string // e.stack
|
9
|
+
}): string {
|
10
|
+
return `[OrbCharts warn]: ${e.message}`
|
11
|
+
}
|
12
|
+
|
13
|
+
|
14
|
+
// // 未預期的錯誤
|
15
|
+
// export function createUnexpectedErrorMessage ({ from, systemMessage }: {
|
16
|
+
// from: string //
|
17
|
+
// systemMessage: string // catch 給的的原生錯誤訊息
|
18
|
+
// }): string {
|
19
|
+
// return `${createMessagePrefix('error')} unexpected error from '${from}':
|
20
|
+
// ${systemMessage}`
|
21
|
+
// }
|
22
|
+
|
23
|
+
// validator 的 error 訊息
|
24
|
+
export function createValidatorErrorMessage ({ columnName, expectToBe, from }: {
|
25
|
+
columnName: string // e.g. 'seriesLabels'
|
26
|
+
expectToBe: string // e.g. 'string[]'
|
27
|
+
from: string // e.g. Chart.chartParams$, Pie.params$
|
28
|
+
}): string {
|
29
|
+
return `Invalid value: '${columnName}' must be '${expectToBe}'
|
30
|
+
|
31
|
+
----> find in '${from}'`
|
32
|
+
}
|
33
|
+
|
34
|
+
// validator 的 warning 訊息
|
35
|
+
export function createValidatorWarningMessage ({ columnName, expectToBe, from }: {
|
36
|
+
columnName: string // e.g. 'seriesLabels'
|
37
|
+
expectToBe: string // e.g. 'string[]'
|
38
|
+
from: string // e.g. Chart.chartParams$, Pie.params$
|
39
|
+
}): string {
|
40
|
+
return `Value is not correct: '${columnName}' suppose to be '${expectToBe}', it may cause unexpected errors.'
|
41
|
+
|
42
|
+
----> find in '${from}'`
|
43
|
+
}
|