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

115 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
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-05-16 18:02:50 +02:00
var label = data.badge;
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-16 18:02:50 +02:00
while (chart.data.badges.length < data.results.length) {
chart.data.badges.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;
color = color.substr(0, 7);
2019-07-11 12:04:22 +02:00
chart.data.datasets.splice(dataset_index, 1);
2024-05-16 18:02:50 +02:00
chart.data.badges.splice(dataset_index, 1);
2019-07-11 12:04:22 +02:00
}
2024-05-16 18:02:50 +02:00
while (chart.data.badges.length > max_length) {
chart.data.badges.pop("");
2019-07-11 12:04:22 +02:00
}
chart.update();
return color;
}
}