Skip to content

Commit

Permalink
added check for json encoder, cleaned up error handling a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
2m committed Aug 6, 2010
1 parent 47f6761 commit 59f220e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 33 deletions.
6 changes: 6 additions & 0 deletions js/pMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@

var imageMap = {
'mouseMoved': function(event, cont) {
// return if no imageMap set
// this can happen if server has no json
if (!this.imageMap) {
return;
}

// get mouse coordinated relative to image
var mouseX = event.pageX - cont.offsetLeft;
var mouseY = event.pageY - cont.offsetTop;
Expand Down
14 changes: 8 additions & 6 deletions libraries/chart.lib.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

define('ERR_NO_GD', 0);
define('ERR_UNKNOWN_FORMAT', 1);
define('ERR_NO_JSON', 1);

require_once './libraries/chart/pma_pchart_pie.php';
require_once './libraries/chart/pma_pchart_single_bar.php';
Expand Down Expand Up @@ -66,7 +66,7 @@ function PMA_chart_profiling($data)
/*
* Formats a chart for query results page.
*/
function PMA_chart_results($data, &$chartSettings, &$chartErrors = array())
function PMA_chart_results($data, &$chartSettings)
{
$chartData = array();
$chart = null;
Expand Down Expand Up @@ -103,7 +103,7 @@ function PMA_chart_results($data, &$chartSettings, &$chartErrors = array())
}

if (count($data[0]) == 1 || count($data[0]) == 2) {
// Two columns in every row.
// One or two columns in every row.
// This data is suitable for a simple bar chart.

if ($chartSettings['type'] == 'pie') {
Expand Down Expand Up @@ -220,14 +220,13 @@ function PMA_chart_results($data, &$chartSettings, &$chartErrors = array())
}
}
else {
// unknown data
array_push($chartErrors, ERR_UNKNOWN_FORMAT);
// unknown data format
return '';
}

$chartCode = $chart->toString();
$chartSettings = $chart->getSettings();
$chartErrors = array_merge($chartErrors, $chart->getErrors());
$chartErrors = $chart->getErrors();
PMA_handle_chart_err($chartErrors);

return $chartCode;
Expand All @@ -241,6 +240,9 @@ function PMA_handle_chart_err($errors)
if (in_array(ERR_NO_GD, $errors)) {
PMA_warnMissingExtension('GD', false, 'GD extension is needed for charts.');
}
else if (in_array(ERR_NO_JSON, $errors)) {
PMA_warnMissingExtension('JSON', false, 'JSON encoder is needed for chart tooltips.');
}
}

?>
4 changes: 2 additions & 2 deletions libraries/chart/pChart/pChart.class
Original file line number Diff line number Diff line change
Expand Up @@ -3495,7 +3495,7 @@
/* Get the current image map */
function getImageMap()
{
return json_encode($this->ImageMap);
return $this->ImageMap;
}

/* Load and cleanup the image map from disk */
Expand Down Expand Up @@ -3544,7 +3544,7 @@
}
else
{
fwrite($Handle, $this->getImageMap());
fwrite($Handle, serialize($this->getImageMap()));
}
fclose ($Handle);
}
Expand Down
47 changes: 26 additions & 21 deletions libraries/chart/pma_pchart_chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,37 +171,42 @@ protected function render($parts = 1)

public function toString()
{
if (function_exists('gd_info')) {
$this->init();
$this->prepareDataSet();
$this->prepareChart();
if (!function_exists('gd_info')) {
array_push($this->errors, ERR_NO_GD);
return '';
}

//$this->chart->debugImageMap();
$this->init();
$this->prepareDataSet();
$this->prepareChart();

if ($this->isContinuous()) {
$this->render(1);
}
else {
$this->render(20);
}
//$this->chart->debugImageMap();

$returnData = '<div id="chart">';
foreach ($this->partsEncoded as $part) {
$returnData .= '<img src="data:image/png;base64,'.$part.'" />';
}
$returnData .= '</div>';
if ($this->isContinuous()) {
$this->render(1);
}
else {
$this->render(20);
}

$returnData = '<div id="chart">';
foreach ($this->partsEncoded as $part) {
$returnData .= '<img src="data:image/png;base64,'.$part.'" />';
}
$returnData .= '</div>';

if (function_exists('json_encode')) {
$returnData .= '
<script type="text/javascript">
imageMap.loadImageMap(\''.$this->getImageMap().'\');
imageMap.loadImageMap(\''.json_encode($this->getImageMap()).'\');
</script>
';

return $returnData;
}
else {
array_push($this->errors, ERR_NO_GD);
return '';
array_push($this->errors, ERR_NO_JSON);
}

return $returnData;
}

protected function getLabelHeight()
Expand Down
7 changes: 3 additions & 4 deletions tbl_chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@
$chartSettings = $_REQUEST['chartSettings'];
}

// get the chart and settings and errors after chart generation
$chartErrors = array();
$chart = PMA_chart_results($data, $chartSettings, $chartErrors);
// get the chart and settings after chart generation
$chart = PMA_chart_results($data, $chartSettings);

if (empty($chartErrors)) {
if (!empty($chart)) {
$message = PMA_Message::success(__('Chart generated successfully.'));
}
else {
Expand Down

0 comments on commit 59f220e

Please sign in to comment.