/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import React from 'react';
import { max } from 'd3-array';
import { scaleLinear, scaleBand } from 'd3-scale';
import { ResizeMixin } from './../mixins/resize-mixin';
import { TooltipsMixin } from './../mixins/tooltips-mixin';
export const BarChart = React.createClass({
propTypes: {
data: React.PropTypes.arrayOf(React.PropTypes.object).isRequired,
xTicks: React.PropTypes.arrayOf(React.PropTypes.any),
xValues: React.PropTypes.arrayOf(React.PropTypes.any),
height: React.PropTypes.number,
padding: React.PropTypes.arrayOf(React.PropTypes.number),
barsWidth: React.PropTypes.number.isRequired,
onBarClick: React.PropTypes.func
},
mixins: [ResizeMixin, TooltipsMixin],
getDefaultProps() {
return {
xTicks: [],
xValues: [],
padding: [10, 10, 10, 10]
};
},
getInitialState() {
return { width: this.props.width, height: this.props.height };
},
handleClick(point) {
this.props.onBarClick(point);
},
renderXTicks(xScale, yScale) {
if (!this.props.xTicks.length) {
return null;
}
const ticks = this.props.xTicks.map((tick, index) => {
const point = this.props.data[index];
const x = Math.round(xScale(point.x) + xScale.bandwidth() / 2);
const y = yScale.range()[0];
const d = this.props.data[index];
const tooltipAtts = {};
if (d.tooltip) {
tooltipAtts['title'] = d.tooltip;
tooltipAtts['data-toggle'] = 'tooltip';
}
return (