antonio.goncalves
Posts: 1
Joined: Wed Aug 03, 2022 11:15 am

Generate gantt chart image

Hi,

I have a requirement to display a gantt chart on pdf report.

I know that somehow, there's a way of using php curl in order to generate a chart and save it to a specific folder.

Something like this (generates a column chart and save it as image on the public folder, on laravel):

public function genGantChart()
{
$type = 'png'; // Can be png, jpeg or pdf
// Chart options
$options = [
'title' => [
'text' => 'Column Chart 1'
],
'series' => [[
'data' => [1, 4, 3, 5],
'type' => 'column'
]]
];
$arr = [
'type' => $type,
'width' => 400,
'infile' => $options
];
$data = json_encode($arr);
$curlProcess = curl_init();
curl_setopt( $curlProcess, CURLOPT_URL, 'https://export.highcharts.com/' );
curl_setopt(
$curlProcess,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Content-Length: ' . strlen( $data ), 'Accept: application/json'
)
);
curl_setopt( $curlProcess, CURLOPT_HEADER, 0 );
curl_setopt( $curlProcess, CURLOPT_TIMEOUT, 30 );
curl_setopt( $curlProcess, CURLOPT_POST, 1 );
curl_setopt( $curlProcess, CURLOPT_POSTFIELDS, $data );
curl_setopt( $curlProcess, CURLOPT_RETURNTRANSFER, TRUE );
$content = curl_exec( $curlProcess );
curl_close( $curlProcess );
file_put_contents("chart.$type", $content);
}

But when i try to generating a gantt chart using the same approach it does not work (the image is generate, but does not look as a gantt chart), bellow is the code:

public function genGantChart()
{
$type = 'png'; // Can be png, jpeg or pdf
// Chart options
$options = [
"title" => [
"text" => 'Gantt Chart 0000'
],

"xAxis" => [
"min" => Helpersproj::convertToUTC([10, 17, 2014]),
"max" => Helpersproj::convertToUTC([10, 30, 2014])
],

"series" => [
"name"=> 'Project 1',
"data"=> [[
"name"=> 'Start prototype',
"start"=> Helpersproj::convertToUTC([10, 18, 2014]),
"end"=> Helpersproj::convertToUTC([10, 25, 2014]),
"completed"=> "0.25"
], [
"name"=> 'Test prototype',
"start"=> Helpersproj::convertToUTC([10, 27, 2014]),
"end"=> Helpersproj::convertToUTC([10, 29, 2014])
], [
"name"=> 'Develop',
"start"=> Helpersproj::convertToUTC([10, 20, 2014]),
"end"=> Helpersproj::convertToUTC([10, 25, 2014]),
"completed"=> [
"amount"=> "0.12",
"fill"=> '#fa0'
]
], [
"name"=> 'Run acceptance tests',
"start"=> Helpersproj::convertToUTC([10, 23, 2014]),
"end"=> Helpersproj::convertToUTC([10, 26, 2014])
]]
]
];
$arr = [
'type' => $type,
'width' => 400,
'infile' => $options
];
$data = json_encode($arr);
$curlProcess = curl_init();
// dd($data);
curl_setopt( $curlProcess, CURLOPT_URL, 'https://export.highcharts.com/' );
curl_setopt(
$curlProcess,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Content-Length: ' . strlen( $data ), 'Accept: application/json'
)
);
curl_setopt( $curlProcess, CURLOPT_HEADER, 0 );
curl_setopt( $curlProcess, CURLOPT_TIMEOUT, 30 );
curl_setopt( $curlProcess, CURLOPT_POST, 1 );
curl_setopt( $curlProcess, CURLOPT_POSTFIELDS, $data );
curl_setopt( $curlProcess, CURLOPT_RETURNTRANSFER, TRUE );
$content = curl_exec( $curlProcess );
curl_close( $curlProcess );
file_put_contents("chart.$type", $content);
}

The convertToUTC method:
public static function convertToUTC(array $date) : int
{
return mktime(0, 0, 0, $date[0], $date[1], $date[2]);
}
kamil.m
Posts: 892
Joined: Thu May 19, 2022 1:33 pm

Re: Generate gantt chart image

Hi there,

Welcome to our forum and thank you for contacting us with this question.

Unfortunately, we don't provide official support for PHP, but since you just need to export your Gantt chart to PDF, have you thought about using our official Export Server?

It seems that it will be a perfect fit for your use case. Take a look at the links below:
https://www.highcharts.com/docs/export- ... e-overview
https://www.highcharts.com/blog/news/24 ... exporting/

If you will need any further assistance, do not hesitate to contact us!
Best regards
Kamil Musiałowski
Highcharts Developer

Return to “Highcharts Gantt”