2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
2021-01-20 15:08:51 +01:00
|
|
|
* Copyright (C) DevCode s.r.l.
|
2020-09-07 15:04:06 +02:00
|
|
|
*
|
|
|
|
* 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);
|
2024-06-26 12:03:42 +02:00
|
|
|
var label = data.label;
|
2019-07-11 12:04:22 +02:00
|
|
|
|
|
|
|
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"));
|
|
|
|
});
|
|
|
|
|
2024-05-17 16:49:14 +02:00
|
|
|
while (chart.data.labels.length < data.results.length) {
|
|
|
|
chart.data.labels.push("");
|
2019-07-11 12:04:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2023-05-31 09:17:42 +02:00
|
|
|
color = color.substr(0, 7);
|
2019-07-11 12:04:22 +02:00
|
|
|
|
|
|
|
chart.data.datasets.splice(dataset_index, 1);
|
2024-05-17 16:49:14 +02:00
|
|
|
chart.data.labels.splice(dataset_index, 1);
|
2019-07-11 12:04:22 +02:00
|
|
|
}
|
|
|
|
|
2024-05-17 16:49:14 +02:00
|
|
|
while (chart.data.labels.length > max_length) {
|
|
|
|
chart.data.labels.pop("");
|
2019-07-11 12:04:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
chart.update();
|
|
|
|
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
}
|