function newFilledArray(len, val) {
var rv = new Array(len);
while (--len >= 0) {
rv[len] = val;
}
return rv;
}
data = JSON.parse('[{"project_ref_id":"479","project_title":"Environment project","estimated_project_cost":"0","amount":"0","months":"Dec-2016"},{"project_ref_id":"479","project_title":"Environment project","estimated_project_cost":"0","amount":"0","months":"Jan-2017"},{"project_ref_id":"479","project_title":"Environment project","estimated_project_cost":"0","amount":"0","months":"Feb-2017"},{"project_ref_id":"479","project_title":"Environment project","estimated_project_cost":"","amount":"85964710","months":"Mar-2017"},{"project_ref_id":"479","project_title":"Environment project","estimated_project_cost":"0","amount":"0","months":"Apr-2017"},{"project_ref_id":"479","project_title":"Environment project","estimated_project_cost":"0","amount":"0","months":"May-2017"}]'),
months1 = data.reduce(function(p, c) {
return ~p.indexOf(c.months) ? p : p.concat(c.months)
}, []),
series = data.reduce(function(p, c) {
var f = undefined;
console.log(p);
p.map(function(x) {
if (x.name == c.project_title) {
f = x;
}
});
!!f ? f.data[months1.indexOf(c.months)] = c.amount * 1 :
p.push({
name: c.project_title,
id: c.project_title,
data: (
Array.apply(null, new Array(months1.length)).map(Number.prototype.valueOf, 0)
.map(
function(e, i) {
return i === months1.indexOf(c.months) ? c.amount * 1 : e
}
))
});
return p;
}, []);
var chart1 = new Highcharts.Chart({
chart: {
renderTo: 'container1'
},
xAxis: {
categories: months1,
title: {
text: 'Months'
}
},
yAxis: {
min: 0,
title: {
text: 'Rupees'
}
},
title: {
text: ''
},
credits: {
enabled: false
},
plotOptions: {
series: {
events: {
legendItemClick: function(event) {
var selected = this.index;
var allSeries = this.chart.series;
$.each(allSeries, function(index, series) {
selected == index ? series.show() : series.hide();
});
return false;
}
}
}
},
series: series,
}, function(chart1) {
$.each(chart1.series, function(i, serie) {
var pname = serie.name;
var pjname = pname.replace(/([[email protected]#$%^&*()_+=`{}\[\]\|\\:;'<>,.\/? ])+/g, '').replace(/^(-)+|(-)+$/g, '');
var chartcolor = $('#container1 .highcharts-series-' + i + ' path').attr('stroke');
if (chartcolor == 'rgba(192,192,192,0.0001)') {
chartcolor = '#7cb5ec';
}
chart1.series[i].graph.attr({
stroke: chartcolor
});
var $customLegend = $('.prj-label1' + pjname).css('background-color', chartcolor);
});
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container1" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
_「任何人都可以請給這個解決方案」_嗯......只需使用一個普通的舊函數(){...} Oo – Andreas
爲什麼不嘗試使用babel – user93
我是新來的jquery 。這是我在谷歌找到的代碼。我試圖重寫,但我無法將其轉換爲舊功能。你能幫我 –