|
1 | 1 | # quickchart-python |
2 | | -Python client for quickchart.io image charts web service |
| 2 | + |
| 3 | +A Python client for the [quickchart.io](https://quickchart.io/) image charts web service. |
| 4 | + |
| 5 | +# Installation |
| 6 | + |
| 7 | +Use the `quickchart.py` library in this project, or install through pip: |
| 8 | + |
| 9 | +``` |
| 10 | +pip install quickchart |
| 11 | +``` |
| 12 | + |
| 13 | +# Usage |
| 14 | + |
| 15 | +This library provides a `QuickChart` class. Import and instantiate it. Then set properties on it and specify a [Chart.js](https://chartjs.org) config: |
| 16 | + |
| 17 | +```python |
| 18 | +from quickchart import QuickChart |
| 19 | + |
| 20 | +qc = QuickChart() |
| 21 | +qc.width = 500 |
| 22 | +qc.height = 300 |
| 23 | +qc.device_pixel_ratio = 2.0 |
| 24 | +qc.config = { |
| 25 | + "type": "bar", |
| 26 | + "data": { |
| 27 | + "labels": ["Hello world", "Test"], |
| 28 | + "datasets": [{ |
| 29 | + "label": "Foo", |
| 30 | + "data": [1, 2] |
| 31 | + }] |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +Use `get_url()` on your quickchart object to get the encoded URL that renders your chart: |
| 36 | + |
| 37 | +```python |
| 38 | +print(qc.get_url()) |
| 39 | +# https://quickchart.io/chart?c=%7B%22chart%22%3A+%7B%22type%22%3A+%22bar%22%2C+%22data%22%3A+%7B%22labels%22%3A+%5B%22Hello+world%22%2C+%22Test%22%5D%2C+%22datasets%22%3A+%5B%7B%22label%22%3A+%22Foo%22%2C+%22data%22%3A+%5B1%2C+2%5D%7D%5D%7D%7D%7D&w=600&h=300&bkg=%23ffffff&devicePixelRatio=2.0&f=png |
| 40 | +``` |
| 41 | + |
| 42 | +If you have a long or complicated chart, use `get_short_url()` to get a fixed-length URL using the quickchart.io web service (note that these URLs only persist for a short time unless you have a subscription): |
| 43 | + |
| 44 | +```python |
| 45 | +print(qc.get_short_url()) |
| 46 | +# https://quickchart.io/chart/render/f-a1d3e804-dfea-442c-88b0-9801b9808401 |
| 47 | +``` |
| 48 | + |
| 49 | +The URLs will render an image of a chart: |
| 50 | + |
| 51 | +<img src="https://quickchart.io/chart?c=%7B%22type%22%3A+%22bar%22%2C+%22data%22%3A+%7B%22labels%22%3A+%5B%22Hello+world%22%2C+%22Test%22%5D%2C+%22datasets%22%3A+%5B%7B%22label%22%3A+%22Foo%22%2C+%22data%22%3A+%5B1%2C+2%5D%7D%5D%7D%7D&w=600&h=300&bkg=%23ffffff&devicePixelRatio=2.0&f=png" width="500" /> |
| 52 | + |
| 53 | +## Customizing your chart |
| 54 | + |
| 55 | +You can set the following properties: |
| 56 | + |
| 57 | +### config: dict |
| 58 | +The actual Chart.js chart configuration. |
| 59 | + |
| 60 | +### width: int |
| 61 | +Width of the chart image in pixels. Defaults to 500 |
| 62 | + |
| 63 | +### height: int |
| 64 | +Height of the chart image in pixels. Defaults to 300 |
| 65 | + |
| 66 | +### format: str |
| 67 | +Format of the chart. Defaults to png. svg is also valid. |
| 68 | + |
| 69 | +### background_color: str |
| 70 | +The background color of the chart. Any valid HTML color works. Defaults to #ffffff (white). Also takes rgb, rgba, and hsl values. |
| 71 | + |
| 72 | +### device_pixel_ratio: float |
| 73 | +The device pixel ratio of the chart. This will multiply the number of pixels by the value. This is usually used for retina displays. Defaults to 1.0. |
| 74 | + |
| 75 | +## Getting URLs |
| 76 | + |
| 77 | +There are two ways to get a URL for your chart object. |
| 78 | + |
| 79 | +### getUrl(): string |
| 80 | + |
| 81 | +Returns a URL that will display the chart image when loaded. |
| 82 | + |
| 83 | +### getShortUrl(): Promise |
| 84 | + |
| 85 | +Uses the quickchart.io web service to create a fixed-length chart URL that displays the chart image. The Promise resolves with a URL such as `https://quickchart.io/chart/render/f-a1d3e804-dfea-442c-88b0-9801b9808401`. |
| 86 | + |
| 87 | +Note that short URLs expire after a few days for users of the free service. You can [subscribe](https://quickchart.io/pricing/) to keep them around longer. |
| 88 | + |
| 89 | +## More examples |
| 90 | + |
| 91 | +Checkout the `examples` directory to see other usage. |
0 commit comments