如何从数组创建 morris.js 图表?
How to create a morris.js chart from array?
我在从数组获取数据以在 morris.js
图表中正确显示时遇到问题。
正如您从该片段中看到的那样,图表上仅输出了最后一个对象:
示例:
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var dates = ['2015-01','2016-01','2016-03'];
var valueA = ['10','30','60'];
var valueB = ['100','50','70'];
var z=0;
for (tot=dates.length; z < tot; z++) {
var myArray = [{'d': dates[z], 'a': valueA[z], 'b': valueB[z]}];
}
Morris.Bar({
element: 'morris-bar-chart',
data: myArray,
xkey: 'd',
ykeys: ['a', 'b'],
labels: ['2014', '2015'],
xLabelFormat: function (x) { // <-- changed
console.log("this is the new object:" + x);
var month = months[x.x];
return month;
},
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://cdn.oesmith.co.uk/morris-0.4.3.min.css" rel="stylesheet" />
<script src="http://cdn.oesmith.co.uk/morris-0.5.0.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<div id="morris-bar-chart" style="height: 250px;"></div>
我的数组 myArray
在 for 循环中有 3 个对象,那么为什么 morris.js
只输出图表中的最后一个?
请试试这个:
//......
var myArray = [];
for (tot=dates.length; z < tot; z++) {
myArray.push({'d': dates[z], 'a': valueA[z], 'b': valueB[z]});
}
//......
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var dates = ['2015-01','2016-01','2016-03'];
var valueA = ['10','30','60'];
var valueB = ['100','50','70'];
var z=0;
var myArray = [];
for (tot=dates.length; z < tot; z++) {
myArray.push({'d': dates[z], 'a': valueA[z], 'b': valueB[z]});
}
Morris.Bar({
element: 'morris-bar-chart',
data: myArray,
xkey: 'd',
ykeys: ['a', 'b'],
labels: ['2014', '2015'],
xLabelFormat: function (x) { // <-- changed
console.log("this is the new object:" + x);
var month = months[x.x];
return month;
},
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://cdn.oesmith.co.uk/morris-0.4.3.min.css" rel="stylesheet" />
<script src="http://cdn.oesmith.co.uk/morris-0.5.0.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<div id="morris-bar-chart" style="height: 250px;"></div>
我在从数组获取数据以在 morris.js
图表中正确显示时遇到问题。
正如您从该片段中看到的那样,图表上仅输出了最后一个对象:
示例:
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var dates = ['2015-01','2016-01','2016-03'];
var valueA = ['10','30','60'];
var valueB = ['100','50','70'];
var z=0;
for (tot=dates.length; z < tot; z++) {
var myArray = [{'d': dates[z], 'a': valueA[z], 'b': valueB[z]}];
}
Morris.Bar({
element: 'morris-bar-chart',
data: myArray,
xkey: 'd',
ykeys: ['a', 'b'],
labels: ['2014', '2015'],
xLabelFormat: function (x) { // <-- changed
console.log("this is the new object:" + x);
var month = months[x.x];
return month;
},
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://cdn.oesmith.co.uk/morris-0.4.3.min.css" rel="stylesheet" />
<script src="http://cdn.oesmith.co.uk/morris-0.5.0.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<div id="morris-bar-chart" style="height: 250px;"></div>
我的数组 myArray
在 for 循环中有 3 个对象,那么为什么 morris.js
只输出图表中的最后一个?
请试试这个:
//......
var myArray = [];
for (tot=dates.length; z < tot; z++) {
myArray.push({'d': dates[z], 'a': valueA[z], 'b': valueB[z]});
}
//......
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var dates = ['2015-01','2016-01','2016-03'];
var valueA = ['10','30','60'];
var valueB = ['100','50','70'];
var z=0;
var myArray = [];
for (tot=dates.length; z < tot; z++) {
myArray.push({'d': dates[z], 'a': valueA[z], 'b': valueB[z]});
}
Morris.Bar({
element: 'morris-bar-chart',
data: myArray,
xkey: 'd',
ykeys: ['a', 'b'],
labels: ['2014', '2015'],
xLabelFormat: function (x) { // <-- changed
console.log("this is the new object:" + x);
var month = months[x.x];
return month;
},
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://cdn.oesmith.co.uk/morris-0.4.3.min.css" rel="stylesheet" />
<script src="http://cdn.oesmith.co.uk/morris-0.5.0.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<div id="morris-bar-chart" style="height: 250px;"></div>