为什么我的进度条显示浮点值?

发布于 2024-11-30 23:56:33 字数 1831 浏览 0 评论 0原文

本例中的“value”为 5,“maxValue”为 39。进度条上显示的值 (a wijmo 进度条)是 5.07。 {0} 应告诉进度条显示进度值而不是百分比。如果我将值更改为 8,保持最大值为 39,则显示的值将变为 8.19。我有点困惑为什么我看到的是浮点数而不是整数。我在进度条代码中看不到任何内容,除了 self.value() 以外的任何内容,它以整数形式记录到控制台。那么什么可能导致这种行为呢?

$("#proBarAdherence").wijprogressbar({

  value: Gymloop.completedCount,
  animationOptions: {
    duration: 1000
  },

  maxValue: Gymloop.targetSessions,
  indicatorImage: '/images/progbar_red.png',
  labelAlign: 'running',
  labelFormatString: '{0} sessions completed',
  beforeProgressChanging: function(e,data) {
    console.log('beforeprogresschanging',data);
  },
  progressChanging: function (e,data) {
    console.log('progresschanging',data);

  },
  progressChanged : function (e,data) {
    console.log('progresschanged',data);
  }
});

beforeprogresschanging Object { oldValue="5", newValue=5}
progresschanging Object { oldValue="5", newValue=0}
progresschanging Object { oldValue="0", newValue=1.17}
progresschanging Object { oldValue="1.17", newValue=1.56}
progresschanging Object { oldValue="1.56", newValue=1.95}
progresschanging Object { oldValue="1.95", newValue=2.34}
progresschanging Object { oldValue="2.34", newValue=2.73}
progresschanging Object { oldValue="2.73", newValue=3.12}
progresschanging Object { oldValue="3.12", newValue=3.51}
progresschanging Object { oldValue="3.51", newValue=3.9}
progresschanging Object { oldValue="3.9", newValue=4.29}
progresschanging Object { oldValue="4.29", newValue=4.68}
progresschanging Object { oldValue="4.68", newValue=5.07}
progresschanged Object { oldValue="5", newValue=5}

更新

我可以在 ProgressChanging 事件期间看到该值,但不确定如何在动画完成后使标签显示 5 而不是 5.07? 这里有一个实例

'value' in this example is 5, 'maxValue' is 39. The value displayed on the progress bar (a wijmo progressbar) is 5.07. {0} should tell the progressbar to show a progress value rather than a percentage. If I change the value to 8, maintaining the max value as 39, the value displayed becomes 8.19. I'm a bit confused as to why I am seeing floating point numbers and not integers. I can't see anything in the progressbar code which would display anything other than self.value() which logs to the console as an integer. So what might be causing this behaviour?

$("#proBarAdherence").wijprogressbar({

  value: Gymloop.completedCount,
  animationOptions: {
    duration: 1000
  },

  maxValue: Gymloop.targetSessions,
  indicatorImage: '/images/progbar_red.png',
  labelAlign: 'running',
  labelFormatString: '{0} sessions completed',
  beforeProgressChanging: function(e,data) {
    console.log('beforeprogresschanging',data);
  },
  progressChanging: function (e,data) {
    console.log('progresschanging',data);

  },
  progressChanged : function (e,data) {
    console.log('progresschanged',data);
  }
});

beforeprogresschanging Object { oldValue="5", newValue=5}
progresschanging Object { oldValue="5", newValue=0}
progresschanging Object { oldValue="0", newValue=1.17}
progresschanging Object { oldValue="1.17", newValue=1.56}
progresschanging Object { oldValue="1.56", newValue=1.95}
progresschanging Object { oldValue="1.95", newValue=2.34}
progresschanging Object { oldValue="2.34", newValue=2.73}
progresschanging Object { oldValue="2.73", newValue=3.12}
progresschanging Object { oldValue="3.12", newValue=3.51}
progresschanging Object { oldValue="3.51", newValue=3.9}
progresschanging Object { oldValue="3.9", newValue=4.29}
progresschanging Object { oldValue="4.29", newValue=4.68}
progresschanging Object { oldValue="4.68", newValue=5.07}
progresschanged Object { oldValue="5", newValue=5}

