Commit 90c7b0e4bfaed50790388f822e67a552e4f8ce10
1 parent
67ab2300
Exists in
master
and in
4 other branches
Small comments and code reformat
Showing
2 changed files
with
31 additions
and
28 deletions
Show diff stats
app/main/routes.py
... | ... | @@ -134,10 +134,7 @@ def charge_project_csv(project_id, category): |
134 | 134 | @role_required('service') |
135 | 135 | def charge_agent_csv(agent_id): |
136 | 136 | csv_table = [] |
137 | - stacked_charges = db_mgr.charges_by_agent_stacked(agent_id) | |
138 | - print(stacked_charges) | |
139 | 137 | for line in db_mgr.charges_by_agent_stacked(agent_id): |
140 | - print(line) | |
141 | 138 | line = [cell.replace(",", "-") for cell in line] |
142 | 139 | line_string = ",".join(line) |
143 | 140 | csv_table.append(line_string) | ... | ... |
app/main/static/js/charges.js
1 | -function build_chart(div_selector, data_url, entity_name, category) { | |
1 | +function build_chart(div_selector, data_url, entity_name, category_type) { | |
2 | + | |
2 | 3 | const main_elt = document.getElementById("main") |
3 | 4 | |
4 | 5 | const margin = {top: 60, right: 350, bottom: 100, left: 90}, |
5 | 6 | width = main_elt.offsetWidth * 0.95 - margin.left - margin.right, |
6 | 7 | height = 500 - margin.top - margin.bottom; |
7 | 8 | |
8 | - const height_ratio = 1.2 | |
9 | + const height_ratio = 1.2; // how murch room to give above chart for lisibility | |
9 | 10 | |
10 | 11 | const tooltip_offset = {dx: 0, dy: 100} |
11 | 12 | const tooltip_offset_dot = {dx: 20, dy: 60} |
12 | 13 | |
14 | + const y_ticks_num = 5 // dont show to many y ticks for lisibility | |
13 | 15 | |
14 | - const y_ticks_num = 5 | |
15 | - | |
16 | - const legend_cell_size = 15; // colored scare size | |
16 | + const legend_cell_size = 15; // size of legend colored scare | |
17 | 17 | const legend_x = width + 20; // begin legend a bit after the end of the chart |
18 | 18 | |
19 | - const dot_radius = 6; | |
19 | + const dot_radius = 6; // size of total line dot | |
20 | 20 | |
21 | 21 | const xScale = d3.scaleBand() |
22 | 22 | .range([0, width]) |
23 | 23 | .padding(0.4); |
24 | 24 | |
25 | 25 | const yScale = d3.scaleLinear() |
26 | - .range([height, 0]) | |
26 | + .range([height, 0]); | |
27 | 27 | |
28 | - var colorScale = d3.scaleOrdinal([]) | |
28 | + var colorScale = d3.scaleOrdinal([]); // Will be really set later by category type | |
29 | 29 | |
30 | + | |
31 | + // | |
32 | + // Configure chart by the category given as arg | |
33 | + // | |
30 | 34 | var chart_title = "" |
31 | 35 | var category_title = "" |
32 | - var draw_total_line = function (data, categories) { } | |
33 | - var draw_categories_bars = function (data, categories) { } | |
36 | + var draw_total_line = () => void 0; | |
37 | + var draw_categories_bars = () => void 0; | |
34 | 38 | |
35 | - if (category == 'capacity') { | |
39 | + if (category_type == 'capacity') { | |
36 | 40 | chart_title = "Charges par fonction" |
37 | 41 | category_title = "Fonction" |
38 | 42 | //draw_total_line |
39 | 43 | draw_categories_bars = draw_categories_grouped |
40 | 44 | colorScale = d3.scaleOrdinal(d3.schemeSet3); |
41 | - } else if (category == 'service') { | |
45 | + } else if (category_type == 'service') { | |
42 | 46 | chart_title = "Charges par service" |
43 | 47 | category_title = "Service" |
44 | 48 | draw_total_line = draw_totalcharge_line |
45 | 49 | draw_categories_bars = draw_categories_stacked |
46 | 50 | colorScale = d3.scaleOrdinal(d3.schemeTableau10); |
47 | - } else if (category == 'project') { | |
51 | + } else if (category_type == 'project') { | |
48 | 52 | chart_title = "Charges par projet" |
49 | 53 | category_title = "Projet" |
50 | 54 | draw_categories_bars = draw_categories_stacked |
... | ... | @@ -208,14 +212,14 @@ function build_chart(div_selector, data_url, entity_name, category) { |
208 | 212 | |
209 | 213 | yScale.domain([0, y_max]); |
210 | 214 | |
211 | - // first one group for one category containing all bars over periods | |
215 | + // first draw one group for one category containing all bars over periods | |
212 | 216 | let groups = svg.selectAll("g.category") |
213 | 217 | .data(stacked_data) |
214 | 218 | .enter() |
215 | 219 | .append("g") |
216 | 220 | .style("fill", (d) => colorScale(d.key)); |
217 | 221 | |
218 | - // then each period/category bar | |
222 | + // then draw each period/category bar | |
219 | 223 | let rect = groups.selectAll("rect") |
220 | 224 | .data(d => d) |
221 | 225 | .enter() |
... | ... | @@ -282,14 +286,16 @@ function build_chart(div_selector, data_url, entity_name, category) { |
282 | 286 | |
283 | 287 | d3.csv(data_url).then(data => { |
284 | 288 | // we could get categories that way, |
285 | - // but we intend to filter by non zero, see later | |
289 | + // but instead we want to filter by non zero, see later | |
286 | 290 | // var categories = data.columns.slice(1) |
291 | + | |
287 | 292 | var periods = d3.map(data, d => d.period) |
288 | 293 | xScale.domain(periods) |
289 | 294 | |
290 | - // Filter datas to only keep non zero categori/s | |
295 | + // Filter datas to only keep non zero categories | |
296 | + // TODO: should be done on server (python/flask) side | |
291 | 297 | // |
292 | - // 1- Get the charge sum for each categories over periods | |
298 | + // 1- Get the charge sum for each category over periods | |
293 | 299 | // That will leave '0' to categories with no charge at all |
294 | 300 | // TODO: to be done with Object.keys, Object.values and d3 filtering methods |
295 | 301 | var categories_total_charge = {} |
... | ... | @@ -306,6 +312,7 @@ function build_chart(div_selector, data_url, entity_name, category) { |
306 | 312 | |
307 | 313 | // 2- Now, filter only categories that have a non-zero charge |
308 | 314 | // TODO: to be done with Object.keys, Object.values and d3 filtering methods |
315 | + // | |
309 | 316 | var categories = [] |
310 | 317 | for (var key in categories_total_charge) { |
311 | 318 | if (categories_total_charge[key] > 0) { |
... | ... | @@ -314,7 +321,7 @@ function build_chart(div_selector, data_url, entity_name, category) { |
314 | 321 | } |
315 | 322 | |
316 | 323 | // |
317 | - // Draw the bars, stacked or group | |
324 | + // Draw the bars, stacked or grouped | |
318 | 325 | // |
319 | 326 | draw_categories_bars(data, categories) |
320 | 327 | |
... | ... | @@ -334,12 +341,6 @@ function build_chart(div_selector, data_url, entity_name, category) { |
334 | 341 | // |
335 | 342 | const xAxis = d3.axisBottom(xScale) |
336 | 343 | |
337 | - // Yaxis | |
338 | - // | |
339 | - | |
340 | - const yAxis = d3.axisLeft(yScale) | |
341 | - .ticks(y_ticks_num) | |
342 | - | |
343 | 344 | // Draw Xaxis |
344 | 345 | svg.append("g") |
345 | 346 | .attr("class", "x axis") |
... | ... | @@ -351,6 +352,11 @@ function build_chart(div_selector, data_url, entity_name, category) { |
351 | 352 | .attr("dy", ".15em") |
352 | 353 | .attr("transform", "rotate(-65)"); |
353 | 354 | |
355 | + // Yaxis | |
356 | + // | |
357 | + const yAxis = d3.axisLeft(yScale) | |
358 | + .ticks(y_ticks_num) | |
359 | + | |
354 | 360 | // Draw Yaxis |
355 | 361 | svg.append("g") |
356 | 362 | .attr("class", "y axis") | ... | ... |