Estri. P Lestari
Estri. P Lestari

Reputation: 161

How to filter chart.js with datepicker?

I am new to programming. I have a chart made by chart.js with data from API. This is my API URL:https://gmlews.com/api/data/?node_id=1

I want to make my moisture data from API plot on the chart. The y-axis is for moisture data and X-axis is from timestamp that I have on the API. I try to using datepicker to manage which data I want to show on the chart.

So, far this is my code :

$(function() {

$('input[name="datefilter"]').daterangepicker({
    showDropdowns : true,
autoUpdateInput: false,
locale: {
    cancelLabel: 'Clear'
}
});

$('input[name="datefilter"]').on('apply.daterangepicker', function(ev, picker) {
$(this).val(picker.startDate.format('DD/MM/YYYY') + ' - ' + picker.endDate.format('DD/MM/YYYY'));
var fromDate = picker.startDate.format('DD/MM/YYYY');
var toDate = picker.endDate.format('DD/MM/YYY');
if (fromDate != '' && toDate != '') {
      console.log(fromDate, toDate);
      var endpoint = 'https://gmlews.com/api/data/?node_id=1';

 $.ajax({
 method: "GET",
 url: endpoint,
 data: {
 fromDate: fromDate,
 toDate: toDate
 },
 success: function(data){
 console.log(data); //get all data
 //get data by fields
 var hum = [], time = [];
 for (var i=0; i<data.length; i++){
  hum.push(data[i].moisture);
  time.push(data[i].timestamp);
 }
 console.log(hum);
 console.log(time);
//plot the chart

var ctx = document.getElementById("moistureChart").getContext('2d');
var moistureChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: time,
        datasets: [{
            label: 'kelembaban',
            data: hum,
            backgroundColor: [
                '#ff000000'
            ],
            borderColor: [
            '#331698'
            ],
            borderCapStyle: 'round',
            borderWidth: 1
        }]
    },
    options: {
        reponsive: true,
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:false,
                    stepSize:10
                },
                  scaleLabel: {
                  display:     true,
                  labelString: 'kelembaban'
              }
            }],
            xAxes: [{

                    display: true,
                    type: "time",
                    time: {
                          minUnit: "hour",
                          unit: "hour",
                          unitStepSize: 6,
                          min: moment(fromDate).toDate(),//Date object type
                          max: moment(toDate).toDate()//Date object type
                    },
                  scaleLabel: {
                  display:     true,
                  labelString: 'Time'
              }
            }]
        }
    }
});
},
error: function(error_data){
console.log(error_data)
} 

});
} //if function
}); //tombol apply


$('input[name="datefilter"]').on('cancel.daterangepicker', function(ev, picker) {
$(this).val('');
});

});

you can access the code from the fiddle in here : https://jsfiddle.net/estri012/ftu8g74c/5/

The problem is when I try to pick the date from datepicke, example : date from 7 May until 8 May. It won't load anything, but actually the data in API for 7 May is exist. And then when I try to pick the date from 13 May until 14 May, the datepicker filter doesn't work, it just show all the data that I have in the API. Can you help me with this problem? Thank you!

Upvotes: 0

Views: 2500

Answers (1)

Aurel
Aurel

Reputation: 359

first of all, you're trying to filter your data from the API by sending fromDate and toDate parameters. I reached https://gmlews.com/api/data/?node_id=1 but didn't find the documentation on parameters which can be used.

If i try a GET with &fromDate=2020-05-29&toDate=2020-05-29 i receive the same data as if i call the endpoint without parameter. Same in POST.

Shouldn't you send a timestamp instead a date format ?

Is your formatted date in YYYY-MM-DD format ?

Upvotes: 1

Related Questions