Update

I can see the value's during the progressChanging event but am not sure how to make the label display 5 instead of 5.07 once the the animation is complete? There is a live example here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

今天小雨转甜 2024-12-07 23:56:33

如果你想显示整数,你需要四舍五入 JavaScript 对所有内容都使用浮点数。
如何转换浮点数JavaScript 中的整数?

--- 对更新的响应 ---
我对那个确切的进度条不是特别熟悉,但它们都很相似。
发生的情况是,您在变量初始化时调用舍入一次,而不是在变量更新时调用舍入一次。

有一个与正在更新的加载栏关联的事件,该事件是您想要对已完成的舍入进行舍入的位置在传递到进度条之前进行计数。

我不确定我是否很好地解释了发生的事情
value = Math.round(myobj.completedCount), ---update event--> CompletedCount = newValue
---更新事件---> CompletedCount = newValue
您需要做的是捕获该更新事件并将变量舍入那里。
---更新事件--> CompletedCount = round(newValue)

------ 试试这个-----
问题线是这个
percent = (value - self.min) / (self.max - self.min) * 100;
将 wijmo 栏源代码中的第 328 行替换为
percent = Math.round((value - self.min) / (self.max - self.min)) * 100; < br> 它应该按照您想要的方式运行。

You need to round if you want to display integers JavaScript uses floats for everything.
How do I convert a float number to a whole number in JavaScript?

--- Response to the update ---

I am not particularly familiar with that exact progress bar but they are all similar.
Whats happening is you are calling round once when the variable is initialized and not when the variable is updated.

There is an event associated with the loading bar being updated that is where you want to round the completed Count before it is passed to the progress bar.

I am not sure if I am doing a good job explaining whats happening
value = Math.round(myobj.completedCount), ---update event--> completedCount = newValue
---update event---> completedCount = newValue
what you need to do is catch that update event and round the variable there.
---update event--> completedCount = round(newValue)

------ TRY THIS-------
The trouble line is this
percent = (value - self.min) / (self.max - self.min) * 100;

replace line 328 from the source code of the wijmo bar with
percent = Math.round((value - self.min) / (self.max - self.min)) * 100;
and it should act the way you want it to.

旧城烟雨 2024-12-07 23:56:33

我最终选择了这个(编辑进度条js文件 - 恶心):

self.label.html(self._getFormatString(
            o.labelFormatString, Math.round(percent), Math.round(curValue)));

我还记录了 wijmo 开发者的 bug

无需编辑源代码的另一个选项是:

(function(){
// Store a reference to the original html method.
var originalHtmlMethod = jQuery.fn.html;

// Define overriding method
jQuery.fn.html = function(){

// Execute our override - rounding the value
// being passed to jQuery's html() function
// only when the progressbar label is being
// updated
if ($(this).hasClass('ui-progressbar-label')) {
arguments[0] = Math.round(arguments[0]);
}

// Execute the original jquery method for all other html() calls on the     page
originalHtmlMethod.apply( this, arguments );
}
})();

I went with this in the end (editing the progressbar js file - yuck):

self.label.html(self._getFormatString(
            o.labelFormatString, Math.round(percent), Math.round(curValue)));

I also logged a bug with the wijmo developers.

Another option without editing the source is :

(function(){
// Store a reference to the original html method.
var originalHtmlMethod = jQuery.fn.html;

// Define overriding method
jQuery.fn.html = function(){

// Execute our override - rounding the value
// being passed to jQuery's html() function
// only when the progressbar label is being
// updated
if ($(this).hasClass('ui-progressbar-label')) {
arguments[0] = Math.round(arguments[0]);
}

// Execute the original jquery method for all other html() calls on the     page
originalHtmlMethod.apply( this, arguments );
}
})();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文