Commit 6b44860dcd510d1afc7d6a4f5d123c0b5a4494c7

Authored by hitier
1 parent 01429777

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,15 +27,27 @@ const dot_radius = 6;
27 // const y = d3.scaleLinear() 27 // const y = d3.scaleLinear()
28 // .range([height, 0]); 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 function build_chart(div_selector, data_url, project_name, category) { 38 function build_chart(div_selector, data_url, project_name, category) {
32 39
33 if (category == 'capacity') { 40 if (category == 'capacity') {
34 var chart_title = "Charges par fonction" 41 var chart_title = "Charges par fonction"
35 var category_title = "Fonction" 42 var category_title = "Fonction"
  43 + var draw_total_line = function(data){ //do nothing
  44 + }
36 } else if (category == 'service') { 45 } else if (category == 'service') {
37 var chart_title = "Charges par service" 46 var chart_title = "Charges par service"
38 var category_title = "Service" 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 const svg = d3.select(div_selector).append("svg") 53 const svg = d3.select(div_selector).append("svg")
@@ -82,6 +94,7 @@ function build_chart(div_selector, data_url, project_name, category) { @@ -82,6 +94,7 @@ function build_chart(div_selector, data_url, project_name, category) {
82 .attr("r", dot_radius); 94 .attr("r", dot_radius);
83 mouseleave(d); 95 mouseleave(d);
84 } 96 }
  97 +
85 var mouseoverdot = function (e, d) { 98 var mouseoverdot = function (e, d) {
86 d3.select(this).transition() 99 d3.select(this).transition()
87 .duration(1) 100 .duration(1)
@@ -122,15 +135,12 @@ function build_chart(div_selector, data_url, project_name, category) { @@ -122,15 +135,12 @@ function build_chart(div_selector, data_url, project_name, category) {
122 .text(d => d); 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 // it will be used for the line drawing above bars. 140 // it will be used for the line drawing above bars.
133 // 141 //
  142 + var periods_total_charge = []
  143 +
134 data.forEach(function (d) { 144 data.forEach(function (d) {
135 // get the list of values for all columns except the first one which is the period name 145 // get the list of values for all columns except the first one which is the period name
136 var period_values = Object.values(d).slice(1) 146 var period_values = Object.values(d).slice(1)
@@ -140,11 +150,48 @@ function build_chart(div_selector, data_url, project_name, category) { @@ -140,11 +150,48 @@ function build_chart(div_selector, data_url, project_name, category) {
140 periods_total_charge.push(row) 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 // 1- Get the charge sum for each categories over periods 191 // 1- Get the charge sum for each categories over periods
146 // That will leave '0' to categories with no charge at all 192 // That will leave '0' to categories with no charge at all
147 // 193 //
  194 + var categories_total_charge = {}
148 data.forEach(function (d) { 195 data.forEach(function (d) {
149 Object.keys(d).forEach(function (k) { 196 Object.keys(d).forEach(function (k) {
150 if (categories_total_charge.hasOwnProperty(k)) { 197 if (categories_total_charge.hasOwnProperty(k)) {
@@ -181,12 +228,6 @@ function build_chart(div_selector, data_url, project_name, category) { @@ -181,12 +228,6 @@ function build_chart(div_selector, data_url, project_name, category) {
181 228
182 // Xaxis 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 const xAxis = d3.axisBottom(xScale) 231 const xAxis = d3.axisBottom(xScale)
191 232
192 // Yaxis 233 // Yaxis
@@ -199,9 +240,7 @@ function build_chart(div_selector, data_url, project_name, category) { @@ -199,9 +240,7 @@ function build_chart(div_selector, data_url, project_name, category) {
199 // Enhance it by %ratio to leave room on the top of the graph 240 // Enhance it by %ratio to leave room on the top of the graph
200 y_max = y_max * height_ratio 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 const yAxis = d3.axisLeft(yScale) 245 const yAxis = d3.axisLeft(yScale)
207 .ticks(y_ticks_num) 246 .ticks(y_ticks_num)
@@ -241,7 +280,10 @@ function build_chart(div_selector, data_url, project_name, category) { @@ -241,7 +280,10 @@ function build_chart(div_selector, data_url, project_name, category) {
241 .attr("x", -margin.top - 70) 280 .attr("x", -margin.top - 70)
242 .text("Charge (% ETP)") 281 .text("Charge (% ETP)")
243 282
  283 + //
244 // Write chart Title 284 // Write chart Title
  285 + //
  286 +
245 // part 1 287 // part 1
246 svg.append("text") 288 svg.append("text")
247 .attr("x", (width / 2)) 289 .attr("x", (width / 2))
@@ -257,37 +299,16 @@ function build_chart(div_selector, data_url, project_name, category) { @@ -257,37 +299,16 @@ function build_chart(div_selector, data_url, project_name, category) {
257 .style("font-size", "12px") 299 .style("font-size", "12px")
258 .text(chart_title); 300 .text(chart_title);
259 301
  302 + //
260 // Draw the total charge line 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 // Draw the stacked bars 309 // Draw the stacked bars
290 // 310 //
  311 +
291 // first one group for one category containing all bars over periods 312 // first one group for one category containing all bars over periods
292 let groups = svg.selectAll("g.category") 313 let groups = svg.selectAll("g.category")
293 .data(stacked_data) 314 .data(stacked_data)
@@ -311,6 +332,6 @@ function build_chart(div_selector, data_url, project_name, category) { @@ -311,6 +332,6 @@ function build_chart(div_selector, data_url, project_name, category) {
311 .on("mouseleave", mouseleave); 332 .on("mouseleave", mouseleave);
312 333
313 334
314 - }); 335 + }); // end of d3.csv().then({})
315 336
316 } 337 }