<div class="box-widget {{ widget.classes }}">
  {% if widget.name %}<h2 class="box-widget-name">{{ widget.name }}</h2>{% endif %}
  <div class="box-widget-content" id="{{ widget.html_id }}"></div>
  {% with chart_url='admin:'|add:widget.url_name %}
    <script>
      var dataset = {{ widget.content|safe }};
      {% for js_code in widget.content.js_code %}
        try {
          if (dataset.{{ js_code }}) {
            dataset.{{ js_code }} = new Function(dataset.{{ js_code }});
          }
        } catch (err) {
          console.log(err);
        }
      {% endfor %}

      var chart = $('#{{ widget.html_id }}').highcharts(dataset);

      $(function () {
        setInterval(function() {
          $.getJSON("{% url chart_url %}", function(refreshed_data) {
            chart.series.forEach(function(v, i) {
              v.setData(refreshed_data.series[i].data, false);
            });
            chart.redraw();
          });
        }, {{ widget.time_interval }});
      });
    </script>
  {% endwith %}
</div>
