Commit 13c144c8dbeb2584189320231cdc5935ba77cb93

Authored by Anais Amato
Committed by hitier
1 parent ef5fae02

Add horizontal legend for graphs

Showing 1 changed file with 55 additions and 20 deletions   Show diff stats
app/main/static/js/charges.js
... ... @@ -6,9 +6,9 @@ function build_chart(div_selector, data_url, entity_name, category_type) {
6 6  
7 7 const main_elt = document.getElementById("main")
8 8  
9   - const margin = {top: 60, right: 350, bottom: 100, left: 90},
  9 + const margin = {top: 60, right: 150, bottom: 200, left: 90},
10 10 width = main_elt.offsetWidth * 0.95 - margin.left - margin.right,
11   - height = 500 - margin.top - margin.bottom;
  11 + height = 600 - margin.top - margin.bottom;
12 12  
13 13 const height_ratio = 1.2; // how murch room to give above chart for lisibility
14 14  
... ... @@ -17,8 +17,11 @@ function build_chart(div_selector, data_url, entity_name, category_type) {
17 17  
18 18 const y_ticks_num = 5 // dont show to many y ticks for lisibility
19 19  
20   - const legend_cell_size = 15; // size of legend colored scare
21   - const legend_x = width + 20; // begin legend a bit after the end of the chart
  20 + const legendRectSize = 15; // size of rectangle in legend
  21 +
  22 + // const legend_cell_size = 15; // size of legend colored scare
  23 + // const legend_x = width + 20; // begin legend a bit after the end of the chart
  24 + // const legend_y = 600 - margin.bottom + 20; // begin legend a bit after the end of the chart
22 25  
23 26 const dot_radius = 6; // size of total line dot
24 27  
... ... @@ -131,28 +134,60 @@ function build_chart(div_selector, data_url, entity_name, category_type) {
131 134  
132 135 var addlegend = function (color_scale) {
133 136  
  137 + // add horizontal legend
134 138 let reverse_keys = color_scale.domain().reverse();
  139 + var legendSpacing = 5;
135 140  
136   - let legend = svg.append('g')
137   - .attr('transform', 'translate(' + legend_x + ', -30)');
  141 + var legendWrap = svg.append('g')
  142 + .attr('width', '100%')
  143 + .attr('class', 'legendwrap');
138 144  
139   - legend.selectAll()
  145 + var legend = svg.select('.legendwrap').selectAll('.legend')
140 146 .data(reverse_keys)
141   - .enter().append('rect')
142   - .attr('height', legend_cell_size + 'px')
143   - .attr('width', legend_cell_size + 'px')
144   - .attr('class', 'legend')
145   - .attr('x', 5)
146   - .attr('y', (d, i) => i * legend_cell_size)
147   - .style("fill", d => color_scale(d));
  147 + .enter()
  148 + .append('g')
  149 + .attr('class', 'legend');
148 150  
149   - legend.selectAll()
150   - .data(reverse_keys)
151   - .enter().append('text')
152   - .attr("transform", (d, i) => "translate(30, " + (i * legend_cell_size + legend_cell_size / 1.6) + ")")
  151 + legend.append('rect')
  152 + .attr('width', legendRectSize)
  153 + .attr('height', legendRectSize)
  154 + .style('fill', color_scale)
  155 + .style('stroke', color_scale);
  156 +
  157 + legend.append('text')
153 158 .attr('class', 'legend')
154   - .style("fill", "black")
155   - .text(d => d);
  159 + .attr('x', legendRectSize + legendSpacing * 1.5)
  160 + .attr('y', 13)
  161 + .text(function(d) {
  162 + return d;
  163 + });
  164 +
  165 + var ypos = 0,
  166 + newxpos = 0,
  167 + maxwidth = 0,
  168 + xpos;
  169 +
  170 + legend
  171 + .attr("transform", function(d, i) {
  172 + var length = d3.select(this).select("text").node().getComputedTextLength() + 40;
  173 + xpos = newxpos;
  174 + if (width < xpos + length) {
  175 + newxpos = xpos = 0;
  176 + ypos += 30;
  177 + }
  178 + newxpos += length;
  179 + if (newxpos > maxwidth) maxwidth = newxpos;
  180 +
  181 + return 'translate(' + xpos + ',' + ypos + ')';
  182 +
  183 + });
  184 +
  185 +
  186 + legendWrap
  187 + .attr("transform", function(d, i) {
  188 + return "translate(0 ," + (height + 100) + ")";
  189 + });
  190 +
156 191 }
157 192  
158 193 function draw_categories_grouped(data, categories) {
... ...