Commit 518c7cc5b4094b3a162a8fc93f11fbeddae3e373
1 parent
7c1b38be
Exists in
master
and in
4 other branches
Draw the total categorie's charge curve
Showing
2 changed files
with
53 additions
and
13 deletions
Show diff stats
app/main/static/css/charges.css
1 | +.line { | |
2 | + fill: none; | |
3 | + stroke: orangered; | |
4 | + stroke-width: 2px; | |
5 | + z-index: -1; | |
6 | +} | |
7 | + | |
1 | 8 | .horizontalY { |
2 | 9 | /*fill : none;*/ |
3 | - shape-rendering : crispEdges; | |
4 | - stroke : lightgrey; | |
5 | - stroke-width : 1px; | |
10 | + shape-rendering: crispEdges; | |
11 | + stroke: lightgrey; | |
12 | + stroke-width: 1px; | |
6 | 13 | } |
14 | + | |
7 | 15 | .horizontalY0 { |
8 | - stroke-width : 0px; | |
16 | + stroke-width: 0px; | |
9 | 17 | } |
10 | 18 | |
11 | 19 | #charge_table, | ... | ... |
app/main/static/js/charges.js
... | ... | @@ -103,9 +103,25 @@ function build_chart(div_selector, data_url, project_name, category) { |
103 | 103 | // var categories = data.columns.slice(1) |
104 | 104 | var periods = d3.map(data, d => d.period) |
105 | 105 | var categories_total_charge = {} |
106 | + var periods_total_charge = [] | |
106 | 107 | |
107 | - // Get the charge sum for each categories over periods | |
108 | + // First, we build the total charge by period; | |
109 | + // it will be used for the line drawing above bars. | |
110 | + // | |
111 | + data.forEach(function (d) { | |
112 | + // get the list of values for all columns except the first one which is the period name | |
113 | + var period_values = Object.values(d).slice(1) | |
114 | + var row = {} | |
115 | + row['period'] = d.period | |
116 | + row['total'] = d3.sum(period_values) | |
117 | + periods_total_charge.push(row) | |
118 | + }); | |
119 | + | |
120 | + // Then we filter datas to only keep non zero categori/s | |
121 | + // | |
122 | + // 1- Get the charge sum for each categories over periods | |
108 | 123 | // That will leave '0' to categories with no charge at all |
124 | + // | |
109 | 125 | data.forEach(function (d) { |
110 | 126 | Object.keys(d).forEach(function (k) { |
111 | 127 | if (categories_total_charge.hasOwnProperty(k)) { |
... | ... | @@ -117,7 +133,8 @@ function build_chart(div_selector, data_url, project_name, category) { |
117 | 133 | ) |
118 | 134 | }) |
119 | 135 | |
120 | - // Now, filter only categories that have some charge | |
136 | + // 2- Now, filter only categories that have a non-zero charge | |
137 | + // | |
121 | 138 | var categories = [] |
122 | 139 | for (var key in categories_total_charge) { |
123 | 140 | if (categories_total_charge[key] > 0) { |
... | ... | @@ -125,12 +142,14 @@ function build_chart(div_selector, data_url, project_name, category) { |
125 | 142 | } |
126 | 143 | } |
127 | 144 | |
128 | - // This forme list is the color_scale domain. | |
145 | + // This former list we use as color_scale domain. | |
129 | 146 | // And that allows us to build the legend |
147 | + // | |
130 | 148 | color_scale.domain(categories) |
131 | 149 | addlegend(color_scale) |
132 | 150 | |
133 | - // Build the stacked data for stacked bars | |
151 | + // Now build the stacked data for stacked bars | |
152 | + // | |
134 | 153 | var stack = d3.stack() |
135 | 154 | .keys(categories) |
136 | 155 | // .order(d3.stackOrderNone) |
... | ... | @@ -140,17 +159,17 @@ function build_chart(div_selector, data_url, project_name, category) { |
140 | 159 | // Xaxis |
141 | 160 | // |
142 | 161 | // x scale from the periods list |
143 | - const x = d3.scaleBand() | |
162 | + const xScale = d3.scaleBand() | |
144 | 163 | .domain(periods) |
145 | 164 | .range([0, width]) |
146 | 165 | .padding(0.2); |
147 | 166 | |
148 | - const xAxis = d3.axisBottom(x) | |
167 | + const xAxis = d3.axisBottom(xScale) | |
149 | 168 | |
150 | 169 | // Yaxis |
151 | 170 | // |
152 | 171 | // Get the max y to plot |
153 | - y_max = d3.max(stacked_data[stacked_data.length - 1], d => d[1]); | |
172 | + var y_max = d3.max(stacked_data[stacked_data.length - 1], d => d[1]); | |
154 | 173 | if (y_max == 0) { |
155 | 174 | y_max = 100 |
156 | 175 | } |
... | ... | @@ -215,6 +234,18 @@ function build_chart(div_selector, data_url, project_name, category) { |
215 | 234 | .style("font-size", "12px") |
216 | 235 | .text(chart_title); |
217 | 236 | |
237 | + // Draw the total charge line | |
238 | + // | |
239 | + const line = d3.line() | |
240 | + .x(d => (xScale.bandwidth() / 2) + xScale(d.period)) // décalage pour centrer au milieu des barres | |
241 | + .y(d => yScale(d.total)) | |
242 | + .curve(d3.curveMonotoneX); // Fonction de courbe permettant de l'adoucir | |
243 | + | |
244 | + svg.append("path") | |
245 | + .datum(periods_total_charge) | |
246 | + .attr("class", "line") | |
247 | + .attr("d", line); | |
248 | + | |
218 | 249 | // Draw the stacked bars |
219 | 250 | // |
220 | 251 | // first one group for one category containing all bars over periods |
... | ... | @@ -230,8 +261,8 @@ function build_chart(div_selector, data_url, project_name, category) { |
230 | 261 | .enter() |
231 | 262 | .append("rect") |
232 | 263 | .attr("class", "bar") |
233 | - .attr("x", d => x(d.data.period)) | |
234 | - .attr("width", x.bandwidth()) | |
264 | + .attr("x", d => xScale(d.data.period)) | |
265 | + .attr("width", xScale.bandwidth()) | |
235 | 266 | // .attr("width", 5) |
236 | 267 | .attr("y", d => yScale(d[1])) |
237 | 268 | .attr("height", d => height - yScale(d[1] - d[0])) |
... | ... | @@ -239,6 +270,7 @@ function build_chart(div_selector, data_url, project_name, category) { |
239 | 270 | .on("mousemove", mousemove) |
240 | 271 | .on("mouseleave", mouseleave); |
241 | 272 | |
273 | + | |
242 | 274 | }); |
243 | 275 | |
244 | 276 | } | ... | ... |