1 var chart_xaxis_idx = -1;
3 var chart_series_index = -1;
5 $(document).ready(function() {
6 var currentChart = null;
7 var chart_data = jQuery.parseJSON($('#querychart').html());
8 chart_series = 'columns';
9 chart_xaxis_idx = $('select[name="chartXAxis"]').attr('value');
11 $('#resizer').resizable({
14 // On resize, set the chart size to that of the
15 // resizer minus padding. If your chart has a lot of data or other
16 // content, the redrawing might be slow. In that case, we recommend
17 // that you use the 'stop' event instead of 'resize'.
20 this.offsetWidth - 20,
21 this.offsetHeight - 20,
27 var currentSettings = {
30 width: $('#resizer').width() - 20,
31 height: $('#resizer').height() - 20
34 title: { text: $('input[name="xaxis_label"]').attr('value') }
37 title: { text: $('input[name="yaxis_label"]').attr('value') }
40 text: $('input[name="chartTitle"]').attr('value'),
48 $('#querychart').html('');
50 $('input[name="chartType"]').click(function() {
51 currentSettings.chart.type = $(this).attr('value');
55 if($(this).attr('value') == 'bar' || $(this).attr('value') == 'column')
56 $('span.barStacked').show();
58 $('span.barStacked').hide();
61 $('input[name="barStacked"]').click(function() {
63 $.extend(true,currentSettings,{ plotOptions: { series: { stacking:'normal' } } });
65 $.extend(true,currentSettings,{ plotOptions: { series: { stacking:null } } });
69 $('input[name="chartTitle"]').keyup(function() {
70 var title = $(this).attr('value');
71 if(title.length == 0) title = ' ';
72 currentChart.setTitle({ text: title });
75 $('select[name="chartXAxis"]').change(function() {
76 chart_xaxis_idx = this.value;
79 $('select[name="chartSeries"]').change(function() {
80 chart_series = this.value;
81 chart_series_index = this.selectedIndex;
85 /* Sucks, we cannot just set axis labels, we have to redraw the chart completely */
86 $('input[name="xaxis_label"]').keyup(function() {
87 currentSettings.xAxis.title.text = $(this).attr('value');
90 $('input[name="yaxis_label"]').keyup(function() {
91 currentSettings.yAxis.title.text = $(this).attr('value');
95 function drawChart(noAnimation) {
96 currentSettings.chart.width = $('#resizer').width() - 20;
97 currentSettings.chart.height = $('#resizer').height() - 20;
99 if(currentChart != null) currentChart.destroy();
101 if(noAnimation) currentSettings.plotOptions.series.animation = false;
102 currentChart = PMA_queryChart(chart_data,currentSettings);
103 if(noAnimation) currentSettings.plotOptions.series.animation = true;
107 $('#querychart').show();
110 function in_array(element,array) {
111 for(var i=0; i < array.length; i++)
112 if(array[i] == element) return true;
116 function PMA_queryChart(data,passedSettings) {
117 if($('#querychart').length == 0) return;
119 var columnNames = Array();
121 var series = new Array();
122 var xaxis = { type: 'linear' };
123 var yaxis = new Object();
125 $.each(data[0],function(index,element) {
126 columnNames.push(index);
129 switch(passedSettings.chart.type) {
134 xaxis.categories = new Array();
136 if(chart_series == 'columns') {
138 for(var i=0; i<columnNames.length; i++)
139 if(i != chart_xaxis_idx) {
140 series[j] = new Object();
141 series[j].data = new Array();
142 series[j].name = columnNames[i];
144 $.each(data,function(key,value) {
145 series[j].data.push(parseFloat(value[columnNames[i]]));
146 if( j== 0 && chart_xaxis_idx != -1 && ! xaxis.categories[value[columnNames[chart_xaxis_idx]]])
147 xaxis.categories.push(value[columnNames[chart_xaxis_idx]]);
153 var seriesIndex = new Object();
154 // Get series types and build series object from the query data
155 $.each(data,function(index,element) {
156 var contains = false;
157 for(var i=0; i < series.length; i++)
158 if(series[i].name == element[chart_series]) contains = true;
161 seriesIndex[element[chart_series]] = j;
162 series[j] = new Object();
163 series[j].data = new Array();
164 series[j].name = element[chart_series]; // columnNames[i];
170 // Get series points from query data
171 $.each(data,function(key,value) {
172 type = value[chart_series];
173 series[seriesIndex[type]].data.push(parseFloat(value[columnNames[0]]));
175 if( !in_array(value[columnNames[chart_xaxis_idx]],xaxis.categories))
176 xaxis.categories.push(value[columnNames[chart_xaxis_idx]]);
182 if(columnNames.length == 2)
183 yaxis.title = { text: columnNames[0] };
187 series[0] = new Object();
188 series[0].data = new Array();
189 $.each(data,function(key,value) {
190 series[0].data.push({
191 name: value[columnNames[chart_xaxis_idx]],
192 y: parseFloat(value[columnNames[0]])
198 // Prevent the user from seeing the JSON code
199 $('div#profilingchart').html('').show();
203 renderTo: 'querychart'
214 allowPointSelect: true,
219 formatter: function() {
220 return '<b>'+ this.point.name +'</b><br/>'+ Highcharts.numberFormat(this.percentage, 2) +' %';
226 formatter: function() {
227 if(this.point.name) return '<b>'+this.series.name+'</b><br/>'+this.point.name+'<br/>'+this.y;
228 return '<b>'+this.series.name+'</b><br/>'+this.y;
233 if(passedSettings.chart.type == 'pie')
234 settings.tooltip.formatter = function() { return '<b>'+columnNames[0]+'</b><br/>'+this.y; }
236 // Overwrite/Merge default settings with passedsettings
237 $.extend(true,settings,passedSettings);
239 return PMA_createChart(settings);