Home > Software engineering >  Slider implementation for the dates on the x-axis not working correctly in Highcharts
Slider implementation for the dates on the x-axis not working correctly in Highcharts

Time:09-18

I need to add a slider to my line chart that will allow user to jump to the particular dates on the x-axis. I have a slider already added to the chart but it doesn't work properly for some reason and I can't figure out what is the issue. Can someone please help?

https://jsfiddle.net/samwhite/t5sabeyv/1/

afterSetExtremes: function(e) {
                        $("#slider-range").slider("values", 0, 1975   e.min);
                        $("#slider-range").slider("values", 1, 1975   e.max);
                        $("#year").val(Math.round(e.min   1975)  
                            " - "   Math.round(e.max   1975));
                    }

$("#slider-range").slider({
      range: true,
      min: 1975,
      max: 2020,
      values: [1975, 2020],
      slide: function(event, ui) {
        $("#year").val(ui.values[0]   " - "   ui.values[1]);
        chart.xAxis[0].setExtremes(ui.values[0] - 1975, ui.values[1] - 1975)
      }
    });
    $("#year").val($("#slider-range").slider("values", 0)  
      " - "   $("#slider-range").slider("values", 1));

Here is an example of the same behavior that I am trying to achieve https://jsfiddle.net/uvat8u05/27/

Any help would be appreciate it.

CodePudding user response:

You need to use Date.UTC to set new interval inside your chart .So , you can update your code inside slider function i.e :

slide: function(event, ui) {
         $("#amount").val(ui.values[0]   " - "   ui.values[1]);
        chart.xAxis[0].setExtremes(Date.UTC(ui.values[0] ,0,1),Date.UTC(ui.values[1] ,0,1) )            
 }

Then , you where getting error chart is not defined so i have use this.axis.chart to access series and tooltip .

Working Code :

  • Related