Gantt Chart - Date Issues

Is there a way to directly right a date value to the Gantt Chart´s “Start Time” and “End Time”? I am looking for something that will work using a process such as:

date string (from popup Calendar component) > parse > data calculation > write to Gantt Chart

I am having issues with the Gantt Chart component. I keep getting an error message when I try to set the Gantt Chart dataset to a built data set. The code is below.

# reads task data from root´s Custom Property 'taskData' 
taskData = event.source.parent.taskData

# Builds ganttData dataset with no row data
headers = ['Task Name', 'Start Date', 'End Date', 'Percentage Done']
data =[]
ganttData = system.dataset.toDataSet(headers, data)

# sets percentage and startDate
percentage = 0
startDate = event.source.parent.getComponent('calStartDate').date
	
rows = taskData.getRowCount()
for row in range(rows):
	rowData =[]
	taskName = taskData.getValueAt(row,'task_name')
	weeks = taskData.getValueAt(row,'task_week')
	if row == 0:
		startDate = system.date.parse(startDate, "yyyy-MM-dd HH:mm:ss")
		endDate =  system.date.parse(system.date.addWeeks(startDate, weeks), "yyyy-MM-dd HH:mm:ss")

	else:
		prevEndDate = system.date.parse(ganttData.getValueAt(row-1,'End Date'), "yyyy-MM-dd HH:mm:ss")
		startDate = system.date.parse(system.date.addDays(prevEndDate, 1), "yyyy-MM-dd HH:mm:ss")
		endDate = system.date.parse(system.date.addWeeks(startDate, weeks), "yyyy-MM-dd HH:mm:ss")

	rowData.append(taskName)
	rowData.append(startDate)
	rowData.append(endDate)
	rowData.append(percentage)
	
	ganttData = system.dataset.addRow(ganttData, rowData)
		
	#system.db.runPrepUpdate("INSERT INTO temp_timelines (taskName, startDate, endDate, percentage) VALUES (?,?,?,?)", [taskName, startDate, endDate, percentage])


#chartData = system.db.runQuery("SELECT taskName AS 'Task Name', startDate AS 'Start Date', endDate AS 'End Date', percentage AS 'Percentage' FROM temp_timelines")

event.source.parent.getComponent('Gantt Chart').data = ganttData

I have tried many different ways to parse the string - python and Java methods, but I admit to being a rookie so I could have easily overlooked some detail.

Having said that, I explored another method that did work, but via a circuitous route. I first INSERT the row data into to a MySQL table with DATETIME columns and then SELECT this data assigning it to the Gantt Chart table property. While this process works I do not like it as I am trying to reduce DB transactions when possible.

working

system.db.runPrepUpdate("DELETE FROM temp_timelines WHERE taskName LIKE ?", ['%'])

# reads task data from root´s Custom Property 'taskData' 
taskData = event.source.parent.taskData

# Builds ganttData dataset with no row data
headers = ['Task Name', 'Start Date', 'End Date', 'Percentage Done']
data =[]
ganttData = system.dataset.toDataSet(headers, data)

# sets percentage and startDate
percentage = 0
startDate = event.source.parent.getComponent('calStartDate').date
	
rows = taskData.getRowCount()
for row in range(rows):
	rowData =[]
	taskName = taskData.getValueAt(row,'task_name')
	weeks = taskData.getValueAt(row,'task_week')
	if row == 0:
		startDate = system.date.parse(startDate, "yyyy-MM-dd HH:mm:ss")
		endDate =  system.date.parse(system.date.addWeeks(startDate, weeks), "yyyy-MM-dd HH:mm:ss")

	else:
		prevEndDate = system.date.parse(ganttData.getValueAt(row-1,'End Date'), "yyyy-MM-dd HH:mm:ss")
		startDate = system.date.parse(system.date.addDays(prevEndDate, 1), "yyyy-MM-dd HH:mm:ss")
		endDate = system.date.parse(system.date.addWeeks(startDate, weeks), "yyyy-MM-dd HH:mm:ss")

	rowData.append(taskName)
	rowData.append(startDate)
	rowData.append(endDate)
	rowData.append(percentage)
	
	ganttData = system.dataset.addRow(ganttData, rowData)
		
	system.db.runPrepUpdate("INSERT INTO temp_timelines (taskName, startDate, endDate, percentage) VALUES (?,?,?,?)", [taskName, startDate, endDate, percentage])


chartData = system.db.runQuery("SELECT taskName AS 'Task Name', startDate AS 'Start Date', endDate AS 'End Date', percentage AS 'Percentage' FROM temp_timelines")

event.source.parent.getComponent('Gantt Chart').data = chartData

Why are you parsing the date in the first place?

It is my understanding the functions like system.date.addDays need to be a Date datatype to work. Also, the Gantt Chart component requires Date for Start Date and End Date.

Since, the pop calendar returns a string, I assumed that this data needs to be converted and Parse is used for that function.

Am I missing something.

The popup calendar returns a date.

print event.source.parent.getComponent('Popup Calendar').date
print type(event.source.parent.getComponent('Popup Calendar').date)

Mon Jun 01 15:52:33 PDT 2020
<type 'java.util.Date'>

I haven’t analyzed all of your code, but one flaw jumped out at me: you are constructing a dataset with no rows, then adding rows to it one by one. Don’t do this. Column datatypes are established when a dataset is created–with no data, I believe all columns are string. So when you add rows, the data in those rows has to be coerced to match the established column types, breaking your expectations.

Append your rowData to a plain python list, rows, perhaps. Then, after the loop, construct the gantt dataset in one call.

1 Like

Thank you Phil,

Your response got me headed in the right direction. It now works. As you said, the way I was creating the data caused all columns to be strings. So when I wrote the date to the Start Date or End Date column it was coerced into a string thereby causing the error.

Here is the code for anyone who might be interested.

# reads task data from root´s Custom Property 'taskData' 
taskData = event.source.parent.taskData

# sets percentage and startDate
percentage = 0
startDate = event.source.parent.getComponent('calStartDate').date

rowData =[]	
rows = taskData.getRowCount()

for row in range(rows):

	taskName = taskData.getValueAt(row,'task_name')
	weeks = taskData.getValueAt(row,'task_week')
	
	if row == 0:
		endDate =  system.date.addWeeks(startDate, weeks)
	
	else:
		prevEndDate = rowData[row-1][2]
		startDate = system.date.addDays(prevEndDate, 1)
		endDate = system.date.addWeeks(startDate,weeks)

	rowData.append([taskName, startDate, endDate, percentage])

# Builds ganttData dataset with no row data
headers = ['Task Name', 'Start Date', 'End Date', 'Percentage Done']

ganttData = system.dataset.toDataSet(headers, rowData)
event.source.parent.getComponent('Table').data = ganttData
event.source.parent.getComponent('Gantt Chart').data = ganttData