Blame view

flaskr/templates/home.html 6.18 KB
4c862b54   Antoine Goutenoir   Add a grouped bar...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{% 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>

    <div class="row">

b7411dd3   Antoine Goutenoir   Improve the plots...
19
        <div class="col-lg-12 text-center mb-5">
4c862b54   Antoine Goutenoir   Add a grouped bar...
20
            <div id="scaling_laws_spinner" class="lds-ripple text-center"><div></div><div></div><div></div></div>
b7411dd3   Antoine Goutenoir   Improve the plots...
21
            <div id="scaling_laws_d3viz" class="plot-container"></div>
4c862b54   Antoine Goutenoir   Add a grouped bar...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
        </div>

    </div>

    {% for section in content.home.sections -%}
    <div class="row">
        {% for block in section.blocks -%}
        <div class="col-md-4">
            {% if block.title -%}
            <h3>{{ block.title }}</h3>
            {%- endif %}
            {{ block.content | markdown | safe }}
        </div>
        {%- endfor %}
    </div>
    {%- endfor %}

{% endblock %}

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

{% block js %}
b7411dd3   Antoine Goutenoir   Improve the plots...
45
46
47
48
49
<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...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<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;
};


{#var models = {{ models | tojson }};#}


/** 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...
95
    var margin = {top: 30, right: 30, bottom: 75, left: 70},
4c862b54   Antoine Goutenoir   Add a grouped bar...
96
        height = 666 - margin.top - margin.bottom;
b7411dd3   Antoine Goutenoir   Improve the plots...
97
    var width = Math.max(666, $(vizid).parent().width());
4c862b54   Antoine Goutenoir   Add a grouped bar...
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
    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...
169
                return y(0);
4c862b54   Antoine Goutenoir   Add a grouped bar...
170
171
172
            })
            .attr("width", xSubgroup.bandwidth())
            .attr("height", function (d) {
5b73d035   Antoine Goutenoir   Animate the scali...
173
                return 0;
4c862b54   Antoine Goutenoir   Add a grouped bar...
174
            })
5b73d035   Antoine Goutenoir   Animate the scali...
175
176
177
            //.attr("height", function (d) {
            //    return height - y(d.value);
            //})
4c862b54   Antoine Goutenoir   Add a grouped bar...
178
179
180
181
            .attr("fill", function (d) {
                return color(d.key);
            });

5b73d035   Antoine Goutenoir   Animate the scali...
182
183
184
185
186
187
188
189
190
191
192
        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...
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
        // 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)#}
            .scale(color);

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

4c862b54   Antoine Goutenoir   Add a grouped bar...
214
215
        // …

b7411dd3   Antoine Goutenoir   Improve the plots...
216

4c862b54   Antoine Goutenoir   Add a grouped bar...
217
218
219
220
221
222
223
        $('#scaling_laws_spinner').hide();

    })

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