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