Django Dynamic Google Scatter Charts Example

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

Hi Dev,

Today, I would like to show you scatter chart example using google chart in django python. We will look at example of django dynamic google scatter charts example. This tutorial will give you simple example of django dynamic google scatter charts example from scratch. This article will give you simple example of django python google scatter 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 scatter chart. So you can follow the below step and learn how to use the google scatter chart in django.

Here i explained simply step by step example of here you will learn django python google scatter 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':['scatter']});
      // 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.Scatter(document.getElementById('chart_div'));

        chart.draw(data, google.charts.Scatter.convertOptions(options));
    <div class="container" style="margin:40px;">
        <div class="row">
            <div class="col-md-12" style="text-align: center;">
                <h2>Django Google Scatter 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..

scatter chart example using google chart in django python, django dynamic google scatter charts example, django dynamic google scatter charts example from scratch, django python google scatter chart example tutorial, how to use google chart in django python