Skip to content

Commit

Permalink
Histogram animate state
Browse files Browse the repository at this point in the history
  • Loading branch information
sxywu committed Nov 15, 2018
1 parent e3175a2 commit 9186413
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions src/components/Histogram.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<template>
<svg class='histogram' :width='width' :height='height'>
<transition-group tag='g' :css='false' @enter='enter' @leave='leave'>
<g class='bars'>
<rect v-for='d in bars' :key='d.id' :x='d.x' :width='d.width'
:y='d.y' :height='d.height' :fill=d.fill :stroke='d.fill' />
</transition-group>
</g>
<g ref='xAxis' :transform='`translate(0, ${height - margin.bottom})`' />
<g ref='brush' />
</svg>
Expand All @@ -12,6 +12,7 @@
<script>
import _ from 'lodash'
import * as d3 from 'd3'
import {TweenMax} from 'gsap'
const width = 500
const height = 120
Expand All @@ -38,6 +39,7 @@ export default {
[margin.left, margin.top],
[width - margin.right, height - margin.bottom]
]).on('brush', this.brushEnd)
.on('end', this.brushEnd)
},
mounted() {
d3.select(this.$refs.brush).call(this.brush)
Expand Down Expand Up @@ -80,21 +82,38 @@ export default {
const bins = this.histogram(this.filtered)
// calculate rect bar for each bin
this.bars = bins.map(d => {
this.bars = bins.map((d, i) => {
const {x0, x1} = d
const x = this.xScale(x0)
const y = this.yScale(d.length)
const median = d3.median(d, d => d.score) || 0
const y = this.bars[i] ? this.bars[i].toY : this.yScale(0)
const toY = this.yScale(d.length)
const fill = this.colorScale(d3.median(d, d => d.score) || 0)
return {
id: `${x0}-${x1}`,
id: i,
x,
width: this.xScale(x1) - x,
y,
y, toY,
height: height - margin.bottom - y,
fill: this.colorScale(median),
toHeight: height - margin.bottom - toY,
fill,
}
})
// and then animate them
const tween = TweenMax.staggerFromTo(this.bars, 0.25, {
// from
cycle: {
y: (i) => this.bars[i].y,
height: (i) => this.bars[i].height,
}
}, {
// to
cycle: {
y: (i) => this.bars[i].toY,
height: (i) => this.bars[i].toHeight,
},
})
},
brushEnd: function() {
let bounds = null;
Expand All @@ -107,18 +126,6 @@ export default {
}
this.updateFilters({[this.id]: bounds});
},
enter: function (el, done) {
TweenLite.fromTo(el, 0.5,
{scaleY: 0},
{scaleY: 1, onComplete: done}
)
},
leave: function (el, done) {
TweenLite.fromTo(el, 0.5,
{scaleY: 1},
{scaleY: 0, onComplete: done}
)
},
}
}
</script>
Expand Down

0 comments on commit 9186413

Please sign in to comment.