D3.js vs Vue.js examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.0 KiB

7 years ago
<!--
7 years ago
Links:
Events: https://vuejs.org/v2/guide/events.html
Computed properties: https://vuejs.org/v2/guide/computed.html
Data:
http://worldpopulationreview.com/states/
7 years ago
-->
<template>
<div id="holder">
7 years ago
<div class="mapHolder">
<us-map
v-on:stateSelected="onStateSelected"
v-on:stateDeselected="onStateDeselected"
/>
</div>
<tooltip
v-if="currentState"
:title="currentState.Name"
:description="currentStateDescription"
/>
7 years ago
</div>
</template>
<script>
var _ = require('lodash');
7 years ago
7 years ago
const map = require('d3-components/basic-map-tooltip/map');
const tooltip = require('d3-components/basic-map-tooltip/tooltip');
7 years ago
const STATES_DATA_PATH = 'static/data/states-data.csv';
// lets load with vue-resource, but parse with d3
// just because we can
import * as d3 from 'd3-dsv';
7 years ago
export default {
components: {
usMap: map,
tooltip: tooltip
},
7 years ago
created: function() {
var that = this;
this.$http.get(STATES_DATA_PATH)
.then(function(res) {
this.statesData = {};
d3.dsvFormat(';')
.parse(res.data, d => {
var population = d["2017 Population"].split(',').join('');
d.value = +population;
that.statesData[d.STATE_ABBR] = d;
delete d["2017 Population"];
delete d["STATE_ABBR"];
return d;
});
})
},
7 years ago
data: function() {
return {
7 years ago
statesData: undefined,
currentState: undefined
}
},
computed: {
currentStateDescription: function() {
return "Population: " + this.currentState.value;
7 years ago
}
},
methods: {
7 years ago
onStateSelected: function(stateCode) {
this.currentState = this.statesData[stateCode];
},
onStateDeselected: function(stateCode) {
this.currentState = undefined;
}
7 years ago
}
}
</script>
7 years ago
<style scoped>
#holder {
position: relative;
height: 300px;
width: 500px;
margin: auto;
}
.mapHolder {
position: absolute;
margin: auto;
}
7 years ago
</style>