2 * Chart type enumerations
15 * Abstract chart factory which defines the contract for chart factories
17 var ChartFactory = function () {
19 ChartFactory.prototype = {
20 createChart : function (type, options) {
21 throw new Error("createChart must be implemented by a subclass");
26 * Abstract chart which defines the contract for charts
29 * id of the div element the chart is drawn in
31 var Chart = function (elementId) {
32 this.elementId = elementId;
35 draw : function (data, options) {
36 throw new Error("draw must be implemented by a subclass");
38 redraw : function (options) {
39 throw new Error("redraw must be implemented by a subclass");
41 destroy : function () {
42 throw new Error("destroy must be implemented by a subclass");
47 * Abstract representation of charts that operates on DataTable where,<br />
49 * <li>First column provides index to the data.</li>
50 * <li>Each subsequent columns are of type
51 * <code>ColumnType.NUMBER<code> and represents a data series.</li>
53 * Line chart, area chart, bar chart, column chart are typical examples.
56 * id of the div element the chart is drawn in
58 var BaseChart = function (elementId) {
59 Chart.call(this, elementId);
61 BaseChart.prototype = new Chart();
62 BaseChart.prototype.constructor = BaseChart;
63 BaseChart.prototype.validateColumns = function (dataTable) {
64 var columns = dataTable.getColumns();
65 if (columns.length < 2) {
66 throw new Error("Minimum of two columns are required for this chart");
68 for (var i = 1; i < columns.length; i++) {
69 if (columns[i].type != ColumnType.NUMBER) {
70 throw new Error("Column " + (i + 1) + " should be of type 'Number'");
80 * id of the div element the chart is drawn in
82 var PieChart = function (elementId) {
83 BaseChart.call(this, elementId);
85 PieChart.prototype = new BaseChart();
86 PieChart.prototype.constructor = PieChart;
87 PieChart.prototype.validateColumns = function (dataTable) {
88 var columns = dataTable.getColumns();
89 if (columns.length > 2) {
90 throw new Error("Pie charts can draw only one series");
92 return BaseChart.prototype.validateColumns.call(this, dataTable);
96 * Abstract timeline chart
99 * id of the div element the chart is drawn in
101 var TimelineChart = function (elementId) {
102 BaseChart.call(this, elementId);
104 TimelineChart.prototype = new BaseChart();
105 TimelineChart.prototype.constructor = TimelineChart;
106 TimelineChart.prototype.validateColumns = function (dataTable) {
107 var result = BaseChart.prototype.validateColumns.call(this, dataTable);
109 var columns = dataTable.getColumns();
110 if (columns[0].type != ColumnType.DATE) {
111 throw new Error("First column of timeline chart need to be a date column");
118 * The data table contains column information and data for the chart.
120 var DataTable = function () {
124 this.addColumn = function (type, name) {
131 this.getColumns = function () {
135 this.setData = function (rows) {
140 this.getData = function () {
144 var fillMissingValues = function () {
145 if (columns.length === 0) {
146 throw new Error("Set columns first");
149 for (var i = 0; i < data.length; i++) {
151 if (row.length > columns.length) {
152 row.splice(columns.length - 1, row.length - columns.length);
153 } else if (row.length < columns.length) {
154 for (var j = row.length; j < columns.length; j++) {
163 * Column type enumeration
172 /*******************************************************************************
173 * JQPlot specifc code
174 ******************************************************************************/
177 * Chart factory that returns JQPlotCharts
179 var JQPlotChartFactory = function () {
181 JQPlotChartFactory.prototype = new ChartFactory();
182 JQPlotChartFactory.prototype.createChart = function (type, elementId) {
186 chart = new JQPlotLineChart(elementId);
188 case ChartType.SPLINE:
189 chart = new JQPlotSplineChart(elementId);
191 case ChartType.TIMELINE:
192 chart = new JQPlotTimelineChart(elementId);
195 chart = new JQPlotAreaChart(elementId);
198 chart = new JQPlotBarChart(elementId);
200 case ChartType.COLUMN:
201 chart = new JQPlotColumnChart(elementId);
204 chart = new JQPlotPieChart(elementId);
212 * Abstract JQplot chart
215 * id of the div element the chart is drawn in
217 var JQPlotChart = function (elementId) {
218 Chart.call(this, elementId);
222 JQPlotChart.prototype = new Chart();
223 JQPlotChart.prototype.constructor = JQPlotChart;
224 JQPlotChart.prototype.draw = function (data, options) {
225 if (this.validator.validateColumns(data)) {
226 this.plot = $.jqplot(this.elementId, this.prepareData(data), this
227 .populateOptions(data, options));
230 JQPlotChart.prototype.destroy = function () {
231 if (this.plot !== null) {
235 JQPlotChart.prototype.redraw = function (options) {
236 if (this.plot !== null) {
237 this.plot.replot(options);
240 JQPlotChart.prototype.populateOptions = function (dataTable, options) {
241 throw new Error("populateOptions must be implemented by a subclass");
243 JQPlotChart.prototype.prepareData = function (dataTable) {
244 throw new Error("prepareData must be implemented by a subclass");
251 * id of the div element the chart is drawn in
253 var JQPlotLineChart = function (elementId) {
254 JQPlotChart.call(this, elementId);
255 this.validator = BaseChart.prototype;
257 JQPlotLineChart.prototype = new JQPlotChart();
258 JQPlotLineChart.prototype.constructor = JQPlotLineChart;
260 JQPlotLineChart.prototype.populateOptions = function (dataTable, options) {
261 var columns = dataTable.getColumns();
265 label : columns[0].name,
266 renderer : $.jqplot.CategoryAxisRenderer,
270 label : (columns.length == 2 ? columns[1].name : 'Values'),
271 labelRenderer : $.jqplot.CanvasAxisLabelRenderer
276 $.extend(true, optional, options);
278 if (optional.series.length === 0) {
279 for (var i = 1; i < columns.length; i++) {
280 optional.series.push({
281 label : columns[i].name.toString()
285 if (optional.axes.xaxis.ticks.length === 0) {
286 var data = dataTable.getData();
287 for (var i = 0; i < data.length; i++) {
288 optional.axes.xaxis.ticks.push(data[i][0].toString());
294 JQPlotLineChart.prototype.prepareData = function (dataTable) {
295 var data = dataTable.getData(), row;
296 var retData = [], retRow;
297 for (var i = 0; i < data.length; i++) {
299 for (var j = 1; j < row.length; j++) {
300 retRow = retData[j - 1];
301 if (retRow === undefined) {
303 retData[j - 1] = retRow;
312 * JQPlot spline chart
315 * id of the div element the chart is drawn in
317 var JQPlotSplineChart = function (elementId) {
318 JQPlotLineChart.call(this, elementId);
320 JQPlotSplineChart.prototype = new JQPlotLineChart();
321 JQPlotSplineChart.prototype.constructor = JQPlotSplineChart;
323 JQPlotSplineChart.prototype.populateOptions = function (dataTable, options) {
325 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
334 $.extend(true, optional, opt, compulsory);
339 * JQPlot timeline chart
342 * id of the div element the chart is drawn in
344 var JQPlotTimelineChart = function (elementId) {
345 JQPlotLineChart.call(this, elementId);
346 this.validator = TimelineChart.prototype;
348 JQPlotTimelineChart.prototype = new JQPlotLineChart();
349 JQPlotTimelineChart.prototype.constructor = JQPlotAreaChart;
351 JQPlotTimelineChart.prototype.populateOptions = function (dataTable, options) {
356 formatString: '%b %#d, %y'
361 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable, options);
365 renderer : $.jqplot.DateAxisRenderer
369 $.extend(true, optional, opt, compulsory);
373 JQPlotTimelineChart.prototype.prepareData = function (dataTable) {
374 var data = dataTable.getData(), row, d;
375 var retData = [], retRow;
376 for (var i = 0; i < data.length; i++) {
379 for (var j = 1; j < row.length; j++) {
380 retRow = retData[j - 1];
381 if (retRow === undefined) {
383 retData[j - 1] = retRow;
386 retRow.push([d.getTime(), row[j]]);
397 * id of the div element the chart is drawn in
399 var JQPlotAreaChart = function (elementId) {
400 JQPlotLineChart.call(this, elementId);
402 JQPlotAreaChart.prototype = new JQPlotLineChart();
403 JQPlotAreaChart.prototype.constructor = JQPlotAreaChart;
405 JQPlotAreaChart.prototype.populateOptions = function (dataTable, options) {
411 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
418 $.extend(true, optional, opt, compulsory);
423 * JQPlot column chart
426 * id of the div element the chart is drawn in
428 var JQPlotColumnChart = function (elementId) {
429 JQPlotLineChart.call(this, elementId);
431 JQPlotColumnChart.prototype = new JQPlotLineChart();
432 JQPlotColumnChart.prototype.constructor = JQPlotColumnChart;
434 JQPlotColumnChart.prototype.populateOptions = function (dataTable, options) {
440 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
444 renderer : $.jqplot.BarRenderer
447 $.extend(true, optional, opt, compulsory);
455 * id of the div element the chart is drawn in
457 var JQPlotBarChart = function (elementId) {
458 JQPlotLineChart.call(this, elementId);
460 JQPlotBarChart.prototype = new JQPlotLineChart();
461 JQPlotBarChart.prototype.constructor = JQPlotBarChart;
463 JQPlotBarChart.prototype.populateOptions = function (dataTable, options) {
464 var columns = dataTable.getColumns();
468 label : columns[0].name,
469 labelRenderer : $.jqplot.CanvasAxisLabelRenderer,
470 renderer : $.jqplot.CategoryAxisRenderer,
474 label : (columns.length == 2 ? columns[1].name : 'Values'),
475 labelRenderer : $.jqplot.CanvasAxisLabelRenderer
485 renderer : $.jqplot.BarRenderer,
487 barDirection : 'horizontal'
491 $.extend(true, optional, options, compulsory);
493 if (optional.axes.yaxis.ticks.length === 0) {
494 var data = dataTable.getData();
495 for (var i = 0; i < data.length; i++) {
496 optional.axes.yaxis.ticks.push(data[i][0].toString());
499 if (optional.series.length === 0) {
500 for (var i = 1; i < columns.length; i++) {
501 optional.series.push({
502 label : columns[i].name.toString()
513 * id of the div element the chart is drawn in
515 var JQPlotPieChart = function (elementId) {
516 JQPlotChart.call(this, elementId);
517 this.validator = PieChart.prototype;
519 JQPlotPieChart.prototype = new JQPlotChart();
520 JQPlotPieChart.prototype.constructor = JQPlotPieChart;
522 JQPlotPieChart.prototype.populateOptions = function (dataTable, options) {
526 renderer : $.jqplot.PieRenderer
529 $.extend(true, optional, options, compulsory);
533 JQPlotPieChart.prototype.prepareData = function (dataTable) {
534 var data = dataTable.getData(), row;
536 for (var i = 0; i < data.length; i++) {
538 retData.push([ row[0], row[1] ]);