Skip to content

Commit febb721

Browse files
committed
Added the documentation for charts-api.md #22
1 parent 27c36b0 commit febb721

File tree

3 files changed

+224
-58
lines changed

3 files changed

+224
-58
lines changed

docs/charts-api.md

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
## Charts API
2+
3+
**Rocket Django** empowers you to create interactive charts effortlessly using [Apexcharts](https://apexcharts.com/). Rocket Django simplifies integrating data from Django's res framework into Apexcharts, making it easier than ever to visualize your data.
4+
5+
The home page features a static line chart and several bar charts that can customized as you see fit.
6+
7+
![Rocket Django - Styled with Tailwind-Flowbite AppSeed](https://github.com/app-generator/dummy/assets/57325382/7988e817-841d-4e88-8a10-726cad0ae3c7)
8+
9+
10+
### Benefit of Rocket Django charts
11+
**Rocket Django** seamlessly integrates charts into your Django applications, maximizing modularity and reusability. Charts are treated as a separate Django application, ensuring flexibility and ease of customization.
12+
13+
Key Benefits:
14+
15+
- Dynamic Data Visualization: Leverage models from other applications within your project to create charts that dynamically adapt to your data.
16+
17+
- Customized Chart Appearance: Customize the appearance and behaviour of your charts using the readily available template located at `templates/apps/charts.html`.
18+
19+
- Chart Creation using API data: Create charts using API data in specific pages or sections of your application. Static charts `static/assets/charts.js`.
20+
21+
22+
## How it works
23+
24+
> Codebase: related app, model, template, js
25+
26+
Dynamic charts created in Rocket Django using API data are created in `static/assets/charts.js` and rendered on the page it is needed.
27+
28+
- Create the HTML element with the `id` property you want the chart created on, in this case, `products-bar-chart-api` and `products-pie-chart-api`
29+
```html
30+
<!-- templates/apps/charts.html-->
31+
<!-- line 183-->
32+
<div class="flex gap-5 items-center justify-between">
33+
<div class="w-full" id="products-bar-chart-api"></div>
34+
<div class="w-full" id="products-pie-chart-api"></div>
35+
</div>
36+
```
37+
38+
- The data for the chart is pulled and rendered on the HTML element targeting a specific `id`. This is done in `static/assets/charts.js` as seen below:
39+
```js
40+
// static/assets/charts.js
41+
// line 481
42+
43+
if (document.getElementById('products-bar-chart-api')) {
44+
const apiUrl = '/api/product/';
45+
let dt = []
46+
47+
const fetchData = async () => {
48+
try {
49+
const response = await fetch(apiUrl);
50+
const data = await response.json();
51+
dt = data
52+
} catch (error) {
53+
console.error('Error fetching data:', error);
54+
}
55+
};
56+
await fetchData();
57+
58+
const options = {
59+
colors: ['#1A56DB', '#FDBA8C'],
60+
series: [
61+
{
62+
name: 'Product',
63+
color: '#1A56DB',
64+
data: dt.map(product => ({ x: product.name, y: product.price }))
65+
},
66+
],
67+
chart: {
68+
type: 'bar',
69+
height: '420px',
70+
fontFamily: 'Inter, sans-serif',
71+
foreColor: '#4B5563',
72+
toolbar: {
73+
show: false
74+
}
75+
},
76+
...
77+
};
78+
79+
const chart = new ApexCharts(document.getElementById('products-bar-chart-api'), options);
80+
chart.render();
81+
}
82+
83+
if (document.getElementById('products-pie-chart-api')) {
84+
const apiUrl = '/api/product/';
85+
let dt = []
86+
87+
const fetchData = async () => {
88+
try {
89+
const response = await fetch(apiUrl);
90+
const data = await response.json();
91+
dt = data
92+
} catch (error) {
93+
console.error('Error fetching data:', error);
94+
}
95+
};
96+
await fetchData();
97+
98+
99+
const chart = new ApexCharts(document.getElementById('products-pie-chart-api'), pieChartOptions(dt));
100+
chart.render();
101+
102+
// init again when toggling dark mode
103+
document.addEventListener('dark-mode', function () {
104+
chart.updateOptions(pieChartOptions(dt));
105+
});
106+
}
107+
```
108+
The `fecthData` function is used to get API data using JavaScript `fetch` API. The data from the `product` route is in the format:
109+
```json
110+
[
111+
{
112+
"id": 3,
113+
"name": "Adidas",
114+
"info": "Just another cool product",
115+
"price": 201
116+
},
117+
{
118+
"id": 4,
119+
"name": "Nike",
120+
"info": "This is a shoe shop",
121+
"price": 66
122+
},
123+
{
124+
"id": 5,
125+
"name": "Puma",
126+
"info": "Over priced Puma",
127+
"price": 666
128+
}
129+
]
130+
```
131+
132+
- The data from the API is added to the chart options for both charts and rendered.
133+
134+
Under the App menu in the sidebar, you will see a route called `Charts`. This page features a dynamic bar chart and pie chart under the `Charts via API` section that utilizes the product API data.
135+
136+
![Rocket Django Charts Page - Styled with Tailwind-Flowbite AppSeed](https://github.com/app-generator/dummy/assets/57325382/26234f9f-ffdc-45ef-942e-f779a4bc6c50)
137+
138+
### Dashboard charts
139+
Charts on the homepage are rendered using the `getMainChartOptions`, `getVisitorsChartOptions`, and `getSignupsChartOptions` functions in `static/assets/charts.js`. These functions use static data to create the charts as seen below:
140+
```js
141+
// static/assets/charts.js
142+
const getMainChartOptions = () => {
143+
let mainChartColors = {}
144+
...
145+
146+
return {
147+
chart: {
148+
...
149+
series: [
150+
{
151+
name: 'Revenue',
152+
data: [6356, 6218, 6156, 6526, 6356, 6256, 6056],
153+
color: '#1A56DB'
154+
},
155+
{
156+
name: 'Revenue (previous period)',
157+
data: [6556, 6725, 6424, 6356, 6586, 6756, 6616],
158+
color: '#FDBA8C'
159+
}
160+
],
161+
...
162+
xaxis: {
163+
categories: ['01 Feb', '02 Feb', '03 Feb', '04 Feb', '05 Feb', '06 Feb', '07 Feb'],
164+
...
165+
},
166+
...
167+
};
168+
}
169+
}
170+
171+
if (document.getElementById('main-chart')) {
172+
const chart = new ApexCharts(document.getElementById('main-chart'), getMainChartOptions());
173+
chart.render();
174+
175+
// init again when toggling dark mode
176+
document.addEventListener('dark-mode', function () {
177+
chart.updateOptions(getMainChartOptions());
178+
});
179+
}
180+
181+
const getSignupsChartOptions = () => {
182+
let signupsChartColors = {}
183+
...
184+
185+
return {
186+
series: [{
187+
name: 'Users',
188+
data: [1334, 2435, 1753, 1328, 1155, 1632, 1336]
189+
}],
190+
labels: ['01 Feb', '02 Feb', '03 Feb', '04 Feb', '05 Feb', '06 Feb', '07 Feb'],
191+
chart: {
192+
type: 'bar',
193+
height: '140px',
194+
foreColor: '#4B5563',
195+
fontFamily: 'Inter, sans-serif',
196+
toolbar: {
197+
show: false
198+
}
199+
},
200+
...
201+
};
202+
}
203+
204+
if (document.getElementById('week-signups-chart')) {
205+
const chart = new ApexCharts(document.getElementById('week-signups-chart'), getSignupsChartOptions());
206+
chart.render();
207+
208+
// init again when toggling dark mode
209+
document.addEventListener('dark-mode', function () {
210+
chart.updateOptions(getSignupsChartOptions());
211+
});
212+
}
213+
```
214+
You can customize these functions to better align with the requirements of your specific application.
215+
216+
217+
## Conclusion
218+
**Rocket Django** provides an easy way to add charts to your application using API data, creating interactive dynamic charts. Harness the power of Rocket Django to create data-driven applications.
219+
220+
221+
## ✅ Resources
222+
- 👉 [ApexCharts](https://apexcharts.com/) official website
223+
- 👉 Join the [Community](https://discord.com/invite/fZC6hup) and chat with the team behind **Rocket Django**

static/dist/main.css

Lines changed: 0 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)