All files / src ChartBuilder.js

75% Statements 12/16
50% Branches 1/2
75% Functions 9/12
75% Lines 12/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177        1x                                                                                                                                                                                                   2x                     4x               1x               1x       1x               1x   1x       1x     1x                       1x         1x                  
import Chart from 'chart.js'
import React, {Component} from 'react'
import PropTypes from 'prop-types'
 
const REV_CHART_SPEC = {
  bar: {
    defaultOptions: {
      // Whether the scale should start at zero, or an order of magnitude down from the lowest value
      scaleBeginAtZero: true,
      scaleShowGridLines: true,
      scaleGridLineColor: 'rgba(0,0,0,.05)',
      scaleGridLineWidth: 1,
      // Whether to show horizontal lines (except X axis)
      scaleShowHorizontalLine: true,
      // Whether to show vertical lines (except Y axis)
      scaleShowVerticalLine: false,
      barShowStroke: true,
      barStrokeWidth: 2,
      // Spacing between each of the X value sets
      barValueSpacing: 5,
      // Spacing between data sets within X values
      barDatasetSpacing: 1,
      // Amount extra to add to the radius to cater for hit detection outside the drawn point
      pointHitDetectionRadius: 20,
      // Whether to show a stroke for datasets
      datasetStroke: true,
      datasetStrokeWidth: 2,
      datasetFill: false,
    },
  },
  donut: {
    defaultOptions: {
      segmentShowStroke: true,
      segmentStrokeColor: '#fff',
      segmentStrokeWidth: 2,
      percentageInnerCutout: 50,
      animationSteps: 100,
      animationEasing: 'easeOutBounce',
      animateRotate: true,
      animateScale: false,
    },
  },
  line: {
    defaultOptions: {
      scaleShowGridLines: true,
      scaleGridLineColor: 'rgba(0,0,0,.05)',
      scaleGridLineWidth: 1,
      scaleShowHorizontalLine: true,
      scaleShowVerticalLine: false,
      bezierCurve: false,
      bezierCurveTension: 0.4,
      pointDot: true,
      pointDotRadius: 4,
      pointDotStrokeWidth: 1,
      pointHitDetectionRadius: 20,
      datasetStroke: true,
      datasetStrokeWidth: 2,
      datasetFill: false,
    },
  },
  pie: {
    defaultOptions: {
      segmentShowStroke: true,
      segmentStrokeColor: '#fff',
      segmentStrokeWidth: 2,
      percentageInnerCutout: 0,
      animationSteps: 100,
      animationEasing: 'easeOutBounce',
      animateRotate: true,
      animateScale: false,
    },
  },
  radar: {
    defaultOptions: {
      // Whether to show lines for each scale point
      scaleShowLine: true,
      // Whether we show the angle lines out of the radar
      angleShowLineOut: true,
      scaleShowLabels: false,
      scaleBeginAtZero: true,
      // Colour of the angle line
      angleLineColor: 'rgba(0,0,0,.1)',
      // Pixel width of the angle line
      angleLineWidth: 1,
      pointLabelFontFamily: 'Arial',
      pointLabelFontStyle: 'normal',
      pointLabelFontSize: 10,
      pointLabelFontColor: '#666',
      pointDot: true,
      pointDotRadius: 3,
      pointDotStrokeWidth: 1,
      // amount extra to add to the radius to cater for hit detection outside the drawn point
      pointHitDetectionRadius: 20,
      datasetStroke: true,
      datasetStrokeWidth: 2,
      datasetFill: true,
    },
  },
}
 
export default class ChartBuilder extends Component {
  static get propTypes() {
    return {
      data: PropTypes.object.isRequired,
      type: PropTypes.oneOf(Object.keys(REV_CHART_SPEC)).isRequired,
      options: PropTypes.object,
      width: PropTypes.number,
      height: PropTypes.number,
      title: PropTypes.string,
    }
  }
 
  static get defaultProps() {
    return {
      title: 'Chart Title',
      width: 300,
      height: 300,
    }
  }
 
  componentDidMount() {
    this.createChart()
  }
 
  componentWillUnmount() {
    this.destroyChart()
  }
 
  get ctx() {
    return this.canvas.getContext('2d')
  }
 
  get chartSpec() {
    return REV_CHART_SPEC[this.props.type]
  }
 
  get chartFuncName() {
    return this.props.type
  }
 
  get options() {
    const opts = Object.assign({}, this.chartSpec.defaultOptions)
 
    return Object.assign(opts, this.props.options)
  }
 
  createChart() {
    Iif (this._chart) {
      throw new Error('`createChart` may only be called once.`')
    }
    this._chart = new Chart(this.ctx, {
      type: this.props.type,
      data: this.props.data,
      options: this.options,
    })
  }
 
  destroyChart() {
    this._chart.destroy()
  }
 
  render() {
    return (
      <div>
        <h3 className="ChartTitle">{this.props.title}</h3>
        <canvas
          ref={(self) => {
            this.canvas = self
          }}
          width={this.props.width}
          height={this.props.height}
        />
      </div>
    )
  }
}