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