UNPKG

23.7 kBMarkdownView Raw
1If you're looking to **build a website or a cross-platform mobile app** – we will be happy to help you! Send a note to clients@ui1.io and we will be in touch with you shortly.
2
3![Chart Kit](https://i.imgur.com/Idp4WIX.jpg)
4
5[📲See example app](https://github.com/indiespirit/react-native-chart-kit-example)
6
7# React Native Chart Kit Documentation
8
9## Import components
10
111. `yarn add react-native-chart-kit`
122. `yarn add react-native-svg prop-types` install peer dependencies
133. Use with ES6 syntax to import components
14
15```js
16import {
17 LineChart,
18 BarChart,
19 PieChart,
20 ProgressChart,
21 ContributionGraph,
22 StackedBarChart
23} from "react-native-chart-kit";
24```
25
26## Quick Example
27
28```jsx
29<View>
30 <Text>Bezier Line Chart</Text>
31 <LineChart
32 data={{
33 labels: ["January", "February", "March", "April", "May", "June"],
34 datasets: [
35 {
36 data: [
37 Math.random() * 100,
38 Math.random() * 100,
39 Math.random() * 100,
40 Math.random() * 100,
41 Math.random() * 100,
42 Math.random() * 100
43 ]
44 }
45 ]
46 }}
47 width={Dimensions.get("window").width} // from react-native
48 height={220}
49 yAxisLabel={"$"}
50 yAxisSuffix={"k"}
51 chartConfig={{
52 backgroundColor: "#e26a00",
53 backgroundGradientFrom: "#fb8c00",
54 backgroundGradientTo: "#ffa726",
55 decimalPlaces: 2, // optional, defaults to 2dp
56 color: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
57 labelColor: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
58 style: {
59 borderRadius: 16
60 },
61 propsForDots: {
62 r: "6",
63 strokeWidth: "2",
64 stroke: "#ffa726"
65 }
66 }}
67 bezier
68 style={{
69 marginVertical: 8,
70 borderRadius: 16
71 }}
72 />
73</View>
74```
75
76## Chart style object
77
78Define a chart style object with following properies as such:
79
80```js
81const chartConfig = {
82 backgroundGradientFrom: "#1E2923",
83 backgroundGradientFromOpacity: 0,
84 backgroundGradientTo: "#08130D",
85 backgroundGradientToOpacity: 0.5,
86 color: (opacity = 1) => `rgba(26, 255, 146, ${opacity})`,
87 strokeWidth: 2, // optional, default 3
88 barPercentage: 0.5
89};
90```
91
92| Property | Type | Description |
93| ----------------------------- | ------------------ | ------------------------------------------------------------------------------------------------------ |
94| backgroundGradientFrom | string | Defines the first color in the linear gradient of a chart's background |
95| backgroundGradientFromOpacity | Number | Defines the first color opacity in the linear gradient of a chart's background |
96| backgroundGradientTo | string | Defines the second color in the linear gradient of a chart's background |
97| backgroundGradientToOpacity | Number | Defines the second color opacity in the linear gradient of a chart's background |
98| fillShadowGradient | string | Defines the color of the area under data |
99| fillShadowGradientOpacity | Number | Defines the intial opacity of the area under data |
100| color | function => string | Defines the base color function that is used to calculate colors of labels and sectors used in a chart |
101| strokeWidth | Number | Defines the base stroke width in a chart |
102| barPercentage | Number | Defines the percent (0-1) of the available width each bar width in a chart |
103| propsForBackgroundLines | props | Override styles of the background lines, refer to react-native-svg's Line documentation |
104| propsForLabels | props | Override styles of the labels, refer to react-native-svg's Text documentation |
105
106## Responsive charts
107
108To render a responsive chart, use `Dimensions` react-native library to get the width of the screen of your device like such
109
110```js
111import { Dimensions } from "react-native";
112const screenWidth = Dimensions.get("window").width;
113```
114
115## Line Chart
116
117![Line Chart](https://i.imgur.com/Wt26snd.jpg)
118
119```js
120const data = {
121 labels: ["January", "February", "March", "April", "May", "June"],
122 datasets: [
123 {
124 data: [20, 45, 28, 80, 99, 43],
125 color: (opacity = 1) => `rgba(134, 65, 244, ${opacity})`, // optional
126 strokeWidth: 2 // optional
127 }
128 ]
129};
130```
131
132```html
133<LineChart
134 data="{data}"
135 width="{screenWidth}"
136 height="{220}"
137 chartConfig="{chartConfig}"
138/>
139```
140
141| Property | Type | Description |
142| ----------------------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
143| data | Object | Data for the chart - see example above |
144| width | Number | Width of the chart, use 'Dimensions' library to get the width of your screen for responsive |
145| height | Number | Height of the chart |
146| withDots | boolean | Show dots on the line - default: True |
147| withShadow | boolean | Show shadow for line - default: True |
148| withInnerLines | boolean | Show inner dashed lines - default: True |
149| withOuterLines | boolean | Show outer dashed lines - default: True |
150| withVerticalLabels | boolean | Show vertical labels - default: True |
151| withHorizontalLabels | boolean | Show horizontal labels - default: True |
152| fromZero | boolean | Render charts from 0 not from the minimum value. - default: False |
153| yAxisLabel | string | Prepend text to horizontal labels -- default: '' |
154| yAxisSuffix | string | Append text to horizontal labels -- default: '' |
155| xAxisLabel | string | Prepend text to vertical labels -- default: '' |
156| chartConfig | Object | Configuration object for the chart, see example config object above |
157| decorator | Function | This function takes a [whole bunch](https://github.com/indiespirit/react-native-chart-kit/blob/master/src/line-chart.js#L266) of stuff and can render extra elements, such as data point info or additional markup. |
158| onDataPointClick | Function | Callback that takes `{value, dataset, getColor}` |
159| horizontalLabelRotation | number (degree) | Rotation angle of the horizontal labels - default 0 |
160| verticalLabelRotation | number (degree) | Rotation angle of the vertical labels - default 0 |
161| getDotColor | function => string | Defines the dot color function that is used to calculate colors of dots in a line chart and takes `(dataPoint, dataPointIndex)` |
162| yLabelsOffset | number | Offset for Y axis labels |
163| xLabelsOffset | number | Offset for X axis labels |
164| hidePointsAtIndex | number[] | Indices of the data points you don't want to display |
165| formatYLabel | Function | This function change the format of the display value of the Y label. Takes the Y value as argument and should return the desirable string. |
166| formatXLabel | Function | This function change the format of the display value of the X label. Takes the X value as argument and should return the desirable string. |
167| getDotProps | (value, index) => props | This is an alternative to chartConfig's propsForDots |
168
169## Bezier Line Chart
170
171![Line Chart](https://i.imgur.com/EnUiZZU.jpg)
172
173```html
174<LineChart
175 data="{data}"
176 width="{screenWidth}"
177 height="{256}"
178 verticalLabelRotation="{30}"
179 chartConfig="{chartConfig}"
180 bezier
181/>
182```
183
184| Property | Type | Description |
185| -------- | ------- | ----------------------------------------------------- |
186| bezier | boolean | Add this prop to make the line chart smooth and curvy |
187
188## Progress Ring
189
190![Progress Chart](https://i.imgur.com/U4lkW0K.jpg)
191
192```js
193// each value represents a goal ring in Progress chart
194const data = {
195 labels: ["Swim", "Bike", "Run"], // optional
196 data: [0.4, 0.6, 0.8]
197};
198```
199
200```html
201<ProgressChart
202 data="{data}"
203 width="{screenWidth}"
204 height="{220}"
205 chartConfig="{chartConfig}"
206 hideLegend="{false}"
207/>
208```
209
210| Property | Type | Description |
211| ----------- | ------- | ------------------------------------------------------------------------------------------- |
212| data | Object | Data for the chart - see example above |
213| width | Number | Width of the chart, use 'Dimensions' library to get the width of your screen for responsive |
214| height | Number | Height of the chart |
215| chartConfig | Object | Configuration object for the chart, see example config in the beginning of this file |
216| hideLegend | Boolean | Switch to hide chart legend (defaults to false) |
217
218## Bar chart
219
220![Bat Chart](https://i.imgur.com/jVHEWiI.jpg)
221
222```js
223const data = {
224 labels: ["January", "February", "March", "April", "May", "June"],
225 datasets: [
226 {
227 data: [20, 45, 28, 80, 99, 43]
228 }
229 ]
230};
231```
232
233```html
234<BarChart style={graphStyle} data={data} width={screenWidth} height={220}
235yAxisLabel={'$'} chartConfig={chartConfig} verticalLabelRotation={30} />
236```
237
238| Property | Type | Description |
239| ----------------------- | --------------- | ------------------------------------------------------------------------------------------- |
240| data | Object | Data for the chart - see example above |
241| width | Number | Width of the chart, use 'Dimensions' library to get the width of your screen for responsive |
242| height | Number | Height of the chart |
243| withVerticalLabels | boolean | Show vertical labels - default: True |
244| withHorizontalLabels | boolean | Show horizontal labels - default: True |
245| fromZero | boolean | Render charts from 0 not from the minimum value. - default: False |
246| withInnerLines | boolean | Show inner dashed lines - default: True |
247| yAxisLabel | string | Prepend text to horizontal labels -- default: '' |
248| yAxisSuffix | string | Append text to horizontal labels -- default: '' |
249| chartConfig | Object | Configuration object for the chart, see example config in the beginning of this file |
250| horizontalLabelRotation | number (degree) | Rotation angle of the horizontal labels - default 0 |
251| verticalLabelRotation | number (degree) | Rotation angle of the vertical labels - default 0 |
252
253## StackedBar chart
254
255![StackedBar_Chart](https://imgur.com/JkBtxt8.jpg)
256
257```js
258const data = {
259 labels: ["Test1", "Test2"],
260 legend: ["L1", "L2", "L3"],
261 data: [[60, 60, 60], [30, 30, 60]],
262 barColors: ["#dfe4ea", "#ced6e0", "#a4b0be"]
263};
264```
265
266```html
267<StackedBarChart
268 style="{graphStyle}"
269 data="{data}"
270 width="{screenWidth}"
271 height="{220}"
272 chartConfig="{chartConfig}"
273/>
274```
275
276| Property | Type | Description |
277| -------------------- | ------- | ------------------------------------------------------------------------------------------- |
278| data | Object | Data for the chart - see example above |
279| width | Number | Width of the chart, use 'Dimensions' library to get the width of your screen for responsive |
280| height | Number | Height of the chart |
281| withVerticalLabels | boolean | Show vertical labels - default: True |
282| withHorizontalLabels | boolean | Show horizontal labels - default: True |
283| chartConfig | Object | Configuration object for the chart, see example config in the beginning of this file |
284
285## Pie chart
286
287![Pie Chart](https://i.imgur.com/JMz3obk.jpg)
288
289### Modified Pie Chart Screenshot
290
291![Pie Chart_modified](/src/piechart_modified.png)
292
293```js
294const data = [
295 {
296 name: "Seoul",
297 population: 21500000,
298 color: "rgba(131, 167, 234, 1)",
299 legendFontColor: "#7F7F7F",
300 legendFontSize: 15
301 },
302 {
303 name: "Toronto",
304 population: 2800000,
305 color: "#F00",
306 legendFontColor: "#7F7F7F",
307 legendFontSize: 15
308 },
309 {
310 name: "Beijing",
311 population: 527612,
312 color: "red",
313 legendFontColor: "#7F7F7F",
314 legendFontSize: 15
315 },
316 {
317 name: "New York",
318 population: 8538000,
319 color: "#ffffff",
320 legendFontColor: "#7F7F7F",
321 legendFontSize: 15
322 },
323 {
324 name: "Moscow",
325 population: 11920000,
326 color: "rgb(0, 0, 255)",
327 legendFontColor: "#7F7F7F",
328 legendFontSize: 15
329 }
330];
331```
332
333```html
334<PieChart
335 data="{data}"
336 width="{screenWidth}"
337 height="{220}"
338 chartConfig="{chartConfig}"
339 accessor="population"
340 backgroundColor="transparent"
341 paddingLeft="15"
342 absolute
343/>
344```
345
346| Property | Type | Description |
347| ----------- | ------- | ------------------------------------------------------------------------------------------- |
348| data | Object | Data for the chart - see example above |
349| width | Number | Width of the chart, use 'Dimensions' library to get the width of your screen for responsive |
350| height | Number | Height of the chart |
351| chartConfig | Object | Configuration object for the chart, see example config in the beginning of this file |
352| accessor | string | Property in the `data` object from which the number values are taken |
353| bgColor | string | background color - if you want to set transparent, input `transparent` or `none`. |
354| paddingLeft | string | left padding of the pie chart |
355| absolute | boolean | shows the values as absolute numbers |
356| hasLegend | boolean | Defaults to `true`, set it to `false` to remove the legend |
357
358## Contribution graph (heatmap)
359
360![Contribution Graph](https://i.imgur.com/NKURRt6.jpg)
361
362This type of graph is often use to display a developer contribution activity. However, there many other use cases this graph is used when you need to visualize a frequency of a certain event over time.
363
364```js
365const commitsData = [
366 { date: "2017-01-02", count: 1 },
367 { date: "2017-01-03", count: 2 },
368 { date: "2017-01-04", count: 3 },
369 { date: "2017-01-05", count: 4 },
370 { date: "2017-01-06", count: 5 },
371 { date: "2017-01-30", count: 2 },
372 { date: "2017-01-31", count: 3 },
373 { date: "2017-03-01", count: 2 },
374 { date: "2017-04-02", count: 4 },
375 { date: "2017-03-05", count: 2 },
376 { date: "2017-02-30", count: 4 }
377];
378```
379
380```html
381<ContributionGraph values={commitsData} endDate={new Date('2017-04-01')}
382numDays={105} width={screenWidth} height={220} chartConfig={chartConfig} />
383```
384
385| Property | Type | Description |
386| ----------- | ------ | ------------------------------------------------------------------------------------------- |
387| data | Object | Data for the chart - see example above |
388| width | Number | Width of the chart, use 'Dimensions' library to get the width of your screen for responsive |
389| height | Number | Height of the chart |
390| chartConfig | Object | Configuration object for the chart, see example config in the beginning of this file |
391| accessor | string | Property in the `data` object from which the number values are taken |
392
393## More styling
394
395Every charts also accepts `style` props, which will be applied to parent `svg` or `View` component of each chart.
396
397## Abstract Chart
398
399`src/abstract-chart.js` is an extendable class which can be used to create your own charts!
400
401The following methods are available:
402
403### renderHorizontalLines(config)
404
405Renders background horizontal lines like in the Line Chart and Bar Chart. Takes a config object with following properties:
406
407```js
408{
409 // width of your chart
410 width: Number,
411 // height of your chart
412 height: Number,
413 // how many lines to render
414 count: Number,
415 // top padding from the chart top edge
416 paddingTop: Number
417}
418```
419
420### renderVerticalLabels(config)
421
422Render background vertical lines. Takes a config object with following properties:
423
424```js
425{
426 // data needed to calculate the number of lines to render
427 data: Array,
428 // width of your chart
429 width: Number,
430 // height of your chart
431 height: Number,
432 paddingTop: Number,
433 paddingRight: Number
434}
435```
436
437### renderDefs(config)
438
439Render definitions of background and shadow gradients
440
441```js
442{
443 // width of your chart
444 width: Number,
445 // height of your chart
446 height: Number,
447 // first color of background gradient
448 backgroundGradientFrom: String,
449 // first color opacity of background gradient (0 - 1.0)
450 backgroundGradientFromOpacity: Number,
451 // second color of background gradient
452 backgroundGradientTo: String,
453 // second color opacity of background gradient (0 - 1.0)
454 backgroundGradientToOpacity: Number,
455}
456```
457
458## More information
459
460This library is built on top of the following open-source projects:
461
462- react-native-svg (https://github.com/react-native-community/react-native-svg)
463- paths-js (https://github.com/andreaferretti/paths-js)
464- react-native-calendar-heatmap (https://github.com/ayooby/react-native-calendar-heatmap)
465
466## Contribute
467
468See the [contribution guide](contributing.md) and join [the contributors](https://github.com/indiespirit/react-native-chart-kit/graphs/contributors)!