@ -35,6 +35,52 @@ export const LoadingState = {
isLoadedOrRefreshing : function ( state ) { return [ this . loaded , this . refreshing ] . includes ( state ) }
}
const renderBucketLabel = function ( query , graphData , label , comparison = false ) {
let isPeriodFull = graphData . full _intervals ? . [ label ]
if ( comparison ) isPeriodFull = true
const formattedLabel = dateFormatter ( graphData . interval , true , query . period , isPeriodFull ) ( label )
if ( query . period === 'realtime' ) {
return dateFormatter ( graphData . interval , true , query . period ) ( label )
}
if ( graphData . interval === 'hour' || graphData . interval == 'minute' ) {
const date = dateFormatter ( "date" , true , query . period ) ( label )
return ` ${ date } , ${ formattedLabel } `
}
return formattedLabel
}
const calculatePercentageDifference = function ( oldValue , newValue ) {
if ( oldValue == 0 && newValue > 0 ) {
return 100
} else if ( oldValue == 0 && newValue == 0 ) {
return 0
} else {
return Math . round ( ( newValue - oldValue ) / oldValue * 100 )
}
}
const buildTooltipData = function ( query , graphData , metric , tooltipModel ) {
const data = tooltipModel . dataPoints . find ( ( dataPoint ) => dataPoint . dataset . yAxisID == "y" )
const comparisonData = tooltipModel . dataPoints . find ( ( dataPoint ) => dataPoint . dataset . yAxisID == "yComparison" )
const label = renderBucketLabel ( query , graphData , graphData . labels [ data . dataIndex ] )
const comparisonLabel = comparisonData && renderBucketLabel ( query , graphData , graphData . comparison _labels [ data . dataIndex ] , true )
const value = data ? . raw || 0
const comparisonValue = comparisonData ? . raw || 0
const comparisonDifference = comparisonData && calculatePercentageDifference ( comparisonValue , value )
const metricFormatter = METRIC _FORMATTER [ metric ]
const formattedValue = metricFormatter ( value )
const formattedComparisonValue = comparisonData && metricFormatter ( comparisonValue )
return { label , formattedValue , comparisonLabel , formattedComparisonValue , comparisonDifference }
}
export const GraphTooltip = ( graphData , metric , query ) => {
return ( context ) => {
const tooltipModel = context . tooltip ;
@ -65,57 +111,35 @@ export const GraphTooltip = (graphData, metric, query) => {
return ;
}
function getBody ( bodyItem ) {
return bodyItem . lines ;
}
// Returns a string describing the bucket. Used when hovering the graph to
// show time buckets.
function renderBucketLabel ( label ) {
const isPeriodFull = graphData . full _intervals ? . [ label ]
const formattedLabel = dateFormatter ( graphData . interval , true , query . period , isPeriodFull ) ( label )
if ( query . period === 'realtime' ) {
return dateFormatter ( graphData . interval , true , query . period ) ( label )
}
if ( graphData . interval === 'hour' || graphData . interval == 'minute' ) {
const date = dateFormatter ( "date" , true , query . period ) ( label )
return ` ${ date } , ${ formattedLabel } `
}
return formattedLabel
}
// Set Tooltip Body
if ( tooltipModel . body ) {
var bodyLines = tooltipModel . body . map ( getBody ) ;
// Remove duplicated line on overlap between dashed and normal
if ( bodyLines . length == 3 ) {
bodyLines [ 1 ] = false
}
const data = tooltipModel . dataPoints [ 0 ]
const label = graphData . labels [ data . dataIndex ]
const point = data . raw || 0
const tooltipData = buildTooltipData ( query , graphData , metric , tooltipModel )
let innerHtml = `
< div class = 'text-gray-100 flex flex-col' >
< div class = 'flex justify-between items-center' >
< span class = 'font-semibold mr-4 text-lg' > $ { METRIC _LABELS [ metric ] } < / s p a n >
$ { tooltipData . comparisonDifference ? ` <span class='font-semibold text-sm'> ${ tooltipData . comparisonDifference } %</span> ` : '' }
< / d i v >
< div class = 'flex flex-col' >
< div class = 'flex flex-row justify-between items-center' >
< span class = 'flex items-center mr-4' >
< div class = 'w-3 h-3 mr-1 rounded-full' style = 'background-color: rgba(101,116,205)' > < / d i v >
< span > $ { renderBucketLabel( label ) } < / s p a n >
< span > $ { tooltipData . label } < / s p a n >
< / s p a n >
< span class = 'text-base font-bold' > $ { METRIC _FORMATTER [ metric ] ( point ) } < / s p a n >
< span class = 'text-base font-bold' > $ { tooltipData . formattedValue } < / s p a n >
< / d i v >
$ { tooltipData . comparisonLabel ? ` <div class='flex flex-row justify-between items-center'>
< span class = 'flex items-center mr-4' >
< div class = 'w-3 h-3 mr-1 rounded-full bg-gray-500' > < / d i v >
< span > $ { tooltipData . comparisonLabel } < / s p a n >
< / s p a n >
< span class = 'text-base font-bold' > $ { tooltipData . formattedComparisonValue } < / s p a n >
< / d i v >
< / d i v >
< span class = 'font-semibold italic' > $ { graphData . interval === 'month' ? 'Click to view month' : graphData . interval === 'date' ? 'Click to view day' : '' } < / s p a n >
< / d i v >
< / d i v > ` : ' ' }
` ;
tooltipEl . innerHTML = innerHtml ;
@ -124,7 +148,11 @@ export const GraphTooltip = (graphData, metric, query) => {
}
}
export const buildDataSet = ( plot , present _index , ctx , label , isPrevious ) => {
const truncateToPresentIndex = function ( array , presentIndex ) {
return array . slice ( 0 , presentIndex )
}
export const buildDataSet = ( plot , comparisonPlot , present _index , ctx , label ) => {
var gradient = ctx . createLinearGradient ( 0 , 0 , 0 , 300 ) ;
var prev _gradient = ctx . createLinearGradient ( 0 , 0 , 0 , 300 ) ;
gradient . addColorStop ( 0 , 'rgba(101,116,205, 0.2)' ) ;
@ -132,16 +160,32 @@ export const buildDataSet = (plot, present_index, ctx, label, isPrevious) => {
prev _gradient . addColorStop ( 0 , 'rgba(101,116,205, 0.075)' ) ;
prev _gradient . addColorStop ( 1 , 'rgba(101,116,205, 0)' ) ;
if ( ! isPrevious ) {
if ( present _index ) {
var dashedPart = plot . slice ( present _index - 1 , present _index + 1 ) ;
var dashedPlot = ( new Array ( present _index - 1 ) ) . concat ( dashedPart )
const _plot = [ ... plot ]
for ( var i = present _index ; i < _plot . length ; i ++ ) {
_plot [ i ] = undefined
let comparisonDataSet = [ ]
if ( comparisonPlot ) {
comparisonDataSet = [
{
label ,
data : truncateToPresentIndex ( comparisonPlot , present _index ) ,
borderWidth : 3 ,
borderColor : 'rgba(60,70,110,0.2)' ,
pointBackgroundColor : 'rgba(60,70,110,0.2)' ,
pointHoverBackgroundColor : 'rgba(60, 70, 110)' ,
pointBorderColor : 'transparent' ,
pointHoverRadius : 4 ,
backgroundColor : gradient ,
fill : true ,
yAxisID : 'yComparison' ,
}
]
}
if ( present _index ) {
var dashedPart = plot . slice ( present _index - 1 , present _index + 1 ) ;
var dashedPlot = ( new Array ( present _index - 1 ) ) . concat ( dashedPart )
const _plot = truncateToPresentIndex ( [ ... plot ] , present _index )
return [ {
return [
{
label ,
data : _plot ,
borderWidth : 3 ,
@ -152,6 +196,7 @@ export const buildDataSet = (plot, present_index, ctx, label, isPrevious) => {
pointHoverRadius : 4 ,
backgroundColor : gradient ,
fill : true ,
yAxisID : 'y' ,
} ,
{
label ,
@ -164,9 +209,12 @@ export const buildDataSet = (plot, present_index, ctx, label, isPrevious) => {
pointHoverRadius : 4 ,
backgroundColor : gradient ,
fill : true ,
} ]
} else {
return [ {
yAxisID : 'y' ,
}
] . concat ( comparisonDataSet )
} else {
return [
{
label ,
data : plot ,
borderWidth : 3 ,
@ -176,20 +224,8 @@ export const buildDataSet = (plot, present_index, ctx, label, isPrevious) => {
pointHoverRadius : 4 ,
backgroundColor : gradient ,
fill : true ,
} ]
}
} else {
return [ {
label ,
data : plot ,
borderWidth : 2 ,
borderColor : 'rgba(166,187,210,0.5)' ,
pointHoverBackgroundColor : 'rgba(166,187,210,0.8)' ,
pointBorderColor : 'transparent' ,
pointHoverBorderColor : 'transparent' ,
pointHoverRadius : 4 ,
backgroundColor : prev _gradient ,
fill : true ,
} ]
yAxisID : 'y' ,
}
] . concat ( comparisonDataSet )
}
}