added check for json encoder, cleaned up error handling a bit

This commit is contained in:
Martynas Mickevicius
2010-08-06 13:22:04 +03:00
parent 47f67613a2
commit 59f220eb13
5 changed files with 52 additions and 40 deletions

View File

@@ -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;

View File

@@ -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';
@@ -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;
@@ -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') {
@@ -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;
@@ -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.');
}
}
?>

View File

@@ -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 */
@@ -3544,7 +3544,7 @@
}
else
{
fwrite($Handle, $this->getImageMap());
fwrite($Handle, serialize($this->getImageMap()));
}
fclose ($Handle);
}

View File

@@ -171,37 +171,42 @@ abstract class PMA_pChart_Chart extends PMA_Chart
public function toString()
{
if (function_exists('gd_info')) {
$this->init();
$this->prepareDataSet();
$this->prepareChart();
//$this->chart->debugImageMap();
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>';
$returnData .= '
<script type="text/javascript">
imageMap.loadImageMap(\''.$this->getImageMap().'\');
</script>
';
return $returnData;
}
else {
if (!function_exists('gd_info')) {
array_push($this->errors, ERR_NO_GD);
return '';
}
$this->init();
$this->prepareDataSet();
$this->prepareChart();
//$this->chart->debugImageMap();
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(\''.json_encode($this->getImageMap()).'\');
</script>
';
}
else {
array_push($this->errors, ERR_NO_JSON);
}
return $returnData;
}
protected function getLabelHeight()

View File

@@ -50,11 +50,10 @@ if (PMA_isValid($_REQUEST['chartSettings'], 'array')) {
$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 {