private function renderTimeLineChart($data, $title, $filename) { $chart = new Libchart\View\Chart\LineChart(750, 450); $dataSet = new Libchart\Model\XYDataSet(); foreach ($data as $key => $value) { $dataSet->addPoint(new Libchart\Model\Point($key, $value)); } $chart->setDataSet($dataSet); $chart->setTitle($title); $chart->render(CACHE_PATH . $filename); $this->view->assign('cache_path', BASE_URI . 'cache/'); }
$createdAt = new \DateTime($tweet['created_at']); if (!isset($hours[$createdAt->format('H')])) { $hours[$createdAt->format('H')] = 0; } $hours[$createdAt->format('H')]++; } ksort($hours); // create a histogram $outputFolder = 'generated'; $outputDirectory = __DIR__ . DIRECTORY_SEPARATOR . $outputFolder; $outputFile = 'sample.png'; if (!file_exists($outputDirectory)) { mkdir($outputDirectory, 0777, true); } $chart = new \Libchart\View\Chart\VerticalBarChart(); $dataSet = new \Libchart\Model\XYDataSet(); foreach ($hours as $hour => $count) { $time = date('ga', strtotime("{$hour}:00")); $dataSet->addPoint(new \Libchart\Model\Point($time, $count)); } $chart->setDataSet($dataSet); $chart->setTitle("Active hours user tweeted"); $chart->render($outputDirectory . DIRECTORY_SEPARATOR . $outputFile); // show histogram echo '<img alt="Vertical bars chart" src="' . $outputFolder . '/' . $outputFile . '" style="border: 1px solid gray;"/>'; /** * Obviously, this needs to be in MVC using some framework. */ } catch (\Exception $e) { throw $e; }