Skip to content

Commit b8c71fd

Browse files
initial draw
1 parent 00c4b68 commit b8c71fd

File tree

1 file changed

+96
-30
lines changed

1 file changed

+96
-30
lines changed

src/components/HBarChart/index.js

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

56
class HBarChart extends Component {
67
constructor(props) {
78
super(props);
89

910
this.state = {
10-
svgWidth: props.width,
11-
svgHeight: props.height,
12-
svgMargin: props.margin,
13-
padding: props.padding,
14-
15-
xScale: d3
16-
.scaleBand()
17-
.range([props.margin.left, props.width - props.margin.right])
18-
.padding(props.padding),
19-
yScale: d3
20-
.scaleLinear()
21-
.range([props.height - props.margin.bottom, props.margin.top]),
22-
xAxisRef: null,
23-
yAxisRef: null,
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,
2425
};
2526

26-
this.xAxis = d3.axisBottom().scale(this.state.xScale);
27-
this.yAxis = d3
28-
.axisLeft()
29-
.scale(this.state.yScale)
30-
.tickFormat(d => `${d}%`);
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}%`);
3132
}
3233

34+
componentDidMount() {
35+
this.calculateChart();
36+
};
37+
3338
componentDidUpdate() {
34-
d3.select(this.state.xAxisRef).call(this.xAxis);
35-
d3.select(this.state.yAxisRef).call(this.yAxis);
39+
// d3.select(this.state.xAxisRef).call(this.xAxis);
40+
// d3.select(this.state.yAxisRef).call(this.yAxis);
3641
}
3742

38-
xAxisRef = element => {
39-
this.setState({ xAxisRef: element });
40-
d3.select(element).call(this.xAxis);
41-
};
43+
// xAxisRef = element => {
44+
// this.setState({ xAxisRef: element });
45+
// d3.select(element).call(this.xAxis);
46+
// };
4247

43-
yAxisRef = element => {
44-
this.setState({ yAxisRef: element });
45-
d3.select(element).call(this.yAxis);
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;
4656
};
4757

58+
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()
71+
.scale(x);
72+
73+
var yAxis = d3.axisRight()
74+
.scale(y)
75+
.tickSize(0)
76+
.tickPadding(6);
77+
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 => ({
85+
name: d.name,
86+
value: d.value,
87+
}))
88+
.then(data => {
89+
x.domain(d3.extent(data, d => parseInt(d.value))).nice()
90+
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 + ")")
103+
.call(xAxis);
104+
105+
svg.append("g")
106+
.attr("class", "y axis")
107+
.attr("transform", "translate(" + x(0) + ",0)")
108+
.call(yAxis);
109+
});
110+
}
111+
48112
render() {
49113
const { svgWidth, svgHeight, svgMargin } = this.state;
50114
return (
51115
<div id="container">
52-
<svg id="hbarchart" width={svgWidth} height={svgHeight}>
116+
{/* <svg id="hbarchart" width={svgWidth} height={svgHeight}>
117+
<g id="wrapper" transform="translate(40, 20)">
118+
</g>
53119
<g>
54120
<g
55121
ref={this.xAxisRef}
@@ -59,7 +125,7 @@ class HBarChart extends Component {
59125
ref={this.yAxisRef}
60126
transform={`translate(${svgMargin.left}, 0)`} />
61127
</g>
62-
</svg>
128+
</svg> */}
63129
</div>
64130
);
65131
}

0 commit comments

Comments
 (0)