Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Charts! #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"redux": "^4.0.5"
},
"devDependencies": {
"d3": "^5.15.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-react-hooks": "^2.3.0",
"prettier": "^1.19.1"
Expand Down
72 changes: 72 additions & 0 deletions src/components/Chart.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React, { useMemo } from 'react'
import * as d3 from 'd3'

const textMax = 120
const svgWidth = 600
const svgHeight = 300
const margin = {
top: 20,
right: 20,
bottom: 100,
left: 100,
}
const graphWidth = svgWidth - margin.left - margin.right
const graphHeight = svgHeight - margin.top - margin.bottom

export default ({ fortunes = [] }) => {
const svg = d3
.select('.canvas')
.append('svg')
.attr('width', svgWidth)
.attr('height', svgHeight)
const graph = svg
.append('g')
.attr('width', graphWidth)
.attr('height', graphHeight)
.attr('transform', `translate(${margin.left}, ${margin.top})`)
const xAxisGroup = graph
.append('g')
.attr('transform', `translate(0, ${graphHeight})`)
const yAxisGroup = graph.append('g')
//const maxShown = d3.max(fortunes, d => d.shown)
const maxScore = d3.max(fortunes, d => d.score)
//const maxVotes = d3.max(fortunes, d => d.votes)

const yScale = d3
.scaleLinear()
.domain([0, maxScore])
.range([graphHeight, 0])
const xScale = d3
.scaleBand()
.domain(fortunes.map(item => item.text))
.range([0, graphWidth])
.paddingInner(0.2)

const rects = graph.selectAll('rect').data(fortunes)
rects
.enter()
.append('rect')
.attr('width', xScale.bandwidth)
.attr('height', d => graphHeight - yScale(d.score))
.attr('x', d => xScale(d.text.slice(0, textMax)))
.attr('y', d => yScale(d.score))
.style('fill', 'white')

const xAxis = d3.axisBottom(xScale)
const yAxis = d3
.axisLeft(yScale)
.ticks(3)
.tickFormat(d => `${d} shown`)

xAxisGroup.call(xAxis)
yAxisGroup.call(yAxis)

xAxisGroup
.selectAll('text')
.attr('text-anchor', 'end')
.attr('transform', 'rotate(-40)')
.attr('fill', 'white')
.attr('font-size', '.5rem')

return <div className="canvas"></div>
}
11 changes: 9 additions & 2 deletions src/components/Stats.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useMemo } from 'react'
import { css } from 'glamor'
import Chart from './Chart'

let style = css({
fontSize: '2.5rem',
Expand All @@ -12,22 +13,28 @@ let style = css({
},
})

export default props => {
export default ({ fortunes = [] }) => {
let totals = { votes: 1, shown: 1 }

const findTotal = fortunes => {
let votes = 0, shown = 0
if (fortunes) {
fortunes.forEach(f => {
votes += f.votes
shown += f.shown
f.score = votes / shown
})
}
return { votes: votes, shown: shown }
}
totals = useMemo(() => findTotal(props.fortunes), [props.fortunes])

// Remove empty items from Firebase.
fortunes = useMemo(() => fortunes.filter(obj => obj), [fortunes])
totals = useMemo(() => findTotal(fortunes), [fortunes])

return (
<section className={style}>
<Chart fortunes={fortunes} />
{Math.round(
(parseInt(totals.votes) / parseInt(totals.shown)) * 100
)}
Expand Down