All files / hourly-chart/components/PostCountByHour index.jsx

28% Statements 7/25
0% Branches 0/4
0% Functions 0/6
28% Lines 7/25
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          1x                                                                                 1x                                                                                       1x                 1x                 1x                                                                       1x         1x                
import React from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
import ReactHighcharts from 'react-highcharts';
 
const highChartsTweetsConfigXAxis = {
  title: {
    text: null,
  },
  type: 'datetime',
  gridLineWidth: 0,
  gridLineColor: '#F3F5F7',
  lineColor: '#F3F5F7',
  lineWidth: '2',
  min: moment().startOf('day').valueOf(),
  max: moment().endOf('day').valueOf(),
  minorTickWidth: 0,
  minorGridLineWidth: 0,
  minorGridLineColor: '#F3F5F7',
  minorTickInterval: 3600 * 1000,
  maxPadding: 0.0,
  minPadding: 0.0,
  tickInterval: 6 * 3600 * 1000, // Show label every six hours
  tickPixelInterval: 3600 * 24 * 1000,
  tickLength: 25,
  tickWidth: 2,
  tickColor: '#F3F5F7',
  showLastLabel: true,
  endOnTick: false,
  startOnTick: false,
  labels: {
    formatter: function() { // eslint-disable-line object-shorthand
      /* istanbul ignore next */
      return moment(new Date(this.value)).format('h A');
    },
    align: 'left',
    x: 10,
    y: 20,
    style: {
      'font-size': '0.875rem',
      'font-weight': '400',
      'font-family': 'Roboto, sans serif',
    },
  },
};
 
const chartConfig = {
  title: null,
  chart: {
    spacingTop: 10,
    spacingBottom: 0,
    height: 100,
    marginLeft: 65,
    spacingRight: 40,
  },
  xAxis: highChartsTweetsConfigXAxis,
  yAxis: {
    showLastLabel: false,
    showFirstLabel: false,
    title: {
      text: null,
    },
    lineWidth: '0',
    gridLineColor: '#F3F5F7',
    gridLineWidth: 2,
  },
  plotOptions: {
    column: {
      borderWidth: 0,
      borderRadius: 2,
      states: {
        hover: {
          animation: false,
          color: '#E7F0F8',
        },
      },
    },
  },
  legend: {
    enabled: false,
  },
  credits: {
    enabled: false,
  },
  tooltip: {
    enabled: false,
  },
  series: [],
};
 
const seriesConfig = {
  type: 'column',
  color: '#ced7df',
  pointInterval: 3600 * 1000, // one hour
  pointWidth: 21,
  pointPlacement: 0.5,
  name: 'Tweets',
};
 
const mouseOut = chart => {
  chart.tooltip.hide();
  //manually remove hover state from all points except this one
  for (var i in chart.series[0].points) {
    let point = chart.series[0].points[i];
    point.setState('');
  }
};
 
const PostCountByHour = ({ posts, hourlyChart }) => {
  const hour = moment().startOf('day');
  const postsByHour = posts.map((postCount) => {
    const post = {
      x: hour.valueOf(),
      y: postCount,
      color: '#ced7df',
      events: {
        mouseOver: function() { // eslint-disable-line object-shorthand
          const chart = hourlyChart.getChart();
          const index = this.series.data.indexOf(this);
          const pointsToRefresh = [chart.series[0].points[index]];
 
          if (chart.series[1] && chart.series[1].visible) {
            pointsToRefresh.push(chart.series[1].points[index]);
          }
 
          chart.tooltip.refresh(pointsToRefresh);
        },
        mouseOut: (() => mouseOut(hourlyChart.getChart())),
      },
    };
    hour.add(1, 'hour');
    return post;
  });
  const config = {
    ...chartConfig,
    series: [{
      ...seriesConfig,
      data: postsByHour,
    }],
  };
 
  return <ReactHighcharts isPureConfig config={config} />;
};
 
PostCountByHour.defaultProps = {
  posts: [],
  hourlyChart: null,
};
 
PostCountByHour.propTypes = {
  posts: PropTypes.arrayOf(PropTypes.number),
  hourlyChart: PropTypes.shape({
    getChart: PropTypes.func,
  }),
};
 
export default PostCountByHour;