Python 高位图表不是从正确的月份开始的
我在django的项目中有一个图表,我需要显示一年中12个月的两个系列数据(预测和合并) 问题是,如果我的数据不是从一月开始的(例如),我仍然必须用空数据显示一月 这适用于其中一个系列(prvisiosns),但不适用于另一个系列,这意味着预测从2月份开始,但在图表中,预测从1月份开始,尽管其数据从2月份开始 请看一下我下面的代码,特别是{%block graph\u data%} 这是我的密码:Python 高位图表不是从正确的月份开始的,python,django,highcharts,django-templates,Python,Django,Highcharts,Django Templates,我在django的项目中有一个图表,我需要显示一年中12个月的两个系列数据(预测和合并) 问题是,如果我的数据不是从一月开始的(例如),我仍然必须用空数据显示一月 这适用于其中一个系列(prvisiosns),但不适用于另一个系列,这意味着预测从2月份开始,但在图表中,预测从1月份开始,尽管其数据从2月份开始 请看一下我下面的代码,特别是{%block graph\u data%} 这是我的密码: <script type="text/javascript"> $(document)
<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);
});
$(文档).ready(函数(){
{%if数据%}
var图表=新的Highcharts.图表({
图表:{
renderTo:'图形',
{%block graph_type%}defaultSeriesType:'列',{%endblock%}
marginRight:125,
marginBottom:25
},
标题:{
文本:“{%block graph_title%}集合{%endblock%}”,
x:-20/中心
},
xAxis:{
{%block graph_xaxis%}
类别:[{%fori-in-xaxis%}{{i | title}}{%if-not-forloop.last%},{%endif%}{%endfor%}]
{%endblock%},
},
亚克斯:{
标题:{
文本:“联合汇总(MWh)”
},
绘图线:[{
值:0,
宽度:1,
颜色:'#808080'
}]
},
{%块工具提示%}
工具提示:{
格式化程序:函数(){
返回“+this.series.name+”
+
this.x++{%block graph\工具提示_unite%}{%endblock%}:'+this.y.toFixed(2)+'MWh';
}
},
{%endblock%}
图例:{
布局:“垂直”,
对齐:“右”,
垂直排列:“顶部”,
x:20,
y:100,
边框宽度:0
},
系列:[
{%block graph_data%}
{
名称:'联合声明',
数据:[
{数据中h的百分比| dictsort:“date_umois”%}
{h.quantite | safe}}/1000
{%if不是forloop.last%},{%endif%}
{%endfor%}
]
}
{%if user.profil.is_gold%}
,{
名称:“Prévisions”,
数据:[
{预测中h的百分比| dictsort:“mois”%}
{{h.mwh | safe}}
{%if不是forloop.last%},{%endif%}
{%endfor%}
]
}
{%endif%}
{%endblock%}
]
});
{%else%}
{%if request.POST%}
$(“#graph').html('Aucune donneée disponable pour les critères séselection');
{%endif%}
{%endif%}
var blanco=“”;
$(“#图”)。追加(布兰科);
});
这让我一周来都快发疯了,但我知道问题出在哪里了,但这是我第一次使用highcharts。因此,非常感谢您的帮助,谢谢您在xAxis中定义您的类别
xAxis: {
categories: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
]
}
在序列数据中使用月份索引(以0为基数)而不是月份文字(在服务器或JS上都应该很容易做到)
使用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;
}
并将数据添加为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;
}
@您可以使用默认值为1月份添加一个虚拟数据。我无法确定该数据是动态的,因此可以从任何其他月份开始,甚至从1月份开始。我刚才就我的问题给出了一个示例
var months=[
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
];
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: {
categories: months
}