- How to display value that get from some URL using jQuery Knob Chart in Bootstrap 4. I got "NaN" value. (Please refer code below)
- How to display "-" dash symbol if the data is null. (Please refer code below)
HTML
<input type="text" class="knob" id="avg_temperature" data-width="120" data-height="120" data-thickness="0.40" data-fgColor="#000000" readonly>
JS
$.ajax({
url : XXX,
type : 'POST',
dataType : 'json',
cache: false,
async: false,
dataSrc : 'data',
contentType: false,
processData: true,
success: function(response){
if (response.status == "Success"){
if (response.data[0]["avg_temperature"] == null){
response.data[0]["avg_temperature"] = "-";
$("#avg_temperature").text("-");
}
$("#avg_temperature").text(response.data[0]["avg_temperature"]);
var colors = ['#11E117', '#FFC300', '#C00']
//knob chart for temperature
$('#avg_temperature').knob();
$('#avg_temperature').attr('data-fgColor', '#11E117');
$({animatedVal: 0}).animate({animatedVal: response},{
duration: 3000,
easing: "swing",
async: false,
step: function() {
var val = Math.ceil(this.animatedVal);
$("#avg_temperature").trigger('configure', {
'fgColor': colors[(val < 40) ? 0 : (val < 70) ? 1 : 2]
}).val(val).trigger("change");
var newVal = val + String.fromCharCode(176) + 'C'; $('#avg_temperature').val(newVal);
}
});
}
},
});

From the context of your code it seems that
responseis an object, and this is the cause of the issue asanimate()expects the value you provide to be an integer.From the context of your usage of
responseelsewhere in the code it appears that you need to access a specifictemperatureproperty from it, like this:Also note the removal of
async: false. It's bad practice, and you don't need it here anyway