Debian Codegraph @

README.md

raw symbols SCIP
1 # Chartkick.py
2
3 Create beautiful JavaScript charts with one line of Python. No more fighting with charting libraries!
4
5 [See it in action](https://chartkick.com/python)
6
7 [![Build Status](https://github.com/ankane/chartkick.py/actions/workflows/build.yml/badge.svg)](https://github.com/ankane/chartkick.py/actions)
8
9 ## Quick Start
10
11 Run:
12
13 ```sh
14 pip install chartkick
15 ```
16
17 Then follow the instructions for your web framework:
18
19 - [Django](#django)
20 - [Flask](#flask)
21
22 This sets up Chartkick with [Chart.js](https://www.chartjs.org/). For other charting libraries, see [these instructions](#additional-charting-libraries).
23
24 ### Django
25
26 Add to `INSTALLED_APPS` in `settings.py`
27
28 ```python
29 INSTALLED_APPS = [
30 'chartkick.django',
31 # ...
32 ]
33 ```
34
35 Load the JavaScript
36
37 ```django
38 {% load static %}
39
40 <script src="{% static 'chartkick/Chart.bundle.js' %}"></script>
41 <script src="{% static 'chartkick/chartkick.js' %}"></script>
42 ```
43
44 Create a chart in a view
45
46 ```python
47 from chartkick.django import PieChart
48
49 def index(request):
50 chart = PieChart({'Blueberry': 44, 'Strawberry': 23})
51 return render(request, 'home/index.html', {'chart': chart})
52 ```
53
54 And add it to the template
55
56 ```django
57 {{ chart }}
58 ```
59
60 ### Flask
61
62 Register the blueprint
63
64 ```python
65 from chartkick.flask import chartkick_blueprint
66
67 app.register_blueprint(chartkick_blueprint)
68 ```
69
70 Load the JavaScript
71
72 ```jinja
73 <script src="{{ url_for('chartkick.static', filename='Chart.bundle.js') }}"></script>
74 <script src="{{ url_for('chartkick.static', filename='chartkick.js') }}"></script>
75 ```
76
77 Create a chart in a route
78
79 ```python
80 from chartkick.flask import PieChart
81
82 def index():
83 chart = PieChart({'Blueberry': 44, 'Strawberry': 23})
84 return render_template('home/index.html', chart=chart)
85 ```
86
87 And add it to the template
88
89 ```jinja
90 {{ chart }}
91 ```
92
93 ## Charts
94
95 Line chart
96
97 ```python
98 LineChart({'2025-01-01': 11, '2025-01-02': 6})
99 ```
100
101 Pie chart
102
103 ```python
104 PieChart({'Blueberry': 44, 'Strawberry': 23})
105 ```
106
107 Column chart
108
109 ```python
110 ColumnChart({'Sun': 32, 'Mon': 46, 'Tue': 28})
111 ```
112
113 Bar chart
114
115 ```python
116 BarChart({'Work': 32, 'Play': 1492})
117 ```
118
119 Area chart
120
121 ```python
122 AreaChart({'2025-01-01': 11, '2025-01-02': 6})
123 ```
124
125 Scatter chart
126
127 ```python
128 ScatterChart([[174.0, 80.0], [176.5, 82.3]], xtitle='Size', ytitle='Population')
129 ```
130
131 Geo chart - *Google Charts*
132
133 ```python
134 GeoChart({'United States': 44, 'Germany': 23, 'Brazil': 22})
135 ```
136
137 Timeline - *Google Charts*
138
139 ```python
140 Timeline([['Washington', '1789-04-29', '1797-03-03'], ['Adams', '1797-03-03', '1801-03-03']])
141 ```
142
143 Multiple series
144
145 ```python
146 data = [
147 {'name': 'Workout', 'data': {'2025-01-01': 3, '2025-01-02': 4}},
148 {'name': 'Call parents', 'data': {'2025-01-01': 5, '2025-01-02': 3}}
149 ]
150 LineChart(data)
151 ```
152
153 ## Data
154
155 Data can be a dictionary, list, or URL.
156
157 #### Dictionary
158
159 ```python
160 LineChart({'2025-01-01': 2, '2025-01-02': 3})
161 ```
162
163 #### List
164
165 ```python
166 LineChart([['2025-01-01', 2], ['2025-01-02', 3]])
167 ```
168
169 #### URL
170
171 Make your pages load super fast and stop worrying about timeouts. Give each chart its own endpoint.
172
173 ```python
174 LineChart('/charts/tasks')
175 ```
176
177 ## Options
178
179 Width and height
180
181 ```python
182 LineChart(data, width='800px', height='500px')
183 ```
184
185 Min and max values
186
187 ```python
188 LineChart(data, min=1000, max=5000)
189 ```
190
191 `min` defaults to 0 for charts with non-negative values. Use `None` to let the charting library decide.
192
193 Min and max for x-axis - *Chart.js*
194
195 ```python
196 LineChart(data, xmin='2025-01-01', xmax='2025-02-01')
197 ```
198
199 Colors
200
201 ```python
202 LineChart(data, colors=['#b00', '#666'])
203 ```
204
205 Stacked columns or bars
206
207 ```python
208 ColumnChart(data, stacked=True)
209 ```
210
211 Discrete axis
212
213 ```python
214 LineChart(data, discrete=True)
215 ```
216
217 Label (for single series)
218
219 ```python
220 LineChart(data, label='Value')
221 ```
222
223 Axis titles
224
225 ```python
226 LineChart(data, xtitle='Time', ytitle='Population')
227 ```
228
229 Straight lines between points instead of a curve
230
231 ```python
232 LineChart(data, curve=False)
233 ```
234
235 Hide points
236
237 ```python
238 LineChart(data, points=False)
239 ```
240
241 Show or hide legend
242
243 ```python
244 LineChart(data, legend=False)
245 ```
246
247 Specify legend position
248
249 ```python
250 LineChart(data, legend='bottom')
251 ```
252
253 Donut chart
254
255 ```python
256 PieChart(data, donut=True)
257 ```
258
259 Prefix, useful for currency - *Chart.js, Highcharts*
260
261 ```python
262 LineChart(data, prefix='$')
263 ```
264
265 Suffix, useful for percentages - *Chart.js, Highcharts*
266
267 ```python
268 LineChart(data, suffix='%')
269 ```
270
271 Set a thousands separator - *Chart.js, Highcharts*
272
273 ```python
274 LineChart(data, thousands=',')
275 ```
276
277 Set a decimal separator - *Chart.js, Highcharts*
278
279 ```python
280 LineChart(data, decimal=',')
281 ```
282
283 Set significant digits - *Chart.js, Highcharts*
284
285 ```python
286 LineChart(data, precision=3)
287 ```
288
289 Set rounding - *Chart.js, Highcharts*
290
291 ```python
292 LineChart(data, round=2)
293 ```
294
295 Show insignificant zeros, useful for currency - *Chart.js, Highcharts*
296
297 ```python
298 LineChart(data, round=2, zeros=True)
299 ```
300
301 Friendly byte sizes - *Chart.js*
302
303 ```python
304 LineChart(data, bytes=True)
305 ```
306
307 Specify the message when data is loading
308
309 ```python
310 LineChart(data, loading='Loading...')
311 ```
312
313 Specify the message when data is empty
314
315 ```python
316 LineChart(data, empty='No data')
317 ```
318
319 Refresh data from a remote source every `n` seconds
320
321 ```python
322 LineChart(url, refresh=60)
323 ```
324
325 You can pass options directly to the charting library with:
326
327 ```python
328 LineChart(data, library={'backgroundColor': '#eee'})
329 ```
330
331 See the documentation for [Chart.js](https://www.chartjs.org/docs/), [Google Charts](https://developers.google.com/chart/interactive/docs/gallery), and [Highcharts](https://api.highcharts.com/highcharts) for more info.
332
333 To customize datasets in Chart.js, use:
334
335 ```python
336 LineChart(data, dataset={'borderWidth': 10})
337 ```
338
339 You can pass this option to individual series as well.
340
341 ### Multiple Series
342
343 You can pass a few options with a series:
344
345 - `name`
346 - `data`
347 - `color`
348 - `dataset` - *Chart.js only*
349 - `points` - *Chart.js only*
350 - `curve` - *Chart.js only*
351
352 ### Code
353
354 If you want to use the charting library directly, get the code with:
355
356 ```python
357 LineChart(data, code=True)
358 ```
359
360 The code will be logged to the JavaScript console. JavaScript functions cannot be logged, so it may not be identical.
361
362 ### Download Charts
363
364 *Chart.js only*
365
366 Give users the ability to download charts. It all happens in the browser - no server-side code needed.
367
368 ```python
369 LineChart(data, download=True)
370 ```
371
372 Safari will open the image in a new window instead of downloading.
373
374 Set the filename
375
376 ```python
377 LineChart(data, download={'filename': 'boom'})
378 ```
379
380 Set the background color
381
382 ```python
383 LineChart(data, download={'background': '#ffffff'})
384 ```
385
386 Set title
387
388 ```python
389 LineChart(data, title='Awesome chart')
390 ```
391
392 ## Additional Charting Libraries
393
394 - [Google Charts](#google-charts)
395 - [Highcharts](#highcharts)
396
397 ### Google Charts
398
399 Load the JavaScript
400
401 Django
402
403 ```django
404 {% load static %}
405
406 <script src="https://www.gstatic.com/charts/loader.js"></script>
407 <script src="{% static 'chartkick/chartkick.js' %}"></script>
408 ```
409
410 Flask
411
412 ```jinja
413 <script src="https://www.gstatic.com/charts/loader.js"></script>
414 <script src="{{ url_for('chartkick.static', filename='chartkick.js') }}"></script>
415 ```
416
417 ### Highcharts
418
419 Download [highcharts.js](https://code.highcharts.com/highcharts.js) and load the JavaScript
420
421 Django
422
423 ```django
424 {% load static %}
425
426 <script src="{% static 'highcharts.js' %}"></script>
427 <script src="{% static 'chartkick/chartkick.js' %}"></script>
428 ```
429
430 Flask
431
432 ```jinja
433 <script src="{{ url_for('static', filename='highcharts.js') }}"></script>
434 <script src="{{ url_for('chartkick.static', filename='chartkick.js') }}"></script>
435 ```
436
437 ### Multiple Libraries
438
439 If more than one charting library is loaded, choose between them with:
440
441 ```python
442 LineChart(data, adapter='google') # or highcharts or chartjs
443 ```
444
445 ## Credits
446
447 A big thanks to [Mher Movsisyan](https://github.com/mher) for creating the [initial version](https://github.com/mher/chartkick.py).
448
449 ## History
450
451 View the [changelog](https://github.com/ankane/chartkick.py/blob/master/CHANGELOG.md)
452
453 ## Contributing
454
455 Everyone is encouraged to help improve this project. Here are a few ways you can help:
456
457 - [Report bugs](https://github.com/ankane/chartkick.py/issues)
458 - Fix bugs and [submit pull requests](https://github.com/ankane/chartkick.py/pulls)
459 - Write, clarify, or fix documentation
460 - Suggest or add new features
461
462 To get started with development:
463
464 ```sh
465 git clone https://github.com/ankane/chartkick.py.git
466 cd chartkick.py
467 pip install -r requirements.txt
468 pytest
469 ```