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(), row;
297 var retData = [], retRow;
298 for (var i = 0; i < data.length; i++) {
300 for (var j = 1; j < row.length; j++) {
301 retRow = retData[j - 1];
302 if (retRow === undefined) {
304 retData[j - 1] = retRow;
313 * JQPlot spline chart
316 * id of the div element the chart is drawn in
318 var JQPlotSplineChart = function (elementId) {
319 JQPlotLineChart.call(this, elementId);
321 JQPlotSplineChart.prototype = new JQPlotLineChart();
322 JQPlotSplineChart.prototype.constructor = JQPlotSplineChart;
324 JQPlotSplineChart.prototype.populateOptions = function (dataTable, options) {
326 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
335 $.extend(true, optional, opt, compulsory);
340 * JQPlot scatter chart
343 * id of the div element the chart is drawn in
345 var JQPlotScatterChart = function (elementId) {
346 JQPlotChart.call(this, elementId);
347 this.validator = ScatterChart.prototype;
349 JQPlotScatterChart.prototype = new JQPlotChart();
350 JQPlotScatterChart.prototype.constructor = JQPlotScatterChart;
352 JQPlotScatterChart.prototype.populateOptions = function (dataTable, options) {
353 var columns = dataTable.getColumns();
357 label : columns[0].name
360 label : (columns.length == 2 ? columns[1].name : 'Values'),
361 labelRenderer : $.jqplot.CanvasAxisLabelRenderer
367 formatString:'%d, %d'
371 for (var i = 1; i < columns.length; i++) {
372 optional.series.push({
373 label : columns[i].name.toString()
387 $.extend(true, optional, options, compulsory);
391 JQPlotScatterChart.prototype.prepareData = function (dataTable) {
392 var data = dataTable.getData(), row;
393 var retData = [], retRow;
394 for (var i = 0; i < data.length; i++) {
397 for (var j = 1; j < row.length; j++) {
398 retRow = retData[j - 1];
399 if (retRow === undefined) {
401 retData[j - 1] = retRow;
403 retRow.push([row[0], row[j]]);
411 * JQPlot timeline chart
414 * id of the div element the chart is drawn in
416 var JQPlotTimelineChart = function (elementId) {
417 JQPlotLineChart.call(this, elementId);
418 this.validator = TimelineChart.prototype;
420 JQPlotTimelineChart.prototype = new JQPlotLineChart();
421 JQPlotTimelineChart.prototype.constructor = JQPlotTimelineChart;
423 JQPlotTimelineChart.prototype.populateOptions = function (dataTable, options) {
428 formatString: '%b %#d, %y'
433 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable, options);
437 renderer : $.jqplot.DateAxisRenderer
441 $.extend(true, optional, opt, compulsory);
445 JQPlotTimelineChart.prototype.prepareData = function (dataTable) {
446 var data = dataTable.getData(), row, d;
447 var retData = [], retRow;
448 for (var i = 0; i < data.length; i++) {
451 for (var j = 1; j < row.length; j++) {
452 retRow = retData[j - 1];
453 if (retRow === undefined) {
455 retData[j - 1] = retRow;
458 retRow.push([d.getTime(), row[j]]);
469 * id of the div element the chart is drawn in
471 var JQPlotAreaChart = function (elementId) {
472 JQPlotLineChart.call(this, elementId);
474 JQPlotAreaChart.prototype = new JQPlotLineChart();
475 JQPlotAreaChart.prototype.constructor = JQPlotAreaChart;
477 JQPlotAreaChart.prototype.populateOptions = function (dataTable, options) {
483 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
490 $.extend(true, optional, opt, compulsory);
495 * JQPlot column chart
498 * id of the div element the chart is drawn in
500 var JQPlotColumnChart = function (elementId) {
501 JQPlotLineChart.call(this, elementId);
503 JQPlotColumnChart.prototype = new JQPlotLineChart();
504 JQPlotColumnChart.prototype.constructor = JQPlotColumnChart;
506 JQPlotColumnChart.prototype.populateOptions = function (dataTable, options) {
512 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
516 renderer : $.jqplot.BarRenderer
519 $.extend(true, optional, opt, compulsory);
527 * id of the div element the chart is drawn in
529 var JQPlotBarChart = function (elementId) {
530 JQPlotLineChart.call(this, elementId);
532 JQPlotBarChart.prototype = new JQPlotLineChart();
533 JQPlotBarChart.prototype.constructor = JQPlotBarChart;
535 JQPlotBarChart.prototype.populateOptions = function (dataTable, options) {
536 var columns = dataTable.getColumns();
540 label : columns[0].name,
541 labelRenderer : $.jqplot.CanvasAxisLabelRenderer,
542 renderer : $.jqplot.CategoryAxisRenderer,
546 label : (columns.length == 2 ? columns[1].name : 'Values'),
547 labelRenderer : $.jqplot.CanvasAxisLabelRenderer
562 renderer : $.jqplot.BarRenderer,
564 barDirection : 'horizontal'
568 $.extend(true, optional, options, compulsory);
570 if (optional.axes.yaxis.ticks.length === 0) {
571 var data = dataTable.getData();
572 for (var i = 0; i < data.length; i++) {
573 optional.axes.yaxis.ticks.push(data[i][0].toString());
576 if (optional.series.length === 0) {
577 for (var j = 1; j < columns.length; j++) {
578 optional.series.push({
579 label : columns[j].name.toString()
590 * id of the div element the chart is drawn in
592 var JQPlotPieChart = function (elementId) {
593 JQPlotChart.call(this, elementId);
594 this.validator = PieChart.prototype;
596 JQPlotPieChart.prototype = new JQPlotChart();
597 JQPlotPieChart.prototype.constructor = JQPlotPieChart;
599 JQPlotPieChart.prototype.populateOptions = function (dataTable, options) {
604 formatString:'%s, %d',
605 useAxesFormatters: false
610 renderer : $.jqplot.PieRenderer
613 $.extend(true, optional, options, compulsory);
617 JQPlotPieChart.prototype.prepareData = function (dataTable) {
618 var data = dataTable.getData(), row;
620 for (var i = 0; i < data.length; i++) {
622 retData.push([ row[0], row[1] ]);
628 * Chart factory that returns JQPlotCharts
630 var JQPlotChartFactory = function () {
632 JQPlotChartFactory.prototype = new ChartFactory();
633 JQPlotChartFactory.prototype.createChart = function (type, elementId) {
637 chart = new JQPlotLineChart(elementId);
639 case ChartType.SPLINE:
640 chart = new JQPlotSplineChart(elementId);
642 case ChartType.TIMELINE:
643 chart = new JQPlotTimelineChart(elementId);
646 chart = new JQPlotAreaChart(elementId);
649 chart = new JQPlotBarChart(elementId);
651 case ChartType.COLUMN:
652 chart = new JQPlotColumnChart(elementId);
655 chart = new JQPlotPieChart(elementId);
657 case ChartType.SCATTER:
658 chart = new JQPlotScatterChart(elementId);