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