gifted-charts-core 0.0.18 → 0.0.20

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.18",
3
+ "version": "0.0.20",
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": [
@@ -10,7 +10,7 @@ import {
10
10
  screenWidth,
11
11
  } from "../utils/constants";
12
12
  import {
13
- clone,
13
+ adjustToOffset,
14
14
  computeMaxAndMinItems,
15
15
  getAllArrowProperties,
16
16
  getArrowPoints,
@@ -22,6 +22,7 @@ import {
22
22
  getMaxValue,
23
23
  getNoOfSections,
24
24
  getPathWithHighlight,
25
+ getSanitisedData,
25
26
  getSecondaryDataWithOffsetIncluded,
26
27
  getSegmentString,
27
28
  svgPath,
@@ -39,6 +40,7 @@ export const useLineChart = (props: extendedLineChartPropsType) => {
39
40
  showDataPointsForMissingValues,
40
41
  interpolateMissingValues = true,
41
42
  onlyPositive,
43
+ yAxisOffset,
42
44
  } = props;
43
45
  const curvature = props.curvature ?? LineDefaults.curvature;
44
46
  const curveType = props.curveType ?? LineDefaults.curveType;
@@ -113,96 +115,33 @@ export const useLineChart = (props: extendedLineChartPropsType) => {
113
115
  props.height ??
114
116
  ((props.stepHeight ?? 0) * noOfSections ||
115
117
  AxesAndRulesDefaults.containerHeight);
116
- const data = useMemo(() => {
117
- if (!props.data) {
118
- return [];
119
- }
120
- const nullishHandledData = getInterpolatedData(
121
- props.data,
122
- showDataPointsForMissingValues,
123
- interpolateMissingValues,
124
- onlyPositive
125
- );
126
- if (props.yAxisOffset) {
127
- return nullishHandledData.map((item) => {
128
- item.value = item.value - (props.yAxisOffset ?? 0);
129
- return item;
130
- });
131
- }
132
- return nullishHandledData;
133
- }, [props.yAxisOffset, props.data]);
134
- const data2 = useMemo(() => {
135
- if (!props.data2) {
136
- return [];
137
- }
138
- const nullishHandledData = getInterpolatedData(
139
- props.data2,
140
- showDataPointsForMissingValues,
141
- interpolateMissingValues,
142
- onlyPositive
143
- );
144
- if (props.yAxisOffset) {
145
- return nullishHandledData.map((item) => {
146
- item.value = item.value - (props.yAxisOffset ?? 0);
147
- return item;
148
- });
149
- }
150
- return nullishHandledData;
151
- }, [props.yAxisOffset, props.data2]);
152
- const data3 = useMemo(() => {
153
- if (!props.data3) {
154
- return [];
155
- }
156
- const nullishHandledData = getInterpolatedData(
157
- props.data3,
158
- showDataPointsForMissingValues,
159
- interpolateMissingValues,
160
- onlyPositive
161
- );
162
- if (props.yAxisOffset) {
163
- return nullishHandledData.map((item) => {
164
- item.value = item.value - (props.yAxisOffset ?? 0);
165
- return item;
166
- });
167
- }
168
- return nullishHandledData;
169
- }, [props.yAxisOffset, props.data3]);
170
- const data4 = useMemo(() => {
171
- if (!props.data4) {
172
- return [];
173
- }
174
- const nullishHandledData = getInterpolatedData(
175
- props.data4,
176
- showDataPointsForMissingValues,
177
- interpolateMissingValues,
178
- onlyPositive
179
- );
180
- if (props.yAxisOffset) {
181
- return nullishHandledData.map((item) => {
182
- item.value = item.value - (props.yAxisOffset ?? 0);
183
- return item;
184
- });
185
- }
186
- return nullishHandledData;
187
- }, [props.yAxisOffset, props.data4]);
188
- const data5 = useMemo(() => {
189
- if (!props.data5) {
190
- return [];
191
- }
192
- const nullishHandledData = getInterpolatedData(
193
- props.data5,
194
- showDataPointsForMissingValues,
195
- interpolateMissingValues,
196
- onlyPositive
197
- );
198
- if (props.yAxisOffset) {
199
- return nullishHandledData.map((item) => {
200
- item.value = item.value - (props.yAxisOffset ?? 0);
201
- return item;
202
- });
203
- }
204
- return nullishHandledData;
205
- }, [props.yAxisOffset, props.data5]);
118
+
119
+ const dataSanitisationProps = {
120
+ showDataPointsForMissingValues,
121
+ interpolateMissingValues,
122
+ onlyPositive,
123
+ yAxisOffset,
124
+ };
125
+ const data = useMemo(
126
+ () => getSanitisedData(props.data, dataSanitisationProps),
127
+ [yAxisOffset, props.data]
128
+ );
129
+ const data2 = useMemo(
130
+ () => getSanitisedData(props.data2, dataSanitisationProps),
131
+ [yAxisOffset, props.data2]
132
+ );
133
+ const data3 = useMemo(
134
+ () => getSanitisedData(props.data3, dataSanitisationProps),
135
+ [yAxisOffset, props.data3]
136
+ );
137
+ const data4 = useMemo(
138
+ () => getSanitisedData(props.data4, dataSanitisationProps),
139
+ [yAxisOffset, props.data4]
140
+ );
141
+ const data5 = useMemo(
142
+ () => getSanitisedData(props.data5, dataSanitisationProps),
143
+ [yAxisOffset, props.data5]
144
+ );
206
145
 
207
146
  const secondaryData =
208
147
  getSecondaryDataWithOffsetIncluded(
@@ -215,26 +154,23 @@ export const useLineChart = (props: extendedLineChartPropsType) => {
215
154
 
216
155
  let dataSet = props.dataSet;
217
156
  if (dataSet?.length) {
218
- dataSet = dataSet.map((dataSetItem) => ({
219
- ...dataSetItem,
220
- data: getInterpolatedData(
221
- dataSetItem.data,
222
- showDataPointsForMissingValues,
223
- interpolateMissingValues,
224
- onlyPositive
225
- ),
226
- }));
227
- }
228
- const data0 = useMemo(() => {
229
- if (props.yAxisOffset) {
230
- return dataSet?.[0]?.data;
231
- } else {
232
- return dataSet?.[0]?.data?.map((item) => {
233
- item.value = item.value - (props.yAxisOffset ?? 0);
234
- return item;
157
+ dataSet = useMemo(() => {
158
+ return dataSet?.map((dataSetItem) => {
159
+ const nullishHandledDataItem = getInterpolatedData(
160
+ dataSetItem.data,
161
+ showDataPointsForMissingValues,
162
+ interpolateMissingValues,
163
+ onlyPositive
164
+ );
165
+ return {
166
+ ...dataSetItem,
167
+ data: adjustToOffset(nullishHandledDataItem, yAxisOffset),
168
+ };
235
169
  });
236
- }
237
- }, [props.yAxisOffset, dataSet]);
170
+ }, [yAxisOffset, dataSet]);
171
+ }
172
+
173
+ const data0 = dataSet?.[0]?.data;
238
174
 
239
175
  const scrollToEnd = props.scrollToEnd || LineDefaults.scrollToEnd;
240
176
  const scrollAnimation = props.scrollAnimation ?? LineDefaults.scrollAnimation;
@@ -248,7 +184,7 @@ export const useLineChart = (props: extendedLineChartPropsType) => {
248
184
  const onDataChangeAnimationDuration =
249
185
  props.onDataChangeAnimationDuration || 400;
250
186
  const animateTogether = props.animateTogether || LineDefaults.animateTogether;
251
- const animateOnDataChange = props.yAxisOffset
187
+ const animateOnDataChange = yAxisOffset
252
188
  ? false
253
189
  : props.animateOnDataChange || false;
254
190
 
@@ -807,18 +743,14 @@ export const useLineChart = (props: extendedLineChartPropsType) => {
807
743
  const arrowPointsArray: string[] = [];
808
744
  const fillPointsArray: string[] = [];
809
745
  dataSet.map((set, index) => {
810
- const setData = set.data.map((item) => {
811
- item.value = item.value - (props.yAxisOffset ?? 0);
812
- return item;
813
- });
814
746
  if (set.curved ?? props.curved) {
815
747
  const pArray: Array<Array<number>> = [];
816
- for (let i = 0; i < setData.length; i++) {
748
+ for (let i = 0; i < set.data.length; i++) {
817
749
  if (
818
750
  i >= (set.startIndex ?? 0) &&
819
751
  i <= (set.endIndex ?? set.data.length - 1)
820
752
  ) {
821
- pArray.push([getX(i), getY(setData[i].value)]);
753
+ pArray.push([getX(i), getY(set.data[i].value)]);
822
754
  }
823
755
  }
824
756
  let xx = svgPath(
@@ -836,7 +768,7 @@ export const useLineChart = (props: extendedLineChartPropsType) => {
836
768
  );
837
769
 
838
770
  // For Arrow-
839
- if (setData.length > 1 && (set.showArrow ?? props.showArrows)) {
771
+ if (set.data.length > 1 && (set.showArrow ?? props.showArrows)) {
840
772
  let arrowTipY = pArray[pArray.length - 1][1];
841
773
  let arrowTipX = pArray[pArray.length - 1][0];
842
774
  let y1 = pArray[pArray.length - 2][1];
@@ -856,26 +788,26 @@ export const useLineChart = (props: extendedLineChartPropsType) => {
856
788
  }
857
789
 
858
790
  // For Area charts-
859
- if ((set.areaChart || areaChart) && setData.length) {
791
+ if ((set.areaChart || areaChart) && set.data.length) {
860
792
  xx = addLeadingAndTrailingPathForAreaFill(
861
793
  xx,
862
- setData[0].value,
863
- setData.length
794
+ set.data[0].value,
795
+ set.data.length
864
796
  );
865
797
  fillPointsArray.push(xx);
866
798
  }
867
799
  } else {
868
800
  let pp = "";
869
- for (let i = 0; i < setData.length; i++) {
801
+ for (let i = 0; i < set.data.length; i++) {
870
802
  if (
871
803
  i >= (set.startIndex ?? 0) &&
872
804
  i <= (set.endIndex ?? set.data.length - 1)
873
805
  ) {
874
806
  if (set.stepChart || stepChart) {
875
- pp += getStepPath(setData, i);
807
+ pp += getStepPath(set.data, i);
876
808
  } else {
877
809
  pp += getSegmentPath(
878
- setData,
810
+ set.data,
879
811
  i,
880
812
  set.lineSegments,
881
813
  set.startIndex ?? 0,
@@ -887,7 +819,7 @@ export const useLineChart = (props: extendedLineChartPropsType) => {
887
819
  pointsArray.push(pp.replace("L", "M"));
888
820
 
889
821
  // For Arrow-
890
- if (setData.length > 1 && (set.showArrow ?? props.showArrows)) {
822
+ if (set.data.length > 1 && (set.showArrow ?? props.showArrows)) {
891
823
  let ppArray = pp.trim().split(" ");
892
824
  let arrowTipY = parseInt(ppArray[ppArray.length - 1]);
893
825
  let arrowTipX = parseInt(
@@ -910,12 +842,12 @@ export const useLineChart = (props: extendedLineChartPropsType) => {
910
842
  }
911
843
 
912
844
  // For Area charts-
913
- if ((set.areaChart || areaChart) && setData.length) {
845
+ if ((set.areaChart || areaChart) && set.data.length) {
914
846
  let ppp = "L" + initialSpacing + " " + heightUptoXaxis + " ";
915
847
  ppp += pp;
916
848
  ppp +=
917
849
  "L" +
918
- (initialSpacing + spacing * (setData.length - 1)) +
850
+ (initialSpacing + spacing * (set.data.length - 1)) +
919
851
  " " +
920
852
  heightUptoXaxis;
921
853
  ppp += "L" + initialSpacing + " " + heightUptoXaxis + " ";
@@ -1824,7 +1756,7 @@ export const useLineChart = (props: extendedLineChartPropsType) => {
1824
1756
  axesAndRulesProps: getAxesAndRulesProps(props, stepValue, undefined),
1825
1757
 
1826
1758
  yAxisLabelTexts: props.yAxisLabelTexts,
1827
- yAxisOffset: props.yAxisOffset,
1759
+ yAxisOffset: yAxisOffset,
1828
1760
  rotateYAxisTexts: 0,
1829
1761
  hideAxesAndRules: props.hideAxesAndRules,
1830
1762
 
@@ -1381,3 +1381,31 @@ export const getTextSizeForPieLabels = (
1381
1381
  textSize: number,
1382
1382
  radius: number
1383
1383
  ): number => (textSize ? Math.min(textSize, radius / 5) : 16);
1384
+
1385
+ export const adjustToOffset = (data, yAxisOffset) =>
1386
+ data.map((item) => {
1387
+ item.value = item.value - (yAxisOffset ?? 0);
1388
+ return item;
1389
+ });
1390
+
1391
+ export const getSanitisedData = (data, dataSanitisationProps) => {
1392
+ if (!data) {
1393
+ return [];
1394
+ }
1395
+ const {
1396
+ showDataPointsForMissingValues,
1397
+ interpolateMissingValues,
1398
+ onlyPositive,
1399
+ yAxisOffset,
1400
+ } = dataSanitisationProps;
1401
+ const nullishHandledData = getInterpolatedData(
1402
+ data,
1403
+ showDataPointsForMissingValues,
1404
+ interpolateMissingValues,
1405
+ onlyPositive
1406
+ );
1407
+ if (yAxisOffset) {
1408
+ return adjustToOffset(nullishHandledData, yAxisOffset);
1409
+ }
1410
+ return nullishHandledData;
1411
+ };