UNPKG

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