react-native-gifted-charts 0.2.1 → 0.2.2

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": "react-native-gifted-charts",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "The most complete library for Bar, Line, Area, Pie and Donut charts in React Native. Allows 2D, 3D, gradient, animations and live data updates.",
5
5
  "main": "src/index.tsx",
6
6
  "files": [
@@ -146,6 +146,9 @@ type propTypes = {
146
146
  focusedDataPointColor?: ColorValue | String | any;
147
147
  focusedDataPointRadius?: number;
148
148
  focusedCustomDataPoint?: Function;
149
+ dataPointLabelWidth?: number;
150
+ dataPointLabelShiftX?: number;
151
+ dataPointLabelShiftY?: number;
149
152
 
150
153
  startFillColor?: string;
151
154
  endFillColor?: string;
@@ -190,6 +193,7 @@ type referenceConfigType = {
190
193
  type itemType = {
191
194
  value?: number;
192
195
  label: String;
196
+ labelComponent: Function;
193
197
  labelTextStyle?: any;
194
198
  dataPointText?: string;
195
199
  textShiftX?: number;
@@ -197,7 +201,7 @@ type itemType = {
197
201
  textColor?: string;
198
202
  textFontSize?: number;
199
203
 
200
- showDataPoint?: Boolean;
204
+ hideDataPoint?: Boolean;
201
205
  dataPointHeight?: number;
202
206
  dataPointWidth?: number;
203
207
  dataPointRadius?: number;
@@ -205,6 +209,11 @@ type itemType = {
205
209
  dataPointShape?: string;
206
210
  customDataPoint?: Function;
207
211
 
212
+ stripHeight?: number;
213
+ stripWidth?: number;
214
+ stripColor?: ColorValue | String | any;
215
+ stripOpacity?: number;
216
+
208
217
  focusedDataPointShape?: String;
209
218
  focusedDataPointWidth?: number;
210
219
  focusedDataPointHeight?: number;
@@ -212,6 +221,13 @@ type itemType = {
212
221
  focusedDataPointRadius?: number;
213
222
  focusedCustomDataPoint?: Function;
214
223
 
224
+ dataPointLabelComponent?: Function;
225
+ focusedDataPointLabelComponent?: Function;
226
+ dataPointLabelWidth?: number;
227
+ dataPointLabelShiftX?: number;
228
+ dataPointLabelShiftY?: number;
229
+ showStrip?: Boolean;
230
+
215
231
  showVerticalLine?: Boolean;
216
232
  verticalLineColor?: string;
217
233
  verticalLineThickness?: number;
@@ -815,7 +831,12 @@ export const LineChart = (props: propTypes) => {
815
831
  });
816
832
  }
817
833
 
818
- const renderLabel = (index: number, label: String, labelTextStyle: any) => {
834
+ const renderLabel = (
835
+ index: number,
836
+ label: String,
837
+ labelTextStyle: any,
838
+ labelComponent: Function,
839
+ ) => {
819
840
  return (
820
841
  <View
821
842
  style={[
@@ -832,9 +853,15 @@ export const LineChart = (props: propTypes) => {
832
853
  },
833
854
  rotateLabel && {transform: [{rotate: '60deg'}]},
834
855
  ]}>
835
- <Text style={[labelTextStyle, {textAlign: 'center'}]} numberOfLines={1}>
836
- {label || ''}
837
- </Text>
856
+ {labelComponent ? (
857
+ labelComponent()
858
+ ) : (
859
+ <Text
860
+ style={[labelTextStyle, {textAlign: 'center'}]}
861
+ numberOfLines={1}>
862
+ {label || ''}
863
+ </Text>
864
+ )}
838
865
  </View>
839
866
  );
840
867
  };
@@ -843,6 +870,7 @@ export const LineChart = (props: propTypes) => {
843
870
  index: number,
844
871
  label: String,
845
872
  labelTextStyle: any,
873
+ labelComponent: Function,
846
874
  ) => {
847
875
  // console.log('label', label);
848
876
  return (
@@ -863,9 +891,15 @@ export const LineChart = (props: propTypes) => {
863
891
  },
864
892
  rotateLabel && {transform: [{rotate: '60deg'}]},
865
893
  ]}>
866
- <Text style={[labelTextStyle, {textAlign: 'center'}]} numberOfLines={1}>
867
- {label || ''}
868
- </Text>
894
+ {labelComponent ? (
895
+ labelComponent()
896
+ ) : (
897
+ <Text
898
+ style={[labelTextStyle, {textAlign: 'center'}]}
899
+ numberOfLines={1}>
900
+ {label || ''}
901
+ </Text>
902
+ )}
869
903
  </Animated.View>
870
904
  );
871
905
  };
@@ -1036,96 +1070,6 @@ export const LineChart = (props: propTypes) => {
1036
1070
  );
1037
1071
  };
1038
1072
 
1039
- const renderSpecificDataPoints = dataForRender => {
1040
- return dataForRender.map((item: itemType, index: number) => {
1041
- if (item.showDataPoint) {
1042
- if (item.dataPointShape === 'rectangular') {
1043
- return (
1044
- <Fragment key={index}>
1045
- <Rect
1046
- x={
1047
- initialSpacing -
1048
- (item.dataPointWidth || 2) / 2 -
1049
- 1 +
1050
- spacing * index
1051
- }
1052
- y={
1053
- containerHeight -
1054
- (item.dataPointHeight || 2) / 2 +
1055
- 10 -
1056
- (item.value * containerHeight) / maxValue
1057
- }
1058
- width={item.dataPointWidth || 2}
1059
- height={item.dataPointHeight || 2}
1060
- fill={item.dataPointColor || 'black'}
1061
- />
1062
- {item.dataPointText && (
1063
- <CanvasText
1064
- fill={item.textColor || 'black'}
1065
- fontSize={item.textFontSize || 10}
1066
- x={
1067
- initialSpacing -
1068
- (item.dataPointWidth || 2) +
1069
- spacing * index +
1070
- (item.textShiftX || props.textShiftX || 0)
1071
- }
1072
- y={
1073
- containerHeight -
1074
- (item.dataPointHeight || 2) / 2 +
1075
- 10 -
1076
- (item.value * containerHeight) / maxValue +
1077
- (item.textShiftY || props.textShiftY || 0)
1078
- }>
1079
- {item.dataPointText}
1080
- </CanvasText>
1081
- )}
1082
- </Fragment>
1083
- );
1084
- } else {
1085
- return (
1086
- <Fragment key={index}>
1087
- <Circle
1088
- cx={
1089
- initialSpacing -
1090
- (item.dataPointWidth || 2) / 2 +
1091
- spacing * index
1092
- }
1093
- cy={
1094
- containerHeight +
1095
- 10 -
1096
- (item.value * containerHeight) / maxValue
1097
- }
1098
- r={item.dataPointRadius || 3}
1099
- fill={item.dataPointColor || 'black'}
1100
- />
1101
- {item.dataPointText && (
1102
- <CanvasText
1103
- fill={item.textColor || 'black'}
1104
- fontSize={item.textFontSize || 10}
1105
- x={
1106
- initialSpacing -
1107
- (item.dataPointWidth || 2) +
1108
- spacing * index +
1109
- (item.textShiftX || props.textShiftX || 0)
1110
- }
1111
- y={
1112
- containerHeight -
1113
- (item.dataPointHeight || 2) / 2 +
1114
- 10 -
1115
- (item.value * containerHeight) / maxValue +
1116
- (item.textShiftY || props.textShiftY || 0)
1117
- }>
1118
- {item.dataPointText}
1119
- </CanvasText>
1120
- )}
1121
- </Fragment>
1122
- );
1123
- }
1124
- }
1125
- return null;
1126
- });
1127
- };
1128
-
1129
1073
  const onStripPress = (item, index) => {
1130
1074
  setSelectedIndex(index);
1131
1075
  if (props.onPress) {
@@ -1144,13 +1088,17 @@ export const LineChart = (props: propTypes) => {
1144
1088
  textFontSize,
1145
1089
  ) => {
1146
1090
  return dataForRender.map((item: itemType, index: number) => {
1091
+ if (item.hideDataPoint) {
1092
+ return null;
1093
+ }
1147
1094
  let dataPointsShape,
1148
1095
  dataPointsWidth,
1149
1096
  dataPointsHeight,
1150
1097
  dataPointsColor,
1151
1098
  dataPointsRadius,
1152
1099
  text,
1153
- customDataPoint;
1100
+ customDataPoint,
1101
+ dataPointLabelComponent;
1154
1102
  if (index === selectedIndex) {
1155
1103
  dataPointsShape =
1156
1104
  item.focusedDataPointShape ||
@@ -1185,6 +1133,8 @@ export const LineChart = (props: propTypes) => {
1185
1133
  props.focusedCustomDataPoint ||
1186
1134
  item.customDataPoint ||
1187
1135
  props.customDataPoint;
1136
+ dataPointLabelComponent =
1137
+ item.focusedDataPointLabelComponent || item.dataPointLabelComponent;
1188
1138
  } else {
1189
1139
  dataPointsShape = item.dataPointShape || dataPtsShape;
1190
1140
  dataPointsWidth = item.dataPointWidth || dataPtsWidth;
@@ -1195,8 +1145,17 @@ export const LineChart = (props: propTypes) => {
1195
1145
  text = '';
1196
1146
  }
1197
1147
  customDataPoint = item.customDataPoint || props.customDataPoint;
1148
+ dataPointLabelComponent = item.dataPointLabelComponent;
1198
1149
  }
1199
- // console.log('comes in');
1150
+
1151
+ const currentStripHeight =
1152
+ item.stripHeight === 0 ? 0 : item.stripHeight || stripHeight;
1153
+ const currentStripWidth =
1154
+ item.stripWidth === 0 ? 0 : item.stripWidth || stripWidth;
1155
+ const currentStripOpacity =
1156
+ item.stripOpacity === 0 ? 0 : item.stripOpacity || stripOpacity;
1157
+ const currentStripColor = item.stripColor || stripColor;
1158
+
1200
1159
  return (
1201
1160
  <Fragment key={index}>
1202
1161
  {pressEnabled ? (
@@ -1223,40 +1182,41 @@ export const LineChart = (props: propTypes) => {
1223
1182
  fill={'none'}
1224
1183
  />
1225
1184
  )}
1226
- {index === selectedIndex && showStripOnPress ? (
1227
- <Rect
1228
- x={initialSpacing + (spacing * index - dataPointsWidth / 2)}
1229
- y={
1230
- stripHeight
1231
- ? containerHeight - stripHeight + 8
1232
- : containerHeight -
1233
- dataPointsHeight / 2 +
1234
- 20 -
1235
- (item.value * containerHeight) / maxValue
1236
- }
1237
- width={stripWidth}
1238
- height={
1239
- stripHeight || containerHeight - dataPointsHeight / 2 + 20
1240
- }
1241
- opacity={stripOpacity}
1242
- fill={stripColor}
1243
- />
1244
- ) : null}
1245
1185
  </>
1246
1186
  ) : null}
1187
+ {item.showStrip ||
1188
+ (pressEnabled && index === selectedIndex && showStripOnPress) ? (
1189
+ <Rect
1190
+ x={initialSpacing + (spacing * index - dataPointsWidth / 2)}
1191
+ y={
1192
+ currentStripHeight
1193
+ ? containerHeight - currentStripHeight + 8
1194
+ : containerHeight -
1195
+ dataPointsHeight / 2 +
1196
+ 20 -
1197
+ (item.value * containerHeight) / maxValue
1198
+ }
1199
+ width={currentStripWidth}
1200
+ height={
1201
+ currentStripHeight ||
1202
+ containerHeight - dataPointsHeight / 2 + 20
1203
+ }
1204
+ opacity={currentStripOpacity}
1205
+ fill={currentStripColor}
1206
+ />
1207
+ ) : null}
1247
1208
  {customDataPoint ? (
1248
1209
  <View
1249
- style={{
1250
- position: 'absolute',
1251
- height: dataPointsHeight,
1252
- width: dataPointsWidth,
1253
- // backgroundColor: 'orange',
1254
- top:
1255
- containerHeight - (item.value * containerHeight) / maxValue,
1256
- left: initialSpacing - dataPointsWidth + spacing * index,
1257
- justifyContent: 'center',
1258
- alignItems: 'center',
1259
- }}>
1210
+ style={[
1211
+ styles.customDataPointContainer,
1212
+ {
1213
+ height: dataPointsHeight,
1214
+ width: dataPointsWidth,
1215
+ top:
1216
+ containerHeight - (item.value * containerHeight) / maxValue,
1217
+ left: initialSpacing - dataPointsWidth + spacing * index,
1218
+ },
1219
+ ]}>
1260
1220
  {customDataPoint()}
1261
1221
  </View>
1262
1222
  ) : null}
@@ -1282,35 +1242,10 @@ export const LineChart = (props: propTypes) => {
1282
1242
  }
1283
1243
  />
1284
1244
  )}
1285
- {text ? (
1286
- !showTextOnPress || index === selectedIndex ? (
1287
- <CanvasText
1288
- fill={item.textColor || textColor}
1289
- fontSize={item.textFontSize || textFontSize}
1290
- x={
1291
- initialSpacing -
1292
- dataPointsWidth +
1293
- spacing * index +
1294
- (item.textShiftX ||
1295
- props.textShiftX ||
1296
- props.textShiftX ||
1297
- 0)
1298
- }
1299
- y={
1300
- containerHeight -
1301
- dataPointsHeight / 2 +
1302
- 10 -
1303
- (item.value * containerHeight) / maxValue +
1304
- (item.textShiftY || props.textShiftY || 0)
1305
- }>
1306
- {text}
1307
- </CanvasText>
1308
- ) : null
1309
- ) : null}
1310
1245
  </Fragment>
1311
1246
  ) : (
1312
1247
  <Fragment key={index}>
1313
- {props.customDataPoint ? null : (
1248
+ {customDataPoint ? null : (
1314
1249
  <Circle
1315
1250
  cx={initialSpacing - dataPointsWidth / 2 + spacing * index}
1316
1251
  cy={
@@ -1328,30 +1263,59 @@ export const LineChart = (props: propTypes) => {
1328
1263
  }
1329
1264
  />
1330
1265
  )}
1331
- {text ? (
1332
- !showTextOnPress || index === selectedIndex ? (
1333
- <CanvasText
1334
- fill={item.textColor || textColor}
1335
- fontSize={item.textFontSize || textFontSize}
1336
- x={
1337
- initialSpacing -
1338
- dataPointsWidth +
1339
- spacing * index +
1340
- (item.textShiftX || props.textShiftX || 0)
1341
- }
1342
- y={
1343
- containerHeight -
1344
- dataPointsHeight / 2 +
1345
- 10 -
1346
- (item.value * containerHeight) / maxValue +
1347
- (item.textShiftY || props.textShiftY || 0)
1348
- }>
1349
- {text}
1350
- </CanvasText>
1351
- ) : null
1352
- ) : null}
1353
1266
  </Fragment>
1354
1267
  )}
1268
+ {dataPointLabelComponent ? (
1269
+ !showTextOnPress || index === selectedIndex ? (
1270
+ <View
1271
+ style={[
1272
+ styles.customDataPointContainer,
1273
+ {
1274
+ top:
1275
+ containerHeight +
1276
+ (item.dataPointLabelShiftY ||
1277
+ props.dataPointLabelShiftY ||
1278
+ 0) -
1279
+ (item.value * containerHeight) / maxValue,
1280
+ left:
1281
+ initialSpacing +
1282
+ (item.dataPointLabelShiftX ||
1283
+ props.dataPointLabelShiftX ||
1284
+ 0) -
1285
+ (item.dataPointLabelWidth
1286
+ ? item.dataPointLabelWidth + 20
1287
+ : props.dataPointLabelWidth
1288
+ ? props.dataPointLabelWidth + 20
1289
+ : 50) /
1290
+ 2 +
1291
+ spacing * index,
1292
+ },
1293
+ ]}>
1294
+ {dataPointLabelComponent()}
1295
+ </View>
1296
+ ) : null
1297
+ ) : text ? (
1298
+ !showTextOnPress || index === selectedIndex ? (
1299
+ <CanvasText
1300
+ fill={item.textColor || textColor}
1301
+ fontSize={item.textFontSize || textFontSize}
1302
+ x={
1303
+ initialSpacing -
1304
+ dataPointsWidth +
1305
+ spacing * index +
1306
+ (item.textShiftX || props.textShiftX || 0)
1307
+ }
1308
+ y={
1309
+ containerHeight -
1310
+ dataPointsHeight / 2 +
1311
+ 10 -
1312
+ (item.value * containerHeight) / maxValue +
1313
+ (item.textShiftY || props.textShiftY || 0)
1314
+ }>
1315
+ {text}
1316
+ </CanvasText>
1317
+ ) : null
1318
+ ) : null}
1355
1319
  </Fragment>
1356
1320
  );
1357
1321
  });
@@ -1453,7 +1417,7 @@ export const LineChart = (props: propTypes) => {
1453
1417
  textColor1,
1454
1418
  textFontSize1,
1455
1419
  )
1456
- : renderSpecificDataPoints(data)}
1420
+ : null}
1457
1421
  {!hideDataPoints2
1458
1422
  ? renderDataPoints(
1459
1423
  data2,
@@ -1465,7 +1429,7 @@ export const LineChart = (props: propTypes) => {
1465
1429
  textColor2,
1466
1430
  textFontSize2,
1467
1431
  )
1468
- : renderSpecificDataPoints(data2)}
1432
+ : null}
1469
1433
  {!hideDataPoints3
1470
1434
  ? renderDataPoints(
1471
1435
  data3,
@@ -1477,7 +1441,7 @@ export const LineChart = (props: propTypes) => {
1477
1441
  textColor3,
1478
1442
  textFontSize3,
1479
1443
  )
1480
- : renderSpecificDataPoints(data3)}
1444
+ : null}
1481
1445
  </Svg>
1482
1446
  </View>
1483
1447
  );
@@ -1560,7 +1524,7 @@ export const LineChart = (props: propTypes) => {
1560
1524
  textColor1,
1561
1525
  textFontSize1,
1562
1526
  )
1563
- : renderSpecificDataPoints(data)}
1527
+ : null}
1564
1528
  {!hideDataPoints2
1565
1529
  ? renderDataPoints(
1566
1530
  data2,
@@ -1572,7 +1536,7 @@ export const LineChart = (props: propTypes) => {
1572
1536
  textColor2,
1573
1537
  textFontSize2,
1574
1538
  )
1575
- : renderSpecificDataPoints(data2)}
1539
+ : null}
1576
1540
  {!hideDataPoints3
1577
1541
  ? renderDataPoints(
1578
1542
  data3,
@@ -1584,7 +1548,7 @@ export const LineChart = (props: propTypes) => {
1584
1548
  textColor3,
1585
1549
  textFontSize3,
1586
1550
  )
1587
- : renderSpecificDataPoints(data3)}
1551
+ : null}
1588
1552
  </Svg>
1589
1553
  </Animated.View>
1590
1554
  );
@@ -1729,8 +1693,18 @@ export const LineChart = (props: propTypes) => {
1729
1693
  return (
1730
1694
  <View key={index}>
1731
1695
  {isAnimated
1732
- ? renderAnimatedLabel(index, item.label, item.labelTextStyle)
1733
- : renderLabel(index, item.label, item.labelTextStyle)}
1696
+ ? renderAnimatedLabel(
1697
+ index,
1698
+ item.label,
1699
+ item.labelTextStyle,
1700
+ item.labelComponent,
1701
+ )
1702
+ : renderLabel(
1703
+ index,
1704
+ item.label,
1705
+ item.labelTextStyle,
1706
+ item.labelComponent,
1707
+ )}
1734
1708
  {/* {renderLabel(index, item.label, item.labelTextStyle)} */}
1735
1709
  </View>
1736
1710
  );
@@ -39,4 +39,9 @@ export const styles = StyleSheet.create({
39
39
  bottomLabel: {
40
40
  width: '100%',
41
41
  },
42
+ customDataPointContainer: {
43
+ position: 'absolute',
44
+ justifyContent: 'center',
45
+ alignItems: 'center',
46
+ },
42
47
  });