2 * Chart type enumerations
16 * Column type enumeration
26 * Abstract chart factory which defines the contract for chart factories
28 var ChartFactory = function () {
30 ChartFactory.prototype = {
31 createChart : function () {
32 throw new Error('createChart must be implemented by a subclass');
37 * Abstract chart which defines the contract for charts
40 * id of the div element the chart is drawn in
42 var Chart = function (elementId) {
43 this.elementId = elementId;
47 throw new Error('draw must be implemented by a subclass');
49 redraw : function () {
50 throw new Error('redraw must be implemented by a subclass');
52 destroy : function () {
53 throw new Error('destroy must be implemented by a subclass');
55 toImageString : function () {
56 throw new Error('toImageString must be implemented by a subclass');
61 * Abstract representation of charts that operates on DataTable where,<br>
63 * <li>First column provides index to the data.</li>
64 * <li>Each subsequent columns are of type
65 * <code>ColumnType.NUMBER<code> and represents a data series.</li>
67 * Line chart, area chart, bar chart, column chart are typical examples.
70 * id of the div element the chart is drawn in
72 var BaseChart = function (elementId) {
73 Chart.call(this, elementId);
75 BaseChart.prototype = new Chart();
76 BaseChart.prototype.constructor = BaseChart;
77 BaseChart.prototype.validateColumns = function (dataTable) {
78 var columns = dataTable.getColumns();
79 if (columns.length < 2) {
80 throw new Error('Minimum of two columns are required for this chart');
82 for (var i = 1; i < columns.length; i++) {
83 if (columns[i].type !== ColumnType.NUMBER) {
84 throw new Error('Column ' + (i + 1) + ' should be of type \'Number\'');
94 * id of the div element the chart is drawn in
96 var PieChart = function (elementId) {
97 BaseChart.call(this, elementId);
99 PieChart.prototype = new BaseChart();
100 PieChart.prototype.constructor = PieChart;
101 PieChart.prototype.validateColumns = function (dataTable) {
102 var columns = dataTable.getColumns();
103 if (columns.length > 2) {
104 throw new Error('Pie charts can draw only one series');
106 return BaseChart.prototype.validateColumns.call(this, dataTable);
110 * Abstract timeline chart
113 * id of the div element the chart is drawn in
115 var TimelineChart = function (elementId) {
116 BaseChart.call(this, elementId);
118 TimelineChart.prototype = new BaseChart();
119 TimelineChart.prototype.constructor = TimelineChart;
120 TimelineChart.prototype.validateColumns = function (dataTable) {
121 var result = BaseChart.prototype.validateColumns.call(this, dataTable);
123 var columns = dataTable.getColumns();
124 if (columns[0].type !== ColumnType.DATE) {
125 throw new Error('First column of timeline chart need to be a date column');
132 * Abstract scatter chart
135 * id of the div element the chart is drawn in
137 var ScatterChart = function (elementId) {
138 BaseChart.call(this, elementId);
140 ScatterChart.prototype = new BaseChart();
141 ScatterChart.prototype.constructor = ScatterChart;
142 ScatterChart.prototype.validateColumns = function (dataTable) {
143 var result = BaseChart.prototype.validateColumns.call(this, dataTable);
145 var columns = dataTable.getColumns();
146 if (columns[0].type !== ColumnType.NUMBER) {
147 throw new Error('First column of scatter chart need to be a numeric column');
154 * The data table contains column information and data for the chart.
156 // eslint-disable-next-line no-unused-vars
157 var DataTable = function () {
161 this.addColumn = function (type, name) {
168 this.getColumns = function () {
172 this.setData = function (rows) {
177 this.getData = function () {
181 var fillMissingValues = function () {
182 if (columns.length === 0) {
183 throw new Error('Set columns first');
186 for (var i = 0; i < data.length; i++) {
188 if (row.length > columns.length) {
189 row.splice(columns.length - 1, row.length - columns.length);
190 } else if (row.length < columns.length) {
191 for (var j = row.length; j < columns.length; j++) {
199 /** *****************************************************************************
200 * JQPlot specific code
201 ******************************************************************************/
204 * Abstract JQplot chart
207 * id of the div element the chart is drawn in
209 var JQPlotChart = function (elementId) {
210 Chart.call(this, elementId);
212 this.validator = null;
214 JQPlotChart.prototype = new Chart();
215 JQPlotChart.prototype.constructor = JQPlotChart;
216 JQPlotChart.prototype.draw = function (data, options) {
217 if (this.validator.validateColumns(data)) {
218 this.plot = $.jqplot(this.elementId, this.prepareData(data), this
219 .populateOptions(data, options));
222 JQPlotChart.prototype.destroy = function () {
223 if (this.plot !== null) {
227 JQPlotChart.prototype.redraw = function (options) {
228 if (this.plot !== null) {
229 this.plot.replot(options);
232 JQPlotChart.prototype.toImageString = function () {
233 if (this.plot !== null) {
234 return $('#' + this.elementId).jqplotToImageStr({});
237 JQPlotChart.prototype.populateOptions = function () {
238 throw new Error('populateOptions must be implemented by a subclass');
240 JQPlotChart.prototype.prepareData = function () {
241 throw new Error('prepareData must be implemented by a subclass');
248 * id of the div element the chart is drawn in
250 var JQPlotLineChart = function (elementId) {
251 JQPlotChart.call(this, elementId);
252 this.validator = BaseChart.prototype;
254 JQPlotLineChart.prototype = new JQPlotChart();
255 JQPlotLineChart.prototype.constructor = JQPlotLineChart;
257 JQPlotLineChart.prototype.populateOptions = function (dataTable, options) {
258 var columns = dataTable.getColumns();
262 label : columns[0].name,
263 renderer : $.jqplot.CategoryAxisRenderer,
267 label : (columns.length === 2 ? columns[1].name : 'Values'),
268 labelRenderer : $.jqplot.CanvasAxisLabelRenderer
278 $.extend(true, optional, options);
280 if (optional.series.length === 0) {
281 for (var i = 1; i < columns.length; i++) {
282 optional.series.push({
283 label : columns[i].name.toString()
287 if (optional.axes.xaxis.ticks.length === 0) {
288 var data = dataTable.getData();
289 for (var j = 0; j < data.length; j++) {
290 optional.axes.xaxis.ticks.push(data[j][0] !== null ? data[j][0].toString() : null);
296 JQPlotLineChart.prototype.prepareData = function (dataTable) {
297 var data = dataTable.getData();
301 for (var i = 0; i < data.length; i++) {
303 for (var j = 1; j < row.length; j++) {
304 retRow = retData[j - 1];
305 if (retRow === undefined) {
307 retData[j - 1] = retRow;
316 * JQPlot spline chart
319 * id of the div element the chart is drawn in
321 var JQPlotSplineChart = function (elementId) {
322 JQPlotLineChart.call(this, elementId);
324 JQPlotSplineChart.prototype = new JQPlotLineChart();
325 JQPlotSplineChart.prototype.constructor = JQPlotSplineChart;
327 JQPlotSplineChart.prototype.populateOptions = function (dataTable, options) {
329 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
338 $.extend(true, optional, opt, compulsory);
343 * JQPlot scatter chart
346 * id of the div element the chart is drawn in
348 var JQPlotScatterChart = function (elementId) {
349 JQPlotChart.call(this, elementId);
350 this.validator = ScatterChart.prototype;
352 JQPlotScatterChart.prototype = new JQPlotChart();
353 JQPlotScatterChart.prototype.constructor = JQPlotScatterChart;
355 JQPlotScatterChart.prototype.populateOptions = function (dataTable, options) {
356 var columns = dataTable.getColumns();
360 label : columns[0].name
363 label : (columns.length === 2 ? columns[1].name : 'Values'),
364 labelRenderer : $.jqplot.CanvasAxisLabelRenderer
370 formatString:'%d, %d'
374 for (var i = 1; i < columns.length; i++) {
375 optional.series.push({
376 label : columns[i].name.toString()
390 $.extend(true, optional, options, compulsory);
394 JQPlotScatterChart.prototype.prepareData = function (dataTable) {
395 var data = dataTable.getData();
399 for (var i = 0; i < data.length; i++) {
402 for (var j = 1; j < row.length; j++) {
403 retRow = retData[j - 1];
404 if (retRow === undefined) {
406 retData[j - 1] = retRow;
408 retRow.push([row[0], row[j]]);
416 * JQPlot timeline chart
419 * id of the div element the chart is drawn in
421 var JQPlotTimelineChart = function (elementId) {
422 JQPlotLineChart.call(this, elementId);
423 this.validator = TimelineChart.prototype;
425 JQPlotTimelineChart.prototype = new JQPlotLineChart();
426 JQPlotTimelineChart.prototype.constructor = JQPlotTimelineChart;
428 JQPlotTimelineChart.prototype.populateOptions = function (dataTable, options) {
433 formatString: '%b %#d, %y'
438 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable, options);
442 renderer : $.jqplot.DateAxisRenderer
446 $.extend(true, optional, opt, compulsory);
450 JQPlotTimelineChart.prototype.prepareData = function (dataTable) {
451 var data = dataTable.getData();
456 for (var i = 0; i < data.length; i++) {
459 for (var j = 1; j < row.length; j++) {
460 retRow = retData[j - 1];
461 if (retRow === undefined) {
463 retData[j - 1] = retRow;
465 // See https://github.com/phpmyadmin/phpmyadmin/issues/14395 for the block
466 if (d !== null && typeof d === 'object') {
467 retRow.push([d.getTime(), row[j]]);
468 } else if (typeof d === 'string') {
470 retRow.push([d.getTime(), row[j]]);
481 * id of the div element the chart is drawn in
483 var JQPlotAreaChart = function (elementId) {
484 JQPlotLineChart.call(this, elementId);
486 JQPlotAreaChart.prototype = new JQPlotLineChart();
487 JQPlotAreaChart.prototype.constructor = JQPlotAreaChart;
489 JQPlotAreaChart.prototype.populateOptions = function (dataTable, options) {
495 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
502 $.extend(true, optional, opt, compulsory);
507 * JQPlot column chart
510 * id of the div element the chart is drawn in
512 var JQPlotColumnChart = function (elementId) {
513 JQPlotLineChart.call(this, elementId);
515 JQPlotColumnChart.prototype = new JQPlotLineChart();
516 JQPlotColumnChart.prototype.constructor = JQPlotColumnChart;
518 JQPlotColumnChart.prototype.populateOptions = function (dataTable, options) {
524 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
528 renderer : $.jqplot.BarRenderer
531 $.extend(true, optional, opt, compulsory);
539 * id of the div element the chart is drawn in
541 var JQPlotBarChart = function (elementId) {
542 JQPlotLineChart.call(this, elementId);
544 JQPlotBarChart.prototype = new JQPlotLineChart();
545 JQPlotBarChart.prototype.constructor = JQPlotBarChart;
547 JQPlotBarChart.prototype.populateOptions = function (dataTable, options) {
548 var columns = dataTable.getColumns();
552 label : columns[0].name,
553 labelRenderer : $.jqplot.CanvasAxisLabelRenderer,
554 renderer : $.jqplot.CategoryAxisRenderer,
558 label : (columns.length === 2 ? columns[1].name : 'Values'),
559 labelRenderer : $.jqplot.CanvasAxisLabelRenderer
574 renderer : $.jqplot.BarRenderer,
576 barDirection : 'horizontal'
580 $.extend(true, optional, options, compulsory);
582 if (optional.axes.yaxis.ticks.length === 0) {
583 var data = dataTable.getData();
584 for (var i = 0; i < data.length; i++) {
585 optional.axes.yaxis.ticks.push(data[i][0].toString());
588 if (optional.series.length === 0) {
589 for (var j = 1; j < columns.length; j++) {
590 optional.series.push({
591 label : columns[j].name.toString()
602 * id of the div element the chart is drawn in
604 var JQPlotPieChart = function (elementId) {
605 JQPlotChart.call(this, elementId);
606 this.validator = PieChart.prototype;
608 JQPlotPieChart.prototype = new JQPlotChart();
609 JQPlotPieChart.prototype.constructor = JQPlotPieChart;
611 JQPlotPieChart.prototype.populateOptions = function (dataTable, options) {
616 formatString:'%s, %d',
617 useAxesFormatters: false
620 renderer: $.jqplot.EnhancedPieLegendRenderer,
626 renderer : $.jqplot.PieRenderer,
627 rendererOptions: { sliceMargin: 1, showDataLabels: true }
630 $.extend(true, optional, options, compulsory);
634 JQPlotPieChart.prototype.prepareData = function (dataTable) {
635 var data = dataTable.getData();
638 for (var i = 0; i < data.length; i++) {
640 retData.push([row[0], row[1]]);
646 * Chart factory that returns JQPlotCharts
648 var JQPlotChartFactory = function () {
650 JQPlotChartFactory.prototype = new ChartFactory();
651 JQPlotChartFactory.prototype.createChart = function (type, elementId) {
655 chart = new JQPlotLineChart(elementId);
657 case ChartType.SPLINE:
658 chart = new JQPlotSplineChart(elementId);
660 case ChartType.TIMELINE:
661 chart = new JQPlotTimelineChart(elementId);
664 chart = new JQPlotAreaChart(elementId);
667 chart = new JQPlotBarChart(elementId);
669 case ChartType.COLUMN:
670 chart = new JQPlotColumnChart(elementId);
673 chart = new JQPlotPieChart(elementId);
675 case ChartType.SCATTER:
676 chart = new JQPlotScatterChart(elementId);