Browse Source

Simplification du chargement en déplaçant le fillInDate dans une méthode de Graph, à laquelle on transmet comment créer une date manquante.

pull/4/head
Matthieu Marcillaud 2 years ago
parent
commit
949a359a63
  1. 53
      javascript/spip_d3_graph.js
  2. 45
      prive/stats/visites.html

53
javascript/spip_d3_graph.js

@ -117,36 +117,45 @@ class Spip_d3_graph {
}
}
prepare_columns(data) {
const columns = [];
for (const [key, value] of Object.entries(data.meta.columns)) {
columns.push({key: key, label: value});
}
return columns;
}
/**
* Remplit les dates manquantes
*
* onEmpty est une fonction qui reçoit la date manquante,
* et doit retourner un objet décrivant l'élément vide
*/
fillInDates(meta, data, onEmpty) {
const currentDates = {};
const minDate = meta.start_date;
let currentDate = minDate;
const maxDate = meta.end_date;
data.forEach(d => {
currentDates[d.date] = d;
});
// loop data and fill in missing dates
const filledInDates = [];
while (currentDate < maxDate) {
if (currentDates[currentDate]) {
filledInDates.push(currentDates[currentDate]);
} else {
select_otherwise(element, tag, onempty) {
let selection = element.select(tag);
if (selection.empty()) {
if (typeof onempty === "function") {
selection = onempty(element);
filledInDates.push(onEmpty(currentDate));
}
currentDate = this.nextDate(currentDate, meta.unite, 1);
}
return selection;
return filledInDates;
}
select_otherwise_append(element, tag, onappend) {
let selection = element.select(tag);
if (selection.empty()) {
selection = element.append(tag);
if (typeof onappend === "function") {
onappend(selection);
}
prepare_columns(data) {
const columns = [];
for (const [key, value] of Object.entries(data.meta.columns)) {
columns.push({key: key, label: value});
}
return selection;
return columns;
}
update_table(data) {
if (this.inner.select('table').empty()) {
this.prepare_table();

45
prive/stats/visites.html

@ -25,49 +25,18 @@ function spip_d3_statistiques_create(id, options = {}) {
jQuery(id).data('graph', graph);
graph.set_dataLoader(data => {
// ici on peuple les dates manquantes du json
return new Promise((resolve, reject) => {
function fillInDates(meta, data){
// put current data hash for efficient retrieval
// determine min/max of data
const currentDates = {};
const minDate = meta.start_date;
let currentDate = minDate;
const maxDate = meta.end_date;
data.forEach(d => {
currentDates[d.date] = d;
});
// loop data and fill in missing dates
const filledInDates = [];
while (currentDate < maxDate) {
if (currentDates[currentDate]) {
filledInDates.push(currentDates[currentDate]);
} else {
filledInDates.push({"date": currentDate, "visites": 0});
}
currentDate = graph.nextDate(currentDate, meta.unite, 1);
}
return filledInDates;
}
data.data = fillInDates(data.meta, data.data);
resolve(data);
})
.then(data => {
graph.update_table(data);
spip_d3_statistiques_update_graph(id, data);
data.data = graph.fillInDates(data.meta, data.data, currentDate => {
return {"date": currentDate, "visites": 0};
});
graph.update_table(data);
spip_d3_statistiques_update_graph(id, data);
});
graph.loading_start();
Promise.resolve()
.then((d) => {
// charger la locale de date avant de créer les axes… sinon ils ne sont pas traduits
return graph.localize_d3_time(d);
})
// charger la locale de date avant de créer les axes… sinon ils ne sont pas traduits
.then(d => graph.localize_d3_time(d))
.then(() => {
graph.prepare_table(table_visible);
spip_d3_statistiques_prepare_graph(id, !table_visible);

Loading…
Cancel
Save