line chart using json django code example

Example 1: line chart using json django

var endpoint = 'api/'
$.ajax({
    
    method : "GET",
    url    : endpoint,
    
    success: function(data){
    new Chart(document.getElementById("line-chart"), {
      type: 'line',
      data: { labels   : data.labels,
              datasets : data.items
      },
      options : {
        title : {
          display : true,
          text    : data.title }
    } } ); },
    
    error: function(error_data){
        console.log("error")
        console.log(error_data) } } )

Example 2: line chart using json django

from django.http import JsonResponse

def get_data(request, *args, **kwargs):
    title  = 'World population per region (in millions)'
    labels = [1500,1600,1700,1750,1800,1850,1900,1950,1999,2050]
    items  = [{ "data" : [86, 114, 106, 106, 107, 111, 133, 221, 783, 278],
               "label" : "Africa",
               "borderColor" : "#3e95cd",
               "fill" : False},

            { "data" : [282, 350, 411, 502, 635, 809, 947, 1402, 3700, 5267],
              "label" : "Asia",
              "borderColor" : "#8e5ea2",
              "fill" : False}, ]

    data = { "title" : title, 
             "labels": labels,
             "items" : items }

    return JsonResponse(data, safe=False)

Tags:

Misc Example