openstamanager/modules/statistiche/js/stats/line_chart.js

114 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.n.c.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2019-07-11 12:04:22 +02:00
class LineChart extends Stat {
constructor(calendar, file, data = {}, chart){
super(calendar, file, data);
this.chart = chart;
}
add(start, end, color = null) {
var calendar_id = this.calendar.id;
var chart = this.chart;
var color = color ? color : this.getRandomColor();
this.getData(start, end, function(response) {
var data = JSON.parse(response);
var label = data.label;
var dataset = [];
var labels = [];
data.results.forEach(function (item) {
dataset.push(item.result);
var date = moment().month(item.month - 1).year(item.year);
labels.push(date.format("MMMM YYYY"));
});
while (chart.data.labels.length < data.results.length) {
chart.data.labels.push("");
}
chart.data.datasets.push({
calendar_id: calendar_id,
label: label,
2020-07-20 09:15:44 +02:00
backgroundColor: color + '55',
2019-07-11 12:04:22 +02:00
borderColor: color,
data: dataset,
labels: labels,
2020-07-20 09:15:44 +02:00
fill: true,
2019-07-11 12:04:22 +02:00
});
chart.update();
});
}
update(start, end) {
var color = this.remove();
this.add(start, end, color);
}
getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
remove() {
var chart = this.chart;
var calendar_id = this.calendar.id;
var max_length = 0;
var dataset_index = undefined;
chart.data.datasets.forEach(function (item, index) {
if(item.calendar_id == calendar_id) {
dataset_index = index;
} else {
if (item.data.length > max_length) {
max_length = item.data.length;
}
}
});
var color = null;
if (dataset_index != undefined) {
color = chart.data.datasets[dataset_index].backgroundColor;
chart.data.datasets.splice(dataset_index, 1);
chart.data.labels.splice(dataset_index, 1);
}
while (chart.data.labels.length > max_length) {
chart.data.labels.pop("");
}
chart.update();
return color;
}
}