home.html
7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
95
96
97
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
{% 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>
{# PLOT SCALING LAWS #####################################################}
<div class="row">
<div class="col-lg-12 text-center mb-5">
<div id="scaling_laws_spinner" class="lds-ripple text-center"><div></div><div></div><div></div></div>
<div id="scaling_laws_d3viz" class="plot-container"></div>
</div>
</div>
{# THREE COLUMNS TEXT LAYOUT #############################################}
{% if content.home.sections -%}
{% for section in content.home.sections -%}
<div class="row">
{% for block in section.blocks -%}
<div class="col-md-4 text-justify">
{% if block.title -%}
<h3>{{ block.title }}</h3>
{%- endif %}
{{ block.content | markdown | safe }}
</div>
{%- endfor %}
</div>
{%- endfor %}
{%- 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 -%}
<h3{% if block.slug %} id="{{ block.slug }}"{% endif %}>{{ block.title }}</h3>
{%- endif %}
{{ block.content | markdown | safe }}
</article>
{%- endfor %}
</div>
{%- endfor %}
</div>
{%- endif %}
{% endblock %}
{#############################################################################}
{## JAVASCRIPT ###############################################################}
{% block js %}
<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>#}
<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
var margin = {top: 30, right: 30, bottom: 75, left: 70},
height = 666 - margin.top - margin.bottom;
var width = Math.max(666, $(vizid).parent().width());
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) {
return y(0);
})
.attr("width", xSubgroup.bandwidth())
.attr("height", function (d) {
return 0;
})
//.attr("height", function (d) {
// return height - y(d.value);
//})
.attr("fill", function (d) {
return color(d.key);
});
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); });
// 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)#}
.labels({{ labels | tojson }})
.scale(color);
svg.select(".legendQuant")
.call(legend);
// …
$('#scaling_laws_spinner').hide();
})
});
</script>
{% endblock %}