大家好我正在尝试使用canvasJS绘制堆积条以跟随嵌套列表:
pepidstat=[[[2016, 61, 'Feb'], [2011, 367, 'Feb'], [2013, 83, 'Feb'], [2014, 89, 'Feb'], [2015, 106, 'Feb']], [[2016, 43, 'Jan'], [2011, 128, 'Jan'], [2013, 150, 'Jan'], [2014, 74, 'Jan'], [2015, 121, 'Jan']]]
我可行的javascript是:
<script type="text/javascript">
window.onload = function () {
var chartbar = new CanvasJS.Chart("chartContainerBar",
{
title:{
text: "Yearly Growth of Total Peptides"
},
animationEnabled: true,
axisX: {
interval: 1,
labelFontSize: 10,
lineThickness: 0
},
data:pepiddata
});
chartbar.render();
chartbar={};
}
var pepidstatlist = {{ pepidstat|safe }};
var pepiddata = [];var pepiddataSeries = { type: "stackedBar",showInLegend: "true" };
var pepidname= [];
var pepiddataPoints = [];
for (pi = 0; pi < pepidstatlist.length;pi++) {
piname = pepidstatlist[pi][0][2];
for (pj = 0; pj < pepidstatlist[pi].length;pj++) {
pidx=pepidstatlist[pi][pj][0];
pidy=pepidstatlist[pi][pj][1];
pepiddataPoints.push({
x:pidx,
y: pidy,
label: pidx,
name:piname,
toolTipContent: "{name}-{label}:{y}",
});
}
pepiddataSeries.name = piname; // here I want pass each month name
}
pepiddataSeries.dataPoints = pepiddataPoints;
pepiddata.push(pepiddataSeries);
</script>
此脚本工作正常,但我无法在数据系列中指定每个月的名称。你能否建议我在哪里修改我的脚本? 感谢
答案 0 :(得分:4)
您只是创建一个dataSeries。创建两个dataSeries并将其推送到数据。其他一切都很好。
pepidstat=[[[2011, 61, 'Feb'], [2012, 367, 'Feb'], [2013, 83, 'Feb'], [2014, 89, 'Feb'], [2015, 106, 'Feb']], [[2011, 43, 'Jan'], [2012, 128, 'Jan'], [2013, 150, 'Jan'], [2014, 74, 'Jan'], [2015, 121, 'Jan']]]
var pepidstatlist = pepidstat;
var pepiddata = [];
var pepidname= [];
for (pi = 0; pi < pepidstatlist.length;pi++) {
piname = pepidstatlist[pi][0][2]; //month
var pepiddataPoints = [];
for (pj = 0; pj < pepidstatlist[pi].length;pj++) {
pidx=pepidstatlist[pi][pj][0]; //Year
pidy=pepidstatlist[pi][pj][1]; //value
pepiddataPoints.push({
x:pidx,
y: pidy,
label: pidx,
name:piname,
toolTipContent: "{name}-{label}:{y}",
});
}
var pepiddataSeries = { type: "stackedBar", showInLegend: "true" };
pepiddataSeries.name = piname; // here I want pass each month name
pepiddataSeries.dataPoints = pepiddataPoints;
pepiddata.push(pepiddataSeries);
}
var chartbar = new CanvasJS.Chart("chartContainer",
{
title:{
text: "Yearly Growth of Total Peptides"
},
animationEnabled: true,
axisX: {
interval: 1,
labelFontSize: 10,
lineThickness: 0
},
data: pepiddata,
});
chartbar.render();
&#13;
<script src="http://canvasjs.com/assets/script/canvasjs.min.js"></script>
<br/><!-- Just so that JSFiddle's Result label doesn't overlap the Chart -->
<div id="chartContainer" style="height: 360px; width: 100%;"></div>
&#13;
提示:根据数组中的年份值对数组进行排序。