|
| 1 | +# Wagtail Charts |
| 2 | +Chart.js charts in Wagtail, edited and customised from the Wagtail admin |
| 3 | + |
| 4 | +## Getting started |
| 5 | + |
| 6 | +Assuming you have a Wagtail project up and running: |
| 7 | + |
| 8 | +`pip install wagtailtables` |
| 9 | + |
| 10 | +Add `wagtailtables` to your settings.py in the INSTALLED_APPS section, before the core wagtail packages: |
| 11 | + |
| 12 | +```python |
| 13 | +INSTALLED_APPS = [ |
| 14 | + # ... |
| 15 | + 'wagtailtables', |
| 16 | + # ... |
| 17 | +] |
| 18 | +``` |
| 19 | + |
| 20 | +Add a wagtailtables ChartBlock to one of your StreamFields: |
| 21 | + |
| 22 | +```python |
| 23 | +from wagtailtables.blocks import ChartBlock |
| 24 | + |
| 25 | +class ContentBlocks(StreamBlock): |
| 26 | + chart_block = ChartBlock() |
| 27 | +``` |
| 28 | + |
| 29 | +Include your streamblock in one of your pages |
| 30 | + |
| 31 | +```python |
| 32 | +class HomePage(Page): |
| 33 | + body = StreamField(ContentBlocks()) |
| 34 | + |
| 35 | + content_panels = Page.content_panels + [ |
| 36 | + StreamFieldPanel('body'), |
| 37 | + ] |
| 38 | +``` |
| 39 | + |
| 40 | +Add the `wagtailtables_tags` templatetag to your template and call the `render_charts` tag just before your `</body>` closing tag. |
| 41 | +Please note that you must render your chart block so that the `render_charts` tag can detect the charts. |
| 42 | +Here is a tiny example of a page rendering template: |
| 43 | + |
| 44 | +```django |
| 45 | +{% load wagtailcore_tags wagtailtables_tags %} |
| 46 | +
|
| 47 | +{% block content %} |
| 48 | +<div class="container-fluid"> |
| 49 | + <div class="row"> |
| 50 | + <div class="col-6"> |
| 51 | + <h1>{{self.title}}</h1> |
| 52 | + <div class="excerpt">{{self.excerpt|richtext}}</div> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + {% for block in self.body %} |
| 56 | + {% include_block block %} |
| 57 | + {% endfor %} |
| 58 | +</div> |
| 59 | +{% endblock %} |
| 60 | +
|
| 61 | +{% block extra_js %} |
| 62 | +{% render_charts %} |
| 63 | +{% endblock %} |
| 64 | +``` |
| 65 | + |
| 66 | +## Configuration |
| 67 | + |
| 68 | +`ChartBlock` accepts a few extra arguments in addition to the standard `StructBlock` arguments. |
| 69 | + |
| 70 | +### `colors` |
| 71 | +A tuple of color tuples defining the available colors in the editor. |
| 72 | + |
| 73 | +```python |
| 74 | +from wagtailtables.blocks import ChartBlock |
| 75 | + |
| 76 | +COLORS = ( |
| 77 | + ('#ff0000', 'Red'), |
| 78 | + ('#00ff00', 'Green'), |
| 79 | + ('#0000ff', 'Blue'), |
| 80 | +) |
| 81 | + |
| 82 | +class ContentBlocks(StreamBlock): |
| 83 | + chart_block = ChartBlock(colors=COLORS) |
| 84 | +``` |
| 85 | + |
| 86 | +### `chart_types` |
| 87 | + |
| 88 | +You can override the default chart types available for your `ChartBlock` instance: |
| 89 | + |
| 90 | +```python |
| 91 | +from wagtailtables.blocks import ChartBlock |
| 92 | + |
| 93 | +CHART_TYPES = ( |
| 94 | + ('line', 'Custom title for line chart'), |
| 95 | +) |
| 96 | + |
| 97 | +class ContentBlocks(StreamBlock): |
| 98 | + chart_block = ChartBlock(chart_types=CHART_TYPES) |
| 99 | +``` |
| 100 | + |
| 101 | +The default types are: |
| 102 | + |
| 103 | +```python |
| 104 | +CHART_TYPES = ( |
| 105 | + ('line', 'Line Chart'), |
| 106 | + ('bar', 'Vertical Bar Chart'), |
| 107 | + ('bar_horizontal', 'Horizontal Bar Chart'), |
| 108 | + ('area', 'Area Chart'), |
| 109 | + ('multi', 'Combo Line/Bar/Area Chart'), |
| 110 | + ('pie', 'Pie Chart'), |
| 111 | + ('doughnut', 'Doughnut Chart'), |
| 112 | + ('radar', 'Radar Chart'), |
| 113 | + ('polar', 'Polar Chart'), |
| 114 | +) |
| 115 | +``` |
| 116 | + |
| 117 | + |
| 118 | +## Dependencies |
| 119 | +* This project relies on [Jspreadsheet Community Edition](https://bossanova.uk/jspreadsheet/v4/) for data entry and manipulation. |
| 120 | +* Charts are rendered using [Chart.js](https://www.chartjs.org/). |
| 121 | +* 100% stacked bar charts use a plugin [https://github.com/y-takey/chartjs-plugin-stacked100](https://github.com/y-takey/chartjs-plugin-stacked100) |
0 commit comments