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].toString());
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;
466 retRow.push([d.getTime(), row[j]]);
477 * id of the div element the chart is drawn in
479 var JQPlotAreaChart = function (elementId) {
480 JQPlotLineChart.call(this, elementId);
482 JQPlotAreaChart.prototype = new JQPlotLineChart();
483 JQPlotAreaChart.prototype.constructor = JQPlotAreaChart;
485 JQPlotAreaChart.prototype.populateOptions = function (dataTable, options) {
491 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
498 $.extend(true, optional, opt, compulsory);
503 * JQPlot column chart
506 * id of the div element the chart is drawn in
508 var JQPlotColumnChart = function (elementId) {
509 JQPlotLineChart.call(this, elementId);
511 JQPlotColumnChart.prototype = new JQPlotLineChart();
512 JQPlotColumnChart.prototype.constructor = JQPlotColumnChart;
514 JQPlotColumnChart.prototype.populateOptions = function (dataTable, options) {
520 var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
524 renderer : $.jqplot.BarRenderer
527 $.extend(true, optional, opt, compulsory);
535 * id of the div element the chart is drawn in
537 var JQPlotBarChart = function (elementId) {
538 JQPlotLineChart.call(this, elementId);
540 JQPlotBarChart.prototype = new JQPlotLineChart();
541 JQPlotBarChart.prototype.constructor = JQPlotBarChart;
543 JQPlotBarChart.prototype.populateOptions = function (dataTable, options) {
544 var columns = dataTable.getColumns();
548 label : columns[0].name,
549 labelRenderer : $.jqplot.CanvasAxisLabelRenderer,
550 renderer : $.jqplot.CategoryAxisRenderer,
554 label : (columns.length === 2 ? columns[1].name : 'Values'),
555 labelRenderer : $.jqplot.CanvasAxisLabelRenderer
570 renderer : $.jqplot.BarRenderer,
572 barDirection : 'horizontal'
576 $.extend(true, optional, options, compulsory);
578 if (optional.axes.yaxis.ticks.length === 0) {
579 var data = dataTable.getData();
580 for (var i = 0; i < data.length; i++) {
581 optional.axes.yaxis.ticks.push(data[i][0].toString());
584 if (optional.series.length === 0) {
585 for (var j = 1; j < columns.length; j++) {
586 optional.series.push({
587 label : columns[j].name.toString()
598 * id of the div element the chart is drawn in
600 var JQPlotPieChart = function (elementId) {
601 JQPlotChart.call(this, elementId);
602 this.validator = PieChart.prototype;
604 JQPlotPieChart.prototype = new JQPlotChart();
605 JQPlotPieChart.prototype.constructor = JQPlotPieChart;
607 JQPlotPieChart.prototype.populateOptions = function (dataTable, options) {
612 formatString:'%s, %d',
613 useAxesFormatters: false
616 renderer: $.jqplot.EnhancedPieLegendRenderer,
622 renderer : $.jqplot.PieRenderer,
623 rendererOptions: { sliceMargin: 1, showDataLabels: true }
626 $.extend(true, optional, options, compulsory);
630 JQPlotPieChart.prototype.prepareData = function (dataTable) {
631 var data = dataTable.getData();
634 for (var i = 0; i < data.length; i++) {
636 retData.push([row[0], row[1]]);
642 * Chart factory that returns JQPlotCharts
644 var JQPlotChartFactory = function () {
646 JQPlotChartFactory.prototype = new ChartFactory();
647 JQPlotChartFactory.prototype.createChart = function (type, elementId) {
651 chart = new JQPlotLineChart(elementId);
653 case ChartType.SPLINE:
654 chart = new JQPlotSplineChart(elementId);
656 case ChartType.TIMELINE:
657 chart = new JQPlotTimelineChart(elementId);
660 chart = new JQPlotAreaChart(elementId);
663 chart = new JQPlotBarChart(elementId);
665 case ChartType.COLUMN:
666 chart = new JQPlotColumnChart(elementId);
669 chart = new JQPlotPieChart(elementId);
671 case ChartType.SCATTER:
672 chart = new JQPlotScatterChart(elementId);