-
Notifications
You must be signed in to change notification settings - Fork 1
/
LabelByChartComponent.jsx
107 lines (95 loc) · 3.03 KB
/
LabelByChartComponent.jsx
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
import React from 'react'
import * as d3 from "d3"
import dc from 'dc'
import crossfilter from 'crossfilter'
import '../../../node_modules/dc/dc.css'
export default class LabelByChartComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
chartSection : null
};
}
componentWillMount() {
this.makeChartDOM();
}
componentDidMount() { //after DOM make D3 Chart
this.makeChart();
}
componentDidUpdate() {
if(this.state.chartSection == null){
this.makeChartDOM();
}
this.makeChart();
}
makeChart(){
const chartSection1 = [];
let section = d3.keys(this.props.data);
for (const numSection of section){
this.createChart(numSection, this.props.data);
}
}
makeChartDOM(){
const chartSection1 = [];
let section = d3.keys(this.props.data);
for (const numSection of section){
chartSection1.push(<dl className='data-box' key = {numSection}>
<dt><span>{numSection}</span></dt>
<dd id={"_" + numSection}></dd>
</dl>);
}
this.setState({chartSection: chartSection1});
}
createChart(section, allData){
let data = allData[section];
let domianList = this.arraySwap(section, allData);
let colorList = this.createColorArray(section, allData);
let chart = dc.pieChart('#_' + section,'_' + section);
let ndx = crossfilter(data);
let Dimension = ndx.dimension(function(d) {return d.label;});
let dataGroup = Dimension.group().reduceSum(function(d) {return d.value;});
let colorScale = d3.scale.ordinal().domain(domianList).range(colorList);
chart
.width(200)
.height(200)
.innerRadius(0)
.dimension(Dimension)
.colors(function(d){
return colorScale(d)
})
.transitionDuration(1500)
.group(dataGroup);
chart.render('_' + section);
}
arraySwap(section, allData){
let selData = allData[section];
let temp = selData[0];
let domain = []
for(let data of selData){
domain.push(data.label)
}
return domain;
}
createColorArray(section, allData){
let data = allData[section]
let color = [];
let wrong = ['#3182bd', '#CCCCCC']
for(let i = 0 ; i < data.length; i++){
if(data[i].label == section){
color.push('#6baed6')
}else if(i%2 == 0){
color.push(wrong[0])
}else{
color.push(wrong[1])
}
}
return color;
}
render() {
return (
<article>
{this.state.chartSection}
</article>
)
}
}