DaRoGa DaRoGa - 1 month ago 75
TypeScript Question

Angular2 and D3.js Gantt chart

I have an angular2 app which i am attempting to implement a Gantt chart from d3.js into. I am trying to use the following example http://bl.ocks.org/dk8996/5538271. obviously this uses javascript rather than typescript. However i have got most of it seeming ok, but i get one issue.

Here is my whole method:

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;
}

}


The bit where i get the error is:

var gantt = d3.gantt().taskTypes(taskNames).taskStatus(taskStatus).tickFormat(format).height(450).width(800);


It says
Property 'gantt' does not exist on type 'typeof d3'


I import my d3 as follows

import * as d3 from 'd3';


I dont have any errors anywhere else and i am struggling to track down the issue.

Any ideas?

EDIT

Also as a test to make sure d3 is working i implement a simple circle as follows:

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");


This works fine but the .gantt() does not

Answer

You are having that error because gantt() method is not part of the d3 library. It is imported from <script type="text/javascript" src="http://static.mentful.com/gantt-chart-d3v2.js"> </script‌​> in your example.

If you can find the definition file of the gantt library you can add it. If not, you have to create a custom one like:

declare module d3 {
    export function gantt(input:any, input2:any): any;

}
Comments