【发布时间】: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();
【问题讨论】:
-
能否提供源代码?我有同样的问题,我首先在 ProgressBar 上遇到错误,例如来自 progressbar.js 文件的“TypeError: element is null”
标签: javascript jquery user-interface progress-bar