All files / javascripts/explorev2/components ChartContainer.jsx

0% Statements 0/33
100% Branches 0/0
0% Functions 0/20
0% Lines 0/28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106                                                                                                                                                                                                                   
import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import { Panel } from 'react-bootstrap';
import visMap from '../../../visualizations/main';
import { d3format } from '../../modules/utils';
 
const propTypes = {
  sliceName: PropTypes.string.isRequired,
  vizType: PropTypes.string.isRequired,
  height: PropTypes.string.isRequired,
  sliceContainerId: PropTypes.string.isRequired,
  jsonEndpoint: PropTypes.string.isRequired,
  columnFormats: PropTypes.object,
};
 
class ChartContainer extends React.Component {
  componentDidMount() {
    this.renderVis();
  }
 
  componentDidUpdate() {
    this.renderVis();
  }
 
  getMockedSliceObject() {
    return {
      jsonEndpoint: () => this.props.jsonEndpoint,
 
      container: {
        html: () => {
          // this should be a callback to clear the contents of the slice container
        },
 
        css: () => {
          // dimension can be 'height'
          // pixel string can be '300px'
          // should call callback to adjust height of chart
        },
        height: () => parseInt(this.props.height, 10) - 100,
      },
 
      width: () => this.chartContainerRef.getBoundingClientRect().width,
 
      height: () => parseInt(this.props.height, 10) - 100,
 
      selector: `#${this.props.sliceContainerId}`,
 
      done: () => {
        // finished rendering callback
      },
      d3format: (col, number) => {
        // mock d3format function in Slice object in caravel.js
        const format = this.props.columnFormats[col];
        return d3format(format, number);
      },
    };
  }
 
  renderVis() {
    const slice = this.getMockedSliceObject();
    visMap[this.props.vizType](slice).render();
  }
 
  render() {
    return (
      <div className="chart-container">
        <Panel
          style={{ height: this.props.height }}
          header={
            <div
              id="slice-header"
              className="panel-title"
            >
              {this.props.sliceName}
            </div>
          }
        >
          <div
            id={this.props.sliceContainerId}
            ref={(ref) => { this.chartContainerRef = ref; }}
            className={this.props.vizType}
          />
        </Panel>
      </div>
    );
  }
}
 
ChartContainer.propTypes = propTypes;
 
function mapStateToProps(state) {
  return {
    sliceName: state.sliceName,
    vizType: state.viz.formData.vizType,
    sliceContainerId: `slice-container-${state.viz.formData.sliceId}`,
    jsonEndpoint: state.viz.jsonEndPoint,
    columnFormats: state.viz.columnFormats,
  };
}
 
function mapDispatchToProps() {
  return {};
}
 
export default connect(mapStateToProps, mapDispatchToProps)(ChartContainer);