Commit e331f7fce085d93e896e93e48c700233659cee9b

Authored by hitier
1 parent b44941b3

Enhance chart position and size

app/main/static/js/charges.js
... ... @@ -298,7 +298,7 @@ function build_chart(div_selector, data_url, entity_name, category_type) {
298 298 // TODO: use ymax_from_stacked
299 299 // Get the max y to plot
300 300 // If no data at all, force to 0
301   - let y_max = 0;
  301 + let y_max;
302 302 if (categories.length === 0) {
303 303 y_max = 0;
304 304 } else {
... ... @@ -337,9 +337,7 @@ function build_chart(div_selector, data_url, entity_name, category_type) {
337 337 .attr("x", d => xCategories(d.key))
338 338 .attr("width", xCategories.bandwidth())
339 339 /* Transition part */
340   - .attr("y", d => {
341   - return height;
342   - })
  340 + .attr("y", height)
343 341 .attr("height", 0)
344 342 .transition()
345 343 .duration(750)
... ... @@ -441,9 +439,7 @@ function build_chart(div_selector, data_url, entity_name, category_type) {
441 439 .attr("x", d => xScale(d.data.period))
442 440 .attr("width", xScale.bandwidth())
443 441 /* transition part */
444   - .attr("y", d => {
445   - return height;
446   - })
  442 + .attr("y", height)
447 443 .attr("height", 0)
448 444 .transition()
449 445 .duration(750)
... ... @@ -639,20 +635,31 @@ function build_chart(div_selector, data_url, entity_name, category_type) {
639 635  
640 636 }
641 637  
  638 +function make_svg(div_selector, width, height, margin) {
  639 + let svg_height = height + margin.top + margin.bottom;
  640 + let svg_width = width + margin.top + margin.bottom;
  641 +
  642 + const svg = d3.select(div_selector).append("svg")
  643 + .attr("viewBox", [0, 0, svg_width, svg_height])
  644 + .attr("class", "svg_chart")
  645 + .attr("width", svg_width)
  646 + .attr("height", svg_height)
  647 + .append("g")
  648 + .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  649 +
  650 + return svg;
  651 +}
  652 +
642 653 function build_pie_chart(div_selector, data_url, entity_name) {
643   - const main_elt = document.getElementById("main");
644   - let margin = {top: 60, right: 100, bottom: 200, left: 90},
645   - width = main_elt.offsetWidth * 0.95 - margin.left - margin.right,
646   - height = 600 - margin.top - margin.bottom,
647   - radius = Math.min(height, width) / 2;
  654 + let margin = {top: 30, right: 10, bottom: 50, left: 10};
  655 + let main_elt = document.querySelector(div_selector);
  656 + let width = main_elt.offsetWidth,
  657 + // height = width * 0.8;
  658 + height = 400;
648 659  
649   - let svg = d3.select(div_selector)
650   - .append('svg')
651   - .attr('id', 'svg')
652   - .attr('width', width + margin.right + margin.left)
653   - .attr('height', height + margin.top + margin.bottom)
654   - .append('g')
655   - .attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')');
  660 + let svg = make_svg(div_selector, width, height, margin);
  661 + let radius = Math.min(height, width) / 2,
  662 + radius_width = 90;
656 663  
657 664 let color = d3.scaleOrdinal(['#4daf4a', '#377eb8', '#ff7f00', '#984ea3', '#e41a1c']);
658 665 let pie = d3.pie().value(function (d) {
... ... @@ -660,13 +667,15 @@ function build_pie_chart(div_selector, data_url, entity_name) {
660 667 });
661 668  
662 669 let arc = d3.arc()
663   - .innerRadius(radius - 70)
  670 + .innerRadius(radius - radius_width)
664 671 .outerRadius(radius);
665 672  
666 673 d3.csv(data_url).then(data => {
667 674  
668   - console.log("retrieving " + data_url);
669   - let arcs = svg.selectAll('arc')
  675 + let arcs = svg
  676 + .append('g')
  677 + .attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')')
  678 + .selectAll('arc')
670 679 .data(pie(data))
671 680 .enter()
672 681 .append('g')
... ... @@ -676,6 +685,36 @@ function build_pie_chart(div_selector, data_url, entity_name) {
676 685 .attr('stroke', 'black')
677 686 .attr('fill', d => color(d.data.Statut))
678 687 .attr('d', arc);
  688 +
  689 + //
  690 + // Write chart Title
  691 + //
  692 +
  693 + // part 1
  694 + svg.append("text")
  695 + .attr("x", (width / 2))
  696 + .attr("y", 0 - (margin.top / 2) - 25)
  697 + .attr("text-anchor", "middle")
  698 + .style("font-size", "16px")
  699 + .style("font-weight", "bold")
  700 + .text("Répartion des agents par statut");
  701 + // part 2
  702 + svg.append("text")
  703 + .attr("x", (width / 2))
  704 + .attr("y", 0 - (margin.top / 2) )
  705 + .attr("text-anchor", "middle")
  706 + .style("font-size", "12px")
  707 + .text("Période: ");
  708 +
679 709 });
680 710  
681 711 }
  712 +
  713 +function build_line_chart(div_selector, data_url, entity_name) {
  714 + let margin = {top: 0, right: 0, bottom: 0, left: 0};
  715 + let main_elt = document.querySelector(div_selector);
  716 + let width = main_elt.offsetWidth,
  717 + height = 500;
  718 + console.log(main_elt);
  719 + let svg = make_svg(div_selector, width, height, margin);
  720 +}
... ...
app/main/templates/agents_stats.html
... ... @@ -9,13 +9,21 @@
9 9 <!-- Invisible span to definte wich ul and a in the navbar are actived -->
10 10 <span id="nav_actived" style="display: none">agent,agents_stats</span>
11 11  
12   - <h3 class="sub-header">Charge des agents par statut</h3>
13   - <div class="charge_chart" id="agents_by_status_chart"></div>
14   -
15   -{# {% for c in categories %}#}
16   -{# <h3 class="sub-header">Charge pour la catégorie {{ c.name }}</h3>#}
17   -{# <div class="charge_chart" id="labels_stats_chart_{{ c.id }}"></div>#}
18   -{# {% endfor %}#}
  12 + <h3 class="sub-header">Tout le laboratoire</h3>
  13 +
  14 + <div class="row">
  15 + <div class="col-4">
  16 + <div class="charge_chart" id="agents_by_status_chart"></div>
  17 + </div>
  18 + <div class="col-8">
  19 + <div class="charge_chart" id="charge_by_status_chart"></div>
  20 + </div>
  21 + </div>
  22 +
  23 + {# {% for c in categories %}#}
  24 + {# <h3 class="sub-header">Charge pour la catégorie {{ c.name }}</h3>#}
  25 + {# <div class="charge_chart" id="labels_stats_chart_{{ c.id }}"></div>#}
  26 + {# {% endfor %}#}
19 27  
20 28 {% endblock %}
21 29  
... ... @@ -24,16 +32,19 @@
24 32 {% include 'charges-includes.html' %}
25 33  
26 34 <script>
27   - {#TODO: get current period id from session#}
  35 + {#TODO: get current period id from session#}
28 36 build_pie_chart("#agents_by_status_chart",
29 37 "{{url_for('main.rest_agents_status_count', period_id=1)}}",
30 38 "Charge des agents par statut");
  39 + build_line_chart("#charge_by_status_chart",
  40 + "{{url_for('main.rest_agents_status_count', period_id=1)}}",
  41 + "Charge des agents par statut");
31 42  
32   -{# {% for c in categories %}#}
33   -{# build_chart("#labels_stats_chart_{{ c.id }}",#}
34   -{# "{{url_for('main.rest_labels_stats', category_id=c.id)}}",#}
35   -{# "Charge pour la catégorie {{c.name}}",#}
36   -{# "area");#}
37   -{# {% endfor %}#}
  43 + {# {% for c in categories %}#}
  44 + {# build_chart("#labels_stats_chart_{{ c.id }}",#}
  45 + {# "{{url_for('main.rest_labels_stats', category_id=c.id)}}",#}
  46 + {# "Charge pour la catégorie {{c.name}}",#}
  47 + {# "area");#}
  48 + {# {% endfor %}#}
38 49 </script>
39 50 {% endblock %}
... ...