Blame view

app/main/templates/agent.html 6.59 KB
d9f5cfc9   hitier   New agent page dy...
1
{% extends "base_page.html" %}
04e2a90d   hitier   Show agent charge...
2
3

{% block more_heads %}
b15e4db5   hitier   Tooltip on mouseo...
4
<style type="text/css">
70da5dd5   hitier   Insert charges ta...
5
6
7
8
9
10
11
12
13
14
  #charge_table,
  #charge_table  th,
  #charge_table td{
    margin-top: 5em;
    margin-bottom: 5em;
    border: solid 1px lightsteelblue;
  }
  #charge_table td {
    padding: 0.8em;
  }
b15e4db5   hitier   Tooltip on mouseo...
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #charge_div {
    background-color: #fAfAfA;
    border: 1pt solid black;
    display: inline-block;
  }

  rect.bar:hover {
    fill: dimgrey;
  }

  rect.bar {
    fill: rgb(127, 127, 159);
  }

  .tooltip {
    position: absolute;
17a768f8   hitier   Enhance agent cha...
31
    opacity: 1.0;
b15e4db5   hitier   Tooltip on mouseo...
32
33
34
35
36
    z-index: 1000;
    text-align: left;
    border-radius: 4px;
    -moz-border-radius: 4px;
    -webkit-border-radius: 4px;
17a768f8   hitier   Enhance agent cha...
37
38
39
40
41
    padding: 10px;
    border: 1px solid;
    background-color: white;
    color: black;
    /*font: 12px sans-serif;*/
b15e4db5   hitier   Tooltip on mouseo...
42
43
44
45
    max-width: 300px;
  }

</style>
04e2a90d   hitier   Show agent charge...
46
47
{% endblock %}

d9f5cfc9   hitier   New agent page dy...
48
{% block content %}
b15e4db5   hitier   Tooltip on mouseo...
49
<div id="charge_div"></div>
70da5dd5   hitier   Insert charges ta...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<table id="charge_table">
  <thead>
  <tr>
  {% for header in charges[0] %}
    <td>{{header}}</td>
  {% endfor %}
  </tr>
  </thead>
  <tbody>
  {% for line in charges[1:] %}
  <tr>
    {% for cell in line %}
      <td>{{cell}}</td>
    {% endfor %}
  </tr>
  {% endfor %}

  </tbody>
</table>
d9f5cfc9   hitier   New agent page dy...
69
{% endblock %}
04e2a90d   hitier   Show agent charge...
70
71


d9f5cfc9   hitier   New agent page dy...
72
{% block more_scripts %}
b15e4db5   hitier   Tooltip on mouseo...
73
74
75
76
77
78
79
{% include 'd3js-includes.html' %}
<script>

  const margin = {top: 40, right: 40, bottom: 90, left: 120},
      width = 900 - margin.left - margin.right,
      height = 500 - margin.top - margin.bottom;

94071fee   hitier   Better axis position
80
81
  const tooltip_offset = {dx: 300, dy: 120}

b15e4db5   hitier   Tooltip on mouseo...
82
83
  const x = d3.scaleBand()
      .range([0, width])
73359858   hitier   Fix non empty cha...
84
      .padding(0.2);
b15e4db5   hitier   Tooltip on mouseo...
85
86
87
88
89
90
91
92
93
94
95

  const y = d3.scaleLinear()
      .range([height, 0]);

  const svg = d3.select("#charge_div").append("svg")
      .attr("id", "svg")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom)
      .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

b15e4db5   hitier   Tooltip on mouseo...
96
97

  // On demande à D3JS de charger notre fichier
