【问题标题】:How to display value at knob chart using jquery?如何使用 jquery 在旋钮图表上显示值?
【发布时间】:2020-05-30 04:21:29
【问题描述】:
  1. 如何在 Bootstrap 4 中使用 jQuery Knob Chart 显示从某个 URL 获取的值。我得到了“NaN”值。 (请参考下面的代码)
  2. 如果数据为空,如何显示“-”短划线符号。 (请参考下面的代码)

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);
                }
            });
        }
    },
});

【问题讨论】:

  • 这似乎是孤立的:jsfiddle.net/RoryMcCrossan/qzfw36ct/1。您能否将问题的工作示例添加到问题中,以便我们创建 MCVE。请注意,可以删除 AJAX 部分,因为它与问题无关。
  • 我已经在那个链接上提供了代码。用于 HTML 和 JS。如果这还不够,请告诉我。
  • response的值是多少?
  • 我创建了一个变量$("#avg_temperature").val = "90";作为值。
  • 不,我问你$.ajax 调用中response 参数的值是多少。您在 animate() 方法中使用它。我假设它是一个对象,而不是一个整数,因此你的错误。从上下文看来您应该使用...animate({ animatedVal: parseInt(response.data[0]["avg_temperature"], 10) })

标签: jquery html ajax bootstrap-4 jquery-knob


【解决方案1】:

从您的代码上下文来看,response 似乎是一个对象,这是导致问题的原因,因为animate() 期望您提供的值是一个整数。

从您在代码中其他地方使用response 的上下文来看,您似乎需要从中访问特定的temperature 属性,如下所示:

if (response.data[0]["avg_temperature"] == null)
    response.data[0]["avg_temperature"] = "-";

var colors = ['#11E117', '#FFC300', '#C00']

let $avgTemp = $("#avg_temperature").text(response.data[0]["avg_temperature"]);
$avgTemp.data('fgColor', '#11E117').knob();

$({
  animatedVal: 0
}).animate({
  animatedVal: parseInt(response.data[0]["avg_temperature"], 10) // update here
}, {
  duration: 3000,
  easing: "swing",
  step: function() {
    var val = Math.ceil(this.animatedVal);
    $avgTemp.trigger('configure', {
      'fgColor': colors[(val < 40) ? 0 : (val < 70) ? 1 : 2]
    }).val(val).trigger("change");

    var newVal = val + String.fromCharCode(176) + 'C';
    $avgTemp.val(newVal);
  }
});

还要注意 async: false 的删除。这是不好的做法,反正你也不需要它

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多