xValues: React.PropTypes.arrayOf(React.PropTypes.any),
height: React.PropTypes.number,
padding: React.PropTypes.arrayOf(React.PropTypes.number),
- barsWidth: React.PropTypes.number,
+ barsWidth: React.PropTypes.number.isRequired,
onBarClick: React.PropTypes.func
},
return {
xTicks: [],
xValues: [],
- padding: [10, 10, 10, 10],
- barsWidth: 40
+ padding: [10, 10, 10, 10]
};
},
}
let ticks = this.props.xTicks.map((tick, index) => {
let point = this.props.data[index];
- let x = Math.round(xScale(point.x) + xScale.rangeBand() / 2 + this.props.barsWidth / 2);
+ let x = Math.round(xScale(point.x) + xScale.rangeBand() / 2);
let y = yScale.range()[0];
let d = this.props.data[index];
let tooltipAtts = {};
}
let ticks = this.props.xValues.map((value, index) => {
let point = this.props.data[index];
- let x = Math.round(xScale(point.x) + xScale.rangeBand() / 2 + this.props.barsWidth / 2);
+ let x = Math.round(xScale(point.x) + xScale.rangeBand() / 2);
let y = yScale(point.y);
let d = this.props.data[index];
let tooltipAtts = {};
renderBars (xScale, yScale) {
let bars = this.props.data.map((d, index) => {
- let x = Math.round(xScale(d.x) + xScale.rangeBand() / 2);
+ let x = Math.round(xScale(d.x));
let maxY = yScale.range()[0];
let y = Math.round(yScale(d.y)) - /* minimum bar height */ 1;
let height = maxY - y;
let availableWidth = this.state.width - this.props.padding[1] - this.props.padding[3];
let availableHeight = this.state.height - this.props.padding[0] - this.props.padding[2];
+ const innerPadding = (availableWidth - this.props.barsWidth * this.props.data.length) / (this.props.data.length - 1);
+ const relativeInnerPadding = innerPadding / (innerPadding + this.props.barsWidth);
+
let maxY = d3.max(this.props.data, d => d.y);
let xScale = d3.scale.ordinal()
.domain(this.props.data.map(d => d.x))
- .rangeRoundBands([0, availableWidth]);
+ .rangeBands([0, availableWidth], relativeInnerPadding, 0);
let yScale = d3.scale.linear()
.domain([0, maxY])
.range([availableHeight, 0]);
{ x: 2, y: 30 },
{ x: 3, y: 20 }
];
- let chart = TestUtils.renderIntoDocument(<BarChart data={data} width={100} height={100}/>);
+ let chart = TestUtils.renderIntoDocument(<BarChart data={data} width={100} height={100} barsWidth={20}/>);
expect(TestUtils.scryRenderedDOMComponentsWithClass(chart, 'bar-chart-bar')).to.have.length(3);
});
{ x: 3, y: 20 }
];
const ticks = ['A', 'B', 'C'];
- let chart = TestUtils.renderIntoDocument(<BarChart data={data} xTicks={ticks} width={100} height={100}/>);
+ let chart = TestUtils.renderIntoDocument(<BarChart data={data} xTicks={ticks} width={100} height={100} barsWidth={20}/>);
expect(TestUtils.scryRenderedDOMComponentsWithClass(chart, 'bar-chart-tick')).to.have.length(3);
});
{ x: 3, y: 20 }
];
const values = ['A', 'B', 'C'];
- let chart = TestUtils.renderIntoDocument(<BarChart data={data} xValues={values} width={100} height={100}/>);
+ let chart = TestUtils.renderIntoDocument(<BarChart data={data} xValues={values} width={100} height={100} barsWidth={20}/>);
expect(TestUtils.scryRenderedDOMComponentsWithClass(chart, 'bar-chart-tick')).to.have.length(3);
});
const ticks = ['A', 'B', 'C'];
const values = ['A', 'B', 'C'];
let chart = TestUtils.renderIntoDocument(
- <BarChart data={data} xTicks={ticks} xValues={values} width={100} height={100}/>);
+ <BarChart data={data} xTicks={ticks} xValues={values} width={100} height={100} barsWidth={20}/>);
expect(TestUtils.scryRenderedDOMComponentsWithClass(chart, 'bar-chart-bar')).to.have.length(3);
expect(TestUtils.scryRenderedDOMComponentsWithClass(chart, 'bar-chart-tick')).to.have.length(6);
});