Commit 83d933c9b17dc867bf1e1d9aa855e7d2a11e4efc
1 parent
a48677c4
Exists in
master
and in
4 other branches
New draw_categories_grouped() method called on capacities
Showing
1 changed file
with
91 additions
and
48 deletions
Show diff stats
app/main/static/js/charges.js
... | ... | @@ -10,7 +10,7 @@ const height_ratio = 1.2 |
10 | 10 | const tooltip_offset = {dx: 0, dy: 100} |
11 | 11 | const tooltip_offset_dot = {dx: 20, dy: 60} |
12 | 12 | |
13 | -const color_scale = d3.scaleOrdinal(d3.schemeCategory10); | |
13 | +const colorScale = d3.scaleOrdinal(d3.schemeCategory10); | |
14 | 14 | |
15 | 15 | const y_ticks_num = 5 |
16 | 16 | |
... | ... | @@ -37,16 +37,20 @@ const yScale = d3.scaleLinear() |
37 | 37 | |
38 | 38 | function build_chart(div_selector, data_url, project_name, category) { |
39 | 39 | |
40 | + var chart_title = "" | |
41 | + var category_title = "" | |
42 | + var draw_total_line = function(data, categories){} | |
43 | + var draw_categories_bars = function(data, categories){} | |
40 | 44 | if (category == 'capacity') { |
41 | - var chart_title = "Charges par fonction" | |
42 | - var category_title = "Fonction" | |
43 | - var draw_total_line = function (data) { } | |
44 | - var draw_categories_bars = draw_categories_grouped | |
45 | + chart_title = "Charges par fonction" | |
46 | + category_title = "Fonction" | |
47 | + //draw_total_line | |
48 | + draw_categories_bars = draw_categories_grouped | |
45 | 49 | } else if (category == 'service') { |
46 | - var chart_title = "Charges par service" | |
47 | - var category_title = "Service" | |
48 | - var draw_total_line = draw_totalcharge_line | |
49 | - var draw_categories_bars = draw_categories_stacked | |
50 | + chart_title = "Charges par service" | |
51 | + category_title = "Service" | |
52 | + draw_total_line = draw_totalcharge_line | |
53 | + draw_categories_bars = draw_categories_stacked | |
50 | 54 | } else { |
51 | 55 | alert("ALERT ! Every body shall quit the boat, we are sinking ! ALERT !") |
52 | 56 | } |
... | ... | @@ -136,18 +140,72 @@ function build_chart(div_selector, data_url, project_name, category) { |
136 | 140 | .text(d => d); |
137 | 141 | } |
138 | 142 | |
139 | - function draw_categories_grouped(data){ | |
140 | - // do nothing at the moment | |
141 | - console.log("HEY grouped ") | |
143 | + function draw_categories_grouped(data, categories) { | |
144 | + | |
145 | + // Another scale for subgroup position | |
146 | + var xCategories = d3.scaleBand() | |
147 | + .domain(categories) | |
148 | + .range([0, xScale.bandwidth()]) | |
149 | + .padding([0.05]) | |
150 | + | |
151 | + svg.append("g") | |
152 | + .selectAll("g") | |
153 | + // Enter in data = loop group per group | |
154 | + .data(data) | |
155 | + .enter() | |
156 | + .append("g") | |
157 | + .attr("transform", function (d) { | |
158 | + return "translate(" + xScale(d.period) + ",0)"; | |
159 | + }) | |
160 | + .selectAll("rect") | |
161 | + .data(function (d) { | |
162 | + return categories.map(function (key) { | |
163 | + return {key: key, value: d[key]}; | |
164 | + }); | |
165 | + }) | |
166 | + .enter().append("rect") | |
167 | + .attr("x", function (d) { | |
168 | + return xCategories(d.key); | |
169 | + }) | |
170 | + .attr("y", function (d) { | |
171 | + return yScale(d.value); | |
172 | + }) | |
173 | + .attr("width", xCategories.bandwidth()) | |
174 | + .attr("height", function (d) { | |
175 | + return height - yScale(d.value); | |
176 | + }) | |
177 | + .attr("fill", function (d) { | |
178 | + return colorScale(d.key); | |
179 | + }); | |
180 | + | |
142 | 181 | } |
143 | 182 | |
144 | - function draw_categories_stacked(stacked_data) { | |
183 | + function draw_categories_stacked(data, categories) { | |
184 | + | |
185 | + // Now build the stacked data for stacked bars | |
186 | + // | |
187 | + var stack = d3.stack() | |
188 | + .keys(categories) | |
189 | + // .order(d3.stackOrderNone) | |
190 | + // .offset(d3.stackOffsetNone); | |
191 | + var stacked_data = stack(data) | |
192 | + | |
193 | + // Get the max y to plot | |
194 | + var y_max = d3.max(stacked_data[stacked_data.length - 1], d => d[1]); | |
195 | + if (y_max == 0) { | |
196 | + y_max = 100 | |
197 | + } | |
198 | + // Enhance it by %ratio to leave room on the top of the graph | |
199 | + y_max = y_max * height_ratio | |
200 | + | |
201 | + yScale.domain([0, y_max]); | |
202 | + | |
145 | 203 | // first one group for one category containing all bars over periods |
146 | 204 | let groups = svg.selectAll("g.category") |
147 | 205 | .data(stacked_data) |
148 | 206 | .enter() |
149 | 207 | .append("g") |
150 | - .style("fill", (d) => color_scale(d.key)); | |
208 | + .style("fill", (d) => colorScale(d.key)); | |
151 | 209 | |
152 | 210 | // then each period/category bar |
153 | 211 | let rect = groups.selectAll("rect") |
... | ... | @@ -191,7 +249,8 @@ function build_chart(div_selector, data_url, project_name, category) { |
191 | 249 | svg.append("path") |
192 | 250 | .datum(periods_total_charge) |
193 | 251 | .attr("class", "total-line") |
194 | - .attr("d", line); | |
252 | + .attr("d", line) | |
253 | + .lower();// set it below bars if called after | |
195 | 254 | |
196 | 255 | // data dots at each point |
197 | 256 | // |
... | ... | @@ -207,7 +266,8 @@ function build_chart(div_selector, data_url, project_name, category) { |
207 | 266 | return yScale(d.total); |
208 | 267 | }) |
209 | 268 | .on("mouseover", mouseoverdot) |
210 | - .on("mouseleave", mouseleavedot); | |
269 | + .on("mouseleave", mouseleavedot) | |
270 | + .lower();// set it below bars if called after | |
211 | 271 | ; |
212 | 272 | |
213 | 273 | } |
... | ... | @@ -243,19 +303,22 @@ function build_chart(div_selector, data_url, project_name, category) { |
243 | 303 | } |
244 | 304 | } |
245 | 305 | |
246 | - // This former list we use as color_scale domain. | |
247 | - // And that allows us to build the legend | |
248 | 306 | // |
249 | - color_scale.domain(categories) | |
250 | - addlegend(color_scale) | |
307 | + // Draw the bars, stacked or group | |
308 | + // | |
309 | + draw_categories_bars(data, categories) | |
251 | 310 | |
252 | - // Now build the stacked data for stacked bars | |
253 | 311 | // |
254 | - var stack = d3.stack() | |
255 | - .keys(categories) | |
256 | - // .order(d3.stackOrderNone) | |
257 | - // .offset(d3.stackOffsetNone); | |
258 | - var stacked_data = stack(data) | |
312 | + // Draw the total charge line ( may be) | |
313 | + // | |
314 | + draw_total_line(data, categories) | |
315 | + | |
316 | + // | |
317 | + // This former list we use as color_scale domain. | |
318 | + // And that allows us to build the legend | |
319 | + // | |
320 | + colorScale.domain(categories) | |
321 | + addlegend(colorScale) | |
259 | 322 | |
260 | 323 | // Xaxis |
261 | 324 | // |
... | ... | @@ -263,15 +326,6 @@ function build_chart(div_selector, data_url, project_name, category) { |
263 | 326 | |
264 | 327 | // Yaxis |
265 | 328 | // |
266 | - // Get the max y to plot | |
267 | - var y_max = d3.max(stacked_data[stacked_data.length - 1], d => d[1]); | |
268 | - if (y_max == 0) { | |
269 | - y_max = 100 | |
270 | - } | |
271 | - // Enhance it by %ratio to leave room on the top of the graph | |
272 | - y_max = y_max * height_ratio | |
273 | - | |
274 | - yScale.domain([0, y_max]); | |
275 | 329 | |
276 | 330 | const yAxis = d3.axisLeft(yScale) |
277 | 331 | .ticks(y_ticks_num) |
... | ... | @@ -302,6 +356,7 @@ function build_chart(div_selector, data_url, project_name, category) { |
302 | 356 | .attr("x2", width) |
303 | 357 | .attr("y1", d => yScale(d)) |
304 | 358 | .attr("y2", d => yScale(d)) |
359 | + .lower();// set it below bars if called after | |
305 | 360 | |
306 | 361 | // Write Y axis title |
307 | 362 | svg.append("text") |
... | ... | @@ -309,7 +364,7 @@ function build_chart(div_selector, data_url, project_name, category) { |
309 | 364 | .attr("transform", "rotate(-90)") |
310 | 365 | .attr("y", -margin.left + 40) |
311 | 366 | .attr("x", -margin.top - 70) |
312 | - .text("Charge (% ETP)") | |
367 | + .text("Charge (% ETP)"); | |
313 | 368 | |
314 | 369 | // |
315 | 370 | // Write chart Title |
... | ... | @@ -330,18 +385,6 @@ function build_chart(div_selector, data_url, project_name, category) { |
330 | 385 | .style("font-size", "12px") |
331 | 386 | .text(chart_title); |
332 | 387 | |
333 | - // | |
334 | - // Draw the total charge line | |
335 | - // | |
336 | - | |
337 | - draw_total_line(data) | |
338 | - | |
339 | - // | |
340 | - // Draw the stacked bars | |
341 | - // | |
342 | - | |
343 | - draw_categories_bars(stacked_data) | |
344 | - | |
345 | 388 | |
346 | 389 | }); // end of d3.csv().then({}) |
347 | 390 | ... | ... |