-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First draft of outing statistics #390
- Loading branch information
1 parent
41be420
commit e3142bf
Showing
5 changed files
with
189 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<template> | ||
<div class="section"> | ||
<h1> | ||
<span>Outing statistics</span> | ||
<span v-if="!outings">{{ Math.round(loadingPercentage * 100) }}%</span> | ||
</h1> | ||
<div v-if="outings" class="columns"> | ||
<div class="column"> | ||
<h2>By year</h2> | ||
<div ref="year_repartition"></div> | ||
</div> | ||
<div class="column"> | ||
<h2>By month</h2> | ||
<div ref="month_repartition"></div> | ||
</div> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { createHistogram } from './utils/outings-stats'; | ||
import c2c from '@/js/apis/c2c'; | ||
import d3 from '@/js/libs/d3'; | ||
const getOutingYear = function (outing) { | ||
return parseInt(outing.date_start.substring(0, 4), 10); | ||
}; | ||
const getOutingMonth = function (outing) { | ||
return parseInt(outing.date_start.substring(5, 7), 10); | ||
}; | ||
export default { | ||
data() { | ||
return { | ||
outings: null, | ||
loadingPercentage: 0, | ||
}; | ||
}, | ||
watch: { | ||
$route: { | ||
handler: 'load', | ||
immediate: true, | ||
}, | ||
}, | ||
methods: { | ||
load() { | ||
c2c.outing.fullDownload(this.$route.query, 2000, this.progress).then((outings) => { | ||
this.outings = outings; | ||
d3.then(this.createGraphs); | ||
}); | ||
}, | ||
progress(current, total) { | ||
this.loadingPercentage = current / total; | ||
}, | ||
createGraphs() { | ||
const outings = this.outings; | ||
createHistogram(d3, outings, this.$refs.year_repartition, getOutingYear); | ||
createHistogram(d3, outings, this.$refs.month_repartition, getOutingMonth); | ||
}, | ||
}, | ||
}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
export function createHistogram(d3, outings, htmlElement, aggregationFuntion) { | ||
// set the dimensions and margins of the graph | ||
var margin = { top: 10, right: 30, bottom: 30, left: 40 }, | ||
width = 460 - margin.left - margin.right, | ||
height = 400 - margin.top - margin.bottom; | ||
|
||
// append the svg object to the body of the page | ||
var svg = d3 | ||
.select(htmlElement) | ||
.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 + ')'); | ||
|
||
// X axis: scale and draw: | ||
var x = d3 | ||
.scaleLinear() | ||
.domain([d3.min(outings, aggregationFuntion), d3.max(outings, aggregationFuntion) + 1]) // can use this instead of 1000 to have the max of data: | ||
.range([0, width]); | ||
|
||
svg | ||
.append('g') | ||
.attr('transform', 'translate(0,' + height + ')') | ||
.call(d3.axisBottom(x)); | ||
|
||
// set the parameters for the histogram | ||
var histogram = d3 | ||
.histogram() | ||
.value(aggregationFuntion) // I need to give the vector of value | ||
.domain(x.domain()); // then the domain of the graphic | ||
|
||
// And apply this function to data to get the bins | ||
var bins = histogram(outings); | ||
|
||
// Y axis: scale and draw: | ||
var y = d3.scaleLinear().range([height, 0]); | ||
y.domain([ | ||
0, | ||
d3.max(bins, function (d) { | ||
return d.length; | ||
}), | ||
]); // d3.hist has to be called before the Y axis obviously | ||
|
||
svg.append('g').call(d3.axisLeft(y)); | ||
|
||
// append the bar rectangles to the svg element | ||
svg | ||
.selectAll('rect') | ||
.data(bins) | ||
.enter() | ||
.append('rect') | ||
.attr('x', 1) | ||
.attr('transform', function (d) { | ||
return 'translate(' + x(d.x0) + ',' + y(d.length) + ')'; | ||
}) | ||
.attr('width', function (d) { | ||
return x(d.x1) - x(d.x0) - 1; | ||
}) | ||
.attr('height', function (d) { | ||
return height - y(d.length); | ||
}) | ||
.style('fill', '#69b3a2'); | ||
} |