summaryrefslogtreecommitdiff
path: root/client/chart/singlePie.chart.js
diff options
context:
space:
mode:
authoradamhrv <adam@ahprojects.com>2019-07-03 13:47:23 +0200
committeradamhrv <adam@ahprojects.com>2019-07-03 13:47:23 +0200
commit2816f045090b99f48f0b26d305eee27e1ab94d85 (patch)
tree7b2179baa6ab6dcce970218ae9e4935b62b64561 /client/chart/singlePie.chart.js
parent36a226c3fb3379f4f332c1daad7fe85e2bbea954 (diff)
parent411aa602b9cf886758c4ff5ca5550c43ae7b7804 (diff)
fix merge
Diffstat (limited to 'client/chart/singlePie.chart.js')
-rw-r--r--client/chart/singlePie.chart.js36
1 files changed, 12 insertions, 24 deletions
diff --git a/client/chart/singlePie.chart.js b/client/chart/singlePie.chart.js
index fcff3214..2e770bd7 100644
--- a/client/chart/singlePie.chart.js
+++ b/client/chart/singlePie.chart.js
@@ -2,41 +2,28 @@ import React, { Component } from 'react'
import csv from 'parse-csv'
import C3Chart from 'react-c3js'
-import { toTuples } from '../util'
-
import 'c3/c3.css'
import './chart.css'
import {
- rainbow, otherCountriesLabel,
- institutionOrder, institutionLabels,
- initialInstitutionLookup,
+ rainbow, bigRainbow, colorTable
} from './constants'
class SinglePieChart extends Component {
state = {
keys: [],
data: [],
- fields: {},
}
componentDidMount() {
const { payload } = this.props
- console.log(payload)
- console.log(payload.fields)
- const fields = {}
- payload.fields.forEach(field => {
- const [k, v] = field.split(': ')
- fields[k] = v
- })
-
fetch(payload.url, { mode: 'cors' })
.then(r => r.text())
.then(text => {
try {
const keys = text.split('\n')[0].split(',').map(s => s.trim().replace(/"/, ''))
const data = csv.toJSON(text, { headers: { included: true } })
- this.setState({ keys, data, fields })
+ this.setState({ keys, data })
} catch (e) {
console.error("error making json:", payload.url)
console.error(e)
@@ -45,9 +32,9 @@ class SinglePieChart extends Component {
}
render() {
- const { payload } = this.props
- const { keys, data, fields } = this.state
- console.log(keys, data)
+ const { fields } = this.props.payload
+ const { keys, data } = this.state
+ // console.log(keys, data)
const [labelField, numberField] = keys
if (!data.length) return null
@@ -59,15 +46,16 @@ class SinglePieChart extends Component {
return [label, number]
}).sort((a, b) => b[1] - a[1])
- console.log(rows)
-
let chartRows = rows.slice(0, rowsToDisplay)
- let otherCount = rows.slice(rowsToDisplay).reduce((a, b) => { return a + b[1] }, 0)
+ let otherCount = rows.slice(rowsToDisplay).reduce((a, b) => a + b[1], 0)
if (otherCount > 0) {
chartRows.push([fields.OtherLabel, otherCount])
}
- console.log(rowsToDisplay, chartRows)
+ const height = chartRows.length < 6 ? 316 :
+ chartRows.length < 10 ? 336 : 356
+
+ const pattern = colorTable[fields.Colors] || (chartRows.length < 10 ? rainbow : bigRainbow)
return (
<div className='chart'>
@@ -78,7 +66,7 @@ class SinglePieChart extends Component {
type: 'pie',
}}
color={{
- pattern: rainbow,
+ pattern,
}}
tooltip={{
format: {
@@ -86,7 +74,7 @@ class SinglePieChart extends Component {
}
}}
size={{
- height: chartRows.length < 4 ? 316 : 336,
+ height,
}}
/>
<span className='chartCaption'>{fields.Caption}</span>