fathom/assets/js/components/Graph.js

163 lines
4.3 KiB
JavaScript
Raw Normal View History

2016-11-23 18:40:35 +00:00
'use strict';
import { h, render, Component } from 'preact';
2016-12-04 12:23:09 +00:00
import * as d3 from 'd3';
import tip from 'd3-tip';
d3.tip = tip;
2016-11-23 18:40:35 +00:00
2016-12-04 12:23:09 +00:00
const dayInSeconds = 60 * 60 * 24;
2016-11-23 19:30:09 +00:00
2016-11-23 18:40:35 +00:00
class Graph extends Component {
constructor(props) {
super(props)
this.state = {
2016-11-23 19:30:09 +00:00
visitorData: [],
pageviewData: []
2016-11-23 18:40:35 +00:00
}
2016-12-04 12:23:09 +00:00
2016-11-23 18:40:35 +00:00
this.fetchData = this.fetchData.bind(this);
this.refreshChart = this.refreshChart.bind(this);
2016-12-04 12:23:09 +00:00
}
componentDidMount() {
this.fetchData(this.props.period);
2016-11-23 20:29:54 +00:00
}
componentWillReceiveProps(newProps) {
if(this.props.period != newProps.period) {
this.fetchData(newProps.period)
}
2016-11-23 18:40:35 +00:00
}
2016-11-23 19:30:09 +00:00
refreshChart() {
var padt = 10, padb = 20, padr = 40, padl = 40,
2016-12-04 12:23:09 +00:00
h = 300,
w = document.getElementById('graph').parentNode.clientWidth - padl-padr,
x = d3.scaleBand().range([0, w]).padding(0.2).round(true),
2016-12-04 12:23:09 +00:00
y = d3.scaleLinear().range([h, 0]),
yAxis = d3.axisLeft().scale(y).tickSize(-w + padl + padr),
2016-12-05 14:31:34 +00:00
xAxis = d3.axisBottom().scale(x),
visitorData = this.state.visitorData,
pageviewData = this.state.pageviewData,
xTick = Math.round(pageviewData.length / 7);
2016-12-04 12:23:09 +00:00
var pageviewTip = d3.tip()
.attr('class', 'd3-tip')
.html(function(d) { return '<span>' + d.Count + '</span>' + ' pageviews' })
.offset([-12, 0]);
var visitorTip = d3.tip()
2016-12-04 12:23:09 +00:00
.attr('class', 'd3-tip')
.html(function(d) { return '<span>' + d.Count + '</span>' + ' visitors' })
.offset([-12, 0]);
var graph = d3.select('#graph');
// remove previous graph
graph.selectAll('*').remove();
var vis = graph
2016-12-04 12:23:09 +00:00
.append('svg')
.attr('width', w + padl + padr)
2016-12-04 12:23:09 +00:00
.attr('height', h + padt + padb)
.append('g')
.attr('transform', 'translate(' + padl + ',' + padt + ')');
vis.call(pageviewTip);
vis.call(visitorTip);
var max = d3.max(pageviewData, function(d) { return d.Count });
x.domain(pageviewData.map((d) => d.Label))
y.domain([0, (max * 1.1)])
2016-12-04 12:23:09 +00:00
var barWidth = x.bandwidth();
2016-12-04 12:23:09 +00:00
// axes
vis.selectAll('g.axis').remove();
2016-12-04 12:23:09 +00:00
vis.append("g")
.attr("class", "y axis")
.call(yAxis);
vis.append("g")
.attr("class", "x axis")
.attr('transform', 'translate(0,' + h + ')')
.call(xAxis)
.selectAll('g')
.style('display', (d, i) => i % xTick != 0 ? 'none' : 'block')
2016-12-04 12:23:09 +00:00
// bars
vis.selectAll('g.primary-bar').remove();
var bars = vis.selectAll('g.primary-bar')
.data(pageviewData)
2016-12-04 12:23:09 +00:00
.enter().append('g')
.attr('class', 'primary-bar')
2016-12-05 14:31:34 +00:00
.attr('transform', function (d, i) { return "translate(" + x(d.Label) + ", 0)" });
2016-12-04 12:23:09 +00:00
bars.append('rect')
.attr('width', barWidth)
.attr('height', (d) => (h - y(d.Count)) )
.attr('y', (d) => y(d.Count))
.on('mouseover', pageviewTip.show)
.on('mouseout', pageviewTip.hide);
vis.selectAll('g.sub-bar').remove();
var visitorBars = vis.selectAll('g.sub-bar')
.data(visitorData)
.enter().append('g')
.attr('class', 'sub-bar')
.attr('transform', (d, i) => "translate(" + ( x(d.Label) + ( barWidth / 4 ) ) + ", 0)");
visitorBars.append('rect')
.attr('width', barWidth / 2 )
2016-12-04 12:23:09 +00:00
.attr('height', (d) => (h - y(d.Count)) )
.attr('y', (d) => y(d.Count))
.on('mouseover', visitorTip.show)
.on('mouseout', visitorTip.hide);
2016-11-23 18:40:35 +00:00
}
2016-11-23 20:29:54 +00:00
fetchData(period) {
const before = Math.round((+new Date() ) / 1000);
const after = before - ( period * dayInSeconds );
2016-11-23 19:30:09 +00:00
// fetch visitor data
fetch(`/api/visits/count/day?before=${before}&after=${after}`, {
2016-11-23 19:30:09 +00:00
credentials: 'include'
}).then((r) => {
if( r.ok ) {
return r.json();
}
throw new Error();
}).then((data) => {
this.setState({ visitorData: data })
window.requestAnimationFrame(this.refreshChart);
2016-11-23 19:30:09 +00:00
});
// fetch pageview data
fetch(`/api/pageviews/count/day?before=${before}&after=${after}`, {
credentials: 'include'
}).then((r) => {
if( r.ok ) {
return r.json();
}
throw new Error();
}).then((data) => {
this.setState({ pageviewData: data })
window.requestAnimationFrame(this.refreshChart);
});
2016-11-23 18:40:35 +00:00
}
render() {
return (
<div class="block">
2016-12-04 12:23:09 +00:00
<div id="graph"></div>
2016-11-23 18:40:35 +00:00
</div>
)
}
}
export default Graph