【问题标题】:Using dynamic data in a progress bar在进度条中使用动态数据
【发布时间】:2016-12-28 19:10:09
【问题描述】:

我试图弄清楚如何在 progressBar.js 的进度条中使用我自己的数据。我不确定如何将我的数据设置到代码中以使进度条以动态格式运行。

我想将total_goals 设置为 100% 的数字。然后我希望进度条可以缩放到以total_goals 表示的已完成目标,即:goals_completed / total_goals。然后用我的goal_percent作为圆圈内的文字值。

我的值是从 PHP 以 json 编码形式发送的。

var total_goals = result.total_goals;
var goals_completed = result.goals_completed;
var goal_percent = result.completion_percentage;
$('#total-goals').html(total_goals);
$('#goals-completed').html(goals_completed);
$('#goal-percentage').html(goal_percent);

例如清酒:

总进球数 = 6

完成的目标 = 3

目标百分比 = 50%

 step: function(state, circle) {
    circle.path.setAttribute('stroke', state.color);
    circle.path.setAttribute('stroke-width', state.width);

    var value = Math.round(circle.value() * 100);
    if (value === 0) {
      circle.setText('');
    } else {
      circle.setText(value);
    }

  }
});
bar.text.style.fontFamily = '"Raleway", Helvetica, sans-serif';
bar.text.style.fontSize = '2rem';

bar.animate(1.0);

更新

var total_goals;
var goals_completed;
var goal_percent;

    function goalBar(){
        $.ajax({ 
                url: "ajax-php/goal-bar.php",
                type: "get",
                dataType : 'json',
                success: function (result) {
                  //console.log(result);
                    if (result == "Error!") {
                        alert("Unable to retrieve goal bar info!");
                        alert(result);
                    } else {
                        total_goals = result.total_goals;
                        goals_completed = result.goals_completed;
                        goal_percent = result.completion_percentage;
                        $('#total-goals').html(total_goals);
                        $('#goals-completed').html(goals_completed);
                        $('#goal-percentage').html(goal_percent);
                    }
                },
                error: function (xhr, textStatus, errorThrown) {
                    alert(textStatus + " | " + errorThrown);
                    console.log("error"); //otherwise error if status code is other than 200.
                }
            });
            //Goal Bar 

var bar = new ProgressBar.Circle('#goal-bar-container', {
  color: '#aaa',
  // This has to be the same size as the maximum width to
  // prevent clipping
  strokeWidth: 4,
  trailWidth: 1,
  easing: 'easeInOut',
  duration: 1400,
  text: {
    autoStyleContainer: false
  },
  from: { color: '#aaa', width: 1 },
  to: { color: '#333', width: 4 },
  // Set default step function for all animate calls
  step: function(state, circle) {
    circle.path.setAttribute('stroke', state.color);
    circle.path.setAttribute('stroke-width', state.width);

    var value = Math.round(circle.value() * 100);
    if (value === 0) {
      circle.setText('0');
    } else {
      circle.setText(value);
    }

  }
});
bar.text.style.fontFamily = '"Raleway", Helvetica, sans-serif';
bar.text.style.fontSize = '2rem';

bar.animate(goals_completed/total_goals);  // Number from 0.0 to 1.0    
    }
    goalBar();

https://jsfiddle.net/kimmobrunfeldt/72tkyn40/

【问题讨论】:

  • 能否提供源代码?我有同样的问题,我首先在 ProgressBar 上遇到错误,例如来自 progressbar.js 文件的“TypeError: element is null”

标签: javascript jquery user-interface progress-bar


【解决方案1】:

bar.animate(1.0); 更改为bar.animate(goals_completed/total_goals);

该行中的值决定了圆圈被填充的百分比。所以,你需要传入你的变量除法来改变1.0(等于100%)。

https://jsfiddle.net/72tkyn40/4018/

【讨论】:

  • 谢谢。你明白为什么这在我的功能中不起作用吗?我在函数中包含了我的进度条代码,以便能够使用动态变量,但它们一直未定义。
  • 尝试首先将变量定义为全局变量(在任何函数之外:var goals_completed; 等),然后在您的 ajax 中通过省略每个变量的 var 部分来更新变量。
  • 谢谢。刚刚尝试过,现在 progressBar.js 文件为 null 值抛出了一堆错误。我更新了我的问题中的代码。,
  • //Goal Bar 行之后添加console.log(goals_completed)。你能从这个价值中得到什么?
  • 它出现未定义。我的 ajax 中的值在我将它们分配给页面上的 id 时起作用。见图片。
猜你喜欢
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
  • 2021-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多