|
| 1 | +# Charting |
| 2 | + |
| 3 | +## Highcharts |
| 4 | + |
| 5 | +[Highcharts](https://www.highcharts.com/) is a charting library with official React support, that helps visualize data. |
| 6 | + |
| 7 | +### Setup |
| 8 | + |
| 9 | +Install Highcharts by including it in your `package.json` with both `"highcharts"` and `"highcharts-react-official"`. |
| 10 | + |
| 11 | +Once installed, make a new file for your Highcharts react component, and import Highcharts: |
| 12 | + |
| 13 | +``` |
| 14 | +import React from 'react' |
| 15 | +import { createRoot } from "react-dom/client" |
| 16 | +import Highcharts from 'highcharts' |
| 17 | +import HighchartsReact from 'highcharts-react-official' |
| 18 | +
|
| 19 | +function SampleChart({ props }) { |
| 20 | + const options = { |
| 21 | + title: { |
| 22 | + text: 'My chart' |
| 23 | + }, |
| 24 | + series: [{ |
| 25 | + data: [1, 2, 3] |
| 26 | + }] |
| 27 | + } |
| 28 | +
|
| 29 | + return <HighchartsReact highcharts={Highcharts} options={options} /> |
| 30 | +} |
| 31 | +
|
| 32 | +const container = window.reactSampleMount |
| 33 | +const root = createRoot(container) |
| 34 | +root.render(<SampleChart props={window.props} />) |
| 35 | +``` |
| 36 | + |
| 37 | +Then within your django template, set up a `div` for your chart where the id matches the name of the component: |
| 38 | + |
| 39 | +``` |
| 40 | +<div id="SampleChart""> |
| 41 | + <div class="text-center"> |
| 42 | + <i class="fa fa-lg fa-spinner fa-spin"></i><br><br> |
| 43 | + <i class="pending">Loading chart...</i><br><br> |
| 44 | + </div> |
| 45 | +</div> |
| 46 | +``` |
| 47 | + |
| 48 | +And add the appropriate scripts to this same template where `window.<reactMountName>` matches the value of the container in the component, and is using the `div` we just set up: |
| 49 | + |
| 50 | +``` |
| 51 | +<script type="text/javascript"> |
| 52 | + window.props = JSON.parse(document.getElementById('props').textContent) |
| 53 | + window.reactSampleMount = document.getElementById('SampleChart') |
| 54 | +</script> |
| 55 | +
|
| 56 | +{% compress js %} |
| 57 | + <script type="text/jsx" src="{% static 'js/components/SampleChart.js' %}"></script> |
| 58 | +{% endcompress %} |
| 59 | +``` |
| 60 | + |
| 61 | +And now you should have a basic chart up and running! :clap::clap::clap: |
| 62 | + |
| 63 | + |
| 64 | +### Adding Data |
| 65 | + |
| 66 | +A chart's no good without the data you're trying to show. You can have as many series as you like, and to add your data points to each, simply add them to the series: |
| 67 | + |
| 68 | +``` |
| 69 | +const options = { |
| 70 | + title: { |
| 71 | + text: 'My chart' |
| 72 | + }, |
| 73 | + series: [ |
| 74 | + { |
| 75 | + name: 'First Series', |
| 76 | + color: '#FF0000', |
| 77 | + data: [ |
| 78 | + props.point1, |
| 79 | + props.point2, |
| 80 | + props.point3 |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + name: 'Second Series', |
| 85 | + color: '#0000FF', |
| 86 | + data: [ |
| 87 | + props.point4, |
| 88 | + props.point5, |
| 89 | + props.point6 |
| 90 | + ] |
| 91 | + } |
| 92 | + ] |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### Customization |
| 97 | + |
| 98 | +From here you can follow the Highcharts docs to modify your chart. Here are some features you can include. |
| 99 | + |
| 100 | +Changing the type of chart |
| 101 | + |
| 102 | +``` |
| 103 | +const options = { |
| 104 | + title: { |
| 105 | + text: 'My chart' |
| 106 | + }, |
| 107 | + chart: { |
| 108 | + type: 'column' <<< |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +Label the xAxis and/or yAxis: |
| 114 | +``` |
| 115 | +title: { |
| 116 | + text: '# of People Who Prefer Each Color' |
| 117 | +} |
| 118 | +xAxis: { |
| 119 | + categories: [ |
| 120 | + "Red", |
| 121 | + "Blue", |
| 122 | + "Yellow", |
| 123 | + "Green" |
| 124 | + ], |
| 125 | + title: { |
| 126 | + text: "Colors", |
| 127 | + style: { |
| 128 | + fontSize: 11, |
| 129 | + }, |
| 130 | + }, |
| 131 | +}, |
| 132 | +``` |
| 133 | +<!-- TODO --> |
| 134 | +Format the tooltip that shows on hover |
| 135 | + |
| 136 | +Label the amounts for each point on the chart (with dataLabels) |
| 137 | + |
| 138 | +Disable credits |
0 commit comments