1

I have declared t in my views. when I run my django application it shows a blank page but when I press ctrl+u I can see my y values are rendered correctly but my x values are rendered blank . my goal is to have in x the current time .

<script type="text/javascript">
$(function () {
    $(document).ready(function () {
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });

        $('#container').highcharts({
            chart: {
                type: 'spline',
                animation: Highcharts.svg, // don't animate in old IE
                marginRight: 10,
                events: {
                    load: function () {

                        // set up the updating of the chart each second
                       var series = this.series[0];
                        setInterval(function () {
                            var x = (new Date()).getTime(), // current time
                                y =  {{ t }} ;
                            series.addPoint([x, y], true, true);
                        }, 3000);
                    }
                }
            },
            title: {
                text: 'Live temperature sensor values'

            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150
            },
            yAxis: {
                title: {
                    text: 'Value'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function () {
                    return '<b>' + this.series.name + '</b><br/>' +
                        Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
                        Highcharts.numberFormat(this.y, 2);
                }
            },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: 'Sensor data',
                data: (function () {
                    // generate an array of sensor data
                    var data = [],
                        time = (new Date()).getTime(),


                      {% for item in t %}
                        data.push({
                         {% for i in 12 %}
                          {% if  t.i  == item %}
                            x: time + {{ i }} * 3000,
                          {% endif %}    
                         {% endfor %}
                            y: {{ item }}
                        });

                    {% endfor %}
                    return data;
                }())
            }]
        });
    });
});
        </script>

ctrl+u

<script type="text/javascript">
$(function () {
    $(document).ready(function () {
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });

        $('#container').highcharts({
            chart: {
                type: 'spline',
                animation: Highcharts.svg, // don't animate in old IE
                marginRight: 10,
                events: {
                    load: function () {

                        // set up the updating of the chart each second
                       var series = this.series[0];
                        setInterval(function () {
                            var x = (new Date()).getTime(), // current time
                                y =  [&#39;23.125&#39;, &#39;23.125&#39;, &#39;23.125&#39;, &#39;23.125&#39;, &#39;23.125&#39;, &#39;23.125&#39;, &#39;23.125&#39;, &#39;23.125&#39;, &#39;23.125&#39;, &#39;23.187&#39;, &#39;23.125&#39;, &#39;23.187&#39;] ;
                            series.addPoint([x, y], true, true);
                        }, 3000);
                    }
                }
            },
            title: {
                text: 'Live temperature sensor values'

            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150
            },
            yAxis: {
                title: {
                    text: 'Value'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function () {
                    return '<b>' + this.series.name + '</b><br/>' +
                        Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
                        Highcharts.numberFormat(this.y, 2);
                }
            },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: 'Sensor data',
                data: (function () {
                    // generate an array of sensor data
                    var data = [],
                        time = (new Date()).getTime(),
                     

                      
                        data.push({
                            x: time + 1 * 3000,
                            y: 23.125
                        });
                    
                    
                        data.push({
                            x: time + 2 * 3000,
                            y: 23.125
                        });
                    
                    
                        data.push({
                            x: time + 3 * 3000,
                            y: 23.125
                        });
                    
                    
                        data.push({
                            x: time + 4 * 3000,
                            y: 23.125
                        });
                    
                    
                        data.push({
                            x: time + 5 * 3000,
                            y: 23.125
                        });
                    
                    
                        data.push({
                            x: time + 6 * 3000,
                            y: 23.125
                        });
                    
                    
                        data.push({
                            x: time + 7 * 3000,
                            y: 23.125
                        });
                    
                    
                        data.push({
                            x: time + 8 * 3000,
                            y: 23.125
                        });
                    
                    
                        data.push({
                            x: time + 9 * 3000,
                            y: 23.125
                        });
                    
                    
                        data.push({
                            x: time + 10 * 3000,
                            y: 23.187
                        });
                    
                    
                        data.push({
                            x: time + 11 * 3000,
                            y: 23.125
                        });
                    
                    
                        data.push({
                            x: time + 12 * 3000,
                            y: 23.187
                        });
                    
                    
                    return data;
                }())
            }]
        });
    });
});
  </script>
bne
  • 141
  • 1
  • 1
  • 10
  • 1
    There are two obvious things wrong here, you can't do `for i in 12` and you can't lookup an index of an item via a variable (`t.i`), what are you actually trying to do here? – Sayse Apr 28 '16 at 14:39
  • I looked for command that returns a list element position but I didn't find . I tried to do it by my self and I failed . y values are rendered every 3 seconds and x is the current time – bne Apr 28 '16 at 14:47
  • I hope that this example explains what I want : t.0 = item so I will have x = time + 0 * 1000 and y = item. – bne Apr 28 '16 at 14:53
  • in that explanation aren't you always adding `item - 1` then? – Sayse Apr 28 '16 at 14:56
  • this is a part of my ctrl +u data.push({ x: time + 23.062 * 1000, y: 23.062 }); data.push({ x: time + 23.125 * 1000, y: 23.125 }); but what I want is x : time + 1 * 1000 when item is in the second position – bne Apr 28 '16 at 14:59
  • I've updated my answer. – Sayse Apr 28 '16 at 15:03

1 Answers1

0

I'm going to take a guess at what you were trying to do... you're trying to iterate over a list and when you find a value that equals item you want to show an x value, that looks equivalent to just adding item as the x

{% for item in t %}
  data.push({ x: time + {{ item }} * 3000,

time + 1 * 1000 when item is in the second position

You can just use forloop.counter for that

  data.push({ x: time + {{ forloop.counter }} * 3000,

As for reference as to why you can't do the other two things:

iterate over a range - Numeric for loop in Django templates

access array element via variable - How can I use a variable as index in django template?

Community
  • 1
  • 1
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • for example t.0 = item so I will have x = time + 0 * 1000 and y = item. But what you have proposed returns x = time + item *1000 and y = item . thank you but this is not my goal – bne Apr 28 '16 at 14:52