2018-06-15 18:11:25 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-10-17 18:08:34 +00:00
|
|
|
import { find, findIndex, get } from 'lodash';
|
2018-08-12 17:01:10 +00:00
|
|
|
import { max as d3Max } from 'd3-array';
|
2018-06-15 18:11:25 +00:00
|
|
|
import { axisBottom as d3AxisBottom, axisLeft as d3AxisLeft } from 'd3-axis';
|
2018-09-12 11:16:36 +00:00
|
|
|
import { format as d3Format } from 'd3-format';
|
2018-06-15 18:11:25 +00:00
|
|
|
import {
|
|
|
|
scaleBand as d3ScaleBand,
|
|
|
|
scaleLinear as d3ScaleLinear,
|
|
|
|
scaleTime as d3ScaleTime,
|
|
|
|
} from 'd3-scale';
|
2018-09-26 12:17:29 +00:00
|
|
|
import { event as d3Event, select as d3Select } from 'd3-selection';
|
2018-07-03 14:44:10 +00:00
|
|
|
import { line as d3Line } from 'd3-shape';
|
2018-09-24 11:11:18 +00:00
|
|
|
import { format as formatDate } from '@wordpress/date';
|
2018-09-25 09:42:08 +00:00
|
|
|
|
2018-09-04 13:03:46 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { formatCurrency } from 'lib/currency';
|
2018-10-17 13:44:43 +00:00
|
|
|
import { dayTicksThreshold } from 'lib/date';
|
2018-06-15 18:11:25 +00:00
|
|
|
|
2018-09-14 17:43:53 +00:00
|
|
|
/**
|
|
|
|
* Describes `smallestFactor`
|
|
|
|
* @param {number} inputNum - any double or integer
|
|
|
|
* @returns {integer} smallest factor of num
|
|
|
|
*/
|
|
|
|
export const getFactors = inputNum => {
|
2018-10-17 13:44:43 +00:00
|
|
|
const numFactors = [];
|
2018-09-14 17:43:53 +00:00
|
|
|
for ( let i = 1; i <= Math.floor( Math.sqrt( inputNum ) ); i += 1 ) {
|
|
|
|
if ( inputNum % i === 0 ) {
|
2018-10-17 13:44:43 +00:00
|
|
|
numFactors.push( i );
|
|
|
|
inputNum / i !== i && numFactors.push( inputNum / i );
|
2018-09-14 17:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-17 13:44:43 +00:00
|
|
|
numFactors.sort( ( x, y ) => x - y ); // numeric sort
|
|
|
|
|
|
|
|
return numFactors;
|
2018-09-14 17:43:53 +00:00
|
|
|
};
|
|
|
|
|
2018-07-10 14:03:37 +00:00
|
|
|
/**
|
|
|
|
* Describes `getUniqueKeys`
|
|
|
|
* @param {array} data - The chart component's `data` prop.
|
|
|
|
* @returns {array} of unique category keys
|
|
|
|
*/
|
2018-06-15 18:11:25 +00:00
|
|
|
export const getUniqueKeys = data => {
|
|
|
|
return [
|
|
|
|
...new Set(
|
|
|
|
data.reduce( ( accum, curr ) => {
|
|
|
|
Object.keys( curr ).forEach( key => key !== 'date' && accum.push( key ) );
|
|
|
|
return accum;
|
|
|
|
}, [] )
|
|
|
|
),
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2018-07-10 14:03:37 +00:00
|
|
|
/**
|
|
|
|
* Describes `getOrderedKeys`
|
|
|
|
* @param {array} data - The chart component's `data` prop.
|
|
|
|
* @param {array} uniqueKeys - from `getUniqueKeys`.
|
|
|
|
* @returns {array} of unique category keys ordered by cumulative total value
|
|
|
|
*/
|
2018-07-05 12:18:02 +00:00
|
|
|
export const getOrderedKeys = ( data, uniqueKeys ) =>
|
|
|
|
uniqueKeys
|
2018-06-15 18:11:25 +00:00
|
|
|
.map( key => ( {
|
|
|
|
key,
|
2018-07-11 15:23:16 +00:00
|
|
|
focus: true,
|
2018-09-20 14:28:22 +00:00
|
|
|
total: data.reduce( ( a, c ) => a + c[ key ].value, 0 ),
|
2018-07-11 15:23:16 +00:00
|
|
|
visible: true,
|
2018-06-15 18:11:25 +00:00
|
|
|
} ) )
|
2018-07-11 15:23:16 +00:00
|
|
|
.sort( ( a, b ) => b.total - a.total );
|
2018-06-15 18:11:25 +00:00
|
|
|
|
2018-07-10 14:03:37 +00:00
|
|
|
/**
|
|
|
|
* Describes `getLineData`
|
|
|
|
* @param {array} data - The chart component's `data` prop.
|
|
|
|
* @param {array} orderedKeys - from `getOrderedKeys`.
|
|
|
|
* @returns {array} an array objects with a category `key` and an array of `values` with `date` and `value` properties
|
|
|
|
*/
|
2018-07-05 12:18:02 +00:00
|
|
|
export const getLineData = ( data, orderedKeys ) =>
|
2018-07-11 15:23:16 +00:00
|
|
|
orderedKeys.map( row => ( {
|
|
|
|
key: row.key,
|
|
|
|
focus: row.focus,
|
|
|
|
visible: row.visible,
|
2018-06-15 18:11:25 +00:00
|
|
|
values: data.map( d => ( {
|
|
|
|
date: d.date,
|
2018-07-11 15:23:16 +00:00
|
|
|
focus: row.focus,
|
2018-09-25 09:42:08 +00:00
|
|
|
label: get( d, [ row.key, 'label' ], '' ),
|
2018-09-20 14:28:22 +00:00
|
|
|
value: get( d, [ row.key, 'value' ], 0 ),
|
2018-07-11 15:23:16 +00:00
|
|
|
visible: row.visible,
|
2018-06-15 18:11:25 +00:00
|
|
|
} ) ),
|
|
|
|
} ) );
|
|
|
|
|
2018-07-10 14:03:37 +00:00
|
|
|
/**
|
|
|
|
* Describes `getUniqueDates`
|
|
|
|
* @param {array} lineData - from `GetLineData`
|
2018-09-10 12:57:36 +00:00
|
|
|
* @param {function} parseDate - D3 time format parser
|
2018-07-10 14:03:37 +00:00
|
|
|
* @returns {array} an array of unique date values sorted from earliest to latest
|
|
|
|
*/
|
2018-09-10 12:57:36 +00:00
|
|
|
export const getUniqueDates = ( lineData, parseDate ) => {
|
2018-06-15 18:11:25 +00:00
|
|
|
return [
|
|
|
|
...new Set(
|
2018-07-05 12:18:02 +00:00
|
|
|
lineData.reduce( ( accum, { values } ) => {
|
2018-06-15 18:11:25 +00:00
|
|
|
values.forEach( ( { date } ) => accum.push( date ) );
|
|
|
|
return accum;
|
|
|
|
}, [] )
|
|
|
|
),
|
|
|
|
].sort( ( a, b ) => parseDate( a ) - parseDate( b ) );
|
|
|
|
};
|
|
|
|
|
2018-08-13 10:19:32 +00:00
|
|
|
export const getColor = ( key, params ) => {
|
2018-09-10 13:59:14 +00:00
|
|
|
const smallColorScales = [
|
|
|
|
[],
|
|
|
|
[ 0.5 ],
|
|
|
|
[ 0.333, 0.667 ],
|
|
|
|
[ 0.2, 0.5, 0.8 ],
|
|
|
|
[ 0.12, 0.375, 0.625, 0.88 ],
|
|
|
|
];
|
|
|
|
let keyValue = 0;
|
|
|
|
const len = params.orderedKeys.length;
|
|
|
|
const idx = findIndex( params.orderedKeys, d => d.key === key );
|
|
|
|
if ( len < 5 ) {
|
|
|
|
keyValue = smallColorScales[ len ][ idx ];
|
|
|
|
} else {
|
|
|
|
keyValue = idx / ( params.orderedKeys.length - 1 );
|
|
|
|
}
|
2018-08-13 10:19:32 +00:00
|
|
|
return params.colorScheme( keyValue );
|
|
|
|
};
|
|
|
|
|
2018-07-10 14:03:37 +00:00
|
|
|
/**
|
|
|
|
* Describes getXScale
|
|
|
|
* @param {array} uniqueDates - from `getUniqueDates`
|
|
|
|
* @param {number} width - calculated width of the charting space
|
|
|
|
* @returns {function} a D3 scale of the dates
|
|
|
|
*/
|
2018-07-05 12:18:02 +00:00
|
|
|
export const getXScale = ( uniqueDates, width ) =>
|
2018-06-15 18:11:25 +00:00
|
|
|
d3ScaleBand()
|
2018-07-05 12:18:02 +00:00
|
|
|
.domain( uniqueDates )
|
2018-06-15 18:11:25 +00:00
|
|
|
.rangeRound( [ 0, width ] )
|
|
|
|
.paddingInner( 0.1 );
|
|
|
|
|
2018-07-10 14:03:37 +00:00
|
|
|
/**
|
|
|
|
* Describes getXGroupScale
|
|
|
|
* @param {array} orderedKeys - from `getOrderedKeys`
|
|
|
|
* @param {function} xScale - from `getXScale`
|
|
|
|
* @returns {function} a D3 scale for each category within the xScale range
|
|
|
|
*/
|
2018-07-05 12:18:02 +00:00
|
|
|
export const getXGroupScale = ( orderedKeys, xScale ) =>
|
2018-06-15 18:11:25 +00:00
|
|
|
d3ScaleBand()
|
2018-08-13 10:19:32 +00:00
|
|
|
.domain( orderedKeys.filter( d => d.visible ).map( d => d.key ) )
|
2018-07-05 12:18:02 +00:00
|
|
|
.rangeRound( [ 0, xScale.bandwidth() ] )
|
2018-06-15 18:11:25 +00:00
|
|
|
.padding( 0.07 );
|
|
|
|
|
2018-07-10 14:03:37 +00:00
|
|
|
/**
|
|
|
|
* Describes getXLineScale
|
|
|
|
* @param {array} uniqueDates - from `getUniqueDates`
|
|
|
|
* @param {number} width - calculated width of the charting space
|
|
|
|
* @returns {function} a D3 scaletime for each date
|
|
|
|
*/
|
2018-07-05 12:18:02 +00:00
|
|
|
export const getXLineScale = ( uniqueDates, width ) =>
|
|
|
|
d3ScaleTime()
|
2018-06-15 18:11:25 +00:00
|
|
|
.domain( [ new Date( uniqueDates[ 0 ] ), new Date( uniqueDates[ uniqueDates.length - 1 ] ) ] )
|
|
|
|
.rangeRound( [ 0, width ] );
|
|
|
|
|
2018-07-10 14:03:37 +00:00
|
|
|
/**
|
2018-09-07 12:48:07 +00:00
|
|
|
* Describes and rounds the maximum y value to the nearest thousadn, ten-thousand, million etc.
|
2018-07-10 14:03:37 +00:00
|
|
|
* @param {array} lineData - from `getLineData`
|
|
|
|
* @returns {number} the maximum value in the timeseries multiplied by 4/3
|
|
|
|
*/
|
2018-09-05 11:20:12 +00:00
|
|
|
export const getYMax = lineData => {
|
2018-09-07 12:48:07 +00:00
|
|
|
const yMax = 4 / 3 * d3Max( lineData, d => d3Max( d.values.map( date => date.value ) ) );
|
2018-09-05 11:20:12 +00:00
|
|
|
const pow3Y = Math.pow( 10, ( ( Math.log( yMax ) * Math.LOG10E + 1 ) | 0 ) - 2 ) * 3;
|
|
|
|
return Math.ceil( yMax / pow3Y ) * pow3Y;
|
|
|
|
};
|
2018-06-15 18:11:25 +00:00
|
|
|
|
2018-07-10 14:03:37 +00:00
|
|
|
/**
|
|
|
|
* Describes getYScale
|
|
|
|
* @param {number} height - calculated height of the charting space
|
|
|
|
* @param {number} yMax - from `getYMax`
|
|
|
|
* @returns {function} the D3 linear scale from 0 to the value from `getYMax`
|
|
|
|
*/
|
2018-07-05 12:18:02 +00:00
|
|
|
export const getYScale = ( height, yMax ) =>
|
2018-06-15 18:11:25 +00:00
|
|
|
d3ScaleLinear()
|
2018-07-05 12:18:02 +00:00
|
|
|
.domain( [ 0, yMax ] )
|
2018-06-15 18:11:25 +00:00
|
|
|
.rangeRound( [ height, 0 ] );
|
|
|
|
|
2018-07-10 14:03:37 +00:00
|
|
|
/**
|
|
|
|
* Describes getyTickOffset
|
|
|
|
* @param {number} height - calculated height of the charting space
|
|
|
|
* @param {number} scale - ratio of the expected width to calculated width (given the viewbox)
|
|
|
|
* @param {number} yMax - from `getYMax`
|
|
|
|
* @returns {function} the D3 linear scale from 0 to the value from `getYMax`, offset by 12 pixels down
|
|
|
|
*/
|
2018-07-05 12:18:02 +00:00
|
|
|
export const getYTickOffset = ( height, scale, yMax ) =>
|
2018-06-15 18:11:25 +00:00
|
|
|
d3ScaleLinear()
|
2018-07-05 12:18:02 +00:00
|
|
|
.domain( [ 0, yMax ] )
|
2018-06-15 18:11:25 +00:00
|
|
|
.rangeRound( [ height + scale * 12, scale * 12 ] );
|
|
|
|
|
2018-07-10 14:03:37 +00:00
|
|
|
/**
|
|
|
|
* Describes getyTickOffset
|
|
|
|
* @param {function} xLineScale - from `getXLineScale`.
|
|
|
|
* @param {function} yScale - from `getYScale`.
|
|
|
|
* @returns {function} the D3 line function for plotting all category values
|
|
|
|
*/
|
2018-08-13 10:19:32 +00:00
|
|
|
export const getLine = ( xLineScale, yScale ) =>
|
2018-07-05 12:18:02 +00:00
|
|
|
d3Line()
|
2018-07-03 14:44:10 +00:00
|
|
|
.x( d => xLineScale( new Date( d.date ) ) )
|
2018-06-15 18:11:25 +00:00
|
|
|
.y( d => yScale( d.value ) );
|
2018-07-05 12:18:02 +00:00
|
|
|
|
2018-09-14 17:43:53 +00:00
|
|
|
/**
|
2018-10-17 13:44:43 +00:00
|
|
|
* Calculate the maximum number of ticks allowed in the x-axis based on the width and layout of the chart
|
2018-09-14 17:43:53 +00:00
|
|
|
* @param {integer} width - calculated page width
|
|
|
|
* @param {string} layout - standard, comparison or compact chart types
|
|
|
|
* @returns {integer} number of x-axis ticks based on width and chart layout
|
|
|
|
*/
|
2018-10-17 13:44:43 +00:00
|
|
|
const calculateMaxXTicks = ( width, layout ) => {
|
2018-09-14 17:43:53 +00:00
|
|
|
if ( width < 783 ) {
|
2018-10-17 13:44:43 +00:00
|
|
|
return 7;
|
2018-09-14 17:43:53 +00:00
|
|
|
} else if ( width >= 783 && width < 1129 ) {
|
2018-10-17 13:44:43 +00:00
|
|
|
return 12;
|
2018-09-14 17:43:53 +00:00
|
|
|
} else if ( width >= 1130 && width < 1365 ) {
|
|
|
|
if ( layout === 'standard' ) {
|
2018-10-17 13:44:43 +00:00
|
|
|
return 16;
|
2018-09-14 17:43:53 +00:00
|
|
|
} else if ( layout === 'comparison' ) {
|
2018-10-17 13:44:43 +00:00
|
|
|
return 12;
|
2018-09-14 17:43:53 +00:00
|
|
|
} else if ( layout === 'compact' ) {
|
2018-10-17 13:44:43 +00:00
|
|
|
return 7;
|
2018-09-14 17:43:53 +00:00
|
|
|
}
|
|
|
|
} else if ( width >= 1365 ) {
|
|
|
|
if ( layout === 'standard' ) {
|
2018-10-17 13:44:43 +00:00
|
|
|
return 31;
|
2018-09-14 17:43:53 +00:00
|
|
|
} else if ( layout === 'comparison' ) {
|
2018-10-17 13:44:43 +00:00
|
|
|
return 16;
|
2018-09-14 17:43:53 +00:00
|
|
|
} else if ( layout === 'compact' ) {
|
2018-10-17 13:44:43 +00:00
|
|
|
return 12;
|
2018-09-14 17:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-17 13:44:43 +00:00
|
|
|
|
|
|
|
return 16;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter out irrelevant dates so only the first date of each month is kept.
|
|
|
|
* @param {array} dates - string dates.
|
|
|
|
* @returns {array} Filtered dates.
|
|
|
|
*/
|
|
|
|
const getFirstDatePerMonth = dates => {
|
|
|
|
return dates.filter(
|
|
|
|
( date, i ) => i === 0 || new Date( date ).getMonth() !== new Date( dates[ i - 1 ] ).getMonth()
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get x-axis ticks given the unique dates and the increment factor.
|
|
|
|
* @param {array} uniqueDates - all the unique dates from the input data for the chart
|
|
|
|
* @param {integer} incrementFactor - increment factor for the visible ticks.
|
|
|
|
* @returns {array} Ticks for the x-axis.
|
|
|
|
*/
|
|
|
|
const getXTicksFromIncrementFactor = ( uniqueDates, incrementFactor ) => {
|
|
|
|
const ticks = [];
|
|
|
|
|
|
|
|
for ( let idx = 0; idx < uniqueDates.length; idx = idx + incrementFactor ) {
|
|
|
|
ticks.push( uniqueDates[ idx ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the first or last date is missing from the ticks array, add it back in.
|
|
|
|
if ( ticks[ 0 ] !== uniqueDates[ 0 ] ) {
|
|
|
|
ticks.unshift( uniqueDates[ 0 ] );
|
|
|
|
}
|
|
|
|
if ( ticks[ ticks.length - 1 ] !== uniqueDates[ uniqueDates.length - 1 ] ) {
|
|
|
|
ticks.push( uniqueDates[ uniqueDates.length - 1 ] );
|
2018-09-14 17:43:53 +00:00
|
|
|
}
|
2018-10-17 13:44:43 +00:00
|
|
|
|
|
|
|
return ticks;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the increment factor between ticks so there aren't more than maxTicks.
|
|
|
|
* @param {array} uniqueDates - all the unique dates from the input data for the chart
|
|
|
|
* @param {integer} maxTicks - maximum number of ticks that can be displayed in the x-axis
|
|
|
|
* @returns {integer} x-axis ticks increment factor
|
|
|
|
*/
|
|
|
|
const calculateXTicksIncrementFactor = ( uniqueDates, maxTicks ) => {
|
2018-09-14 17:43:53 +00:00
|
|
|
let factors = [];
|
2018-10-17 13:44:43 +00:00
|
|
|
let i = 1;
|
|
|
|
// First we get all the factors of the length of the uniqueDates array
|
2018-09-14 17:43:53 +00:00
|
|
|
// if the number is a prime number or near prime (with 3 factors) then we
|
2018-10-17 13:44:43 +00:00
|
|
|
// step down by 1 integer and try again.
|
2018-09-14 17:43:53 +00:00
|
|
|
while ( factors.length <= 3 ) {
|
2018-10-17 13:44:43 +00:00
|
|
|
factors = getFactors( uniqueDates.length - i );
|
2018-09-14 17:43:53 +00:00
|
|
|
i += 1;
|
|
|
|
}
|
2018-10-17 13:44:43 +00:00
|
|
|
|
|
|
|
return factors.find( f => uniqueDates.length / f < maxTicks );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns ticks for the x-axis.
|
|
|
|
* @param {array} uniqueDates - all the unique dates from the input data for the chart
|
|
|
|
* @param {integer} width - calculated page width
|
|
|
|
* @param {string} layout - standard, comparison or compact chart types
|
|
|
|
* @param {string} interval - string of the interval used in the graph (hour, day, week...)
|
|
|
|
* @returns {integer} number of x-axis ticks based on width and chart layout
|
|
|
|
*/
|
|
|
|
export const getXTicks = ( uniqueDates, width, layout, interval ) => {
|
|
|
|
const maxTicks = calculateMaxXTicks( width, layout );
|
|
|
|
|
|
|
|
if ( uniqueDates.length >= dayTicksThreshold && interval === 'day' ) {
|
|
|
|
uniqueDates = getFirstDatePerMonth( uniqueDates );
|
2018-09-14 17:43:53 +00:00
|
|
|
}
|
2018-10-17 13:44:43 +00:00
|
|
|
if ( uniqueDates.length <= maxTicks ) {
|
|
|
|
return uniqueDates;
|
2018-09-14 17:43:53 +00:00
|
|
|
}
|
2018-10-17 13:44:43 +00:00
|
|
|
|
|
|
|
const incrementFactor = calculateXTicksIncrementFactor( uniqueDates, maxTicks );
|
|
|
|
|
|
|
|
return getXTicksFromIncrementFactor( uniqueDates, incrementFactor );
|
2018-09-14 17:43:53 +00:00
|
|
|
};
|
|
|
|
|
2018-07-10 14:03:37 +00:00
|
|
|
/**
|
|
|
|
* Describes getDateSpaces
|
2018-10-17 18:08:34 +00:00
|
|
|
* @param {array} data - The chart component's `data` prop.
|
2018-07-10 14:03:37 +00:00
|
|
|
* @param {array} uniqueDates - from `getUniqueDates`
|
|
|
|
* @param {number} width - calculated width of the charting space
|
|
|
|
* @param {function} xLineScale - from `getXLineScale`
|
|
|
|
* @returns {array} that icnludes the date, start (x position) and width to layout the mouseover rectangles
|
|
|
|
*/
|
2018-10-17 18:08:34 +00:00
|
|
|
export const getDateSpaces = ( data, uniqueDates, width, xLineScale ) =>
|
2018-07-05 12:18:02 +00:00
|
|
|
uniqueDates.map( ( d, i ) => {
|
2018-10-17 18:08:34 +00:00
|
|
|
const datapoints = find( data, { date: d } );
|
2018-07-05 12:18:02 +00:00
|
|
|
const xNow = xLineScale( new Date( d ) );
|
|
|
|
const xPrev =
|
|
|
|
i >= 1
|
|
|
|
? xLineScale( new Date( uniqueDates[ i - 1 ] ) )
|
|
|
|
: xLineScale( new Date( uniqueDates[ 0 ] ) );
|
|
|
|
const xNext =
|
|
|
|
i < uniqueDates.length - 1
|
|
|
|
? xLineScale( new Date( uniqueDates[ i + 1 ] ) )
|
|
|
|
: xLineScale( new Date( uniqueDates[ uniqueDates.length - 1 ] ) );
|
|
|
|
let xWidth = i === 0 ? xNext - xNow : xNow - xPrev;
|
|
|
|
const xStart = i === 0 ? 0 : xNow - xWidth / 2;
|
|
|
|
xWidth = i === 0 || i === uniqueDates.length - 1 ? xWidth / 2 : xWidth;
|
|
|
|
return {
|
|
|
|
date: d,
|
|
|
|
start: uniqueDates.length > 1 ? xStart : 0,
|
|
|
|
width: uniqueDates.length > 1 ? xWidth : width,
|
2018-10-17 18:08:34 +00:00
|
|
|
values: Object.keys( datapoints )
|
|
|
|
.filter( key => key !== 'date' )
|
|
|
|
.map( key => {
|
|
|
|
return {
|
|
|
|
key,
|
|
|
|
value: datapoints[ key ].value,
|
|
|
|
date: d,
|
|
|
|
};
|
|
|
|
} ),
|
2018-07-05 12:18:02 +00:00
|
|
|
};
|
|
|
|
} );
|
2018-06-15 18:11:25 +00:00
|
|
|
|
2018-08-12 17:01:10 +00:00
|
|
|
export const drawAxis = ( node, params ) => {
|
2018-07-05 12:18:02 +00:00
|
|
|
const xScale = params.type === 'line' ? params.xLineScale : params.xScale;
|
2018-06-15 18:11:25 +00:00
|
|
|
|
|
|
|
const yGrids = [];
|
|
|
|
for ( let i = 0; i < 4; i++ ) {
|
2018-07-05 12:18:02 +00:00
|
|
|
yGrids.push( i / 3 * params.yMax );
|
2018-06-15 18:11:25 +00:00
|
|
|
}
|
2018-07-11 15:23:16 +00:00
|
|
|
|
2018-09-14 17:43:53 +00:00
|
|
|
const ticks = params.xTicks.map( d => ( params.type === 'line' ? new Date( d ) : d ) );
|
|
|
|
|
2018-06-15 18:11:25 +00:00
|
|
|
node
|
|
|
|
.append( 'g' )
|
|
|
|
.attr( 'class', 'axis' )
|
2018-09-25 09:42:08 +00:00
|
|
|
.attr( 'aria-hidden', 'true' )
|
2018-10-17 13:44:43 +00:00
|
|
|
.attr( 'transform', `translate(0, ${ params.height })` )
|
2018-06-15 18:11:25 +00:00
|
|
|
.call(
|
2018-07-03 14:44:10 +00:00
|
|
|
d3AxisBottom( xScale )
|
2018-09-14 17:43:53 +00:00
|
|
|
.tickValues( ticks )
|
2018-09-05 20:52:35 +00:00
|
|
|
.tickFormat( d => params.xFormat( d instanceof Date ? d : new Date( d ) ) )
|
2018-06-15 18:11:25 +00:00
|
|
|
);
|
|
|
|
|
2018-09-04 11:31:18 +00:00
|
|
|
node
|
|
|
|
.append( 'g' )
|
|
|
|
.attr( 'class', 'axis axis-month' )
|
2018-09-25 09:42:08 +00:00
|
|
|
.attr( 'aria-hidden', 'true' )
|
2018-10-17 13:44:43 +00:00
|
|
|
.attr( 'transform', `translate(0, ${ params.height + 20 })` )
|
2018-09-04 11:31:18 +00:00
|
|
|
.call(
|
|
|
|
d3AxisBottom( xScale )
|
2018-09-14 17:43:53 +00:00
|
|
|
.tickValues( ticks )
|
2018-09-12 11:16:44 +00:00
|
|
|
.tickFormat( ( d, i ) => {
|
|
|
|
const monthDate = d instanceof Date ? d : new Date( d );
|
2018-09-17 09:07:11 +00:00
|
|
|
let prevMonth = i !== 0 ? ticks[ i - 1 ] : ticks[ i ];
|
2018-09-14 11:36:53 +00:00
|
|
|
prevMonth = prevMonth instanceof Date ? prevMonth : new Date( prevMonth );
|
2018-10-17 13:44:43 +00:00
|
|
|
return i === 0 || params.x2Format( monthDate ) !== params.x2Format( prevMonth )
|
2018-09-14 11:36:53 +00:00
|
|
|
? params.x2Format( monthDate )
|
|
|
|
: '';
|
2018-09-12 11:16:44 +00:00
|
|
|
} )
|
2018-09-04 11:31:18 +00:00
|
|
|
)
|
|
|
|
.call( g => g.select( '.domain' ).remove() );
|
|
|
|
|
2018-09-04 13:03:46 +00:00
|
|
|
node
|
|
|
|
.selectAll( '.axis-month .tick text' )
|
|
|
|
.style( 'font-size', `${ Math.round( params.scale * 10 ) }px` );
|
|
|
|
|
2018-09-04 11:31:18 +00:00
|
|
|
node
|
|
|
|
.append( 'g' )
|
|
|
|
.attr( 'class', 'pipes' )
|
|
|
|
.attr( 'transform', `translate(0, ${ params.height })` )
|
|
|
|
.call(
|
|
|
|
d3AxisBottom( xScale )
|
2018-09-14 17:43:53 +00:00
|
|
|
.tickValues( ticks )
|
2018-09-04 11:31:18 +00:00
|
|
|
.tickSize( 5 )
|
|
|
|
.tickFormat( '' )
|
|
|
|
);
|
|
|
|
|
2018-06-15 18:11:25 +00:00
|
|
|
node
|
|
|
|
.append( 'g' )
|
|
|
|
.attr( 'class', 'grid' )
|
|
|
|
.attr( 'transform', `translate(-${ params.margin.left },0)` )
|
|
|
|
.call(
|
2018-07-05 12:18:02 +00:00
|
|
|
d3AxisLeft( params.yScale )
|
2018-06-15 18:11:25 +00:00
|
|
|
.tickValues( yGrids )
|
2018-09-04 13:03:46 +00:00
|
|
|
.tickSize( -params.width - params.margin.left - params.margin.right )
|
2018-06-15 18:11:25 +00:00
|
|
|
.tickFormat( '' )
|
|
|
|
)
|
|
|
|
.call( g => g.select( '.domain' ).remove() );
|
|
|
|
|
|
|
|
node
|
|
|
|
.append( 'g' )
|
|
|
|
.attr( 'class', 'axis y-axis' )
|
2018-09-25 09:42:08 +00:00
|
|
|
.attr( 'aria-hidden', 'true' )
|
2018-09-07 10:28:02 +00:00
|
|
|
.attr( 'transform', 'translate(-50, 0)' )
|
|
|
|
.attr( 'text-anchor', 'start' )
|
2018-06-15 18:11:25 +00:00
|
|
|
.call(
|
2018-07-05 12:18:02 +00:00
|
|
|
d3AxisLeft( params.yTickOffset )
|
2018-06-15 18:11:25 +00:00
|
|
|
.tickValues( yGrids )
|
2018-09-07 12:39:31 +00:00
|
|
|
.tickFormat( d => d3Format( params.yFormat )( d !== 0 ? d : 0 ) )
|
2018-06-15 18:11:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
node
|
|
|
|
.selectAll( '.y-axis .tick text' )
|
|
|
|
.style( 'font-size', `${ Math.round( params.scale * 10 ) }px` );
|
|
|
|
|
|
|
|
node.selectAll( '.domain' ).remove();
|
|
|
|
node
|
|
|
|
.selectAll( '.axis' )
|
|
|
|
.selectAll( '.tick' )
|
|
|
|
.select( 'line' )
|
|
|
|
.remove();
|
|
|
|
};
|
|
|
|
|
2018-09-24 11:11:18 +00:00
|
|
|
const getTooltipRowLabel = ( d, row, params ) =>
|
|
|
|
d[ row.key ].labelDate ? formatDate( params.pointLabelFormat, d[ row.key ].labelDate ) : row.key;
|
|
|
|
|
2018-10-19 13:23:47 +00:00
|
|
|
const getFormatedTotal = ( total, valueType ) => {
|
|
|
|
let rowTotal = total;
|
|
|
|
switch ( valueType ) {
|
|
|
|
case 'average':
|
|
|
|
rowTotal = Math.round( total );
|
|
|
|
break;
|
|
|
|
case 'currency':
|
|
|
|
rowTotal = formatCurrency( total );
|
|
|
|
break;
|
|
|
|
case 'number':
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rowTotal;
|
|
|
|
};
|
|
|
|
|
2018-09-26 12:17:29 +00:00
|
|
|
const showTooltip = ( params, d, position ) => {
|
2018-08-13 10:19:32 +00:00
|
|
|
const keys = params.orderedKeys.filter( row => row.visible ).map(
|
2018-08-12 17:01:10 +00:00
|
|
|
row => `
|
2018-09-04 13:03:46 +00:00
|
|
|
<li class="key-row">
|
|
|
|
<div class="key-container">
|
2018-09-07 12:45:29 +00:00
|
|
|
<span class="key-color" style="background-color:${ getColor( row.key, params ) }"></span>
|
2018-09-24 11:11:18 +00:00
|
|
|
<span class="key-key">${ getTooltipRowLabel( d, row, params ) }</span>
|
2018-09-04 13:03:46 +00:00
|
|
|
</div>
|
2018-10-19 13:23:47 +00:00
|
|
|
<span class="key-value">${ getFormatedTotal( d[ row.key ].value, params.valueType ) }</span>
|
2018-08-13 10:19:32 +00:00
|
|
|
</li>
|
|
|
|
`
|
2018-07-03 14:44:10 +00:00
|
|
|
);
|
2018-08-12 17:01:10 +00:00
|
|
|
|
2018-09-20 14:28:22 +00:00
|
|
|
const tooltipTitle = params.tooltipTitle
|
|
|
|
? params.tooltipTitle
|
|
|
|
: params.tooltipFormat( d.date instanceof Date ? d.date : new Date( d.date ) );
|
|
|
|
|
2018-07-25 14:58:32 +00:00
|
|
|
params.tooltip
|
2018-09-26 12:17:29 +00:00
|
|
|
.style( 'left', position.x + 'px' )
|
|
|
|
.style( 'top', position.y + 'px' )
|
|
|
|
.style( 'visibility', 'visible' ).html( `
|
2018-07-03 14:44:10 +00:00
|
|
|
<div>
|
2018-09-20 14:28:22 +00:00
|
|
|
<h4>${ tooltipTitle }</h4>
|
2018-07-03 14:44:10 +00:00
|
|
|
<ul>
|
|
|
|
${ keys.join( '' ) }
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
` );
|
|
|
|
};
|
|
|
|
|
2018-09-25 09:42:08 +00:00
|
|
|
const handleMouseOverBarChart = ( date, parentNode, node, data, params, position ) => {
|
|
|
|
d3Select( parentNode )
|
2018-07-03 14:44:10 +00:00
|
|
|
.select( '.barfocus' )
|
|
|
|
.attr( 'opacity', '0.1' );
|
2018-09-26 12:17:29 +00:00
|
|
|
showTooltip( params, data.find( e => e.date === date ), position );
|
2018-07-03 14:44:10 +00:00
|
|
|
};
|
|
|
|
|
2018-09-25 09:42:08 +00:00
|
|
|
const handleMouseOutBarChart = ( parentNode, params ) => {
|
|
|
|
d3Select( parentNode )
|
2018-07-03 14:44:10 +00:00
|
|
|
.select( '.barfocus' )
|
|
|
|
.attr( 'opacity', '0' );
|
2018-09-26 12:17:29 +00:00
|
|
|
params.tooltip.style( 'visibility', 'hidden' );
|
2018-07-03 14:44:10 +00:00
|
|
|
};
|
|
|
|
|
2018-09-25 09:42:08 +00:00
|
|
|
const handleMouseOverLineChart = ( date, parentNode, node, data, params, position ) => {
|
|
|
|
d3Select( parentNode )
|
2018-07-03 14:44:10 +00:00
|
|
|
.select( '.focus-grid' )
|
|
|
|
.attr( 'opacity', '1' );
|
2018-09-26 12:17:29 +00:00
|
|
|
showTooltip( params, data.find( e => e.date === date ), position );
|
2018-07-03 14:44:10 +00:00
|
|
|
};
|
|
|
|
|
2018-09-25 09:42:08 +00:00
|
|
|
const handleMouseOutLineChart = ( parentNode, params ) => {
|
|
|
|
d3Select( parentNode )
|
2018-07-03 14:44:10 +00:00
|
|
|
.select( '.focus-grid' )
|
|
|
|
.attr( 'opacity', '0' );
|
2018-09-26 12:17:29 +00:00
|
|
|
params.tooltip.style( 'visibility', 'hidden' );
|
2018-07-03 14:44:10 +00:00
|
|
|
};
|
|
|
|
|
2018-09-26 12:17:29 +00:00
|
|
|
const calculateTooltipPosition = ( element, chart, elementWidthRatio = 1 ) => {
|
2018-09-14 12:57:09 +00:00
|
|
|
const elementCoords = element.getBoundingClientRect();
|
|
|
|
const chartCoords = chart.getBoundingClientRect();
|
2018-09-26 12:17:29 +00:00
|
|
|
const tooltipSize = d3Select( '.tooltip' )
|
|
|
|
.node()
|
|
|
|
.getBoundingClientRect();
|
|
|
|
const tooltipMargin = 24;
|
|
|
|
|
|
|
|
let xPosition =
|
2018-10-19 15:39:14 +00:00
|
|
|
elementCoords.left + elementCoords.width * elementWidthRatio + tooltipMargin - chartCoords.left;
|
|
|
|
let yPosition = elementCoords.top + tooltipMargin - chartCoords.top;
|
2018-09-26 12:17:29 +00:00
|
|
|
if ( xPosition + tooltipSize.width + tooltipMargin > chartCoords.width ) {
|
|
|
|
xPosition = Math.max(
|
|
|
|
0,
|
2018-10-19 15:39:14 +00:00
|
|
|
elementCoords.left +
|
2018-09-26 12:17:29 +00:00
|
|
|
elementCoords.width * ( 1 - elementWidthRatio ) -
|
|
|
|
tooltipSize.width -
|
|
|
|
tooltipMargin -
|
2018-10-19 15:39:14 +00:00
|
|
|
chartCoords.left
|
2018-09-26 12:17:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( yPosition + tooltipSize.height + tooltipMargin > chartCoords.height ) {
|
2018-10-19 15:39:14 +00:00
|
|
|
yPosition = Math.max(
|
|
|
|
0,
|
|
|
|
elementCoords.top - tooltipSize.height - tooltipMargin - chartCoords.top
|
|
|
|
);
|
2018-09-26 12:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return { x: xPosition, y: yPosition };
|
2018-09-14 12:57:09 +00:00
|
|
|
};
|
|
|
|
|
2018-06-15 18:11:25 +00:00
|
|
|
export const drawLines = ( node, data, params ) => {
|
2018-07-11 15:23:16 +00:00
|
|
|
const series = node
|
|
|
|
.append( 'g' )
|
|
|
|
.attr( 'class', 'lines' )
|
2018-06-15 18:11:25 +00:00
|
|
|
.selectAll( '.line-g' )
|
2018-10-12 16:08:57 +00:00
|
|
|
.data( params.lineData.filter( d => d.visible ).reverse() )
|
2018-06-15 18:11:25 +00:00
|
|
|
.enter()
|
|
|
|
.append( 'g' )
|
2018-09-25 09:42:08 +00:00
|
|
|
.attr( 'class', 'line-g' )
|
|
|
|
.attr( 'role', 'region' )
|
|
|
|
.attr( 'aria-label', d => d.key );
|
2018-06-15 18:11:25 +00:00
|
|
|
|
|
|
|
series
|
|
|
|
.append( 'path' )
|
|
|
|
.attr( 'fill', 'none' )
|
|
|
|
.attr( 'stroke-width', 3 )
|
|
|
|
.attr( 'stroke-linejoin', 'round' )
|
|
|
|
.attr( 'stroke-linecap', 'round' )
|
2018-08-13 10:19:32 +00:00
|
|
|
.attr( 'stroke', d => getColor( d.key, params ) )
|
2018-07-11 15:23:16 +00:00
|
|
|
.style( 'opacity', d => {
|
|
|
|
const opacity = d.focus ? 1 : 0.1;
|
|
|
|
return d.visible ? opacity : 0;
|
|
|
|
} )
|
2018-07-05 12:18:02 +00:00
|
|
|
.attr( 'd', d => params.line( d.values ) );
|
2018-06-15 18:11:25 +00:00
|
|
|
|
2018-09-14 17:43:53 +00:00
|
|
|
params.uniqueDates.length < 50 &&
|
|
|
|
series
|
|
|
|
.selectAll( 'circle' )
|
|
|
|
.data( ( d, i ) => d.values.map( row => ( { ...row, i, visible: d.visible, key: d.key } ) ) )
|
|
|
|
.enter()
|
|
|
|
.append( 'circle' )
|
|
|
|
.attr( 'r', 6 )
|
|
|
|
.attr( 'fill', d => getColor( d.key, params ) )
|
|
|
|
.attr( 'stroke', '#fff' )
|
|
|
|
.attr( 'stroke-width', 3 )
|
|
|
|
.style( 'opacity', d => {
|
|
|
|
const opacity = d.focus ? 1 : 0.1;
|
|
|
|
return d.visible ? opacity : 0;
|
|
|
|
} )
|
|
|
|
.attr( 'cx', d => params.xLineScale( new Date( d.date ) ) )
|
|
|
|
.attr( 'cy', d => params.yScale( d.value ) )
|
2018-09-25 09:42:08 +00:00
|
|
|
.attr( 'tabindex', '0' )
|
|
|
|
.attr( 'aria-label', d => {
|
|
|
|
const label = d.label
|
|
|
|
? d.label
|
|
|
|
: params.tooltipFormat( d.date instanceof Date ? d.date : new Date( d.date ) );
|
|
|
|
return `${ label } ${ formatCurrency( d.value ) }`;
|
|
|
|
} )
|
|
|
|
.on( 'focus', ( d, i, nodes ) => {
|
2018-09-26 12:17:29 +00:00
|
|
|
const position = calculateTooltipPosition( d3Event.target, node.node() );
|
2018-09-25 09:42:08 +00:00
|
|
|
handleMouseOverLineChart( d.date, nodes[ i ].parentNode, node, data, params, position );
|
|
|
|
} )
|
|
|
|
.on( 'blur', ( d, i, nodes ) => handleMouseOutLineChart( nodes[ i ].parentNode, params ) );
|
2018-09-07 10:41:40 +00:00
|
|
|
|
|
|
|
const focus = node
|
|
|
|
.append( 'g' )
|
|
|
|
.attr( 'class', 'focusspaces' )
|
|
|
|
.selectAll( '.focus' )
|
|
|
|
.data( params.dateSpaces )
|
|
|
|
.enter()
|
|
|
|
.append( 'g' )
|
|
|
|
.attr( 'class', 'focus' );
|
|
|
|
|
2018-10-17 18:08:34 +00:00
|
|
|
const focusGrid = focus
|
|
|
|
.append( 'g' )
|
2018-09-07 10:41:40 +00:00
|
|
|
.attr( 'class', 'focus-grid' )
|
2018-10-17 18:08:34 +00:00
|
|
|
.attr( 'opacity', '0' );
|
|
|
|
|
|
|
|
focusGrid
|
|
|
|
.append( 'line' )
|
2018-09-07 10:41:40 +00:00
|
|
|
.attr( 'x1', d => params.xLineScale( new Date( d.date ) ) )
|
|
|
|
.attr( 'y1', 0 )
|
|
|
|
.attr( 'x2', d => params.xLineScale( new Date( d.date ) ) )
|
2018-10-18 12:56:54 +00:00
|
|
|
.attr( 'y2', params.height );
|
2018-10-17 18:08:34 +00:00
|
|
|
|
|
|
|
focusGrid
|
|
|
|
.selectAll( 'circle' )
|
|
|
|
.data( d => d.values )
|
|
|
|
.enter()
|
|
|
|
.append( 'circle' )
|
|
|
|
.attr( 'r', 8 )
|
|
|
|
.attr( 'fill', d => getColor( d.key, params ) )
|
|
|
|
.attr( 'stroke', '#fff' )
|
|
|
|
.attr( 'stroke-width', 4 )
|
|
|
|
.attr( 'cx', d => params.xLineScale( new Date( d.date ) ) )
|
|
|
|
.attr( 'cy', d => params.yScale( d.value ) );
|
2018-09-07 10:41:40 +00:00
|
|
|
|
|
|
|
focus
|
|
|
|
.append( 'rect' )
|
|
|
|
.attr( 'class', 'focus-g' )
|
|
|
|
.attr( 'x', d => d.start )
|
|
|
|
.attr( 'y', 0 )
|
|
|
|
.attr( 'width', d => d.width )
|
|
|
|
.attr( 'height', params.height )
|
|
|
|
.attr( 'opacity', 0 )
|
2018-09-26 12:17:29 +00:00
|
|
|
.on( 'mouseover', ( d, i, nodes ) => {
|
|
|
|
const elementWidthRatio = i === 0 || i === params.dateSpaces.length - 1 ? 0 : 0.5;
|
|
|
|
const position = calculateTooltipPosition( d3Event.target, node.node(), elementWidthRatio );
|
|
|
|
handleMouseOverLineChart( d.date, nodes[ i ].parentNode, node, data, params, position );
|
|
|
|
} )
|
2018-09-25 09:42:08 +00:00
|
|
|
.on( 'mouseout', ( d, i, nodes ) => handleMouseOutLineChart( nodes[ i ].parentNode, params ) );
|
2018-06-15 18:11:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const drawBars = ( node, data, params ) => {
|
|
|
|
const barGroup = node
|
|
|
|
.append( 'g' )
|
|
|
|
.attr( 'class', 'bars' )
|
|
|
|
.selectAll( 'g' )
|
|
|
|
.data( data )
|
|
|
|
.enter()
|
|
|
|
.append( 'g' )
|
2018-07-05 12:18:02 +00:00
|
|
|
.attr( 'transform', d => `translate(${ params.xScale( d.date ) },0)` )
|
2018-09-25 09:42:08 +00:00
|
|
|
.attr( 'class', 'bargroup' )
|
|
|
|
.attr( 'role', 'region' )
|
|
|
|
.attr(
|
|
|
|
'aria-label',
|
|
|
|
d =>
|
|
|
|
params.mode === 'item-comparison'
|
|
|
|
? params.tooltipFormat( d.date instanceof Date ? d.date : new Date( d.date ) )
|
|
|
|
: null
|
|
|
|
);
|
2018-06-15 18:11:25 +00:00
|
|
|
|
|
|
|
barGroup
|
|
|
|
.append( 'rect' )
|
|
|
|
.attr( 'class', 'barfocus' )
|
|
|
|
.attr( 'x', 0 )
|
|
|
|
.attr( 'y', 0 )
|
2018-07-05 12:18:02 +00:00
|
|
|
.attr( 'width', params.xGroupScale.range()[ 1 ] )
|
2018-06-15 18:11:25 +00:00
|
|
|
.attr( 'height', params.height )
|
|
|
|
.attr( 'opacity', '0' );
|
|
|
|
|
|
|
|
barGroup
|
|
|
|
.selectAll( '.bar' )
|
2018-07-11 15:23:16 +00:00
|
|
|
.data( d =>
|
2018-08-13 10:19:32 +00:00
|
|
|
params.orderedKeys.filter( row => row.visible ).map( row => ( {
|
2018-07-11 15:23:16 +00:00
|
|
|
key: row.key,
|
|
|
|
focus: row.focus,
|
2018-09-25 09:42:08 +00:00
|
|
|
value: get( d, [ row.key, 'value' ], 0 ),
|
|
|
|
label: get( d, [ row.key, 'label' ], '' ),
|
2018-07-11 15:23:16 +00:00
|
|
|
visible: row.visible,
|
2018-09-25 09:42:08 +00:00
|
|
|
date: d.date,
|
2018-07-11 15:23:16 +00:00
|
|
|
} ) )
|
|
|
|
)
|
2018-06-15 18:11:25 +00:00
|
|
|
.enter()
|
|
|
|
.append( 'rect' )
|
|
|
|
.attr( 'class', 'bar' )
|
2018-07-05 12:18:02 +00:00
|
|
|
.attr( 'x', d => params.xGroupScale( d.key ) )
|
|
|
|
.attr( 'y', d => params.yScale( d.value ) )
|
|
|
|
.attr( 'width', params.xGroupScale.bandwidth() )
|
|
|
|
.attr( 'height', d => params.height - params.yScale( d.value ) )
|
2018-08-13 10:19:32 +00:00
|
|
|
.attr( 'fill', d => getColor( d.key, params ) )
|
2018-09-25 09:42:08 +00:00
|
|
|
.attr( 'tabindex', '0' )
|
|
|
|
.attr( 'aria-label', d => {
|
|
|
|
const label = params.mode === 'time-comparison' && d.label ? d.label : d.key;
|
|
|
|
return `${ label } ${ formatCurrency( d.value ) }`;
|
|
|
|
} )
|
2018-07-11 15:23:16 +00:00
|
|
|
.style( 'opacity', d => {
|
|
|
|
const opacity = d.focus ? 1 : 0.1;
|
|
|
|
return d.visible ? opacity : 0;
|
2018-09-04 13:03:46 +00:00
|
|
|
} )
|
2018-09-25 09:42:08 +00:00
|
|
|
.on( 'focus', ( d, i, nodes ) => {
|
2018-09-26 12:17:29 +00:00
|
|
|
const targetNode = d.value > 0 ? d3Event.target : d3Event.target.parentNode;
|
|
|
|
const position = calculateTooltipPosition( targetNode, node.node() );
|
2018-09-25 09:42:08 +00:00
|
|
|
handleMouseOverBarChart( d.date, nodes[ i ].parentNode, node, data, params, position );
|
|
|
|
} )
|
|
|
|
.on( 'blur', ( d, i, nodes ) => handleMouseOutBarChart( nodes[ i ].parentNode, params ) );
|
2018-06-15 18:11:25 +00:00
|
|
|
|
|
|
|
barGroup
|
|
|
|
.append( 'rect' )
|
|
|
|
.attr( 'class', 'barmouse' )
|
|
|
|
.attr( 'x', 0 )
|
|
|
|
.attr( 'y', 0 )
|
2018-07-05 12:18:02 +00:00
|
|
|
.attr( 'width', params.xGroupScale.range()[ 1 ] )
|
2018-06-15 18:11:25 +00:00
|
|
|
.attr( 'height', params.height )
|
2018-07-03 14:44:10 +00:00
|
|
|
.attr( 'opacity', '0' )
|
2018-09-26 12:17:29 +00:00
|
|
|
.on( 'mouseover', ( d, i, nodes ) => {
|
|
|
|
const position = calculateTooltipPosition( d3Event.target, node.node() );
|
|
|
|
handleMouseOverBarChart( d.date, nodes[ i ].parentNode, node, data, params, position );
|
|
|
|
} )
|
2018-09-25 09:42:08 +00:00
|
|
|
.on( 'mouseout', ( d, i, nodes ) => handleMouseOutBarChart( nodes[ i ].parentNode, params ) );
|
2018-06-15 18:11:25 +00:00
|
|
|
};
|