Skip to content

Commit 98f6bc9

Browse files
Merge pull request #13 from app-generator/dynamic-chart
add dynamic chart in two ways
2 parents 21759c5 + e56fbb1 commit 98f6bc9

File tree

7 files changed

+5396
-5399
lines changed

7 files changed

+5396
-5399
lines changed

apps/charts/views.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
from django.http import HttpResponse
21
from django.shortcuts import render
2+
from apps.common.models import Product
3+
from django.core import serializers
4+
35

46
# Create your views here.
57

68
def index(request):
9+
products = serializers.serialize('json', Product.objects.all())
710
context = {
811
'segment' : 'charts',
912
'parent' : 'apps',
13+
'products': products
1014
}
1115
return render(request, 'pages/apps/charts.html', context)

static/assets/charts.js

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -236,48 +236,29 @@ if (document.getElementById('new-products-chart')) {
236236
}
237237

238238
if (document.getElementById('sales-by-category')) {
239+
const apiUrl = '/api/product/';
240+
let dt = []
241+
242+
const fetchData = async () => {
243+
try {
244+
const response = await fetch(apiUrl);
245+
const data = await response.json();
246+
dt = data
247+
} catch (error) {
248+
console.error('Error fetching data:', error);
249+
}
250+
};
251+
await fetchData();
252+
253+
239254
const options = {
240255
colors: ['#1A56DB', '#FDBA8C'],
241256
series: [
242257
{
243-
name: 'Desktop PC',
258+
name: 'Product',
244259
color: '#1A56DB',
245-
data: [
246-
{ x: '01 Feb', y: 170 },
247-
{ x: '02 Feb', y: 180 },
248-
{ x: '03 Feb', y: 164 },
249-
{ x: '04 Feb', y: 145 },
250-
{ x: '05 Feb', y: 194 },
251-
{ x: '06 Feb', y: 170 },
252-
{ x: '07 Feb', y: 155 },
253-
]
260+
data: dt.map(product => ({ x: product.name, y: product.price }))
254261
},
255-
{
256-
name: 'Phones',
257-
color: '#FDBA8C',
258-
data: [
259-
{ x: '01 Feb', y: 120 },
260-
{ x: '02 Feb', y: 294 },
261-
{ x: '03 Feb', y: 167 },
262-
{ x: '04 Feb', y: 179 },
263-
{ x: '05 Feb', y: 245 },
264-
{ x: '06 Feb', y: 182 },
265-
{ x: '07 Feb', y: 143 }
266-
]
267-
},
268-
{
269-
name: 'Gaming/Console',
270-
color: '#17B0BD',
271-
data: [
272-
{ x: '01 Feb', y: 220 },
273-
{ x: '02 Feb', y: 194 },
274-
{ x: '03 Feb', y: 217 },
275-
{ x: '04 Feb', y: 279 },
276-
{ x: '05 Feb', y: 215 },
277-
{ x: '06 Feb', y: 263 },
278-
{ x: '07 Feb', y: 183 }
279-
]
280-
}
281262
],
282263
chart: {
283264
type: 'bar',
@@ -511,7 +492,8 @@ if (document.getElementById('week-signups-chart')) {
511492
});
512493
}
513494

514-
const getTrafficChannelsChartOptions = () => {
495+
496+
const getTrafficChannelsChartOptions = (data) => {
515497

516498
let trafficChannelsChartColors = {}
517499

@@ -526,8 +508,8 @@ const getTrafficChannelsChartOptions = () => {
526508
}
527509

528510
return {
529-
series: [70, 5, 25],
530-
labels: ['Desktop', 'Tablet', 'Phone'],
511+
series: data.map(dt => dt.price),
512+
labels: data.map(dt => dt.name),
531513
colors: ['#16BDCA', '#FDBA8C', '#1A56DB'],
532514
chart: {
533515
type: 'donut',
@@ -574,7 +556,7 @@ const getTrafficChannelsChartOptions = () => {
574556
},
575557
y: {
576558
formatter: function (value) {
577-
return value + '%';
559+
return value;
578560
}
579561
}
580562
},
@@ -591,11 +573,26 @@ const getTrafficChannelsChartOptions = () => {
591573
}
592574

593575
if (document.getElementById('traffic-by-device')) {
594-
const chart = new ApexCharts(document.getElementById('traffic-by-device'), getTrafficChannelsChartOptions());
576+
const apiUrl = '/api/product/';
577+
let dt = []
578+
579+
const fetchData = async () => {
580+
try {
581+
const response = await fetch(apiUrl);
582+
const data = await response.json();
583+
dt = data
584+
} catch (error) {
585+
console.error('Error fetching data:', error);
586+
}
587+
};
588+
await fetchData();
589+
590+
591+
const chart = new ApexCharts(document.getElementById('traffic-by-device'), getTrafficChannelsChartOptions(dt));
595592
chart.render();
596593

597594
// init again when toggling dark mode
598595
document.addEventListener('dark-mode', function () {
599-
chart.updateOptions(getTrafficChannelsChartOptions());
596+
chart.updateOptions(getTrafficChannelsChartOptions(dt));
600597
});
601598
}

static/dist/main.bundle.js

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

static/dist/main.bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/dist/main.css

Lines changed: 4828 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/dist/main.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)