-
Notifications
You must be signed in to change notification settings - Fork 43
/
barChart.tsx
219 lines (195 loc) · 7.04 KB
/
barChart.tsx
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import React, { PureComponent } from "react";
import scaleLinear from "d3-scale/src/linear";
import scaleBand from "d3-scale/src/band";
import max from "d3-array/src/max";
import Bar from "./bar";
import Axis from "./axis";
import barCharQuery from "graphQL/home/barChart.graphql";
import { graphqlClient } from "util/graphql";
import { computeSubjectParentId, getChildSubjectsSorted } from "app/subjectsState";
function getSubjectsList(subjectIds) {
return graphqlClient.runQuery(barCharQuery, { subjectIds, searchChildSubjects: true });
}
export default class BarChart extends PureComponent<any, any> {
constructor(props) {
super(props);
this.state = { left: 0, excluding: {}, data: null, initialWidth: props.width };
}
el: any;
barMap = new Map();
sized = ({ bounds }) => {
if (bounds.left != this.state.left) {
this.setState({ left: bounds.left });
}
};
removeBar = id => this.setState((state, props) => ({ excluding: { ...state.excluding, [id]: true } }));
restoreBar = id => this.setState((state, props) => ({ excluding: { ...state.excluding, [id]: false } }));
componentDidMount() {
this.getChart(() => {
if (this.props.chartIndex > 0) {
this.el.scrollIntoView({ behavior: "smooth" });
}
});
}
componentDidUpdate(prevProps, prevState) {
if (prevProps.subjects != this.props.subjects) {
this.getChart();
}
}
getChart = (cb?: any) => {
let { subjects, subjectHash } = this.props;
let subjectIds = subjects.map(s => s._id);
let targetSubjectsLookup = new Set(subjectIds);
let subjectResultsMap = new Map<string, number>([]);
function getApplicableRootSubject(subject) {
let parentId = computeSubjectParentId(subject.path);
if (!parentId) {
return subject;
} else if (targetSubjectsLookup.has(parentId)) {
return subjectHash[parentId];
} else {
return getApplicableRootSubject(subjectHash[parentId]);
}
}
return getSubjectsList(subjectIds).then(resp => {
resp.data.allBooks.Books.forEach(item => {
let subjectsHeld = item.subjects
.filter(_id => subjectHash[_id])
.map(_id => (targetSubjectsLookup.has(_id) ? _id : getApplicableRootSubject(subjectHash[_id])._id));
let uniqueSubjects = Array.from(new Set(subjectsHeld));
let uniqueSubjectString = uniqueSubjects.sort().join(",");
if (!subjectResultsMap.has(uniqueSubjectString)) {
subjectResultsMap.set(uniqueSubjectString, 0);
}
subjectResultsMap.set(uniqueSubjectString, subjectResultsMap.get(uniqueSubjectString) + 1);
});
let data = Array.from(subjectResultsMap).map(([name, count], i) => {
let _ids = name.split(",").filter(s => s);
let names = _ids
.map(_id => subjectHash[_id].name)
.sort()
.join(",");
return {
groupId: name,
count,
display: names,
entries: _ids.map(_id => {
let subject = subjectHash[_id];
return {
name: subject.name,
color: subject.backgroundColor,
children: getChildSubjectsSorted(_id, subjectHash)
};
})
};
});
this.setState({ data }, cb);
});
};
topRef = el => {
if (!el) return;
el.addEventListener("touchstart", this.svgTouch);
};
clearOnTouch = new Set(["text", "h4", "div", "svg"]);
svgTouch = evt => {
if (this.clearOnTouch.has(evt.target.tagName.toLowerCase())) {
if (Array.isArray(this.state.data)) {
this.state.data.forEach(d => {
let componentMaybe = this.barMap.get(d.groupId);
componentMaybe && componentMaybe.hideTooltip();
});
}
}
};
clearAllTooltips = () => {};
render() {
let margin = { top: 20, right: 10, bottom: 180, left: 0 };
let { subjectsLoaded, width, height, drilldown, chartIndex, header } = this.props;
let { data, excluding } = this.state;
if (subjectsLoaded && data && !data.length) {
return (
<div className="alert alert-warning">
It looks like you haven't entered any books yet. Once you do, you'll see info about your library here.
</div>
);
}
if (!subjectsLoaded || !data || !data.length) {
return null;
}
let fullData = data;
data = data.filter(d => !excluding[d.groupId]);
width = Math.min(width, data.length * 110 + 60);
let dataValues = data.map(({ count }) => count);
let displayValues = data.map(({ display }) => display);
let chartHeight = height - margin.top - margin.bottom;
let dataMax = max(dataValues);
let dataScale = scaleLinear()
.domain([0, dataMax])
.range([0, chartHeight]);
let scaleX = scaleBand()
.domain(displayValues)
.range([0, width])
.paddingInner([0.1])
.paddingOuter([0.3])
.align([0.5]);
let svgStyle = { display: "block", marginLeft: "auto", marginRight: "auto" }; //, marginLeft: 'auto', marginRight: 'auto'};
let excludedCount = Object.keys(excluding).filter(k => excluding[k]).length;
return (
<div
ref={el => {
this.topRef(el);
this.el = el;
}}
>
<div style={{ ...width, height }}>
<div>
<h4 style={{ display: "inline" }}>{header}</h4>
{excludedCount ? (
<span style={{ marginLeft: "10px" }}>
Excluding:{" "}
{fullData
.filter(d => excluding[d.groupId])
.map((d, i, arr) => (
<span style={{ marginLeft: "10px" }}>
{d.display}{" "}
<a style={{ color: "black" }} onClick={() => this.restoreBar(d.groupId)}>
<i className="far fa-redo" />
</a>
</span>
))}
</span>
) : null}
</div>
<svg style={svgStyle} width={width} height={height}>
<g transform={`scale(1, -1) translate(${margin.left}, ${margin.bottom - height})`}>
{data
.filter(d => !this.state.excluding[d.groupId])
.map((d, i) => (
<Bar
ref={el => this.barMap.set(d.groupId, el)}
drilldown={drilldown}
chartIndex={chartIndex}
removeBar={this.removeBar}
key={d.groupId}
index={i}
data={d}
count={data.length}
x={scaleX(d.display)}
y={0}
width={scaleX.bandwidth()}
height={dataScale(d.count)}
graphWidth={width}
adjustTooltip={this.state.left}
/>
))}
</g>
<g transform={`translate(${margin.left}, ${-1 * margin.bottom})`}>
<Axis scale={scaleX} transform={`translate(0, ${height})`} />
</g>
</svg>
</div>
<hr />
</div>
);
}
}