Skip to content

Commit f9d3479

Browse files
Make chart dynamic
1 parent b8081ba commit f9d3479

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

apps/pages/views.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from django.shortcuts import render, redirect
2-
from admin_datta.forms import RegistrationForm, LoginForm, UserPasswordChangeForm, UserPasswordResetForm, UserSetPasswordForm
3-
from django.contrib.auth.views import LoginView, PasswordChangeView, PasswordResetConfirmView, PasswordResetView
4-
from django.views.generic import CreateView
5-
from django.contrib.auth import logout
1+
from django.shortcuts import render
2+
from apps.pages.models import Product
3+
from django.core import serializers
64

75
from .models import *
86

@@ -16,7 +14,9 @@ def index(request):
1614

1715

1816
def charts(request):
17+
products = serializers.serialize('json', Product.objects.all())
1918
context = {
20-
'segment': 'charts'
19+
'segment': 'charts',
20+
'products': products
2121
}
2222
return render(request, 'pages/charts.html', context)

templates/pages/charts.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,22 @@ <h5>Pie Chart</h5>
6464
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
6565
<script>
6666
document.addEventListener("DOMContentLoaded", function() {
67+
var products = JSON.parse('{{ products|escapejs }}');
68+
var names = products.map(p => p.fields.name);
69+
var prices = products.map(p => p.fields.price);
70+
6771
var barOptions = {
6872
chart: { type: 'bar', height: 350 },
69-
series: [{ name: 'Sales', data: [30, 40, 45, 50, 49, 60, 70, 91] }],
70-
xaxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug'] }
73+
series: [{ name: 'Price', data: prices }],
74+
xaxis: { categories: names }
7175
};
7276
var barChart = new ApexCharts(document.querySelector("#bar-chart"), barOptions);
7377
barChart.render();
7478

7579
var pieOptions = {
7680
chart: { type: 'pie', height: 350 },
77-
series: [44, 55, 13, 43, 22],
78-
labels: ['Apple', 'Mango', 'Orange', 'Banana', 'Grapes']
81+
series: prices,
82+
labels: names
7983
};
8084
var pieChart = new ApexCharts(document.querySelector("#pie-chart"), pieOptions);
8185
pieChart.render();

0 commit comments

Comments
 (0)