From a07df2040c49a629664c4b2f371db2278e31addb Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Sat, 10 Apr 2021 20:42:26 +0200 Subject: [PATCH] Compute for grouped bars max y value --- app/main/static/js/charges.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/app/main/static/js/charges.js b/app/main/static/js/charges.js index 9d128a3..ed49471 100644 --- a/app/main/static/js/charges.js +++ b/app/main/static/js/charges.js @@ -4,7 +4,6 @@ const margin = {top: 60, right: 350, bottom: 100, left: 90}, width = main_elt.offsetWidth * 0.95 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; - const height_ratio = 1.2 const tooltip_offset = {dx: 0, dy: 100} @@ -19,15 +18,6 @@ const legend_x = width + 20; // begin legend a bit after the end of the chart const dot_radius = 6; -// TODO: set main scales attr here then domain later -// const x = d3.scaleBand() -// .range([0, width]) -// .padding(0.2); -// -// const y = d3.scaleLinear() -// .range([height, 0]); -// -// x scale from the periods list const xScale = d3.scaleBand() .range([0, width]) .padding(0.2); @@ -39,8 +29,10 @@ function build_chart(div_selector, data_url, project_name, category) { var chart_title = "" var category_title = "" - var draw_total_line = function(data, categories){} - var draw_categories_bars = function(data, categories){} + var draw_total_line = function (data, categories) { + } + var draw_categories_bars = function (data, categories) { + } if (category == 'capacity') { chart_title = "Charges par fonction" category_title = "Fonction" @@ -142,6 +134,13 @@ function build_chart(div_selector, data_url, project_name, category) { function draw_categories_grouped(data, categories) { + var y_max = d3.max(data, function (d) { + return d3.max(categories, function (c) { + return +d[c] + }) + }); + y_max = y_max * height_ratio + yScale.domain([0, y_max]) // Another scale for subgroup position var xCategories = d3.scaleBand() .domain(categories) @@ -273,6 +272,8 @@ function build_chart(div_selector, data_url, project_name, category) { } d3.csv(data_url).then(data => { + // we could get categories that way, + // but we intend to filter by non zero, see later // var categories = data.columns.slice(1) var periods = d3.map(data, d => d.period) xScale.domain(periods) @@ -281,7 +282,7 @@ function build_chart(div_selector, data_url, project_name, category) { // // 1- Get the charge sum for each categories over periods // That will leave '0' to categories with no charge at all - // + // TODO: to be done with Object.keys, Object.values and d3 filtering methods var categories_total_charge = {} data.forEach(function (d) { Object.keys(d).forEach(function (k) { @@ -295,7 +296,7 @@ function build_chart(div_selector, data_url, project_name, category) { }) // 2- Now, filter only categories that have a non-zero charge - // + // TODO: to be done with Object.keys, Object.values and d3 filtering methods var categories = [] for (var key in categories_total_charge) { if (categories_total_charge[key] > 0) { -- libgit2 0.21.2