summaryrefslogtreecommitdiff
path: root/client/chart/pie.charts.js
blob: 939e9262cbed513c5e2a7db412e37e77c0b85051 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import React, { Component } from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import { toArray, toTuples } from '../util'
import C3Chart from 'react-c3js'
import 'c3/c3.css'
import './chart.css'

import {
  rainbow, colorblindSafeRainbow,
  topCountryCount, otherCountriesLabel,
  institutionOrder, institutionLabels, institutionColors,
  initialInstitutionLookup,
} from './constants'

class PieCharts extends Component {
  render() {
    const { payload } = this.props
    const { paper, citations } = payload.data
    // console.log(this.props)
    if (!citations.length) return null

    const countries = {}
    const institutionTypes = { ...initialInstitutionLookup }
    citations.forEach(citation => {
      citation.addresses.forEach(address => {
        const { country } = address
        if (!country) return
        if (!(country in countries)) {
          countries[country] = 1
        } else {
          countries[country] += 1
        }
        switch (address.type) {
          case 'company':
          case 'edu':
          case 'gov':
            institutionTypes[address.type] += 1
            break
          case 'mil':
            institutionTypes['gov'] += 1
            break
          default:
            console.log('weird institution type', address)
            break
        }
      })
    })

    const countryTuples = toTuples(countries).sort((a,b) => b[1] - a[1])
    let countryRows = countryTuples.slice(0, 10)

    const otherCountryCount = countryTuples.slice(10).reduce((a,b) => (a + b[1]), 0)
    if (otherCountryCount > 0) {
      countryRows.push([
        otherCountriesLabel,
        otherCountryCount,
      ])
    }
    // console.log(countryTuples, otherCountryCount, countryRows)

    const institutionRows = toTuples(institutionTypes)
      .map(a => [institutionOrder[a[0]], a])
      .sort((a,b) => a[0] - b[0])
      .map(a => a[1])
      .map(a => [institutionLabels[a[0]], a[1]])

    const colorPattern = rainbow

    return (
      <div className='chart'>
        <div>
          <C3Chart
            data={{
              columns: countryRows,
              type: 'pie',
            }}
            color={{
              pattern: rainbow,
            }}
            tooltip={{
              format: {
                value: value => value,
              }
            }}
            size={{
              height: countryRows.length < 4 ? 316 : 336,
            }}
          />
          <span className='chartCaption'>{citations.length + ' verified ' + paper.name + ' dataset citations by country'}</span>
        </div>
        <div>
          <C3Chart
            data={{
              columns: institutionRows,
              type: 'pie',
            }}
            color={{
              pattern: institutionColors,
            }}
            tooltip={{
              format: {
                value: value => value,
              }
            }}
            size={{
              height: 316,
            }}
          />
          <span className='chartCaption'>{citations.length + ' verified ' + paper.name + ' dataset citations by organization type'}</span>
        </div>
      </div>
    )
  }
}

/*
          legend={{
            position: 'right'
          }}
          tooltip={{
            contents: function (d, defaultTitleFormat, defaultValueFormat, color) {
              const countriesByYearLookup = years[yearList[d[0].x]]
              let countriesByYear = Object.keys(countriesByYearLookup).map(country => [country, countriesByYearLookup[country]]).sort((a,b) => b[1] - a[1])
              let topCountriesForThisYear = countriesByYear.slice(0, topCountryCount)
              let bottomTotal = countriesByYear.slice(topCountryCount).reduce((a,b) => (a + b[1]), 0)
              // console.log(topCountriesForThisYear)
              topCountriesForThisYear.push([otherCountriesLabel, bottomTotal])
              const tableRows = topCountriesForThisYear.filter(pair => !!pair[1]).map(([country, total]) => {
                let colorIndex = topCountries.indexOf(country)
                if (colorIndex < 0) colorIndex = colorPattern.length - 1
                const color = colorPattern[ colorIndex ]
                return [
                  "<tr>",
                  "<td>",
                    "<span style='background-color:" + color + "' class='swatch'></span>",
                    country,
                  "</td>",
                  "<td>",
                    total,
                  "</td>",
                  "</tr>",
                ].join('')
              })
              return [
                "<table class='c3-tooltip'>",
                ...tableRows,
                "</table>",
              ].join('')
            }
          }}
*/

export default PieCharts