javascript - 无法在我的页面内显示旋钮饼图,现在正在显示饼图
问题描述
我想在我们的网页中添加倒计时饼图,饼图将以以下格式显示我们项目的重命名时间:-
**Project A**
starts in 3 weeks and 3 days
**Project B**
starts in 1 month and 2 weeks
这是实现此功能并显示上述结果的 Javascript:-
<script src="/Resources/jquery-1.11.3.js"></script>
<script>
$(function () {
var htmlinit = "";
htmlinit = "<image id= 'customloader' src= '/resources/ajax-loader.gif'></image>";
$("#inserhere").after(htmlinit);
var html="<div class='ms-comm-adminLinks ms-core-defaultFont ms-noList' unselectable='on'><div class='ms-webpart-titleText' unselectable='on'><a style='color:white' href='/Lists/Counter/AllItems.aspx'> Useful Links </a> </div><ul class='ms-comm-adminLinksList' unselectable='on'>";
$.ajax({
url: "/_api/web/lists/getbytitle('Counter')/items?$select=Title,CounterStartDate&$orderby=CounterStartDate asc",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
if(data.d.results.length>0){
var items=data.d.results;
for(var i=0;i<items.length;i++){
var nurl = items[i].CounterStartDate.toString();
var ntitle = items[i].Title.toString();
var loopidcustom = "demo"+i.toString();
var formatteddate = datediff(new Date (nurl));
document.getElementById(loopidcustom).innerHTML = "<b>" + ntitle + "</b><br> Start Date "+ d+"<br/> Starts in " + formatteddate +"<hr/>" ;
if (distance < 0) {
//clearInterval(x);
document.getElementById(loopidcustom).innerHTML = "";
}
}
}
},
error: function (data) {
$("#customloader").hide();
}
});
});
function datediff(date) {
let d1 = date;
let d2 = now = new Date();
if (d2.getTime() < d1.getTime()) {
d1 = now;
d2 = date;
}
let yd = d1.getYear();
let yn = d2.getYear();
let years = yn - yd;
let md = d1.getMonth();
let mn = d2.getMonth();
let months = mn - md;
if (months < 0) {
years--;
months = 12 - md + mn;
}
let dd = d1.getDate();
let dn = d2.getDate();
let days = dn - dd;
if (days < 0) {
months--;
// figure out how many days there are in the last month
d2.setMonth(mn, 0);
days = d2.getDate() - dd + dn;
}
let weeks = Math.floor(days / 7);
days = days % 7;
if (years > 0) return years + ' years' + (months > 0 ? ' and ' + months + ' months' : '');
if (months > 0) return months + ' months' + (weeks > 0 ? ' and ' + weeks + ' weeks' : '');
if (weeks > 0) return weeks + ' weeks' + (days > 0 ? ' and ' + days + ' days' : '');
return days + ' days';
}
</script>
现在我想修改它并在饼图中显示剩余的年、月、周和日,而不是只显示文本。所以我觉得这个只读旋钮饼图很有趣http://anthonyterrien.com/demo/knob/ :-
但是当我尝试在我们的网页中对其进行测试时,我遇到了这些问题:-
我添加了以下代码来测试饼图的外观:-
<script> $(function($) { $(".knob").knob({ change : function (value) { //console.log("change : " + value); }, release : function (value) { //console.log(this.$.attr('value')); console.log("release : " + value); }, cancel : function () { console.log("cancel : ", this); }, /*format : function (value) { return value + '%'; },*/ draw : function () { // "tron" case if(this.$.data('skin') == 'tron') { this.cursorExt = 0.3; var a = this.arc(this.cv) // Arc , pa // Previous arc , r = 1; this.g.lineWidth = this.lineWidth; if (this.o.displayPrevious) { pa = this.arc(this.v); this.g.beginPath(); this.g.strokeStyle = this.pColor; this.g.arc(this.xy, this.xy, this.radius - this.lineWidth, pa.s, pa.e, pa.d); this.g.stroke(); } this.g.beginPath(); this.g.strokeStyle = r ? this.o.fgColor : this.fgColor ; this.g.arc(this.xy, this.xy, this.radius - this.lineWidth, a.s, a.e, a.d); this.g.stroke(); this.g.lineWidth = 2; this.g.beginPath(); this.g.strokeStyle = this.o.fgColor; this.g.arc( this.xy, this.xy, this.radius - this.lineWidth + 1 + this.lineWidth * 2 / 3, 0, 2 * Math.PI, false); this.g.stroke(); return false; } } }); // Example of infinite knob, iPod click wheel var v, up=0,down=0,i=0 ,$idir = $("div.idir") ,$ival = $("div.ival") ,incr = function() { i++; $idir.show().html("+").fadeOut(); $ival.html(i); } ,decr = function() { i--; $idir.show().html("-").fadeOut(); $ival.html(i); }; $("input.infinite").knob( { min : 0 , max : 20 , stopper : false , change : function () { if(v > this.cv){ if(up){ decr(); up=0; }else{up=1;down=0;} } else { if(v < this.cv){ if(down){ incr(); down=0; }else{down=1;up=0;} } } v = this.cv; } }); }); </script> <style> body{ font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-weight: 300; text-rendering: optimizelegibility; } p{font-size: 30px; line-height: 30px} div.demo{text-align: center; width: 280px; float: left} div.demo > p{font-size: 20px} </style>
但我只得到了没有任何饼图的数字,如下所示:-
第二个问题,现在假设我设法正确显示饼图。那么我可以修改饼图的构建方式吗?例如,在我的情况下,我希望饼图显示倒计时,所以如果剩余的是 1 天,那么饼图应该几乎是全彩色的。我将拥有这些不同的范围:-
- 对于 Day 它可以是从 1 到 7
- 月份可以是 1 到 12
- 几周可以从 1 到 4
这可能吗?
解决方案
@john,根据您的评论,我准备了一个使用roundSlider插件的示例。检查下面的演示,让我知道它是否满足您的要求:
DEMO
由于示例目的,我使用了硬编码的 id,您可以根据您的应用场景进行更改。
推荐阅读
- php - 在移动文件上的 move_uploaded_file() 之后调用 PHP unlink() 失败
- jenkins - 将 SonarQube 的 Jenkins 构建步骤转换为管道?
- google-maps - 是否可以在 Thingsboard 中获取地图小部件的警报数据?
- reactjs - 为什么在创建 React 应用程序时会自动创建 .git 文件夹?
- image - Imagemagick:如何减小具有大静态区域的动画 GIF 的大小?
- react-native - React Redux combineReducer | 减速机未通过
- ios - 尝试在 ChartDataEntry 中输入日期/字符串
- java - 在 iOS 上自动化 Safari 页面
- .net - System.TypeLoadException:从负载测试插件使用 KeyVault 时的“方法 'get_SerializationSettings'
- javascript - p5.play.js loadAnimation 无法正常运行