tbl_chart.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. var chart_data = {};
  3. var temp_chart_title;
  4. var currentChart = null;
  5. var currentSettings = null;
  6. var dateTimeCols = [];
  7. var numericCols = [];
  8. function extractDate (dateString) {
  9. var matches;
  10. var match;
  11. var dateTimeRegExp = /[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/;
  12. var dateRegExp = /[0-9]{4}-[0-9]{2}-[0-9]{2}/;
  13. matches = dateTimeRegExp.exec(dateString);
  14. if (matches !== null && matches.length > 0) {
  15. match = matches[0];
  16. return new Date(match.substr(0, 4), parseInt(match.substr(5, 2), 10) - 1, match.substr(8, 2), match.substr(11, 2), match.substr(14, 2), match.substr(17, 2));
  17. } else {
  18. matches = dateRegExp.exec(dateString);
  19. if (matches !== null && matches.length > 0) {
  20. match = matches[0];
  21. return new Date(match.substr(0, 4), parseInt(match.substr(5, 2), 10) - 1, match.substr(8, 2));
  22. }
  23. }
  24. return null;
  25. }
  26. function PMA_queryChart (data, columnNames, settings) {
  27. if ($('#querychart').length === 0) {
  28. return;
  29. }
  30. var plotSettings = {
  31. title : {
  32. text : settings.title,
  33. escapeHtml: true
  34. },
  35. grid : {
  36. drawBorder : false,
  37. shadow : false,
  38. background : 'rgba(0,0,0,0)'
  39. },
  40. legend : {
  41. show : true,
  42. placement : 'outsideGrid',
  43. location : 'e',
  44. rendererOptions: {
  45. numberColumns: 2
  46. }
  47. },
  48. axes : {
  49. xaxis : {
  50. label : escapeHtml(settings.xaxisLabel)
  51. },
  52. yaxis : {
  53. label : settings.yaxisLabel
  54. }
  55. },
  56. stackSeries : settings.stackSeries
  57. };
  58. // create the chart
  59. var factory = new JQPlotChartFactory();
  60. var chart = factory.createChart(settings.type, 'querychart');
  61. // create the data table and add columns
  62. var dataTable = new DataTable();
  63. if (settings.type === 'timeline') {
  64. dataTable.addColumn(ColumnType.DATE, columnNames[settings.mainAxis]);
  65. } else if (settings.type === 'scatter') {
  66. dataTable.addColumn(ColumnType.NUMBER, columnNames[settings.mainAxis]);
  67. } else {
  68. dataTable.addColumn(ColumnType.STRING, columnNames[settings.mainAxis]);
  69. }
  70. var i;
  71. if (settings.seriesColumn === null) {
  72. $.each(settings.selectedSeries, function (index, element) {
  73. dataTable.addColumn(ColumnType.NUMBER, columnNames[element]);
  74. });
  75. // set data to the data table
  76. var columnsToExtract = [settings.mainAxis];
  77. $.each(settings.selectedSeries, function (index, element) {
  78. columnsToExtract.push(element);
  79. });
  80. var values = [];
  81. var newRow;
  82. var row;
  83. var col;
  84. for (i = 0; i < data.length; i++) {
  85. row = data[i];
  86. newRow = [];
  87. for (var j = 0; j < columnsToExtract.length; j++) {
  88. col = columnNames[columnsToExtract[j]];
  89. if (j === 0) {
  90. if (settings.type === 'timeline') { // first column is date type
  91. newRow.push(extractDate(row[col]));
  92. } else if (settings.type === 'scatter') {
  93. newRow.push(parseFloat(row[col]));
  94. } else { // first column is string type
  95. newRow.push(row[col]);
  96. }
  97. } else { // subsequent columns are of type, number
  98. newRow.push(parseFloat(row[col]));
  99. }
  100. }
  101. values.push(newRow);
  102. }
  103. dataTable.setData(values);
  104. } else {
  105. var seriesNames = {};
  106. var seriesNumber = 1;
  107. var seriesColumnName = columnNames[settings.seriesColumn];
  108. for (i = 0; i < data.length; i++) {
  109. if (! seriesNames[data[i][seriesColumnName]]) {
  110. seriesNames[data[i][seriesColumnName]] = seriesNumber;
  111. seriesNumber++;
  112. }
  113. }
  114. $.each(seriesNames, function (seriesName, seriesNumber) {
  115. dataTable.addColumn(ColumnType.NUMBER, seriesName);
  116. });
  117. var valueMap = {};
  118. var xValue;
  119. var value;
  120. var mainAxisName = columnNames[settings.mainAxis];
  121. var valueColumnName = columnNames[settings.valueColumn];
  122. for (i = 0; i < data.length; i++) {
  123. xValue = data[i][mainAxisName];
  124. value = valueMap[xValue];
  125. if (! value) {
  126. value = [xValue];
  127. valueMap[xValue] = value;
  128. }
  129. seriesNumber = seriesNames[data[i][seriesColumnName]];
  130. value[seriesNumber] = parseFloat(data[i][valueColumnName]);
  131. }
  132. var values = [];
  133. $.each(valueMap, function (index, value) {
  134. values.push(value);
  135. });
  136. dataTable.setData(values);
  137. }
  138. // draw the chart and return the chart object
  139. chart.draw(dataTable, plotSettings);
  140. return chart;
  141. }
  142. function drawChart () {
  143. currentSettings.width = $('#resizer').width() - 20;
  144. currentSettings.height = $('#resizer').height() - 20;
  145. // TODO: a better way using .redraw() ?
  146. if (currentChart !== null) {
  147. currentChart.destroy();
  148. }
  149. var columnNames = [];
  150. $('select[name="chartXAxis"] option').each(function () {
  151. columnNames.push(escapeHtml($(this).text()));
  152. });
  153. try {
  154. currentChart = PMA_queryChart(chart_data, columnNames, currentSettings);
  155. if (currentChart !== null) {
  156. $('#saveChart').attr('href', currentChart.toImageString());
  157. }
  158. } catch (err) {
  159. PMA_ajaxShowMessage(err.message, false);
  160. }
  161. }
  162. function getSelectedSeries () {
  163. var val = $('select[name="chartSeries"]').val() || [];
  164. var ret = [];
  165. $.each(val, function (i, v) {
  166. ret.push(parseInt(v, 10));
  167. });
  168. return ret;
  169. }
  170. function onXAxisChange () {
  171. var $xAxisSelect = $('select[name="chartXAxis"]');
  172. currentSettings.mainAxis = parseInt($xAxisSelect.val(), 10);
  173. if (dateTimeCols.indexOf(currentSettings.mainAxis) !== -1) {
  174. $('span.span_timeline').show();
  175. } else {
  176. $('span.span_timeline').hide();
  177. if (currentSettings.type === 'timeline') {
  178. $('input#radio_line').prop('checked', true);
  179. currentSettings.type = 'line';
  180. }
  181. }
  182. if (numericCols.indexOf(currentSettings.mainAxis) !== -1) {
  183. $('span.span_scatter').show();
  184. } else {
  185. $('span.span_scatter').hide();
  186. if (currentSettings.type === 'scatter') {
  187. $('input#radio_line').prop('checked', true);
  188. currentSettings.type = 'line';
  189. }
  190. }
  191. var xaxis_title = $xAxisSelect.children('option:selected').text();
  192. $('input[name="xaxis_label"]').val(xaxis_title);
  193. currentSettings.xaxisLabel = xaxis_title;
  194. }
  195. function onDataSeriesChange () {
  196. var $seriesSelect = $('select[name="chartSeries"]');
  197. currentSettings.selectedSeries = getSelectedSeries();
  198. var yaxis_title;
  199. if (currentSettings.selectedSeries.length === 1) {
  200. $('span.span_pie').show();
  201. yaxis_title = $seriesSelect.children('option:selected').text();
  202. } else {
  203. $('span.span_pie').hide();
  204. if (currentSettings.type === 'pie') {
  205. $('input#radio_line').prop('checked', true);
  206. currentSettings.type = 'line';
  207. }
  208. yaxis_title = PMA_messages.strYValues;
  209. }
  210. $('input[name="yaxis_label"]').val(yaxis_title);
  211. currentSettings.yaxisLabel = yaxis_title;
  212. }
  213. /**
  214. * Unbind all event handlers before tearing down a page
  215. */
  216. AJAX.registerTeardown('tbl_chart.js', function () {
  217. $('input[name="chartType"]').off('click');
  218. $('input[name="barStacked"]').off('click');
  219. $('input[name="chkAlternative"]').off('click');
  220. $('input[name="chartTitle"]').off('focus').off('keyup').off('blur');
  221. $('select[name="chartXAxis"]').off('change');
  222. $('select[name="chartSeries"]').off('change');
  223. $('select[name="chartSeriesColumn"]').off('change');
  224. $('select[name="chartValueColumn"]').off('change');
  225. $('input[name="xaxis_label"]').off('keyup');
  226. $('input[name="yaxis_label"]').off('keyup');
  227. $('#resizer').off('resizestop');
  228. $('#tblchartform').off('submit');
  229. });
  230. AJAX.registerOnload('tbl_chart.js', function () {
  231. // handle manual resize
  232. $('#resizer').on('resizestop', function (event, ui) {
  233. // make room so that the handle will still appear
  234. $('#querychart').height($('#resizer').height() * 0.96);
  235. $('#querychart').width($('#resizer').width() * 0.96);
  236. if (currentChart !== null) {
  237. currentChart.redraw({
  238. resetAxes : true
  239. });
  240. }
  241. });
  242. // handle chart type changes
  243. $('input[name="chartType"]').click(function () {
  244. var type = currentSettings.type = $(this).val();
  245. if (type === 'bar' || type === 'column' || type === 'area') {
  246. $('span.barStacked').show();
  247. } else {
  248. $('input[name="barStacked"]').prop('checked', false);
  249. $.extend(true, currentSettings, { stackSeries : false });
  250. $('span.barStacked').hide();
  251. }
  252. drawChart();
  253. });
  254. // handle chosing alternative data format
  255. $('input[name="chkAlternative"]').click(function () {
  256. var $seriesColumn = $('select[name="chartSeriesColumn"]');
  257. var $valueColumn = $('select[name="chartValueColumn"]');
  258. var $chartSeries = $('select[name="chartSeries"]');
  259. if ($(this).is(':checked')) {
  260. $seriesColumn.prop('disabled', false);
  261. $valueColumn.prop('disabled', false);
  262. $chartSeries.prop('disabled', true);
  263. currentSettings.seriesColumn = parseInt($seriesColumn.val(), 10);
  264. currentSettings.valueColumn = parseInt($valueColumn.val(), 10);
  265. } else {
  266. $seriesColumn.prop('disabled', true);
  267. $valueColumn.prop('disabled', true);
  268. $chartSeries.prop('disabled', false);
  269. currentSettings.seriesColumn = null;
  270. currentSettings.valueColumn = null;
  271. }
  272. drawChart();
  273. });
  274. // handle stacking for bar, column and area charts
  275. $('input[name="barStacked"]').click(function () {
  276. if ($(this).is(':checked')) {
  277. $.extend(true, currentSettings, { stackSeries : true });
  278. } else {
  279. $.extend(true, currentSettings, { stackSeries : false });
  280. }
  281. drawChart();
  282. });
  283. // handle changes in chart title
  284. $('input[name="chartTitle"]')
  285. .focus(function () {
  286. temp_chart_title = $(this).val();
  287. })
  288. .keyup(function () {
  289. currentSettings.title = $('input[name="chartTitle"]').val();
  290. drawChart();
  291. })
  292. .blur(function () {
  293. if ($(this).val() !== temp_chart_title) {
  294. drawChart();
  295. }
  296. });
  297. // handle changing the x-axis
  298. $('select[name="chartXAxis"]').change(function () {
  299. onXAxisChange();
  300. drawChart();
  301. });
  302. // handle changing the selected data series
  303. $('select[name="chartSeries"]').change(function () {
  304. onDataSeriesChange();
  305. drawChart();
  306. });
  307. // handle changing the series column
  308. $('select[name="chartSeriesColumn"]').change(function () {
  309. currentSettings.seriesColumn = parseInt($(this).val(), 10);
  310. drawChart();
  311. });
  312. // handle changing the value column
  313. $('select[name="chartValueColumn"]').change(function () {
  314. currentSettings.valueColumn = parseInt($(this).val(), 10);
  315. drawChart();
  316. });
  317. // handle manual changes to the chart x-axis labels
  318. $('input[name="xaxis_label"]').keyup(function () {
  319. currentSettings.xaxisLabel = $(this).val();
  320. drawChart();
  321. });
  322. // handle manual changes to the chart y-axis labels
  323. $('input[name="yaxis_label"]').keyup(function () {
  324. currentSettings.yaxisLabel = $(this).val();
  325. drawChart();
  326. });
  327. // handler for ajax form submission
  328. $('#tblchartform').submit(function (event) {
  329. var $form = $(this);
  330. if (codemirror_editor) {
  331. $form[0].elements.sql_query.value = codemirror_editor.getValue();
  332. }
  333. if (!checkSqlQuery($form[0])) {
  334. return false;
  335. }
  336. var $msgbox = PMA_ajaxShowMessage();
  337. PMA_prepareForAjaxRequest($form);
  338. $.post($form.attr('action'), $form.serialize(), function (data) {
  339. if (typeof data !== 'undefined' &&
  340. data.success === true &&
  341. typeof data.chartData !== 'undefined') {
  342. chart_data = JSON.parse(data.chartData);
  343. drawChart();
  344. PMA_ajaxRemoveMessage($msgbox);
  345. } else {
  346. PMA_ajaxShowMessage(data.error, false);
  347. }
  348. }, 'json'); // end $.post()
  349. return false;
  350. });
  351. // from jQuery UI
  352. $('#resizer').resizable({
  353. minHeight: 240,
  354. minWidth: 300
  355. })
  356. .width($('#div_view_options').width() - 50)
  357. .trigger('resizestop');
  358. currentSettings = {
  359. type : 'line',
  360. width : $('#resizer').width() - 20,
  361. height : $('#resizer').height() - 20,
  362. xaxisLabel : $('input[name="xaxis_label"]').val(),
  363. yaxisLabel : $('input[name="yaxis_label"]').val(),
  364. title : $('input[name="chartTitle"]').val(),
  365. stackSeries : false,
  366. mainAxis : parseInt($('select[name="chartXAxis"]').val(), 10),
  367. selectedSeries : getSelectedSeries(),
  368. seriesColumn : null
  369. };
  370. var vals = $('input[name="dateTimeCols"]').val().split(' ');
  371. $.each(vals, function (i, v) {
  372. dateTimeCols.push(parseInt(v, 10));
  373. });
  374. vals = $('input[name="numericCols"]').val().split(' ');
  375. $.each(vals, function (i, v) {
  376. numericCols.push(parseInt(v, 10));
  377. });
  378. onXAxisChange();
  379. onDataSeriesChange();
  380. $('#tblchartform').submit();
  381. });