Django Dynamic Google Bar Charts Example

Published On: 26/09/2022 | Category: Django

Hi Dev,

If you need to see example of bar chart example using google chart in django python. I explained simply about django dynamic google bar charts example. This article goes in detailed on django dynamic google bar charts example from scratch. This post will give you simple example of django python google bar chart example tutorial.

In this example, we will create a visitors table click, and views column. we will add dummy records every day. Then we fetch and display it in a google bar chart. So you can follow the below step and learn how to use the google bar chart in django.

Here i explained simply step by step example of here you will learn django python google bar chart example tutorial.

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example

Step 2: Create a App

python3 startapp chart

Step 3: Update

In this step we require to do two things in our file. Add the below lines to your file:

Next, you need to add it in the file as follows:


Step 4: Create a Model

In this step we will require the database model for storing click and visitors data.Open the chart/ file and add the following code:

from django.db import models

class Visitor(models.Model):
    click = models.IntegerField()
    viewer = models.IntegerField()

After creating these model, you need to create migrations using the following command:

Next, you need to migrate your database using the following command:

python makemigrations
python migrate

Step 5: Creating the Views

In this step, we need to create the views for performing fetch record to the database.Open the chart/ file and add:

from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from .models import Visitor
import json

# Create your views here.

def googleChart(request):

    #h_var : The title for horizontal axis
    h_var = 'Click'

    #v_var : The title for horizontal axis
    v_var = 'Visitors'

    data = [[h_var,v_var]]
    visitors = Visitor.objects.all()

    for visitor in visitors:

    h_var_JSON = json.dumps(h_var)

    v_var_JSON = json.dumps(v_var)

    modified_data = json.dumps(data)

    return render(request,"charts.html",{'values':modified_data,\

Step 6: Creating the Templates

Next, open the chart/templates/charts.html file and the add:

    <!--Load the AJAX API-->
        <script type="text/javascript" src=""></script>
        <script type="text/javascript">
            // Load the Visualization API and the corechart package.
            google.charts.load('current', {'packages':['bar']});
            // Set a callback to run when the Google Visualization API is loaded.

            function drawChart() {
                //{{values|safe}} : list of list containing points for the chart
                var data = google.visualization.arrayToDataTable({{values|safe}});
                // Set chart options
                var options = {
                    title: {{h_title|safe}} +' vs. '+ {{v_title|safe}},
                    hAxis: {title: {{h_title|safe}}},
                    vAxis: {title: {{v_title|safe}}},
                    legend: { position: 'bottom' },
                    isStacked: true,

                // Instantiate and draw our chart, passing in some options.
                var chart = new google.charts.Bar(document.getElementById('chart_div'));

                chart.draw(data, google.charts.Bar.convertOptions(options));
    <div class="container" style="margin:40px;">
        <div class="row">
            <div class="col-md-12" style="text-align: center;">
                <h2>Django Google Bar Chart Example Tutorial -</h2>
                <div id="chart_div" style="width: 900px; height: 500px;display: inline-block;"></div>

Step 7: Creating URLs

In this section, we’ll create the urls to access our CRUD views.Go to the chart/ file and update it as follows:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.googleChart),

Next, we will require the modify the your root preoject folder lets update the file.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('chart.urls')),

Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the http://localhost:8000/ address with a web browser.

I Hope It will help you....

Happy Coding..