Highchart datepicker

前端 未结 1 803
南旧
南旧 2020-12-30 18:03

I am trying to get jquery-ui datepicker to work with highcharts so that a user can select a date an example being

A user selects 10th October to 25th October

相关标签:
1条回答
  • 2020-12-30 18:30

    In onSelect callback of datepickers, you should validate, if both #from and #to are selected (or provide sensible defaults if not) and at the end fire and xhr request to server to get new series of data.

    onSelect: function( selectedDate ) {
      var option = this.id == "from" ? "minDate" : "maxDate",
      instance = $( this ).data( "datepicker" ),
        date = $.datepicker.parseDate(
          instance.settings.dateFormat || $.datepicker._defaults.dateFormat,
          selectedDate,
          instance.settings
        );
        dates.not( this ).datepicker( "option", option, date );
    
        // validate if we have both dates and get new series from server
        if ($(dates[0]).datepicker("getDate") &&
            $(dates[1]).datepicker("getDate")) {
          $.ajax({
            type: 'POST',
            url: '<%= user_projects_path(params[:user_id]) %>',
            data: {"from": $(dates[0]).datepicker("getDate"), "to" : $(dates[1]).datepicker("getDate") },
            success: function(data) {
              // now we have new series of data to display in graph
              // we remove the old one, add the new and redraw the chart
              for(var i=0; i<chart.series.length; i++) {
                chart.get(chart.series[i].options.id).remove();
              }
    
              // fiddle with json data from xhr response to form valid series
              var series = data; 
              chart.addSeries(series, true); // second param says we want to redraw chart
            }
          });
        }
    }
    

    Controller method under user_projects_path url needs to exists and return JSON formatted series data for given user_id of course. You can filter your series data before returning with params sent by jquery xhr request (from and to).

    Quick and dirty solution but I hope you got the point...

    0 讨论(0)
提交回复
热议问题