all files / src/import/ pieChart.js

90% Statements 27/30
50% Branches 1/2
62.5% Functions 5/8
93.1% Lines 27/29
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                                                                                                                                                                                                               
/** 
 *  @fileOverview Pie Chart component definition
 *
 *  @author       Brian Greig
 *
 *  @requires     NPM:d3:Vue
 *  @requires     src/v-chart-plugin.js
 */
 
 /* eslint-env browser */
const d3 = Object.assign({},
  require('d3-selection'),
  require('d3-scale'),
  require('d3-axis'),
  require('d3-shape'));
/**
 * Builds an Pie Chart.
 * @module pieChart
 */
 
const pieChart = function chart() {
  /**
   * The SVG that stores the chart
   * @member svgContainer
   */
  const svgContainer = d3.select(`#${this.chartData.selector}`);
  /**
   * The configuration of the coordinate system
   * @member cs
   */
  let cs = {
    radius: null,
    ordinalColors: ['#d1f4fa', '#005792', '#ffe6eb', '#ffcdcd'],
  };
  cs.radius = this.height > this.width ? (
    this.width - this.width * 0.1) / 2 : (this.height - this.height * 0.1) / 2;
 
  const color = d3.scaleOrdinal()
    .range(cs.ordinalColors);
 
  /**
   * Returns colors for pie chart
   * @member getColor
   * @function
   */
  const getColor = (d, i) => color(i);
 
  /**
   * Adds a tooltip on mouse over
   * @member mouseOver
   * @function
   * @param {Object} d (svg element)
   */
  const mouseOver = (d) => {
    this.addTooltip(d.data, window.event);
  };
 
  /**
   * Removes tooltip on mouse out
   * @member mouseOut
   * @function
   * @param {Object} d (svg element)
   */
  const mouseOut = (d) => {
    this.removeTooltip(d);
  };
 
  const path = d3.arc()
    .outerRadius(cs.radius - 10)
    .innerRadius(25);
 
  /**
   * Runs when a new element is added to the dataset
   * @member enter
   * @function
   * @param {Object} arc (svg element)
   */
  const enter = (arc) => {
    arc.enter()
      .append('g')
      .attr('transform', `translate(${this.width / 2},${this.height / 2})`)
      .append('path')
      .merge(arc)
      .attr('class', 'arc')
      .attr('d', path)
      .attr('fill', getColor)
      .on('mouseover', mouseOver)
      .on('mouseout', mouseOut)
      .attr('transform', `translate(0,${this.header})`);
    return arc;
  };
  /**
   * Runs when a value of an element in dataset is changed
   * @member transition
   * @function
   * @param {Object} arc (svg element)
   */
  const transition = (arc) => {
    arc.transition()
      .attr('d', path)
      .attr('fill', getColor);
    return arc;
  };
  /**
   * Runs when an element is removed from the dataset
   * @member exit
   * @function
   * @param {Object} arc (svg element)
   */
  const exit = (arc) => {
    arc.exit().remove();
    return arc;
  };
 
  const pie = d3.pie()
    .sort(null)
    .value(d => d.metric);
 
  const arc = svgContainer.selectAll('.arc')
    .data(pie(this.ds));
 
  cs = this.setOverrides(cs, this.chartData.overrides);
  enter(arc);
  transition(arc);
  exit(arc);
 
  return cs;
};
 
export default pieChart;