我有一个angular2应用程序,我试图从d3.js实现甘特图。我试图使用以下示例http://bl.ocks.org/dk8996/5538271。显然这使用javascript而不是打字稿。但是我的大部分看似没问题,但我遇到了一个问题。
这是我的整个方法:
createSchedule() {
var tasks = [{"startDate":new Date("Sun Dec 09 01:36:45 EST 2012"),"endDate":new Date("Sun Dec 09 02:36:45 EST 2012"),"taskName":"E Job","status":"RUNNING"}];
var taskStatus = {
"SUCCEEDED" : "bar",
"FAILED" : "bar-failed",
"RUNNING" : "bar-running",
"KILLED" : "bar-killed"
};
var taskNames = [ "D Job", "P Job", "E Job", "A Job", "N Job" ];
tasks.sort(function(a,b) {
return a.endDate.getTime() - b.endDate.getTime();
});
var maxDate = tasks[tasks.length -1].endDate;
tasks.sort(function(a,b) {
return a.startDate.getTime() - b.startDate.getTime();
});
var minDate = tasks[0].startDate;
var format = "%H:%M";
var timeDomainString = "1day";
var gantt = d3.gantt().taskTypes(taskNames).taskStatus(taskStatus).tickFormat(format).height(450).width(800);
gantt.timeDomainMode("fixed");
changeTimeDomain(timeDomainString);
gantt(tasks);
function changeTimeDomain(timeDomainString) {
this.timeDomainString = timeDomainString;
switch (timeDomainString) {
case "1hr":
format = "%H:%M:%S";
gantt.timeDomain([ d3.time.hour.offset(new Date(getEndDate()), -1), getEndDate() ]);
break;
case "3hr":
format = "%H:%M";
gantt.timeDomain([ d3.time.hour.offset(new Date(getEndDate()), -3), getEndDate() ]);
break;
case "6hr":
format = "%H:%M";
gantt.timeDomain([ d3.time.hour.offset(new Date(getEndDate()), -6), getEndDate() ]);
break;
case "1day":
format = "%H:%M";
gantt.timeDomain([ d3.time.day.offset(new Date(getEndDate()), -1), getEndDate() ]);
break;
case "1week":
format = "%a %H:%M";
gantt.timeDomain([ d3.time.day.offset(new Date(getEndDate()), -7), getEndDate() ]);
break;
default:
format = "%H:%M"
}
gantt.tickFormat(format);
gantt.redraw(tasks);
}
function getEndDate() {
var lastEndDate = Date.now();
if (tasks.length > 0) {
lastEndDate = tasks[tasks.length - 1].endDate.getTime();
}
return lastEndDate;
}
}
我得到错误的位是:
var gantt = d3.gantt().taskTypes(taskNames).taskStatus(taskStatus).tickFormat(format).height(450).width(800);
它说Property 'gantt' does not exist on type 'typeof d3'
我按如下方式导入我的d3
import * as d3 from 'd3';
我在其他任何地方都没有任何错误,我正在努力追查这个问题。
有什么想法吗?
修改
另外,作为确保d3正常工作的测试,我实现了一个简单的循环,如下所示:
d3.select("#scheduleDiv").append("svg").attr("width",50).attr("height", 50).append("circle").attr("cx", 25).attr("cy", 25).attr("r", 25).style("fill", "purple");
这很好,但.gantt()不是
答案 0 :(得分:3)
您遇到该错误,因为gantt()
方法不属于d3库。它是从中导入的
在你的例子中<script type="text/javascript" src="http://static.mentful.com/gantt-chart-d3v2.js">
</script>
。
如果您可以找到甘特图库的定义文件,则可以添加它。如果没有,您必须创建一个自定义的:
declare module d3 {
export function gantt(input:any, input2:any): any;
}