C
C
Caeterra2019-05-12 17:08:00
JavaScript
Caeterra, 2019-05-12 17:08:00

How to transfer data from a database to Google Charts in Django?

Hello! I've been struggling all day, I can't figure out how to transfer values ​​from the database to the google charts?
data = table.objects.filter(title = key)
The table contains fields: ID (pk), name, number, date. I need to pass a date-number value to data.addRows
Google template looks like this:

google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawBasic);

function drawBasic() {

      var data = new google.visualization.DataTable();
      data.addColumn('number', 'X');
      data.addColumn('number', 'Dogs');

     data.addRows([
        [0, 0],   [1, 10],  [2, 23],  [3, 17],  [4, 18],  [5, 9],
        [6, 15],  [7, 27],  [8, 33],  [9, 40],  [10, 32], [11, 35],
        [12, 30], [13, 40], [14, 42], [15, 47], [16, 44], [17, 48],
        [18, 52], [19, 54], [20, 42], [21, 55], [22, 56], [23, 57],
        [24, 60], [25, 50], [26, 52], [27, 51], [28, 49], [29, 53],
        [30, 55]
      ]);

      var options = {
        axisTitlesPosition: 'none',
        chartArea:{left:30,top:20, right:10, bottom: 20},
        height: '260',
        width:'450',
        vAxis: {
          format: 'short'
        }
      };

      var chart = new google.visualization.LineChart(document.getElementById('chart_div'));

      chart.draw(data, options);
    }

I tried through
dump = serializers.serialize('python', data)
but the output is not possible to transfer it to JS Does
anyone have any thoughts?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Caeterra, 2019-05-13
@Caeterra

The question is removed, I figured it out myself:
you need to declare the array in the template itself

<script type="text/javascript">
var tmp = ({{data|safe}});
</script>

and the rest of Google Charts is connected in an external file

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question