Commit 8dad7ce76569867899aa4bbd3f4fb4562bf43a91

Authored by Anais Amato
Committed by hitier
1 parent fd63e06e

Add animations to load bars in d3.js graphs

Showing 1 changed file with 23 additions and 3 deletions   Show diff stats
app/main/static/js/charges.js
@@ -255,15 +255,26 @@ function build_chart(div_selector, data_url, entity_name, category_type) { @@ -255,15 +255,26 @@ function build_chart(div_selector, data_url, entity_name, category_type) {
255 }) 255 })
256 .enter().append("rect") 256 .enter().append("rect")
257 .attr("x", d => xCategories(d.key)) 257 .attr("x", d => xCategories(d.key))
258 - .attr("y", d => yScale(d.value))  
259 .attr("width", xCategories.bandwidth()) 258 .attr("width", xCategories.bandwidth())
  259 + /* Transition part */
  260 + .attr("y", d => { return height; })
  261 + .attr("height", 0)
  262 + .transition()
  263 + .duration(750)
  264 + .delay(function (d, i) {
  265 + return i * 150;
  266 + })
  267 + .attr("y", d => yScale(d.value))
260 .attr("height", d => height - yScale(d.value)) 268 .attr("height", d => height - yScale(d.value))
261 .attr("fill", d => colorScale(d.key)) 269 .attr("fill", d => colorScale(d.key))
262 - .attr("class", "bar") 270 + .attr("class", "bar");
  271 +
  272 + svg.selectAll("rect")
263 .on("mouseover", mouseovergrouped) 273 .on("mouseover", mouseovergrouped)
264 .on("mousemove", mousemove) 274 .on("mousemove", mousemove)
265 .on("mouseleave", mouseleave); 275 .on("mouseleave", mouseleave);
266 276
  277 +
267 } 278 }
268 279
269 function draw_categories_stacked(data, categories) { 280 function draw_categories_stacked(data, categories) {
@@ -301,9 +312,18 @@ function build_chart(div_selector, data_url, entity_name, category_type) { @@ -301,9 +312,18 @@ function build_chart(div_selector, data_url, entity_name, category_type) {
301 .attr("class", "bar") 312 .attr("class", "bar")
302 .attr("x", d => xScale(d.data.period)) 313 .attr("x", d => xScale(d.data.period))
303 .attr("width", xScale.bandwidth()) 314 .attr("width", xScale.bandwidth())
304 - // .attr("width", 5) 315 + /* transition part */
  316 + .attr("y", d => { return height; })
  317 + .attr("height", 0)
  318 + .transition()
  319 + .duration(750)
  320 + .delay(function (d, i) {
  321 + return i * 150;
  322 + })
305 .attr("y", d => yScale(d[1])) 323 .attr("y", d => yScale(d[1]))
306 .attr("height", d => height - yScale(d[1] - d[0])) 324 .attr("height", d => height - yScale(d[1] - d[0]))
  325 +
  326 + groups.selectAll("rect")
307 .on("mouseover", mouseover) 327 .on("mouseover", mouseover)
308 .on("mousemove", mousemove) 328 .on("mousemove", mousemove)
309 .on("mouseleave", mouseleave); 329 .on("mouseleave", mouseleave);