Skip to content

Commit

Permalink
[TASK] Allow rendering multiple charts when no counter is specified
Browse files Browse the repository at this point in the history
Action requires token to be present; individual charts to not. As a measure to prevent abuse.
  • Loading branch information
NamelessCoder committed Sep 6, 2015
1 parent b0f332e commit 31b5f04
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -2,7 +2,7 @@
"name": "namelesscoder/numerolog-lavacharts",
"description": "Chart renderer for Numerolog data sources",
"require": {
"namelesscoder/numerolog": "^1.0",
"namelesscoder/numerolog": "^1.2",
"khill/lavacharts": "^2.5"
},
"require-dev": {
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions src/ChartQuery.php
Expand Up @@ -55,6 +55,10 @@ class ChartQuery extends Query {
*/
protected $count = 20;

/**
* @var boolean
*/
protected $scriptOnly = FALSE;

/**
* @var array
Expand Down Expand Up @@ -181,6 +185,21 @@ public function setChartDateTimeFormat($chartDateTimeFormat) {
$this->chartDateTimeFormat = $chartDateTimeFormat;
}

/**
* @return boolean
*/
public function getScriptOnly() {
return $this->scriptOnly;
}

/**
* @param boolean $scriptOnly
* @return void
*/
public function setScriptOnly($scriptOnly) {
$this->scriptOnly = $scriptOnly;
}

/**
* @return array
*/
Expand Down
5 changes: 4 additions & 1 deletion src/NumerologChart.php
Expand Up @@ -16,7 +16,7 @@ class NumerologChart extends Lavacharts {
* @param ChartQuery $query
* @return string
*/
public function renderGraphQuery(ChartQuery $query) {
public function renderChartQuery(ChartQuery $query) {
if (Query::ACTION_GET !== $query->getAction()) {
throw new \RuntimeException('NumerologChart only accepts the Numerolog `get` command');
}
Expand All @@ -34,6 +34,9 @@ public function renderGraphQuery(ChartQuery $query) {
$chart->setOptions($chartAttributes);
}
$chart->datatable($table);
if ($query->getScriptOnly()) {
return $this->render($chartType, $chartLabel, $query->getChartId());
}
return $this->render($chartType, $chartLabel, $query->getChartId(), array(
'width' => $query->getChartWidth(),
'height' => $query->getChartHeight()
Expand Down
34 changes: 33 additions & 1 deletion web/index.php
Expand Up @@ -2,5 +2,37 @@
require_once __DIR__ . '/../vendor/autoload.php';

$query = new \NamelessCoder\NumerologLavacharts\ChartQuery($_GET);
$query->setAction(\NamelessCoder\Numerolog\Query::ACTION_GET);
$chart = new \NamelessCoder\NumerologLavacharts\NumerologChart();
echo $chart->renderGraphQuery($query);


if ($query->getCounter()) {
echo $chart->renderChartQuery($query);
}

if (!$query->getCounter()) {
$countersQuery = clone $query;
$countersQuery->setAction(\NamelessCoder\Numerolog\Query::ACTION_COUNTERS);
$client = new \NamelessCoder\Numerolog\Client();
$client->setPackage($query->getPackage());
$client->setToken($query->getToken());
$counters = $client->query($countersQuery);
foreach (array_keys($counters['results']) as $index => $counterName) {
$counterNumber = $i + 1;
$query->setCounter($counterName);
$query->setChartId('chart-' . $counterNumber);
$query->setChartLabel('chart-' . $counterNumber);
if (!$query->getScriptOnly()) {
echo $chart->renderChartQuery($query);
$query->setScriptOnly(TRUE);
} else {
echo sprintf(
'<div id="chart-%s" style="width:%dpx; height:%dpx;"></div>',
$counterNumber,
$query->getChartWidth(),
$query->getChartHeight()
);
}
}
}

0 comments on commit 31b5f04

Please sign in to comment.