Blame view

flaskr/templates/home.html 7.06 KB
4c862b54   Antoine Goutenoir   Add a grouped bar...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{% extends "base.html" %}

{% block title %}Home{% endblock %}

{% block body %}

    <div class="row pb-4">
        <div class="col-lg-4"></div>
        <div class="col-lg-4 text-center">
            <a href="{{ url_for('.estimate') }}" class="btn btn-lg btn-primary">
                Request Estimation
            </a>
        </div>
        <div class="col-lg-4"></div>
    </div>

4f5038b4   Antoine Goutenoir   Text is justified.
17
    {# PLOT SCALING LAWS #####################################################}
4c862b54   Antoine Goutenoir   Add a grouped bar...
18

4f5038b4   Antoine Goutenoir   Text is justified.
19
    <div class="row">
b7411dd3   Antoine Goutenoir   Improve the plots...
20
        <div class="col-lg-12 text-center mb-5">
4c862b54   Antoine Goutenoir   Add a grouped bar...
21
            <div id="scaling_laws_spinner" class="lds-ripple text-center"><div></div><div></div><div></div></div>
b7411dd3   Antoine Goutenoir   Improve the plots...
22
            <div id="scaling_laws_d3viz" class="plot-container"></div>
4c862b54   Antoine Goutenoir   Add a grouped bar...
23
        </div>
4c862b54   Antoine Goutenoir   Add a grouped bar...
24
25
    </div>

77d3f656   Antoine Goutenoir   Support any numbe...
26
    {# THREE COLUMNS TEXT LAYOUT #############################################}
4f5038b4   Antoine Goutenoir   Text is justified.
27

77d3f656   Antoine Goutenoir   Support any numbe...
28
    {% if content.home.sections -%}
4c862b54   Antoine Goutenoir   Add a grouped bar...
29
30
31
    {% for section in content.home.sections -%}
    <div class="row">
        {% for block in section.blocks -%}
4f5038b4   Antoine Goutenoir   Text is justified.
32
        <div class="col-md-4 text-justify">
4c862b54   Antoine Goutenoir   Add a grouped bar...
33
34
35
36
37
38
39
40
            {% if block.title -%}
            <h3>{{ block.title }}</h3>
            {%- endif %}
            {{ block.content | markdown | safe }}
        </div>
        {%- endfor %}
    </div>
    {%- endfor %}
77d3f656   Antoine Goutenoir   Support any numbe...
41
42
43
44
45
46
47
48
49
50
51
52
    {%- endif %}


    {# COLUMNS TEXT LAYOUT #############################################}

    {% if content.home.columns -%}
    <div class="row">
    {% for column in content.home.columns -%}
        <div class="col-md-{{ (12/(content.home.columns | length)) | int }} text-justify">
        {% for block in column.blocks -%}
            <article>
            {% if block.title -%}
1e7e25a7   Antoine Goutenoir   Fix the internal ...
53
            <h3{% if block.slug %} id="{{ block.slug }}"{% endif %}>{{ block.title }}</h3>
77d3f656   Antoine Goutenoir   Support any numbe...
54
55
56
57
58
59
60
61
            {%- endif %}
            {{ block.content | markdown | safe }}
            </article>
        {%- endfor %}
        </div>
    {%- endfor %}
    </div>
    {%- endif %}
4c862b54   Antoine Goutenoir   Add a grouped bar...
62
63
64
65
66
67
68

{% endblock %}

{#############################################################################}
{## JAVASCRIPT ###############################################################}

{% block js %}
b7411dd3   Antoine Goutenoir   Improve the plots...
69
70
71
72
73
<script src="/static/js/vendor/d3.v4.js"></script>
<script src="/static/js/vendor/d3-legend.js"></script>
{#<script src="https://d3js.org/d3.v4.js"></script>#}
{#<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-legend/2.25.6/d3-legend.js"></script>#}

4c862b54   Antoine Goutenoir   Add a grouped bar...
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<script type="text/javascript">

/** POLYFILLS **/
Math.log10 = Math.log10 || function(x) {
  return Math.log(x) * Math.LOG10E;
};

/**
 * Useful for axes' domains on plots.
 * @param value
 * @returns {number}
 */
var ceil_value_to_magnitude = function(value) {
    var sign = 1;
    if (value < 0) {
        value = Math.abs(value);
        sign = -1;
    }
    if (value < 1) {
        return sign;
    }

    var low = Math.pow(10, Math.floor(Math.log10(value)));

    var cursor = low;
    var follop = 0;
    while ((cursor < value) && (follop <= 100)) {
        cursor += 0.1 * low;
        follop += 1;
    }

    return sign * cursor;
};


a3e9d0fc   Antoine Goutenoir   Fix home plot leg...
109
var models = {{ models | tojson }};
4c862b54   Antoine Goutenoir   Add a grouped bar...
110
111
112
113
114
115
116
117
118


/** PLOTS **/
jQuery(document).ready(function($){

    var vizid = "#scaling_laws_d3viz";
    var csvUrl = "/scaling_laws.csv";

    // Set the dimensions and margins of the graph
b7411dd3   Antoine Goutenoir   Improve the plots...
119
    var margin = {top: 30, right: 30, bottom: 75, left: 70},
4c862b54   Antoine Goutenoir   Add a grouped bar...
120
        height = 666 - margin.top - margin.bottom;
b7411dd3   Antoine Goutenoir   Improve the plots...
121
    var width = Math.max(666, $(vizid).parent().width());
4c862b54   Antoine Goutenoir   Add a grouped bar...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    width = width - margin.left - margin.right;

    var svg = d3.select(vizid)
        .append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
        .append("g")
        .attr("transform",
            "translate(" + margin.left + "," + margin.top + ")");

    d3.csv(csvUrl, function (data) {

        // List of subgroups = header of the csv files = soil condition here
        var subgroups = data.columns.slice(1);

        // List of groups = species here = value of the first column called group -> I show them on the X axis
        var groups = d3.map(data, function (d) {
            return (d.distance);
        }).keys();

        // Add X axis
        var x = d3.scaleBand()
            .domain(groups)
            .range([0, width])
            .padding([0.2]);
        svg.append("g")
            .attr("transform", "translate(0," + height + ")")
            .call(d3.axisBottom(x).tickSize(0));

        // Add Y axis
        var y = d3.scaleLinear()
            .domain([0, 4000])
            .range([height, 0]);
        svg.append("g")
            .call(d3.axisLeft(y));

        // Another scale for subgroup position?
        var xSubgroup = d3.scaleBand()
            .domain(subgroups)
            .range([0, x.bandwidth()])
            .padding([0.05]);

{#{% set colors=[model.color for model in models] %}#}

        // color palette = one color per subgroup
        var color = d3.scaleOrdinal()
            .domain(subgroups)
            .range({{ colors | tojson }});
            {#.range(['#e41a1c', '#377eb8', '#4daf4a']);#}

        // Show the bars
        svg.append("g")
            .selectAll("g")
            // Enter in data = loop group per group
            .data(data)
            .enter()
            .append("g")
            .attr("transform", function (d) {
                return "translate(" + x(d.distance) + ",0)";
            })
            .selectAll("rect")
            .data(function (d) {
                return subgroups.map(function (key) {
                    return {key: key, value: d[key]};
                });
            })
            .enter().append("rect")
            .attr("x", function (d) {
                return xSubgroup(d.key);
            })
            .attr("y", function (d) {
5b73d035   Antoine Goutenoir   Animate the scali...
193
                return y(0);
4c862b54   Antoine Goutenoir   Add a grouped bar...
194
195
196
            })
            .attr("width", xSubgroup.bandwidth())
            .attr("height", function (d) {
5b73d035   Antoine Goutenoir   Animate the scali...
197
                return 0;
4c862b54   Antoine Goutenoir   Add a grouped bar...
198
            })
5b73d035   Antoine Goutenoir   Animate the scali...
199
200
201
            //.attr("height", function (d) {
            //    return height - y(d.value);
            //})
4c862b54   Antoine Goutenoir   Add a grouped bar...
202
203
204
205
            .attr("fill", function (d) {
                return color(d.key);
            });

5b73d035   Antoine Goutenoir   Animate the scali...
206
207
208
209
210
211
212
213
214
215
216
        svg.selectAll("rect")
            .transition()
            .duration(500)
            .attr("y", function (d) {
                return y(d.value);
            })
            .attr("height", function (d) {
                return height - y(d.value);
            })
            .delay(function(d, i) { return(i*100); });

b7411dd3   Antoine Goutenoir   Improve the plots...
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
        // Title
        svg.append("g")
            .append('text')
            .attr("transform", "translate(" + (-180 + width / 2.0) + "," + (height + 50) + ")")
            .text("CO\u2082 emissions (kg) as a function of travelling distance (km).");

        // Legend
        svg.append("g")
            .attr("class", "legendQuant")
            .attr("transform", "translate(20,20)");

        var legend = d3.legendColor()
            {#.labelFormat(d3.format(".2f"))#}
            {#.useClass(true)#}
            {#.title("Legend")#}
            {#.titleWidth(100)#}
a3e9d0fc   Antoine Goutenoir   Fix home plot leg...
233
            .labels({{ labels | tojson }})
b7411dd3   Antoine Goutenoir   Improve the plots...
234
235
236
237
238
            .scale(color);

        svg.select(".legendQuant")
            .call(legend);

4c862b54   Antoine Goutenoir   Add a grouped bar...
239
240
        // …

b7411dd3   Antoine Goutenoir   Improve the plots...
241

4c862b54   Antoine Goutenoir   Add a grouped bar...
242
243
244
245
246
247
248
        $('#scaling_laws_spinner').hide();

    })

});
</script>
{% endblock %}