64515ad9   hitier   New agent charges...
98
  d3.json("{{url_for('main.charge_agent_json', agent_id=agent.id)}}").then(data => {
22c5ff60   hitier   New stacked_charg...
99
100
101

    // waiting for structure of the form:
    //
17a768f8   hitier   Enhance agent cha...
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    // {
    // '2014_S1': {'4Q++': None,
    //          'Activité service C2L': None,
    //          'Activité service COMCL': None,
    //          .
    //           ...},
    // '2014_S2': {'4Q++': None,
    //          'Activité service C2L': None,
    //          .
    //          .
    //          .
    // '2021_S2': {'4Q++': None,
    //          .
    //          .
    //          'Test IAS': None,
    //          'cahier des charges DT insu': None,
    //          'ŒIL': None
    //          }
    //   }


22c5ff60   hitier   New stacked_charg...
123
124
125
    var periods = Object.keys(data)
    var first_stacked_charges = Object.values(data)[0]
    var projects = Object.keys(first_stacked_charges)
22c5ff60   hitier   New stacked_charg...
126

22c5ff60   hitier   New stacked_charg...
127
128
129
130
    var x = d3.scaleBand()
        .domain(periods)
        .range([0, width])
        .padding(0.2)
94071fee   hitier   Better axis position
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    // Ajout de l'axe X au SVG
    // Déplacement de l'axe horizontal et du futur texte (via la fonction translate) au bas du SVG
    // Selection des noeuds text, positionnement puis rotation
    // svg.append('g')
    //     .attr('transform', 'translate(20, ' + height + ')')
    //     .call(d3.axisBottom(x).tickSizeOuter(0));
    svg.append("g")
        .attr("transform", "translate(20," + height + ")")
        .call(d3.axisBottom(x).tickSize(5))
        .selectAll("text")
        .style("text-anchor", "end")
        .attr("dx", "-.8em")
        .attr("dy", ".15em")
        .attr("transform", "rotate(-65)");
22c5ff60   hitier   New stacked_charg...
145
146
147
148
149
150
151
152
153

    var y = d3.scaleLinear()
        .domain([0, 100])
        .range([height, 0])

    svg.append('g')
        .attr('transform', 'translate(20, 0)')
        .call(d3.axisLeft(y))

94071fee   hitier   Better axis position
154
155
156
157
158
159
160
161
162

    svg.append("text")
        .attr("text-anchor", "end")
        .attr("transform", "rotate(-90)")
        .attr("y", -margin.left + 60)
        .attr("x", -margin.top - 70)
        .text("Charge (ETP)")


22c5ff60   hitier   New stacked_charg...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
    var color = d3.scaleOrdinal()
        .domain(projects)
        .range(['#e41a1c', '#377eb8', '#4daf4a',
          "#800000",
          "#FFFF00",
          "#808000",
          "#00FF00",
          "#008000",
          "#00FFFF",
          "#008080",
          "#0000FF",
          "#000080",
          "#FF00FF",
          "#800080",
        ]);

17a768f8   hitier   Enhance agent cha...
179
    var stack_generator = d3.stack().keys(projects)
22c5ff60   hitier   New stacked_charg...
180

17a768f8   hitier   Enhance agent cha...
181
182
183
    // Building a list of dict, with projects as keys, like in the input dat,
    // but with a new key, 'period' wich value is the period string for that line
    // This is need for the d3js stack_generator function
22c5ff60   hitier   New stacked_charg...
184
    var stackable_data = []
17a768f8   hitier   Enhance agent cha...
185
    Object.keys(data).forEach(function (period) {
22c5ff60   hitier   New stacked_charg...
186
      line = data[period];
17a768f8   hitier   Enhance agent cha...
187
      line['period'] = period;
22c5ff60   hitier   New stacked_charg...
188
189
190
191
192
      stackable_data.push(line)
    });

    var stacked_data = stack_generator(stackable_data)

17a768f8   hitier   Enhance agent cha...
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

    // ----------------
    // Create a tooltip
    // ----------------
    var tooltip = d3.select("#charge_div")
        .append("div")
        .style("opacity", 0)
        .attr("class", "tooltip")

    // // Three function that change the tooltip when user hover / move / leave a cell
    // var mouseover = function (d) {
    //   var subgroupName = d3.select(this.parentNode).datum().key;
    //   // var subgroupValue = d.data[subgroupName];
    //   tooltip
    //       .html("subgroup: " + subgroupName + "<br>" + "Value: " + subgroupValue)
    //       .style("opacity", 1)
    // }

    var mousemove = function (e, d) {
      tooltip
94071fee   hitier   Better axis position
213
214
          .style("left", (e.x - tooltip_offset.dx) + "px")
          .style("top", (e.y - tooltip_offset.dy) + "px")
17a768f8   hitier   Enhance agent cha...
215
216
217
218
219
220
221
222
223
224
225
226
    }

    var mouseleave = function (d) {
      tooltip
          .transition()
          .duration(100)
          .style("opacity", 0)
    }

    var mouseover = function (e, d) {
      var project_name = d3.select(this.parentNode).datum().key
      var project_charge = d.data[project_name]
94071fee   hitier   Better axis position
227
228
      tooltip
          .transition()
17a768f8   hitier   Enhance agent cha...
229
230
231
          .duration(200)
          .style("opacity", 1);
      tooltip
94071fee   hitier   Better axis position
232
          .html("Project: " + project_name + "<br>" + "Charge: " + project_charge + "%")
17a768f8   hitier   Enhance agent cha...
233
234
235
          .style("left", (e.x - 400) + "px")
          .style("top", (e.y - 90) + "px");
    }
22c5ff60   hitier   New stacked_charg...
236
237
238
239
240
241
242
243
244
245
246

    svg.append('g')
        .selectAll('g')
        .data(stacked_data)
        .enter().append('g')
        .attr("fill", function (d) {
          return color(d.key);
        })
        .selectAll("rect")
        // enter a second time = loop subgroup per subgroup to add all rectangles
        .data(d => d)
b15e4db5   hitier   Tooltip on mouseo...
247
        .enter().append("rect")
22c5ff60   hitier   New stacked_charg...
248
249
250
        .attr("x", d => x(d.data.period) + 20)
        .attr("y", d => y(d[1]))
        .attr("height", d => y(d[0]) - y(d[1]))
b15e4db5   hitier   Tooltip on mouseo...
251
        .attr("width", x.bandwidth())
17a768f8   hitier   Enhance agent cha...
252
253
254
255
        .attr("stroke", "black")
        .on("mouseover", mouseover)
        .on("mousemove", mousemove)
        .on("mouseleave", mouseleave);
b15e4db5   hitier   Tooltip on mouseo...
256
257
  });
</script>
d9f5cfc9   hitier   New agent page dy...
258
{% endblock %}