UNPKG

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