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 (type, options) {
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;
46 draw : function (data, options) {
47 throw new Error('draw must be implemented by a subclass');
49 redraw : function (options) {
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 var DataTable = function () {
160 this.addColumn = function (type, name) {
167 this.getColumns = function () {
171 this.setData = function (rows) {
176 this.getData = function () {
180 var fillMissingValues = function () {
181 if (columns.length === 0) {
182 throw new Error('Set columns first');
185 for (var i = 0; i < data.length; i++) {
187 if (row.length > columns.length) {
188 row.splice(columns.length - 1, row.length - columns.length);
189 } else if (row.length < columns.length) {
190 for (var j = row.length; j < columns.length; j++) {
198 /** *****************************************************************************
199 * JQPlot specific code
200 ******************************************************************************/
203 * Abstract JQplot chart
206 * id of the div element the chart is drawn in
208 var JQPlotChart = function (elementId) {
209 Chart.call(this, elementId);
211 this.validator = null;
213 JQPlotChart.prototype = new Chart();
214 JQPlotChart.prototype.constructor = JQPlotChart;
215 JQPlotChart.prototype.draw = function (data, options) {
216 if (this.validator.validateColumns(data)) {
217 this.plot = $.jqplot(this.elementId, this.prepareData(data), this
218 .populateOptions(data, options));
221 JQPlotChart.prototype.destroy = function () {
222 if (this.plot !== null) {
226 JQPlotChart.prototype.redraw = function (options) {
227 if (this.plot !== null) {
228 this.plot.replot(options);
231 JQPlotChart.prototype.toImageString = function (options) {
232 if (this.plot !== null) {
233 return $('#' + this.elementId).jqplotToImageStr({});
236 JQPlotChart.prototype.populateOptions = function (dataTable, options) {
237 throw new Error('populateOptions must be implemented by a subclass');
239 JQPlotChart.prototype.prepareData = function (dataTable) {
240 throw new Error('prepareData must be implemented by a subclass');
247 * id of the div element the chart is drawn in
249 var JQPlotLineChart = function (elementId) {
250 JQPlotChart.call(this, elementId);
251 this.validator = BaseChart.prototype;
253 JQPlotLineChart.prototype = new JQPlotChart();
254 JQPlotLineChart.prototype.constructor = JQPlotLineChart;
256 JQPlotLineChart.prototype.populateOptions = function (dataTable, options) {
257 var columns = dataTable.getColumns();
261 label : columns[0].name,
262 renderer : $.jqplot.CategoryAxisRenderer,
266 label : (columns.length === 2 ? columns[1].name : 'Values'),
267 labelRenderer : $.jqplot.CanvasAxisLabelRenderer
277 $.extend(true, optional, options);
279 if (optional.series.length === 0) {
280 for (var i = 1; i < columns.length; i++) {
281 optional.series.push({
282 label : columns[i].name.toString()
286 if (optional.axes.xaxis.ticks.length === 0) {
287 var data = dataTable.getData();
288 for (var j = 0; j < data.length; j++) {
289 optional.axes.xaxis.ticks.push(data[j][0].toString());
295 JQPlotLineChart.prototype.prepareData = function (dataTable) {
296 var data = dataTable.getData();
300 for (var i = 0; i < data.length; i++) {
302 for (var j = 1; j < row.length; j++) {
303 retRow = retData[j - 1];
304 if (retRow === undefined) {
306 retData[j - 1] = retRow;
315 * JQPlot spline chart
318 * id of the div element the chart is drawn in
320 var JQPlotSplineChart = function (elementId) {
321 JQPlotLineChart.call(this, elementId);
323 JQPlotSplineChart.prototype = new JQPlotLineChart();
324 JQPlotSplineChart.prototype.constructor = JQPlotSplineChart;
326 JQPlotSplineChart.prototype.populateOptions = function (dataTable, options) {
328 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
337 $.extend(true, optional, opt, compulsory);
342 * JQPlot scatter chart
345 * id of the div element the chart is drawn in
347 var JQPlotScatterChart = function (elementId) {
348 JQPlotChart.call(this, elementId);
349 this.validator = ScatterChart.prototype;
351 JQPlotScatterChart.prototype = new JQPlotChart();
352 JQPlotScatterChart.prototype.constructor = JQPlotScatterChart;
354 JQPlotScatterChart.prototype.populateOptions = function (dataTable, options) {
355 var columns = dataTable.getColumns();
359 label : columns[0].name
362 label : (columns.length === 2 ? columns[1].name : 'Values'),
363 labelRenderer : $.jqplot.CanvasAxisLabelRenderer
369 formatString:'%d, %d'
373 for (var i = 1; i < columns.length; i++) {
374 optional.series.push({
375 label : columns[i].name.toString()
389 $.extend(true, optional, options, compulsory);
393 JQPlotScatterChart.prototype.prepareData = function (dataTable) {
394 var data = dataTable.getData();
398 for (var i = 0; i < data.length; i++) {
401 for (var j = 1; j < row.length; j++) {
402 retRow = retData[j - 1];
403 if (retRow === undefined) {
405 retData[j - 1] = retRow;
407 retRow.push([row[0], row[j]]);
415 * JQPlot timeline chart
418 * id of the div element the chart is drawn in
420 var JQPlotTimelineChart = function (elementId) {
421 JQPlotLineChart.call(this, elementId);
422 this.validator = TimelineChart.prototype;
424 JQPlotTimelineChart.prototype = new JQPlotLineChart();
425 JQPlotTimelineChart.prototype.constructor = JQPlotTimelineChart;
427 JQPlotTimelineChart.prototype.populateOptions = function (dataTable, options) {
432 formatString: '%b %#d, %y'
437 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable, options);
441 renderer : $.jqplot.DateAxisRenderer
445 $.extend(true, optional, opt, compulsory);
449 JQPlotTimelineChart.prototype.prepareData = function (dataTable) {
450 var data = dataTable.getData();
455 for (var i = 0; i < data.length; i++) {
458 for (var j = 1; j < row.length; j++) {
459 retRow = retData[j - 1];
460 if (retRow === undefined) {
462 retData[j - 1] = retRow;
465 retRow.push([d.getTime(), row[j]]);
476 * id of the div element the chart is drawn in
478 var JQPlotAreaChart = function (elementId) {
479 JQPlotLineChart.call(this, elementId);
481 JQPlotAreaChart.prototype = new JQPlotLineChart();
482 JQPlotAreaChart.prototype.constructor = JQPlotAreaChart;
484 JQPlotAreaChart.prototype.populateOptions = function (dataTable, options) {
490 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
497 $.extend(true, optional, opt, compulsory);
502 * JQPlot column chart
505 * id of the div element the chart is drawn in
507 var JQPlotColumnChart = function (elementId) {
508 JQPlotLineChart.call(this, elementId);
510 JQPlotColumnChart.prototype = new JQPlotLineChart();
511 JQPlotColumnChart.prototype.constructor = JQPlotColumnChart;
513 JQPlotColumnChart.prototype.populateOptions = function (dataTable, options) {
519 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
523 renderer : $.jqplot.BarRenderer
526 $.extend(true, optional, opt, compulsory);
534 * id of the div element the chart is drawn in
536 var JQPlotBarChart = function (elementId) {
537 JQPlotLineChart.call(this, elementId);
539 JQPlotBarChart.prototype = new JQPlotLineChart();
540 JQPlotBarChart.prototype.constructor = JQPlotBarChart;
542 JQPlotBarChart.prototype.populateOptions = function (dataTable, options) {
543 var columns = dataTable.getColumns();
547 label : columns[0].name,
548 labelRenderer : $.jqplot.CanvasAxisLabelRenderer,
549 renderer : $.jqplot.CategoryAxisRenderer,
553 label : (columns.length === 2 ? columns[1].name : 'Values'),
554 labelRenderer : $.jqplot.CanvasAxisLabelRenderer
569 renderer : $.jqplot.BarRenderer,
571 barDirection : 'horizontal'
575 $.extend(true, optional, options, compulsory);
577 if (optional.axes.yaxis.ticks.length === 0) {
578 var data = dataTable.getData();
579 for (var i = 0; i < data.length; i++) {
580 optional.axes.yaxis.ticks.push(data[i][0].toString());
583 if (optional.series.length === 0) {
584 for (var j = 1; j < columns.length; j++) {
585 optional.series.push({
586 label : columns[j].name.toString()
597 * id of the div element the chart is drawn in
599 var JQPlotPieChart = function (elementId) {
600 JQPlotChart.call(this, elementId);
601 this.validator = PieChart.prototype;
603 JQPlotPieChart.prototype = new JQPlotChart();
604 JQPlotPieChart.prototype.constructor = JQPlotPieChart;
606 JQPlotPieChart.prototype.populateOptions = function (dataTable, options) {
611 formatString:'%s, %d',
612 useAxesFormatters: false
615 renderer: $.jqplot.EnhancedPieLegendRenderer,
621 renderer : $.jqplot.PieRenderer,
622 rendererOptions: { sliceMargin: 1, showDataLabels: true }
625 $.extend(true, optional, options, compulsory);
629 JQPlotPieChart.prototype.prepareData = function (dataTable) {
630 var data = dataTable.getData();
633 for (var i = 0; i < data.length; i++) {
635 retData.push([row[0], row[1]]);
641 * Chart factory that returns JQPlotCharts
643 var JQPlotChartFactory = function () {
645 JQPlotChartFactory.prototype = new ChartFactory();
646 JQPlotChartFactory.prototype.createChart = function (type, elementId) {
650 chart = new JQPlotLineChart(elementId);
652 case ChartType.SPLINE:
653 chart = new JQPlotSplineChart(elementId);
655 case ChartType.TIMELINE:
656 chart = new JQPlotTimelineChart(elementId);
659 chart = new JQPlotAreaChart(elementId);
662 chart = new JQPlotBarChart(elementId);
664 case ChartType.COLUMN:
665 chart = new JQPlotColumnChart(elementId);
668 chart = new JQPlotPieChart(elementId);
670 case ChartType.SCATTER:
671 chart = new JQPlotScatterChart(elementId);