1 /* vim: set expandtab sw=4 ts=4 sts=4: */
6 var currentChart = null;
7 var currentSettings = null;
10 * Unbind all event handlers before tearing down a page
12 AJAX.registerTeardown('tbl_chart.js', function () {
13 $('input[name="chartType"]').unbind('click');
14 $('input[name="barStacked"]').unbind('click');
15 $('input[name="chartTitle"]').unbind('focus').unbind('keyup').unbind('blur');
16 $('select[name="chartXAxis"]').unbind('change');
17 $('select[name="chartSeries"]').unbind('change');
18 $('input[name="xaxis_label"]').unbind('keyup');
19 $('input[name="yaxis_label"]').unbind('keyup');
20 $('#resizer').unbind('resizestop');
23 AJAX.registerOnload('tbl_chart.js', function () {
26 $('#resizer').resizable({
30 .width($('#div_view_options').width() - 50);
32 $('#resizer').bind('resizestop', function (event, ui) {
33 // make room so that the handle will still appear
34 $('#querychart').height($('#resizer').height() * 0.96);
35 $('#querychart').width($('#resizer').width() * 0.96);
43 width : $('#resizer').width() - 20,
44 height : $('#resizer').height() - 20,
45 xaxisLabel : $('input[name="xaxis_label"]').val(),
46 yaxisLabel : $('input[name="yaxis_label"]').val(),
47 title : $('input[name="chartTitle"]').val(),
49 mainAxis : parseInt($('select[name="chartXAxis"]').val(), 10),
50 selectedSeries : getSelectedSeries()
53 // handle chart type changes
54 $('input[name="chartType"]').click(function () {
55 currentSettings.type = $(this).val();
57 if ($(this).val() == 'bar' || $(this).val() == 'column'
58 || $(this).val() == 'line' || $(this).val() == 'area'
59 || $(this).val() == 'timeline' || $(this).val() == 'spline') {
60 $('span.barStacked').show();
62 $('span.barStacked').hide();
66 // handle stacking for bar, column and area charts
67 $('input[name="barStacked"]').click(function () {
69 $.extend(true, currentSettings, {stackSeries : true});
71 $.extend(true, currentSettings, {stackSeries : false});
76 // handle changes in chart title
77 $('input[name="chartTitle"]').focus(function () {
78 temp_chart_title = $(this).val();
79 }).keyup(function () {
80 var title = $(this).val();
81 if (title.length === 0) {
84 currentSettings.title = $('input[name="chartTitle"]').val();
87 if ($(this).val() != temp_chart_title) {
92 var dateTimeCols = [];
93 var vals = $('input[name="dateTimeCols"]').val().split(' ');
94 $.each(vals, function (i, v) {
95 dateTimeCols.push(parseInt(v, 10));
98 // handle changing the x-axis
99 $('select[name="chartXAxis"]').change(function () {
100 currentSettings.mainAxis = parseInt($(this).val(), 10);
101 if (dateTimeCols.indexOf(currentSettings.mainAxis) != -1) {
102 $('span.span_timeline').show();
104 $('span.span_timeline').hide();
105 if (currentSettings.type == 'timeline') {
106 $('input#radio_line').prop('checked', true);
107 currentSettings.type = 'line';
110 var xaxis_title = $(this).children('option:selected').text();
111 $('input[name="xaxis_label"]').val(xaxis_title);
112 currentSettings.xaxisLabel = xaxis_title;
116 // handle changing the selected data series
117 $('select[name="chartSeries"]').change(function () {
118 currentSettings.selectedSeries = getSelectedSeries();
120 if (currentSettings.selectedSeries.length == 1) {
121 $('span.span_pie').show();
122 yaxis_title = $(this).children('option:selected').text();
124 $('span.span_pie').hide();
125 if (currentSettings.type == 'pie') {
126 $('input#radio_line').prop('checked', true);
127 currentSettings.type = 'line';
129 yaxis_title = PMA_messages.strYValues;
131 $('input[name="yaxis_label"]').val(yaxis_title);
132 currentSettings.yaxisLabel = yaxis_title;
136 // handle manual changes to the chart axis labels
137 $('input[name="xaxis_label"]').keyup(function () {
138 currentSettings.xaxisLabel = $(this).val();
141 $('input[name="yaxis_label"]').keyup(function () {
142 currentSettings.yaxisLabel = $(this).val();
146 $("#tblchartform").submit();
150 * Ajax Event handler for 'Go' button click
153 $("#tblchartform").live('submit', function (event) {
154 if (!checkFormElementInRange(this, 'session_max_rows', PMA_messages.strNotValidRowNumber, 1)
155 || !checkFormElementInRange(this, 'pos', PMA_messages.strNotValidRowNumber, 0 - 1)) {
160 if (!checkSqlQuery($form[0])) {
163 // remove any div containing a previous error message
164 $('.error').remove();
165 var $msgbox = PMA_ajaxShowMessage();
166 PMA_prepareForAjaxRequest($form);
168 $.post($form.attr('action'), $form.serialize(), function (data) {
169 if (data.success === true) {
170 $('.success').fadeOut();
171 if (typeof data.chartData != 'undefined') {
172 chart_data = jQuery.parseJSON(data.chartData);
174 $('div#querychart').height($('div#resizer').height() * 0.96);
175 $('div#querychart').width($('div#resizer').width() * 0.96);
176 currentChart.redraw({
179 $('#querychart').show();
182 PMA_ajaxRemoveMessage($msgbox);
183 PMA_ajaxShowMessage(data.error, false);
187 PMA_ajaxRemoveMessage($msgbox);
188 }, "json"); // end $.post()
193 function drawChart() {
194 currentSettings.width = $('#resizer').width() - 20;
195 currentSettings.height = $('#resizer').height() - 20;
197 // todo: a better way using .redraw() ?
198 if (currentChart !== null) {
199 currentChart.destroy();
202 var columnNames = [];
203 $('select[name="chartXAxis"] option').each(function () {
204 columnNames.push($(this).text());
207 currentChart = PMA_queryChart(chart_data, columnNames, currentSettings);
209 PMA_ajaxShowMessage(err.message, false);
213 function getSelectedSeries() {
214 var val = $('select[name="chartSeries"]').val() || [];
216 $.each(val, function (i, v) {
217 ret.push(parseInt(v, 10));
222 function extractDate(dateString) {
224 var dateTimeRegExp = /[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/;
225 var dateRegExp = /[0-9]{4}-[0-9]{2}-[0-9]{2}/;
227 matches = dateTimeRegExp.exec(dateString);
228 if (matches !== null && matches.length > 0) {
230 return new Date(match.substr(0, 4), match.substr(5, 2), match.substr(8, 2), match.substr(11, 2), match.substr(14, 2), match.substr(17, 2));
232 matches = dateRegExp.exec(dateString);
233 if (matches !== null && matches.length > 0) {
235 return new Date(match.substr(0, 4), match.substr(5, 2), match.substr(8, 2));
241 function PMA_queryChart(data, columnNames, settings) {
242 if ($('#querychart').length === 0) {
246 var jqPlotSettings = {
248 text : settings.title,
254 background : 'rgba(0,0,0,0)'
258 placement : 'outsideGrid',
263 label : settings.xaxisLabel
266 label : settings.yaxisLabel
269 stackSeries : settings.stackSeries,
278 var factory = new JQPlotChartFactory();
279 var chart = factory.createChart(settings.type, "querychart");
281 // create the data table and add columns
282 var dataTable = new DataTable();
283 if (settings.type == 'timeline') {
284 dataTable.addColumn(ColumnType.DATE, columnNames[settings.mainAxis]);
286 dataTable.addColumn(ColumnType.STRING, columnNames[settings.mainAxis]);
288 $.each(settings.selectedSeries, function (index, element) {
289 dataTable.addColumn(ColumnType.NUMBER, columnNames[element]);
292 // set data to the data table
293 var columnsToExtract = [ settings.mainAxis ];
294 $.each(settings.selectedSeries, function (index, element) {
295 columnsToExtract.push(element);
297 var values = [], newRow, row, col;
298 for (var i = 0; i < data.length; i++) {
301 for (var j = 0; j < columnsToExtract.length; j++) {
302 col = columnNames[columnsToExtract[j]];
304 if (settings.type == 'timeline') { // first column is date type
305 newRow.push(extractDate(row[col]));
306 } else { // first column is string type
307 newRow.push(row[col]);
309 } else { // subsequent columns are of type, number
310 newRow.push(parseFloat(row[col]));
315 dataTable.setData(values);
317 // draw the chart and return the chart object
318 chart.draw(dataTable, jqPlotSettings);