You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by GitBox <gi...@apache.org> on 2021/03/18 16:42:56 UTC

[GitHub] [echarts] tamersalama commented on issue #2941: Handling click events on chart grid columns

tamersalama commented on issue #2941:
URL: https://github.com/apache/echarts/issues/2941#issuecomment-802098969


   Here's my work-around (for line charts). It's not very efficient as it tries to extract the closest match in an array.
   Tooltip already has the necessary logic that provides the closest match (or it's passed to it). Unfortunately, I wasn't able to get to that code or use it. The tooltip formatter can be used as a yet another work-around.
   
   
   
   ```
       /**
       * Find the closest number in an array from the passed number argument
       * This is used in click events to get the series number
       * https://www.tutorialspoint.com/get-closest-number-out-of-array-javascript
       */
       const closest = (arr, num) => {
           return arr.reduce((acc, val) => {
              if(Math.abs(val - num) < Math.abs(acc)){
                 return val - num;
              }else{
                 return acc;
              }
           }, Infinity) + num;
        }
   
        zr = chart.getZr();
        zr.on('dblclick', params => {
            var pointInPixel = [params.offsetX, params.offsetY];
            var pointInGrid = chart.convertFromPixel('series', pointInPixel);
   
            seriesDataX = chart.getOption().series[0].data.map(x => x[0])
            closestXPoint = closest(seriesDataX, pointInGrid[0])
   
            // Do something with closestXPoint
       });
   
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org