gifted-charts-core 0.0.24 → 0.0.25

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gifted-charts-core",
3
- "version": "0.0.24",
3
+ "version": "0.0.25",
4
4
  "description": "Mathematical and logical utilities used by react-gifted-charts and react-native-gifted-charts",
5
5
  "main": "index.ts",
6
6
  "files": [
@@ -1,12 +1,11 @@
1
- import {
2
- AxesAndRulesDefaults,
3
- populationDefaults,
4
- ruleTypes
5
- } from '../utils/constants'
6
- import { type PopulationPyramidPropsType, type RulesProps } from './types'
1
+ import { getStrokeDashArray } from '../utils'
2
+ import { AxesAndRulesDefaults, populationDefaults } from '../utils/constants'
3
+ import { Framework } from '../utils/types'
4
+ import { type RulesPropsType, type TPopulationPyramidPropsType } from './types'
7
5
 
8
- export const usePopulationPyramid = (props: PopulationPyramidPropsType) => {
6
+ export const usePopulationPyramid = (props: TPopulationPyramidPropsType) => {
9
7
  const {
8
+ framework,
10
9
  height = populationDefaults.height,
11
10
  width = populationDefaults.width,
12
11
  verticalMarginBetweenBars = populationDefaults.verticalMarginBetweenBars,
@@ -14,24 +13,20 @@ export const usePopulationPyramid = (props: PopulationPyramidPropsType) => {
14
13
  data,
15
14
  hideRules = AxesAndRulesDefaults.hideRules,
16
15
  hideYAxisText = AxesAndRulesDefaults.hideYAxisText,
17
- yAxisColor = AxesAndRulesDefaults.yAxisColor,
18
16
  yAxisThickness = AxesAndRulesDefaults.yAxisThickness,
19
17
 
20
- xAxisColor = AxesAndRulesDefaults.xAxisColor,
21
18
  xAxisThickness = AxesAndRulesDefaults.xAxisThickness,
22
19
  xAxisType = AxesAndRulesDefaults.xAxisType,
23
20
  xAxisNoOfSections = populationDefaults.xAxisNoOfSections,
24
21
  showXAxisIndices = populationDefaults.showXAxisIndices,
25
22
  xAxisIndicesWidth = populationDefaults.xAxisIndicesWidth,
26
23
  xAxisIndicesHeight = populationDefaults.xAxisIndicesHeight,
27
- xAxisIndicesColor = populationDefaults.xAxisIndicesColor,
28
24
  xAxisIndicesShiftY = 0,
29
25
  showXAxisLabelTexts = populationDefaults.showXAxisLabelTexts,
30
26
  xAxisLabelFontSize = populationDefaults.defaultFontSize,
31
27
  xAxisLabelFontStyle = populationDefaults.defaultFontStyle,
32
28
  xAxisLabelFontWeight = populationDefaults.defaultFontWeight,
33
29
  xAxisLabelFontFamily = populationDefaults.defaultFontFamily,
34
- xAxisLabelColor = populationDefaults.defaultFontColor,
35
30
  xAxisLabelShiftX = 0,
36
31
  xAxisLabelShiftY = 0,
37
32
  xAxisLabelPrefix = populationDefaults.prefix,
@@ -39,7 +34,6 @@ export const usePopulationPyramid = (props: PopulationPyramidPropsType) => {
39
34
  formatXAxisLabels,
40
35
 
41
36
  showVerticalLines = populationDefaults.showVerticalLines,
42
- verticalLinesColor = populationDefaults.verticalLinesColor,
43
37
  verticalLinesThickness = populationDefaults.verticalLinesThickness,
44
38
  verticalLinesType = populationDefaults.verticalLinesType,
45
39
  verticalLinesStrokeDashArray = populationDefaults.verticalLinesStrokeDashArray,
@@ -47,18 +41,15 @@ export const usePopulationPyramid = (props: PopulationPyramidPropsType) => {
47
41
  showYAxisIndices = AxesAndRulesDefaults.showYAxisIndices,
48
42
  yAxisIndicesWidth = AxesAndRulesDefaults.yAxisIndicesWidth,
49
43
  yAxisIndicesHeight = AxesAndRulesDefaults.yAxisIndicesHeight,
50
- yAxisIndicesColor = AxesAndRulesDefaults.yAxisIndicesColor,
51
44
  yAxisLabelFontSize = populationDefaults.defaultFontSize,
52
45
  yAxisLabelFontStyle = populationDefaults.defaultFontStyle,
53
46
  yAxisLabelFontWeight = populationDefaults.defaultFontWeight,
54
47
  yAxisLabelFontFamily = populationDefaults.defaultFontFamily,
55
- yAxisLabelColor = populationDefaults.defaultFontColor,
56
48
  yAxisLabelTextMarginRight = populationDefaults.yAxisLabelTextMarginRight,
57
49
  yAxisLabelTexts = [],
58
50
  showValuesAsBarLabels = populationDefaults.showValuesAsBarLabels,
59
51
 
60
52
  rulesThickness = AxesAndRulesDefaults.rulesThickness,
61
- rulesColor = AxesAndRulesDefaults.rulesColor,
62
53
  rulesType = AxesAndRulesDefaults.rulesType,
63
54
  dashWidth = AxesAndRulesDefaults.dashWidth,
64
55
  dashGap = AxesAndRulesDefaults.dashGap,
@@ -66,8 +57,6 @@ export const usePopulationPyramid = (props: PopulationPyramidPropsType) => {
66
57
  leftBarLabelWidth = populationDefaults.leftBarLabelWidth,
67
58
  leftBarLabelFontSize = props.barLabelFontSize ??
68
59
  populationDefaults.defaultFontSize,
69
- leftBarLabelColor = props.barLabelColor ??
70
- populationDefaults.defaultFontColor,
71
60
  leftBarLabelFontStyle = props.barLabelFontStyle ??
72
61
  populationDefaults.defaultFontStyle,
73
62
  leftBarLabelFontWeight = props.barLabelFontWeight ??
@@ -80,8 +69,6 @@ export const usePopulationPyramid = (props: PopulationPyramidPropsType) => {
80
69
  rightBarLabelWidth = populationDefaults.rightBarLabelWidth,
81
70
  rightBarLabelFontSize = props.barLabelFontSize ??
82
71
  populationDefaults.defaultFontSize,
83
- rightBarLabelColor = props.barLabelColor ??
84
- populationDefaults.defaultFontColor,
85
72
  rightBarLabelFontStyle = props.barLabelFontStyle ??
86
73
  populationDefaults.defaultFontStyle,
87
74
  rightBarLabelFontWeight = props.barLabelFontWeight ??
@@ -95,15 +82,10 @@ export const usePopulationPyramid = (props: PopulationPyramidPropsType) => {
95
82
  showMidAxis = populationDefaults.showMidAxis,
96
83
  midAxisLabelWidth = populationDefaults.midAxisLabelWidth,
97
84
  midAxisLabelFontSize = populationDefaults.defaultFontSize,
98
- midAxisLabelColor = populationDefaults.defaultFontColor,
99
85
  midAxisLabelFontStyle = populationDefaults.defaultFontStyle,
100
86
  midAxisLabelFontWeight = populationDefaults.defaultFontWeight,
101
87
  midAxisLabelFontFamily = populationDefaults.defaultFontFamily,
102
88
 
103
- leftBarColor = populationDefaults.leftBarColor,
104
- rightBarColor = populationDefaults.rightBarColor,
105
- leftBarBorderColor = populationDefaults.leftBarBorderColor,
106
- rightBarBorderColor = populationDefaults.rightBarBorderColor,
107
89
  leftBarBorderWidth = props.barBorderWidth ??
108
90
  populationDefaults.leftBarBorderWidth,
109
91
  rightBarBorderWidth = props.barBorderWidth ??
@@ -117,14 +99,56 @@ export const usePopulationPyramid = (props: PopulationPyramidPropsType) => {
117
99
  showSurplus = populationDefaults.showSurplus,
118
100
  showSurplusLeft = populationDefaults.showSurplusLeft,
119
101
  showSurplusRight = populationDefaults.showSurplusRight,
102
+ leftSurplusBorderWidth = populationDefaults.leftSurplusBorderWidth,
103
+ rightSurplusBorderWidth = populationDefaults.rightSurplusBorderWidth
104
+ } = props
105
+
106
+ let {
107
+ yAxisColor = AxesAndRulesDefaults.yAxisColor,
108
+ xAxisColor = AxesAndRulesDefaults.xAxisColor,
109
+ xAxisIndicesColor = populationDefaults.xAxisIndicesColor,
110
+ xAxisLabelColor = populationDefaults.defaultFontColor,
111
+ verticalLinesColor = populationDefaults.verticalLinesColor,
112
+ yAxisIndicesColor = AxesAndRulesDefaults.yAxisIndicesColor,
113
+ yAxisLabelColor = populationDefaults.defaultFontColor,
114
+ rulesColor = AxesAndRulesDefaults.rulesColor,
115
+ leftBarLabelColor = props.barLabelColor ??
116
+ populationDefaults.defaultFontColor,
117
+ rightBarLabelColor = props.barLabelColor ??
118
+ populationDefaults.defaultFontColor,
119
+ midAxisLabelColor = populationDefaults.defaultFontColor,
120
+ leftBarColor = populationDefaults.leftBarColor,
121
+ rightBarColor = populationDefaults.rightBarColor,
122
+ leftBarBorderColor = populationDefaults.leftBarBorderColor,
123
+ rightBarBorderColor = populationDefaults.rightBarBorderColor,
120
124
  leftSurplusColor = populationDefaults.leftSurplusColor,
121
125
  leftSurplusBorderColor = populationDefaults.leftSurplusBorderColor,
122
126
  rightSurplusColor = populationDefaults.rightSurplusColor,
123
- rightSurplusBorderColor = populationDefaults.rightSurplusBorderColor,
124
- leftSurplusBorderWidth = populationDefaults.leftSurplusBorderWidth,
125
- rightSurplusBorderWidth = populationDefaults.rightSurplusBorderWidth
127
+ rightSurplusBorderColor = populationDefaults.rightSurplusBorderColor
126
128
  } = props
127
129
 
130
+ if (framework === Framework.reactJS) {
131
+ yAxisColor = yAxisColor.toString()
132
+ xAxisColor = xAxisColor.toString()
133
+ xAxisIndicesColor = xAxisIndicesColor.toString()
134
+ xAxisLabelColor = xAxisLabelColor.toString()
135
+ verticalLinesColor = verticalLinesColor.toString()
136
+ yAxisIndicesColor = yAxisIndicesColor.toString()
137
+ yAxisLabelColor = yAxisLabelColor.toString()
138
+ rulesColor = rulesColor.toString()
139
+ leftBarLabelColor = leftBarLabelColor.toString()
140
+ rightBarLabelColor = rightBarLabelColor.toString()
141
+ midAxisLabelColor = midAxisLabelColor.toString()
142
+ leftBarColor = leftBarColor.toString()
143
+ rightBarColor = rightBarColor.toString()
144
+ leftBarBorderColor = leftBarBorderColor.toString()
145
+ rightBarBorderColor = rightBarBorderColor.toString()
146
+ leftSurplusColor = leftSurplusColor.toString()
147
+ leftSurplusBorderColor = leftSurplusBorderColor.toString()
148
+ rightSurplusColor = rightSurplusColor.toString()
149
+ rightSurplusBorderColor = rightSurplusBorderColor.toString()
150
+ }
151
+
128
152
  const yAxisLabelWidth = hideYAxisText
129
153
  ? yAxisThickness
130
154
  : props.yAxisLabelWidth ?? AxesAndRulesDefaults.yAxisLabelWidth
@@ -146,8 +170,7 @@ export const usePopulationPyramid = (props: PopulationPyramidPropsType) => {
146
170
  const max = Math.max(leftMax, rightMax)
147
171
 
148
172
  const xAxisRoundToDigits =
149
- props.xAxisRoundToDigits ??
150
- (max < 0.1 ? 3 : max < 1 ? 2 : max < 10 ? 1 : 0)
173
+ props.xAxisRoundToDigits ?? (max < 0.1 ? 3 : max < 1 ? 2 : max < 10 ? 1 : 0)
151
174
 
152
175
  const midAxisAndLabelWidth =
153
176
  (showMidAxis ? midAxisLabelWidth : 0) / 2 +
@@ -158,7 +181,7 @@ export const usePopulationPyramid = (props: PopulationPyramidPropsType) => {
158
181
  const leftXAfterMid = mid - (showMidAxis ? midAxisLabelWidth / 2 : 0)
159
182
  const rightXAfterMid = mid + (showMidAxis ? midAxisLabelWidth / 2 : 0)
160
183
 
161
- const yAxisLineProps: RulesProps = {
184
+ const yAxisLineProps: RulesPropsType = {
162
185
  x1: yAxisLabelWidth,
163
186
  y1: 0,
164
187
  x2: yAxisLabelWidth,
@@ -166,18 +189,22 @@ export const usePopulationPyramid = (props: PopulationPyramidPropsType) => {
166
189
  stroke: yAxisColor,
167
190
  strokeWidth: yAxisThickness
168
191
  }
169
- if (props.yAxisStrokeDashArray?.length === 2) {
170
- yAxisLineProps.strokeDasharray = props.yAxisStrokeDashArray
171
- }
172
192
 
173
- const midAxisLineCommonProps: RulesProps = {
193
+ yAxisLineProps.strokeDasharray = getStrokeDashArray(
194
+ props.yAxisStrokeDashArray,
195
+ framework
196
+ )
197
+
198
+ const midAxisLineCommonProps: RulesPropsType = {
174
199
  y1: 0,
175
200
  y2: containerHeight,
176
201
  strokeWidth: props.midAxisThickness ?? yAxisThickness
177
202
  }
178
- if (props.midAxisStrokeDashArray?.length === 2) {
179
- midAxisLineCommonProps.strokeDasharray = props.midAxisStrokeDashArray
180
- }
203
+
204
+ midAxisLineCommonProps.strokeDasharray = getStrokeDashArray(
205
+ props.midAxisStrokeDashArray,
206
+ framework
207
+ )
181
208
 
182
209
  const xAxisLabelY =
183
210
  containerHeight + xAxisLabelFontSize + 6 + xAxisLabelShiftY
@@ -187,15 +214,16 @@ export const usePopulationPyramid = (props: PopulationPyramidPropsType) => {
187
214
  stroke: xAxisIndicesColor,
188
215
  strokeWidth: xAxisIndicesWidth
189
216
  }
190
- const verticalLinesCommonProps: RulesProps = {
217
+ const verticalLinesCommonProps: RulesPropsType = {
191
218
  y1: 0,
192
219
  y2: containerHeight,
193
220
  stroke: verticalLinesColor,
194
221
  strokeWidth: verticalLinesThickness
195
222
  }
196
- if (verticalLinesType !== ruleTypes.SOLID) {
197
- verticalLinesCommonProps.strokeDasharray = verticalLinesStrokeDashArray
198
- }
223
+ verticalLinesCommonProps.strokeDasharray = getStrokeDashArray(
224
+ verticalLinesStrokeDashArray
225
+ )
226
+
199
227
  const xAxisLabelsCommonProps = {
200
228
  y: xAxisLabelY + xAxisLabelShiftY,
201
229
  stroke: xAxisLabelColor,
@@ -1,5 +1,5 @@
1
1
  import { type ColorValue } from 'react-native'
2
- import { type RuleTypes } from '../utils/types'
2
+ import { type Framework, type RuleTypes } from '../utils/types'
3
3
  import { type FontStyle, type FontWeight } from 'react-native-svg'
4
4
 
5
5
  export interface popnPyramidDataItem {
@@ -60,6 +60,21 @@ export interface popnPyramidDataItem {
60
60
  rightSurplusBorderWidth?: number
61
61
  }
62
62
 
63
+ export type popnPyramidDataItemReactJS = popnPyramidDataItem & {
64
+ leftBarColor?: string
65
+ rightBarColor?: string
66
+ leftBarBorderColor?: string
67
+ rightBarBorderColor?: string
68
+ barLabelColor?: string
69
+ leftBarLabelColor?: string
70
+ rightBarLabelColor?: string
71
+ midAxisLabelColor?: string
72
+ leftSurplusColor?: string
73
+ leftSurplusBorderColor?: string
74
+ rightSurplusColor?: string
75
+ rightSurplusBorderColor?: string
76
+ }
77
+
63
78
  export interface RulesProps {
64
79
  x1?: number
65
80
  y1?: number
@@ -67,7 +82,11 @@ export interface RulesProps {
67
82
  y2?: number
68
83
  stroke?: ColorValue
69
84
  strokeWidth?: number
70
- strokeDasharray?: number[]
85
+ strokeDasharray?: number[] | string
86
+ }
87
+
88
+ export type RulesPropsReactJS = RulesProps & {
89
+ stroke?: string
71
90
  }
72
91
 
73
92
  export interface PopulationPyramidPropsType {
@@ -81,7 +100,7 @@ export interface PopulationPyramidPropsType {
81
100
  yAxisLabelWidth?: number
82
101
  yAxisColor?: ColorValue
83
102
  yAxisThickness?: number
84
- yAxisStrokeDashArray?: number[]
103
+ yAxisStrokeDashArray?: number[] | string
85
104
  xAxisColor?: ColorValue
86
105
  xAxisThickness?: number
87
106
  xAxisType?: RuleTypes
@@ -108,7 +127,7 @@ export interface PopulationPyramidPropsType {
108
127
  verticalLinesColor?: ColorValue
109
128
  verticalLinesThickness?: number
110
129
  verticalLinesType?: RuleTypes
111
- verticalLinesStrokeDashArray?: number[]
130
+ verticalLinesStrokeDashArray?: number[] | string
112
131
 
113
132
  noOfSections?: number
114
133
  barsMapToYAxisSections?: boolean
@@ -139,7 +158,7 @@ export interface PopulationPyramidPropsType {
139
158
  midAxisColor?: ColorValue
140
159
  midAxisLeftColor?: ColorValue
141
160
  midAxisRightColor?: ColorValue
142
- midAxisStrokeDashArray?: number[]
161
+ midAxisStrokeDashArray?: number[] | string
143
162
  midAxisLabelFontSize?: number
144
163
  midAxisLabelColor?: ColorValue
145
164
  midAxisLabelFontStyle?: FontStyle
@@ -195,5 +214,39 @@ export interface PopulationPyramidPropsType {
195
214
  rightSurplusBorderColor?: ColorValue
196
215
  leftSurplusBorderWidth?: number
197
216
  rightSurplusBorderWidth?: number
217
+ }
198
218
 
219
+ export type PopulationPyramidPropsTypeReactJS = PopulationPyramidPropsType & {
220
+ data: popnPyramidDataItemReactJS[]
221
+ yAxisColor?: string
222
+ xAxisColor?: string
223
+ xAxisIndicesColor?: string
224
+ xAxisLabelColor?: string
225
+ verticalLinesColor?: string
226
+ yAxisIndicesColor?: string
227
+ yAxisLabelColor?: string
228
+ rulesColor?: string
229
+ midAxisColor?: string
230
+ midAxisLeftColor?: string
231
+ midAxisRightColor?: string
232
+ midAxisLabelColor?: string
233
+ barLabelColor?: string
234
+ leftBarLabelColor?: string
235
+ rightBarLabelColor?: string
236
+ leftBarColor?: string
237
+ rightBarColor?: string
238
+ leftBarBorderColor?: string
239
+ rightBarBorderColor?: string
240
+ leftSurplusColor?: string
241
+ leftSurplusBorderColor?: string
242
+ rightSurplusColor?: string
243
+ rightSurplusBorderColor?: string
199
244
  }
245
+
246
+ export type RulesPropsType =
247
+ | ({ framework: Framework.reactJS } & RulesPropsReactJS)
248
+ | ({ framework?: Framework.reactNative } & RulesProps)
249
+
250
+ export type TPopulationPyramidPropsType =
251
+ | ({ framework: Framework.reactJS } & PopulationPyramidPropsTypeReactJS)
252
+ | ({ framework?: Framework.reactNative } & PopulationPyramidPropsType)
@@ -18,7 +18,8 @@ import {
18
18
  CurveType,
19
19
  type HighlightedRange,
20
20
  type LineProperties,
21
- type LineSegment
21
+ type LineSegment,
22
+ Framework
22
23
  } from './types'
23
24
  import {
24
25
  type lineConfigType,
@@ -1490,3 +1491,43 @@ export const getSanitisedData = (
1490
1491
  }
1491
1492
  return nullishHandledData
1492
1493
  }
1494
+
1495
+ export const getStrokeDashArray = (
1496
+ strokeDash?: number[] | string,
1497
+ framework?: Framework
1498
+ ): number[] | string | undefined => {
1499
+ let strokeDashArrayOrString: number[] | string | undefined
1500
+ if (framework === Framework.reactJS) {
1501
+ if (strokeDash instanceof Array) {
1502
+ strokeDashArrayOrString = strokeDash.toString().replace(',', ' ')
1503
+ } else if (typeof strokeDash === 'string') {
1504
+ strokeDashArrayOrString = strokeDash
1505
+ .replace(',', ' ')
1506
+ .replace('[', '')
1507
+ .replace(']', '')
1508
+ .replace('{', '')
1509
+ .replace('}', '')
1510
+ }
1511
+ } else {
1512
+ if (strokeDash instanceof Array) {
1513
+ strokeDashArrayOrString = strokeDash
1514
+ } else if (typeof strokeDash === 'string') {
1515
+ const ar = strokeDash
1516
+ .replace(',', ' ')
1517
+ .replace('[', '')
1518
+ .replace(']', '')
1519
+ .replace('{', '')
1520
+ .replace('}', '')
1521
+ .split(' ')
1522
+ if (ar[0] && ar[1]) {
1523
+ const n1 = Number(ar[0])
1524
+ const n2 = Number(ar[1])
1525
+ if (!isNaN(n1) && !isNaN(n2)) {
1526
+ strokeDashArrayOrString = [n1, n2]
1527
+ }
1528
+ }
1529
+ }
1530
+ }
1531
+
1532
+ return strokeDashArrayOrString
1533
+ }
@@ -3,6 +3,11 @@ import { type chartTypes, type yAxisSides } from './constants'
3
3
  import { type lineDataItem } from '../LineChart/types'
4
4
  import { type barDataItem, type stackDataItem } from '../BarChart/types'
5
5
 
6
+ export enum Framework {
7
+ reactJS,
8
+ reactNative
9
+ }
10
+
6
11
  export type RuleType = 'solid' | 'dashed' | 'dotted' | string
7
12
 
8
13
  export interface RuleTypes {