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
155
156
157
158
159
160
161
162
163
164
165
166
167
|
import React, { Component } from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import { toArray } from '../util'
import C3Chart from 'react-c3js'
import 'c3/c3.css'
import './chart.css'
import { rainbow, colorblindSafeRainbow, topCountryCount, otherCountriesLabel } from './constants'
class CountriesByYearChart extends Component {
render() {
const { payload } = this.props
const { paper, citations } = payload.data
if (!citations.length) return null
const years = {}
const countries = {}
citations.forEach(citation => {
let { year, addresses } = citation
if (!year || !parseInt(year)) return
year = parseInt(year)
years[year] = years[year] || {}
addresses.forEach(address => {
const { country } = address
if (!country) return
if (!(country in countries)) {
countries[country] = 1
} else {
countries[country] += 1
}
if (country in years[year]) {
years[year][country] += 1
} else {
years[year][country] = 1
}
})
})
let yearList = Object.keys(years).map(year => parseInt(year)).sort()
for (let i = yearList[0]; i < yearList[-1]; i++) {
if (!(i in years)) {
years[i] = {}
}
}
yearList = Object.keys(years).map(year => parseInt(year)).sort()
Object.keys(countries).forEach(country => {
yearList.forEach(year => {
if (!(country in years[year])) years[year][country] = 0
})
})
// figure out the top N countries in the dataset
const countriesByCount = Object.keys(countries).sort((a,b) => countries[b] - countries[a])
// console.log(countriesByCount)
let topCountries = countriesByCount.slice(0, topCountryCount)
let otherCountries = countriesByCount.slice(topCountryCount)
let citationCountsByYear =
[ ['x'].concat(yearList.map(year => String(year))) ]
.concat(
topCountries
.map(country =>
[country]
.concat(
yearList
.map(year => years[year][country])
)
)
)
citationCountsByYear.push(
[otherCountriesLabel].concat(
yearList.map(year => otherCountries.reduce((a,b) => (a + years[year][b]), 0))
)
)
let maxCitationsInAYear = 0
let currentSum = 0
// for each year...
for (let j = 1; j < citationCountsByYear[0].length; j++) {
// for each country
currentSum = 0
for (let i = 1; i < citationCountsByYear.length; i++) {
currentSum += citationCountsByYear[i][j]
}
maxCitationsInAYear = Math.max(currentSum, maxCitationsInAYear)
}
let ticks = []
for (let i = 0; i < maxCitationsInAYear; i += 50) {
ticks.push(i)
}
if (ticks[ticks.length - 1] < maxCitationsInAYear) {
ticks.push('')
}
const colorPattern = rainbow
return (
<div className='chart'>
<C3Chart
data={{
x: 'x',
columns: citationCountsByYear,
type: 'bar',
groups: [ topCountries.concat(otherCountriesLabel) ],
}}
axis={{
x: {
type: 'category' // this needed to load string x value
},
y: {
show: false,
},
y2: {
tick: {
values: ticks,
},
default: [ 0, maxCitationsInAYear * 286 / 261 ],
show: true,
}
}}
legend={{
position: 'right'
}}
color={{
pattern: colorPattern
}}
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('')
}
}}
/>
<div className='caption'>{paper.name}{' dataset citations by country per year'}</div>
</div>
)
}
}
export default CountriesByYearChart
|