admin管理员组文章数量:1356575
Supposing that I will use line chart from ChartJS, and I have 'value by date' data. example:
5 : 05-11-2014
10 : 04-11-2014
55 : 01-11-2014
the chart will be contain 3 points.
is there any solution, configuration that let me add automatically add (dump) missing date with value = 0, so the data for the example will be :
5 : 05-11-2014
10 : 04-11-2014
0 : 03-11-2014
0 : 02-11-2014
55 : 01-11-2014
and of course the chart will contain 5 points.
I just need some logic to handle this kind of problem exactly when dealing with few points by whole year for example.
Supposing that I will use line chart from ChartJS, and I have 'value by date' data. example:
5 : 05-11-2014
10 : 04-11-2014
55 : 01-11-2014
the chart will be contain 3 points.
is there any solution, configuration that let me add automatically add (dump) missing date with value = 0, so the data for the example will be :
5 : 05-11-2014
10 : 04-11-2014
0 : 03-11-2014
0 : 02-11-2014
55 : 01-11-2014
and of course the chart will contain 5 points.
I just need some logic to handle this kind of problem exactly when dealing with few points by whole year for example.
Share Improve this question asked Nov 5, 2014 at 12:15 NITSNITS 1031 silver badge3 bronze badges1 Answer
Reset to default 9This can be done outside of the actual graph by pre-parsing you labels to check that they are sequential in date. In the example below i have made use of momentjs as it just makes date manipulation, formatting and parsing so much easier.
then it is just a case of when finding a date that is not followed by a squential date to add it in to the labels and also the graph data.
var graphData = [55, 10, 5],
labels = ["01-11-2014", "04-11-2014", "05-11-2014"];
for (var i = 0; i < labels.length; i++) {
//make sure we are not checking the last date in the labels array
if (i + 1 < labels.length) {
var date1 = moment(labels[i], "DD-MM-YYYY");
var date2 = moment(labels[i + 1], "DD-MM-YYYY");
//if the current date +1 is not the same as it's next neighbor we have to add in a new one
if (!date1.add(1, "days").isSame(date2)) {
//add the label
labels.splice(i + 1, 0, date1.format("DD-MM-YYYY"));
//add the data
graphData.splice(i + 1, 0, 0);
}
}
}
var ctx = document.getElementById("chart").getContext("2d");
var data = {
labels:labels,
datasets: [{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: graphData
}]
};
var myLineChart = new Chart(ctx).Line(data);
console.log(labels);
console.log(graphData);
<script src="//cdnjs.cloudflare./ajax/libs/moment.js/2.8.3/moment.min.js"></script>
<script src="https://raw.githack./leighquince/Chart.js/master/Chart.min.js"></script>
<canvas id="chart" width="500px"></canvas>
本文标签: javascriptdump missing date in data for chartjsStack Overflow
版权声明:本文标题:javascript - dump missing date in data for chartjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744001579a2573953.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论