Skip to content

Commit a84e8b0

Browse files
change data.tsv location
1 parent b8c71fd commit a84e8b0

File tree

3 files changed

+49
-103
lines changed

3 files changed

+49
-103
lines changed

src/App.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import React, { Component } from 'react';
22

33
import HBarChart from './components/HBarChart';
44
import './App.css';
5+
import data from './data.tsv';
56

67
class App extends Component {
78
render() {
89
return (
910
<div className="App">
1011
<header className="App-header">
11-
<HBarChart />
12+
<HBarChart data={data} />
1213
</header>
1314
</div>
1415
);

src/components/HBarChart/index.js

Lines changed: 47 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,93 @@
11
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import * as d3 from 'd3';
4-
import tData from './data.tsv';
54

65
class HBarChart extends Component {
7-
constructor(props) {
8-
super(props);
9-
10-
this.state = {
11-
// svgWidth: props.width,
12-
// svgHeight: props.height,
13-
// svgMargin: props.margin,
14-
// padding: props.padding,
15-
16-
// xScale: d3
17-
// .scaleBand()
18-
// .range([props.margin.left, props.width - props.margin.right])
19-
// .padding(props.padding),
20-
// yScale: d3
21-
// .scaleLinear()
22-
// .range([props.height - props.margin.bottom, props.margin.top]),
23-
// xAxisRef: null,
24-
// yAxisRef: null,
25-
};
26-
27-
// this.xAxis = d3.axisBottom().scale(this.state.xScale);
28-
// this.yAxis = d3
29-
// .axisRight()
30-
// .scale(this.state.yScale)
31-
// .tickFormat(d => `${d * 100}%`);
32-
}
33-
346
componentDidMount() {
35-
this.calculateChart();
36-
};
37-
38-
componentDidUpdate() {
39-
// d3.select(this.state.xAxisRef).call(this.xAxis);
40-
// d3.select(this.state.yAxisRef).call(this.yAxis);
41-
}
42-
43-
// xAxisRef = element => {
44-
// this.setState({ xAxisRef: element });
45-
// d3.select(element).call(this.xAxis);
46-
// };
47-
48-
// yAxisRef = element => {
49-
// this.setState({ yAxisRef: element });
50-
// d3.select(element).call(this.yAxis);
51-
// };
52-
53-
type = d => {
54-
d.value = +d.value;
55-
return d;
7+
if (this.props.data !== null)
8+
this.calculateChart();
569
};
5710

5811
calculateChart = () => {
59-
var margin = {top: 20, right: 30, bottom: 40, left: 30},
60-
width = 960 - margin.left - margin.right,
61-
height = 500 - margin.top - margin.bottom;
62-
63-
var x = d3.scaleLinear()
64-
.range([0, width]);
65-
66-
var y = d3.scaleBand()
67-
.range([0, height])
68-
.paddingInner(0.2);
69-
70-
var xAxis = d3.axisBottom()
12+
const { data, width, height, margin } = this.props;
13+
const svgWidth = width - margin.left - margin.right;
14+
const svgHeight = height - margin.top - margin.bottom;
15+
16+
const x = d3
17+
.scaleLinear()
18+
.range([0, svgWidth]);
19+
20+
const y = d3
21+
.scaleBand()
22+
.range([0, svgHeight])
23+
.paddingInner(0.2)
24+
.paddingOuter(0.2);
25+
26+
const xAxis = d3
27+
.axisBottom()
7128
.scale(x);
7229

73-
var yAxis = d3.axisRight()
30+
const yAxis = d3
31+
.axisRight()
7432
.scale(y)
7533
.tickSize(0)
7634
.tickPadding(6);
7735

78-
var svg = d3.select("#container").append("svg")
79-
.attr("width", width + margin.left + margin.right)
80-
.attr("height", height + margin.top + margin.bottom)
81-
.append("g")
82-
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
83-
84-
d3.tsv(tData, d => ({
36+
const svg = d3
37+
.select('#container')
38+
.append('svg')
39+
.attr('width', svgWidth + margin.left + margin.right)
40+
.attr('height', svgHeight + margin.top + margin.bottom)
41+
.append('g')
42+
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
43+
44+
d3.tsv(data, d => ({
8545
name: d.name,
8646
value: d.value,
8747
}))
8848
.then(data => {
8949
x.domain(d3.extent(data, d => parseInt(d.value))).nice()
9050
y.domain(data.map(d => d.name))
91-
svg.selectAll(".bar")
92-
.data(data)
93-
.enter().append("rect")
94-
.attr("class", d => "bar bar--" + (d.value < 0 ? "negative" : "positive"))
95-
.attr("x", d => x(Math.min(0, d.value)))
96-
.attr("y", d => y(d.name))
97-
.attr("width", d => Math.abs(x(d.value) - x(0)))
98-
.attr("height", y.bandwidth);
99-
100-
svg.append("g")
101-
.attr("class", "x axis")
102-
.attr("transform", "translate(0," + height + ")")
51+
svg.selectAll('.bar')
52+
.data(data)
53+
.enter().append('rect')
54+
.attr('class', d => 'bar bar--' + (d.value < 0 ? 'negative' : 'positive'))
55+
.attr('x', d => x(Math.min(0, d.value)))
56+
.attr('y', d => y(d.name))
57+
.attr('width', d => Math.abs(x(d.value) - x(0)))
58+
.attr('height', y.bandwidth);
59+
60+
svg.append('g')
61+
.attr('class', 'x axis')
62+
.attr('transform', 'translate(0,' + svgHeight + ')')
10363
.call(xAxis);
10464

105-
svg.append("g")
106-
.attr("class", "y axis")
107-
.attr("transform", "translate(" + x(0) + ",0)")
65+
svg.append('g')
66+
.attr('class', 'y axis')
67+
.attr('transform', 'translate(' + x(0) + ',0)')
10868
.call(yAxis);
10969
});
11070
}
11171

11272
render() {
113-
const { svgWidth, svgHeight, svgMargin } = this.state;
11473
return (
115-
<div id="container">
116-
{/* <svg id="hbarchart" width={svgWidth} height={svgHeight}>
117-
<g id="wrapper" transform="translate(40, 20)">
118-
</g>
119-
<g>
120-
<g
121-
ref={this.xAxisRef}
122-
transform={`translate(0, ${svgHeight - svgMargin.bottom})`}
123-
/>
124-
<g
125-
ref={this.yAxisRef}
126-
transform={`translate(${svgMargin.left}, 0)`} />
127-
</g>
128-
</svg> */}
129-
</div>
74+
<div id="container" />
13075
);
13176
}
13277
}
13378

13479
HBarChart.propTypes = {
80+
data: PropTypes.string ,
13581
width: PropTypes.number,
13682
height: PropTypes.number,
13783
margin: PropTypes.object,
138-
padding: PropTypes.number,
13984
};
14085

14186
HBarChart.defaultProps = {
87+
data: null,
14288
width: 1000,
14389
height: 600,
14490
margin: { top: 20, right: 5, bottom: 20, left: 35 },
145-
padding: 0.2,
14691
};
14792

14893
export default HBarChart;
File renamed without changes.

0 commit comments

Comments
 (0)