@@ -1088,6 +1088,62 @@ describe('Plugin.Tooltip', function() {
10881088 expect ( Object . prototype . hasOwnProperty . call ( fn . calls . first ( ) . args [ 1 ] , 'y' ) ) . toBe ( true ) ;
10891089 expect ( fn . calls . first ( ) . object instanceof Tooltip ) . toBe ( true ) ;
10901090 } ) ;
1091+
1092+ it ( 'Should ignore same x position when calculating average position with index interaction on stacked bar' , async function ( ) {
1093+ var chart = window . acquireChart ( {
1094+ type : 'bar' ,
1095+ data : {
1096+ datasets : [ {
1097+ label : 'Dataset 1' ,
1098+ data : [ 10 , 20 , 30 ] ,
1099+ pointHoverBorderColor : 'rgb(255, 0, 0)' ,
1100+ pointHoverBackgroundColor : 'rgb(0, 255, 0)' ,
1101+ stack : 'stack1' ,
1102+ } , {
1103+ label : 'Dataset 2' ,
1104+ data : [ 40 , 40 , 40 ] ,
1105+ pointHoverBorderColor : 'rgb(0, 0, 255)' ,
1106+ pointHoverBackgroundColor : 'rgb(0, 255, 255)' ,
1107+ stack : 'stack1' ,
1108+ } , {
1109+ label : 'Dataset 3' ,
1110+ data : [ 90 , 100 , 110 ] ,
1111+ pointHoverBorderColor : 'rgb(0, 0, 255)' ,
1112+ pointHoverBackgroundColor : 'rgb(0, 255, 255)'
1113+ } ] ,
1114+ labels : [ 'Point 1' , 'Point 2' , 'Point 3' ]
1115+ } ,
1116+ options : {
1117+ interaction : {
1118+ mode : 'index'
1119+ } ,
1120+ plugins : {
1121+ position : 'average' ,
1122+ } ,
1123+ }
1124+ } ) ;
1125+
1126+ // Trigger an event over top of the
1127+ var pointIndex = 1 ;
1128+ var datasetIndex = 0 ;
1129+ var meta = chart . getDatasetMeta ( datasetIndex ) ;
1130+ var point = meta . data [ pointIndex ] ;
1131+ await jasmine . triggerMouseEvent ( chart , 'mousemove' , point ) ;
1132+
1133+ var tooltipModel = chart . tooltip ;
1134+ const activeElements = tooltipModel . getActiveElements ( ) ;
1135+
1136+ const xPositionArray = activeElements . map ( ( element ) => element . element . x ) ;
1137+ const xPositionArrayAverage = xPositionArray . reduce ( ( a , b ) => a + b ) / xPositionArray . length ;
1138+
1139+ const xPositionSet = new Set ( xPositionArray ) ;
1140+ const xPositionSetAverage = [ ...xPositionSet ] . reduce ( ( a , b ) => a + b ) / xPositionSet . size ;
1141+
1142+ expect ( xPositionArray . length ) . toBe ( 3 ) ;
1143+ expect ( xPositionSet . size ) . toBe ( 2 ) ;
1144+ expect ( tooltipModel . caretX ) . not . toBe ( xPositionArrayAverage ) ;
1145+ expect ( tooltipModel . caretX ) . toBe ( xPositionSetAverage ) ;
1146+ } ) ;
10911147 } ) ;
10921148
10931149 it ( 'Should avoid tooltip truncation in x axis if there is enough space to show tooltip without truncation' , async function ( ) {
0 commit comments