【问题标题】:high chart graph doesnt start from the correct month高图表图表不是从正确的月份开始
【发布时间】:2012-08-16 10:52:15
【问题描述】:

我在基于 django 的项目中有一个图表,我需要在其中显示一年中十二个月的两个系列数据(预测和确认)

问题是当我的数据不是从一月开始时(例如),我仍然必须用空数据显示一月

这适用于其中一个系列 (prvisiosns),但不适用于其他含义,即预测应该从 2 月开始,但图表中的消费从 1 月开始,尽管它的数据是从 2 月开始的

请看一下我下面的代码,特别是 {% block graph_data %}

这是我的代码:

<script type="text/javascript">
$(document).ready(function() {
   {% if data %}
   var chart = new Highcharts.Chart({
      chart: {
         renderTo: 'graph',
         {% block graph_type %}defaultSeriesType: 'column',{% endblock %}
         marginRight: 125,
         marginBottom: 25
      },
      title: {
         text: '{% block graph_title %}Consommation{% endblock %}',
         x: -20 //center
      },
      xAxis: {
         {% block graph_xaxis %}
categories: [{% for i in xaxis %}'{{ i|title }}'{% if not forloop.last %},{% endif %}{% endfor %}]
{% endblock %},
      },
      yAxis: {
         title: {
            text: 'Consommation (MWh)'
         },
         plotLines: [{
            value: 0,
            width: 1,
            color: '#808080'
         }]
      },
      {% block tooltip %}
      tooltip: {
         formatter: function() {
                   return '<b>'+ this.series.name +'</b><br/>'+
               this.x +'{% block graph_tooltip_unite %}{% endblock %}: '+ this.y.toFixed(2) +' MWh';
         }
      },
      {% endblock %}
      legend: {
         layout: 'vertical',
         align: 'right',
         verticalAlign: 'top',
         x: 20,
         y: 100,
         borderWidth: 0
      },
      series: [


  {% block graph_data %}
 {
 name: 'Consommations',
 data: [
        {% for h in data|dictsort:"date__mois" %}
            {{ h.quantite|safe }}/1000
            {% if not forloop.last %},{% endif %}
        {% endfor %}
       ]
  }
     {% if user.profil.is_gold %}
  ,{
 name: 'Prévisions',
 data: [
        {% for h in previsions|dictsort:"mois" %}
            {{ h.mwh|safe }}
            {% if not forloop.last %},{% endif %}
        {% endfor %}
       ]
  }
 {% endif %}
{% endblock %}


      ]

});
{% else %}
    {% if request.POST %}
        $('#graph').html('<div class="aucune_donnee">Aucune donnée disponible pour les critères sélectionnés.</div>');
    {% endif %}
{% endif %}
var blanco = "<div class='blanco'></div>";
$("#graph").append(blanco);
});

一周以来这让我发疯,但我可以看出问题所在,但这是我第一次使用 highcharts..所以任何帮助将不胜感激,谢谢

【问题讨论】:

  • 您可以为一月添加一个默认值的虚拟数据。
  • 我不能数据是动态的,所以它可以从任何其他月份开始,甚至从一月份开始......我只是举了一个关于我的问题的例子

标签: python django highcharts django-templates


【解决方案1】:

在 xAxis 中定义您的类别

 xAxis: {
            categories: [
                'Jan',
                'Feb',
                'Mar',
                'Apr',
                'May',
                'Jun',
                'Jul',
                'Aug',
                'Sep',
                'Oct',
                'Nov',
                'Dec'
            ]
        }

在您的系列数据中使用月份索引(以 0 为底)而不是月份文字(应该很容易做到,无论是在服务器上还是在 JS 上)

 series: [{
             name: 'Consommations',
            data: normalizeData([[1,5],[9,10],[5,0]])

        }, {
            name: 'Prévisions',
            data: normalizeData([[1,2],[2,4],[5,8]])

        }]

使用 javascript 规范化您的数据以添加缺失月份的数据

function normalizeData(data){
  var normalizedData=[];
  var i;
  for(i=0;i<12;i++){
    if(data[i])
        normalizedData[i]=data[i];
    else
        normalizedData[i]=0;
  }
  return normalizedData;
}

jsFiddle 解决方案:http://jsfiddle.net/jugal/zDnS2/

替代解决方案 在 normalizeData 方法中添加缺失的月份数据
只需创建月份数组来表示可能的月份

var months=[
                'Jan',
                'Feb',
                'Mar',
                'Apr',
                'May',
                'Jun',
                'Jul',
                'Aug',
                'Sep',
                'Oct',
                'Nov',
                'Dec'
            ];

在数据不存在的地方添加数据为0

function normalizeData(data){
var existingMonths=[];
var normalizedData=[];
var i;
for(i=0;i<data.length;i++){
    var datum=data[i];
    datum[0]=  $.inArray(datum[0],months);
    existingMonths.push(datum[0]);
    normalizedData.push(datum);
}
for(i=0;i< months.length;i++){
    if($.inArray(i,existingMonths)==-1)
        normalizedData.push([i ,0]);
}
return normalizedData;
}

将 xAxis 类别设置为月份数组

 xAxis: {
            categories: months
        }

@http://jsfiddle.net/jugal/vYK7H/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多