Home Forums Chart Support Get number of bars in zoomed area

Get number of bars in zoomed area

Viewing 2 posts - 1 through 2 (of 2 total)
  • #17584

    Hello. I’m using this graph:
    https://canvasjs.com/javascript-charts/stacked-column-chart/

    with zoomEnabled: true.

    I want to get the CURRENT number of bars in the selected area on the graph.

    For example, in the link provided, choosing zoom between 2010 and 2016 should return 6, and choosing zoom between 2010 to 2011 should return 1.

    I’m trying to use this code:

    function updateViewByLen(chart) {
      var viewportMinimum = chart.axisX[0].get("viewportMinimum");
      var viewportMaximum = chart.axisX[0].get("viewportMaximum");
      debugger
      var countMal = 0
      var countBen = 0
      var malwareLength = chart.data[0].dataPoints.length
      var benLength = chart.options.data[1].dataPoints.length
        for(var i = 0; i < chart.data[0].dataPoints.length; i++){
            if(chart.data[0].dataPoints[i].x >= viewportMinimum && chart.data[0].dataPoints[i].x <= viewportMaximum){
              countMal = countMal + 1;
            }
        }
        for(var i = 0; i < chart.data[1].dataPoints.length; i++){
            if(chart.data[1].dataPoints[i].x >= viewportMinimum && chart.data[1].dataPoints[i].x <= viewportMaximum){
              countBen = countBen + 1;
            }
        }
      console.log("countMal: " + countMal + ", countBen: " + countBen);
    
    }
    

    To count manually (countMal and countBen), but because i’m changing the dataPoints at every zoom (By function called dataFilter(e) you helped me create)
    The numbers recieved are not always true. By some view it is true, and by other it’s false. I want to get the CURRENT number of bars I have on the graph.

    Thanks.

    #17596

    @johnsnowthedeveloper,

    updateViewByLen(chart) needs to be called after the data is filtered and the chart is rendered.

    Please take a look at this updated jsfiddle.

    ___________
    Indranil Deo,
    Team CanvasJS

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.