Skip to content

Commit 0e6e8cd

Browse files
committed
Make bar chart with only one bar
1 parent d47eb5a commit 0e6e8cd

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

src/visualizations/BarChart.js

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ const margin = {top: 20, right: 10, bottom: 20, left: 25};
88
const red = '#eb6a5b';
99
const green = '#b6e86f';
1010
const blue = '#52b6ca';
11-
const colors = chroma.scale([blue, green, red]);
11+
const colors = chroma.scale([blue, green, red]).mode('hsl');
1212

13-
class LineChart extends Component {
13+
class BarChart extends Component {
1414
state = {
15-
highs: [], // array of rects for high temps
16-
lows: [], // array of rects for low temps
15+
bars: [], // array of rects
1716
// d3 helpers
1817
xScale: d3.scaleTime().range([margin.left, width - margin.right]),
1918
yScale: d3.scaleLinear().range([height - margin.bottom, margin.top]),
@@ -32,30 +31,24 @@ class LineChart extends Component {
3231
// data has changed, so recalculate scale domains
3332
const timeDomain = d3.extent(data, d => d.date);
3433
const tempMax = d3.max(data, d => d.high);
35-
const tempMin = d3.min(data, d => d.low);
34+
const colorDomain = d3.extent(data, d => d.avg);
3635
xScale.domain(timeDomain);
3736
yScale.domain([0, tempMax]);
38-
colorScale.domain([tempMin, tempMax]);
37+
colorScale.domain(colorDomain);
3938

4039
// calculate x and y for each rectangle
41-
const highs = data.map(d => {
42-
const y = yScale(d.high);
40+
const bars = data.map(d => {
41+
const y1 = yScale(d.high);
42+
const y2 = yScale(d.low);
4343
return {
4444
x: xScale(d.date),
45-
y, height: height - margin.bottom - y,
46-
fill: colors(colorScale(d.high)),
47-
}
48-
});
49-
const lows = data.map(d => {
50-
const y = yScale(d.low);
51-
return {
52-
x: xScale(d.date),
53-
y, height: height - margin.bottom - y,
54-
fill: colors(colorScale(d.low)),
45+
y: y1,
46+
height: y2 - y1,
47+
fill: colors(colorScale(d.avg)),
5548
}
5649
});
5750

58-
return {lows, highs};
51+
return {bars};
5952
}
6053

6154
componentDidUpdate() {
@@ -67,9 +60,7 @@ class LineChart extends Component {
6760

6861
return (
6962
<svg width={width} height={height}>
70-
{this.state.highs.map(d =>
71-
(<rect x={d.x} y={d.y} width='2' height={d.height} fill={d.fill} />))}
72-
{this.state.lows.map(d =>
63+
{this.state.bars.map(d =>
7364
(<rect x={d.x} y={d.y} width='2' height={d.height} fill={d.fill} />))}
7465
<g>
7566
<g ref='xAxis' transform={`translate(0, ${height - margin.bottom})`} />
@@ -80,4 +71,4 @@ class LineChart extends Component {
8071
}
8172
}
8273

83-
export default LineChart;
74+
export default BarChart;

0 commit comments

Comments
 (0)