Commit 6b44860dcd510d1afc7d6a4f5d123c0b5a4494c7
1 parent
01429777
Exists in
master
and in
4 other branches
Make a draw_line method to run only when needed
Showing
1 changed file
with
64 additions
and
43 deletions
Show diff stats
app/main/static/js/charges.js
... | ... | @@ -27,15 +27,27 @@ const dot_radius = 6; |
27 | 27 | // const y = d3.scaleLinear() |
28 | 28 | // .range([height, 0]); |
29 | 29 | // |
30 | +// x scale from the periods list | |
31 | +const xScale = d3.scaleBand() | |
32 | + .range([0, width]) | |
33 | + .padding(0.2); | |
34 | + | |
35 | +const yScale = d3.scaleLinear() | |
36 | + .range([height, 0]) | |
30 | 37 | |
31 | 38 | function build_chart(div_selector, data_url, project_name, category) { |
32 | 39 | |
33 | 40 | if (category == 'capacity') { |
34 | 41 | var chart_title = "Charges par fonction" |
35 | 42 | var category_title = "Fonction" |
43 | + var draw_total_line = function(data){ //do nothing | |
44 | + } | |
36 | 45 | } else if (category == 'service') { |
37 | 46 | var chart_title = "Charges par service" |
38 | 47 | var category_title = "Service" |
48 | + var draw_total_line = draw_totalcharge_line | |
49 | + } else { | |
50 | + alert("ALERT ! Every body shall quit the boat, we are sinking ! ALERT !") | |
39 | 51 | } |
40 | 52 | |
41 | 53 | const svg = d3.select(div_selector).append("svg") |
... | ... | @@ -82,6 +94,7 @@ function build_chart(div_selector, data_url, project_name, category) { |
82 | 94 | .attr("r", dot_radius); |
83 | 95 | mouseleave(d); |
84 | 96 | } |
97 | + | |
85 | 98 | var mouseoverdot = function (e, d) { |
86 | 99 | d3.select(this).transition() |
87 | 100 | .duration(1) |
... | ... | @@ -122,15 +135,12 @@ function build_chart(div_selector, data_url, project_name, category) { |
122 | 135 | .text(d => d); |
123 | 136 | } |
124 | 137 | |
125 | - d3.csv(data_url).then(data => { | |
126 | - // var categories = data.columns.slice(1) | |
127 | - var periods = d3.map(data, d => d.period) | |
128 | - var categories_total_charge = {} | |
129 | - var periods_total_charge = [] | |
130 | - | |
131 | - // First, we build the total charge by period; | |
138 | + function draw_totalcharge_line(data) { | |
139 | + // Build the total charge by period; | |
132 | 140 | // it will be used for the line drawing above bars. |
133 | 141 | // |
142 | + var periods_total_charge = [] | |
143 | + | |
134 | 144 | data.forEach(function (d) { |
135 | 145 | // get the list of values for all columns except the first one which is the period name |
136 | 146 | var period_values = Object.values(d).slice(1) |
... | ... | @@ -140,11 +150,48 @@ function build_chart(div_selector, data_url, project_name, category) { |
140 | 150 | periods_total_charge.push(row) |
141 | 151 | }); |
142 | 152 | |
143 | - // Then we filter datas to only keep non zero categori/s | |
153 | + // the line itselet | |
154 | + // | |
155 | + const line = d3.line() | |
156 | + .x(d => (xScale.bandwidth() / 2) + xScale(d.period)) // décalage pour centrer au milieu des barres | |
157 | + .y(d => yScale(d.total)) | |
158 | + .curve(d3.curveMonotoneX); // Fonction de courbe permettant de l'adoucir | |
159 | + | |
160 | + svg.append("path") | |
161 | + .datum(periods_total_charge) | |
162 | + .attr("class", "total-line") | |
163 | + .attr("d", line); | |
164 | + | |
165 | + // data dots at each point | |
166 | + // | |
167 | + svg.selectAll("line-circle") | |
168 | + .data(periods_total_charge) | |
169 | + .enter().append("circle") | |
170 | + .attr("class", "total-circle") | |
171 | + .attr("r", dot_radius) | |
172 | + .attr("cx", function (d) { | |
173 | + return (xScale.bandwidth() / 2) + xScale(d.period) | |
174 | + }) | |
175 | + .attr("cy", function (d) { | |
176 | + return yScale(d.total); | |
177 | + }) | |
178 | + .on("mouseover", mouseoverdot) | |
179 | + .on("mouseleave", mouseleavedot); | |
180 | + ; | |
181 | + | |
182 | + } | |
183 | + | |
184 | + d3.csv(data_url).then(data => { | |
185 | + // var categories = data.columns.slice(1) | |
186 | + var periods = d3.map(data, d => d.period) | |
187 | + xScale.domain(periods) | |
188 | + | |
189 | + // Filter datas to only keep non zero categori/s | |
144 | 190 | // |
145 | 191 | // 1- Get the charge sum for each categories over periods |
146 | 192 | // That will leave '0' to categories with no charge at all |
147 | 193 | // |
194 | + var categories_total_charge = {} | |
148 | 195 | data.forEach(function (d) { |
149 | 196 | Object.keys(d).forEach(function (k) { |
150 | 197 | if (categories_total_charge.hasOwnProperty(k)) { |
... | ... | @@ -181,12 +228,6 @@ function build_chart(div_selector, data_url, project_name, category) { |
181 | 228 | |
182 | 229 | // Xaxis |
183 | 230 | // |
184 | - // x scale from the periods list | |
185 | - const xScale = d3.scaleBand() | |
186 | - .domain(periods) | |
187 | - .range([0, width]) | |
188 | - .padding(0.2); | |
189 | - | |
190 | 231 | const xAxis = d3.axisBottom(xScale) |
191 | 232 | |
192 | 233 | // Yaxis |
... | ... | @@ -199,9 +240,7 @@ function build_chart(div_selector, data_url, project_name, category) { |
199 | 240 | // Enhance it by %ratio to leave room on the top of the graph |
200 | 241 | y_max = y_max * height_ratio |
201 | 242 | |
202 | - const yScale = d3.scaleLinear() | |
203 | - .range([height, 0]) | |
204 | - .domain([0, y_max]); | |
243 | + yScale.domain([0, y_max]); | |
205 | 244 | |
206 | 245 | const yAxis = d3.axisLeft(yScale) |
207 | 246 | .ticks(y_ticks_num) |
... | ... | @@ -241,7 +280,10 @@ function build_chart(div_selector, data_url, project_name, category) { |
241 | 280 | .attr("x", -margin.top - 70) |
242 | 281 | .text("Charge (% ETP)") |
243 | 282 | |
283 | + // | |
244 | 284 | // Write chart Title |
285 | + // | |
286 | + | |
245 | 287 | // part 1 |
246 | 288 | svg.append("text") |
247 | 289 | .attr("x", (width / 2)) |
... | ... | @@ -257,37 +299,16 @@ function build_chart(div_selector, data_url, project_name, category) { |
257 | 299 | .style("font-size", "12px") |
258 | 300 | .text(chart_title); |
259 | 301 | |
302 | + // | |
260 | 303 | // Draw the total charge line |
261 | 304 | // |
262 | - const line = d3.line() | |
263 | - .x(d => (xScale.bandwidth() / 2) + xScale(d.period)) // décalage pour centrer au milieu des barres | |
264 | - .y(d => yScale(d.total)) | |
265 | - .curve(d3.curveMonotoneX); // Fonction de courbe permettant de l'adoucir | |
266 | 305 | |
267 | - // the line itselet | |
268 | - svg.append("path") | |
269 | - .datum(periods_total_charge) | |
270 | - .attr("class", "total-line") | |
271 | - .attr("d", line); | |
272 | - | |
273 | - // data dots at each point | |
274 | - svg.selectAll("line-circle") | |
275 | - .data(periods_total_charge) | |
276 | - .enter().append("circle") | |
277 | - .attr("class", "total-circle") | |
278 | - .attr("r", dot_radius) | |
279 | - .attr("cx", function (d) { | |
280 | - return (xScale.bandwidth() / 2) + xScale(d.period) | |
281 | - }) | |
282 | - .attr("cy", function (d) { | |
283 | - return yScale(d.total); | |
284 | - }) | |
285 | - .on("mouseover", mouseoverdot) | |
286 | - .on("mouseleave", mouseleavedot); | |
287 | - ; | |
306 | + draw_total_line(data) | |
288 | 307 | |
308 | + // | |
289 | 309 | // Draw the stacked bars |
290 | 310 | // |
311 | + | |
291 | 312 | // first one group for one category containing all bars over periods |
292 | 313 | let groups = svg.selectAll("g.category") |
293 | 314 | .data(stacked_data) |
... | ... | @@ -311,6 +332,6 @@ function build_chart(div_selector, data_url, project_name, category) { |
311 | 332 | .on("mouseleave", mouseleave); |
312 | 333 | |
313 | 334 | |
314 | - }); | |
335 | + }); // end of d3.csv().then({}) | |
315 | 336 | |
316 | 337 | } | ... | ... |