tbl_chart.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * handles creation of the chart
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. require_once 'libraries/common.inc.php';
  9. /*
  10. * Execute the query and return the result
  11. */
  12. if (isset($_REQUEST['ajax_request'])
  13. && isset($_REQUEST['pos'])
  14. && isset($_REQUEST['session_max_rows'])
  15. ) {
  16. $response = PMA_Response::getInstance();
  17. if (strlen($GLOBALS['table']) && strlen($GLOBALS['db'])) {
  18. include './libraries/tbl_common.inc.php';
  19. } else {
  20. $response->isSuccess(false);
  21. $response->addJSON('message', __('Error'));
  22. exit;
  23. }
  24. $sql_with_limit = 'SELECT * FROM( ' . $sql_query . ' ) AS `temp_res` LIMIT '
  25. . $_REQUEST['pos'] . ', ' . $_REQUEST['session_max_rows'];
  26. $data = array();
  27. $result = PMA_DBI_try_query($sql_with_limit);
  28. while ($row = PMA_DBI_fetch_assoc($result)) {
  29. $data[] = $row;
  30. }
  31. if (empty($data)) {
  32. $response->isSuccess(false);
  33. $response->addJSON('message', __('No data to display'));
  34. exit;
  35. }
  36. $sanitized_data = array();
  37. foreach ($data as $data_row_number => $data_row) {
  38. $tmp_row = array();
  39. foreach ($data_row as $data_column => $data_value) {
  40. $tmp_row[htmlspecialchars($data_column)] = htmlspecialchars($data_value);
  41. }
  42. $sanitized_data[] = $tmp_row;
  43. }
  44. $response->isSuccess(true);
  45. $response->addJSON('message', null);
  46. $response->addJSON('chartData', json_encode($sanitized_data));
  47. unset($sanitized_data);
  48. exit;
  49. }
  50. $response = PMA_Response::getInstance();
  51. // Throw error if no sql query is set
  52. if (! isset($sql_query) || $sql_query == '') {
  53. $response->isSuccess(false);
  54. $response->addHTML(
  55. PMA_Message::error(__('No SQL query was set to fetch data.'))
  56. );
  57. exit;
  58. }
  59. $header = $response->getHeader();
  60. $scripts = $header->getScripts();
  61. $scripts->addFile('chart.js');
  62. $scripts->addFile('tbl_chart.js');
  63. $scripts->addFile('jqplot/jquery.jqplot.js');
  64. $scripts->addFile('jqplot/plugins/jqplot.barRenderer.js');
  65. $scripts->addFile('jqplot/plugins/jqplot.canvasAxisLabelRenderer.js');
  66. $scripts->addFile('jqplot/plugins/jqplot.canvasTextRenderer.js');
  67. $scripts->addFile('jqplot/plugins/jqplot.categoryAxisRenderer.js');
  68. $scripts->addFile('jqplot/plugins/jqplot.dateAxisRenderer.js');
  69. $scripts->addFile('jqplot/plugins/jqplot.pointLabels.js');
  70. $scripts->addFile('jqplot/plugins/jqplot.pieRenderer.js');
  71. $scripts->addFile('jqplot/plugins/jqplot.highlighter.js');
  72. /* < IE 9 doesn't support canvas natively */
  73. if (PMA_USR_BROWSER_AGENT == 'IE' && PMA_USR_BROWSER_VER < 9) {
  74. $scripts->addFile('canvg/flashcanvas.js');
  75. }
  76. /**
  77. * Runs common work
  78. */
  79. if (strlen($GLOBALS['table'])) {
  80. $url_params['goto'] = $cfg['DefaultTabTable'];
  81. $url_params['back'] = 'tbl_sql.php';
  82. include 'libraries/tbl_common.inc.php';
  83. include 'libraries/tbl_info.inc.php';
  84. } elseif (strlen($GLOBALS['db'])) {
  85. $url_params['goto'] = $cfg['DefaultTabDatabase'];
  86. $url_params['back'] = 'sql.php';
  87. include 'libraries/db_common.inc.php';
  88. include 'libraries/db_info.inc.php';
  89. } else {
  90. $url_params['goto'] = $cfg['DefaultTabServer'];
  91. $url_params['back'] = 'sql.php';
  92. include 'libraries/server_common.inc.php';
  93. }
  94. $data = array();
  95. $result = PMA_DBI_try_query($sql_query);
  96. $fields_meta = PMA_DBI_get_fields_meta($result);
  97. while ($row = PMA_DBI_fetch_assoc($result)) {
  98. $data[] = $row;
  99. }
  100. $keys = array_keys($data[0]);
  101. $numeric_types = array('int', 'real');
  102. $numeric_column_count = 0;
  103. foreach ($keys as $idx => $key) {
  104. if (in_array($fields_meta[$idx]->type, $numeric_types)) {
  105. $numeric_column_count++;
  106. }
  107. }
  108. if ($numeric_column_count == 0) {
  109. $response->isSuccess(false);
  110. $response->addJSON(
  111. 'message',
  112. __('No numeric columns present in the table to plot.')
  113. );
  114. exit;
  115. }
  116. // get settings if any posted
  117. $chartSettings = array();
  118. if (PMA_isValid($_REQUEST['chartSettings'], 'array')) {
  119. $chartSettings = $_REQUEST['chartSettings'];
  120. }
  121. $url_params['db'] = $GLOBALS['db'];
  122. $url_params['reload'] = 1;
  123. /**
  124. * Displays the page
  125. */
  126. // pma_token/url_query needed for chart export
  127. $htmlString = '<script type="text/javascript">'
  128. . "pma_token = '" . $_SESSION[' PMA_token '] . "';"
  129. . "url_query = '" . $url_query . "';"
  130. . '</script>'
  131. . '<!-- Display Chart options -->'
  132. . '<div id="div_view_options">'
  133. . '<form method="post" id="tblchartform" action="tbl_chart.php" class="ajax">'
  134. . PMA_generate_common_hidden_inputs($url_params)
  135. . '<fieldset>'
  136. . '<legend>' . __('Display chart') . '</legend>'
  137. . '<div style="float:left; width:420px;">'
  138. . '<input type="radio" name="chartType" value="bar" id="radio_bar" />'
  139. . '<label for ="radio_bar">' . _pgettext('Chart type', 'Bar') . '</label>'
  140. . '<input type="radio" name="chartType" value="column" id="radio_column" />'
  141. . '<label for ="radio_column">' . _pgettext('Chart type', 'Column') . '</label>'
  142. . '<input type="radio" name="chartType" value="line" id="radio_line"'
  143. . ' checked="checked" />'
  144. . '<label for ="radio_line">' . _pgettext('Chart type', 'Line') . '</label>'
  145. . '<input type="radio" name="chartType" value="spline" id="radio_spline" />'
  146. . '<label for ="radio_spline">' . _pgettext('Chart type', 'Spline') . '</label>'
  147. . '<input type="radio" name="chartType" value="area" id="radio_area" />'
  148. . '<label for ="radio_area">' . _pgettext('Chart type', 'Area') . '</label>'
  149. . '<span class="span_pie" style="display:none;">'
  150. . '<input type="radio" name="chartType" value="pie" id="radio_pie" />'
  151. . '<label for ="radio_pie">' . _pgettext('Chart type', 'Pie') . '</label>'
  152. . '</span>'
  153. . '<span class="span_timeline" style="display:none;">'
  154. . '<input type="radio" name="chartType" value="timeline" id="radio_timeline" />'
  155. . '<label for ="radio_timeline">' . _pgettext('Chart type', 'Timeline')
  156. . '</label>'
  157. . '</span>'
  158. . '<br /><br />'
  159. . '<span class="barStacked">'
  160. . '<input type="checkbox" name="barStacked" value="1"'
  161. . ' id="checkbox_barStacked" />'
  162. . '<label for ="checkbox_barStacked">' . __('Stacked') . '</label>'
  163. . '</span>'
  164. . '<br /><br />'
  165. . '<input type="text" name="chartTitle" value="' . __('Chart title') . '">'
  166. . '</div>';
  167. $htmlString .= '<div style="float:left; padding-left:40px;">'
  168. . '<label for="select_chartXAxis">' . __('X-Axis:') . '</label>'
  169. . '<select name="chartXAxis" id="select_chartXAxis">';
  170. $yaxis = null;
  171. foreach ($keys as $idx => $key) {
  172. if ($yaxis === null) {
  173. $htmlString .= '<option value="' . htmlspecialchars($idx)
  174. . '" selected="selected">' . htmlspecialchars($key) . '</option>';
  175. $yaxis = $idx;
  176. } else {
  177. $htmlString .= '<option value="' . htmlspecialchars($idx) . '">'
  178. . htmlspecialchars($key) . '</option>';
  179. }
  180. }
  181. $htmlString .= '</select><br />'
  182. . '<label for="select_chartSeries">' . __('Series:') . '</label>'
  183. . '<select name="chartSeries" id="select_chartSeries" multiple="multiple">';
  184. foreach ($keys as $idx => $key) {
  185. if (in_array($fields_meta[$idx]->type, $numeric_types)) {
  186. if ($idx == $yaxis && $numeric_column_count > 1) {
  187. $htmlString .= '<option value="' . htmlspecialchars($idx) . '">'
  188. . htmlspecialchars($key) . '</option>';
  189. } else {
  190. $htmlString .= '<option value="' . htmlspecialchars($idx)
  191. . '" selected="selected">' . htmlspecialchars($key)
  192. . '</option>';
  193. }
  194. }
  195. }
  196. $htmlString .= '</select>'
  197. . '<input type="hidden" name="dateTimeCols" value="';
  198. $date_time_types = array('date', 'datetime', 'timestamp');
  199. foreach ($keys as $idx => $key) {
  200. if (in_array($fields_meta[$idx]->type, $date_time_types)) {
  201. $htmlString .= $idx . " ";
  202. }
  203. }
  204. $htmlString .= '" />'
  205. . '</div>';
  206. $htmlString .= '<div style="float:left; padding-left:40px;">'
  207. . '<label for="xaxis_label">' . __('X-Axis label:') . '</label>'
  208. . '<input style="margin-top:0;" type="text" name="xaxis_label" id="xaxis_label"'
  209. . ' value="'
  210. . (($yaxis == -1) ? __('X Values') : htmlspecialchars($keys[$yaxis]))
  211. . '" /><br />'
  212. . '<label for="yaxis_label">' . __('Y-Axis label:') . '</label>'
  213. . '<input type="text" name="yaxis_label" id="yaxis_label" value="'
  214. . __('Y Values') . '" /><br />'
  215. . '</div>'
  216. . '<p style="clear:both;">&nbsp;</p>'
  217. . '<fieldset>'
  218. . '<div>'
  219. . '<label for="pos">' . __('Start row') . ': ' . "\n" . '</label>'
  220. . '<input type="text" name="pos" size="3" value="'
  221. . $_SESSION['tmp_user_values']['pos'] . '" />'
  222. . '<label for="session_max_rows">'
  223. . __('Number of rows') . ': ' . "\n" . '</label>'
  224. . '<input type="text" name="session_max_rows" size="3" value="'
  225. . (($_SESSION['tmp_user_values']['max_rows'] != 'all')
  226. ? $_SESSION['tmp_user_values']['max_rows']
  227. : $GLOBALS['cfg']['MaxRows'])
  228. . '" />'
  229. . '<input type="submit" name="submit" class="Go" value="' . __('Go') . '" />'
  230. . '<input type="hidden" name="sql_query" value="'
  231. . htmlspecialchars($sql_query) . '" />'
  232. . '</div>'
  233. . '</fieldset>'
  234. . '<p style="clear:both;">&nbsp;</p>'
  235. . '<div id="resizer" style="width:600px; height:400px;">'
  236. . '<div id="querychart">'
  237. . '</div>'
  238. . '</div>'
  239. . '</fieldset>'
  240. . '</form>'
  241. . '</div>';
  242. $response->addHTML($htmlString);
  243. ?>