Welcome to mirror list, hosted at ThFree Co, Russian Federation.

chart.js « js - github.com/phpmyadmin/phpmyadmin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 93153add6200b4cacbb058fe8bf2144502ab6cdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/**
 * Chart type enumerations
 */
var ChartType = {
    LINE : 'line',
    SPLINE : 'spline',
    AREA : 'area',
    BAR : 'bar',
    COLUMN : 'column',
    PIE : 'pie',
    TIMELINE: 'timeline',
    SCATTER: 'scatter'
};

/**
 * Column type enumeration
 */
var ColumnType = {
    STRING : 'string',
    NUMBER : 'number',
    BOOLEAN : 'boolean',
    DATE : 'date'
};

/**
 * Abstract chart factory which defines the contract for chart factories
 */
var ChartFactory = function () {
};
ChartFactory.prototype = {
    createChart : function (type, options) {
        throw new Error("createChart must be implemented by a subclass");
    }
};

/**
 * Abstract chart which defines the contract for charts
 *
 * @param elementId
 *            id of the div element the chart is drawn in
 */
var Chart = function (elementId) {
    this.elementId = elementId;
};
Chart.prototype = {
    draw : function (data, options) {
        throw new Error("draw must be implemented by a subclass");
    },
    redraw : function (options) {
        throw new Error("redraw must be implemented by a subclass");
    },
    destroy : function () {
        throw new Error("destroy must be implemented by a subclass");
    },
    toImageString : function() {
        throw new Error("toImageString must be implemented by a subclass");
    }
};

/**
 * Abstract representation of charts that operates on DataTable where,<br />
 * <ul>
 * <li>First column provides index to the data.</li>
 * <li>Each subsequent columns are of type
 * <code>ColumnType.NUMBER<code> and represents a data series.</li>
 * </ul>
 * Line chart, area chart, bar chart, column chart are typical examples.
 *
 * @param elementId
 *            id of the div element the chart is drawn in
 */
var BaseChart = function (elementId) {
    Chart.call(this, elementId);
};
BaseChart.prototype = new Chart();
BaseChart.prototype.constructor = BaseChart;
BaseChart.prototype.validateColumns = function (dataTable) {
    var columns = dataTable.getColumns();
    if (columns.length < 2) {
        throw new Error("Minimum of two columns are required for this chart");
    }
    for (var i = 1; i < columns.length; i++) {
        if (columns[i].type != ColumnType.NUMBER) {
            throw new Error("Column " + (i + 1) + " should be of type 'Number'");
        }
    }
    return true;
};

/**
 * Abstract pie chart
 *
 * @param elementId
 *            id of the div element the chart is drawn in
 */
var PieChart = function (elementId) {
    BaseChart.call(this, elementId);
};
PieChart.prototype = new BaseChart();
PieChart.prototype.constructor = PieChart;
PieChart.prototype.validateColumns = function (dataTable) {
    var columns = dataTable.getColumns();
    if (columns.length > 2) {
        throw new Error("Pie charts can draw only one series");
    }
    return BaseChart.prototype.validateColumns.call(this, dataTable);
};

/**
 * Abstract timeline chart
 *
 * @param elementId
 *            id of the div element the chart is drawn in
 */
var TimelineChart = function (elementId) {
    BaseChart.call(this, elementId);
};
TimelineChart.prototype = new BaseChart();
TimelineChart.prototype.constructor = TimelineChart;
TimelineChart.prototype.validateColumns = function (dataTable) {
    var result = BaseChart.prototype.validateColumns.call(this, dataTable);
    if (result) {
        var columns = dataTable.getColumns();
        if (columns[0].type != ColumnType.DATE) {
            throw new Error("First column of timeline chart need to be a date column");
        }
    }
    return result;
};

/**
 * Abstract scatter chart
 *
 * @param elementId
 *            id of the div element the chart is drawn in
 */
var ScatterChart = function(elementId) {
    BaseChart.call(this, elementId);
};
ScatterChart.prototype = new BaseChart();
ScatterChart.prototype.constructor = ScatterChart;
ScatterChart.prototype.validateColumns = function (dataTable) {
    var result = BaseChart.prototype.validateColumns.call(this, dataTable);
    if (result) {
        var columns = dataTable.getColumns();
        if (columns[0].type != ColumnType.NUMBER) {
            throw new Error("First column of scatter chart need to be a numeric column");
        }
    }
    return result;
};

/**
 * The data table contains column information and data for the chart.
 */
var DataTable = function () {
    var columns = [];
    var data = null;

    this.addColumn = function (type, name) {
        columns.push({
            'type' : type,
            'name' : name
        });
    };

    this.getColumns = function () {
        return columns;
    };

    this.setData = function (rows) {
        data = rows;
        fillMissingValues();
    };

    this.getData = function () {
        return data;
    };

    var fillMissingValues = function () {
        if (columns.length === 0) {
            throw new Error("Set columns first");
        }
        var row;
        for (var i = 0; i < data.length; i++) {
            row = data[i];
            if (row.length > columns.length) {
                row.splice(columns.length - 1, row.length - columns.length);
            } else if (row.length < columns.length) {
                for (var j = row.length; j < columns.length; j++) {
                    row.push(null);
                }
            }
        }
    };
};

/*******************************************************************************
 * JQPlot specific code
 ******************************************************************************/

/**
 * Abstract JQplot chart
 *
 * @param elementId
 *            id of the div element the chart is drawn in
 */
var JQPlotChart = function (elementId) {
    Chart.call(this, elementId);
    this.plot = null;
    this.validator = null;
};
JQPlotChart.prototype = new Chart();
JQPlotChart.prototype.constructor = JQPlotChart;
JQPlotChart.prototype.draw = function (data, options) {
    if (this.validator.validateColumns(data)) {
        this.plot = $.jqplot(this.elementId, this.prepareData(data), this
                .populateOptions(data, options));
    }
};
JQPlotChart.prototype.destroy = function () {
    if (this.plot !== null) {
        this.plot.destroy();
    }
};
JQPlotChart.prototype.redraw = function (options) {
    if (this.plot !== null) {
        this.plot.replot(options);
    }
};
JQPlotChart.prototype.toImageString = function (options) {
    if (this.plot !== null) {
        return $('#' + this.elementId).jqplotToImageStr({});
    }
};
JQPlotChart.prototype.populateOptions = function (dataTable, options) {
    throw new Error("populateOptions must be implemented by a subclass");
};
JQPlotChart.prototype.prepareData = function (dataTable) {
    throw new Error("prepareData must be implemented by a subclass");
};

/**
 * JQPlot line chart
 *
 * @param elementId
 *            id of the div element the chart is drawn in
 */
var JQPlotLineChart = function (elementId) {
    JQPlotChart.call(this, elementId);
    this.validator = BaseChart.prototype;
};
JQPlotLineChart.prototype = new JQPlotChart();
JQPlotLineChart.prototype.constructor = JQPlotLineChart;

JQPlotLineChart.prototype.populateOptions = function (dataTable, options) {
    var columns = dataTable.getColumns();
    var optional = {
        axes : {
            xaxis : {
                label : columns[0].name,
                renderer : $.jqplot.CategoryAxisRenderer,
                ticks : []
            },
            yaxis : {
                label : (columns.length == 2 ? columns[1].name : 'Values'),
                labelRenderer : $.jqplot.CanvasAxisLabelRenderer
            }
        },
        highlighter: {
            show: true,
            tooltipAxes: 'y',
            formatString:'%d'
        },
        series : []
    };
    $.extend(true, optional, options);

    if (optional.series.length === 0) {
        for (var i = 1; i < columns.length; i++) {
            optional.series.push({
                label : columns[i].name.toString()
            });
        }
    }
    if (optional.axes.xaxis.ticks.length === 0) {
        var data = dataTable.getData();
        for (var j = 0; j < data.length; j++) {
            optional.axes.xaxis.ticks.push(data[j][0].toString());
        }
    }
    return optional;
};

JQPlotLineChart.prototype.prepareData = function (dataTable) {
    var data = dataTable.getData(), row;
    var retData = [], retRow;
    for (var i = 0; i < data.length; i++) {
        row = data[i];
        for (var j = 1; j < row.length; j++) {
            retRow = retData[j - 1];
            if (retRow === undefined) {
                retRow = [];
                retData[j - 1] = retRow;
            }
            retRow.push(row[j]);
        }
    }
    return retData;
};

/**
 * JQPlot spline chart
 *
 * @param elementId
 *            id of the div element the chart is drawn in
 */
var JQPlotSplineChart = function (elementId) {
    JQPlotLineChart.call(this, elementId);
};
JQPlotSplineChart.prototype = new JQPlotLineChart();
JQPlotSplineChart.prototype.constructor = JQPlotSplineChart;

JQPlotSplineChart.prototype.populateOptions = function (dataTable, options) {
    var optional = {};
    var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
            options);
    var compulsory = {
        seriesDefaults : {
            rendererOptions : {
                smooth : true
            }
        }
    };
    $.extend(true, optional, opt, compulsory);
    return optional;
};

/**
 * JQPlot scatter chart
 *
 * @param elementId
 *            id of the div element the chart is drawn in
 */
var JQPlotScatterChart = function (elementId) {
    JQPlotChart.call(this, elementId);
    this.validator = ScatterChart.prototype;
};
JQPlotScatterChart.prototype = new JQPlotChart();
JQPlotScatterChart.prototype.constructor = JQPlotScatterChart;

JQPlotScatterChart.prototype.populateOptions = function (dataTable, options) {
    var columns = dataTable.getColumns();
    var optional = {
        axes : {
            xaxis : {
                label : columns[0].name
            },
            yaxis : {
                label : (columns.length == 2 ? columns[1].name : 'Values'),
                labelRenderer : $.jqplot.CanvasAxisLabelRenderer
            }
        },
        highlighter: {
            show: true,
            tooltipAxes: 'xy',
            formatString:'%d, %d'
        },
        series : []
    };
    for (var i = 1; i < columns.length; i++) {
        optional.series.push({
            label : columns[i].name.toString()
        });
    }

    var compulsory = {
        seriesDefaults : {
            showLine: false,
            markerOptions: {
                size: 7,
                style: "x"
            }
        }
    };

    $.extend(true, optional, options, compulsory);
    return optional;
};

JQPlotScatterChart.prototype.prepareData = function (dataTable) {
    var data = dataTable.getData(), row;
    var retData = [], retRow;
    for (var i = 0; i < data.length; i++) {
        row = data[i];
        if (row[0]) {
            for (var j = 1; j < row.length; j++) {
                retRow = retData[j - 1];
                if (retRow === undefined) {
                    retRow = [];
                    retData[j - 1] = retRow;
                }
                retRow.push([row[0], row[j]]);
            }
        }
    }
    return retData;
};

/**
 * JQPlot timeline chart
 *
 * @param elementId
 *            id of the div element the chart is drawn in
 */
var JQPlotTimelineChart = function (elementId) {
    JQPlotLineChart.call(this, elementId);
    this.validator = TimelineChart.prototype;
};
JQPlotTimelineChart.prototype = new JQPlotLineChart();
JQPlotTimelineChart.prototype.constructor = JQPlotTimelineChart;

JQPlotTimelineChart.prototype.populateOptions = function (dataTable, options) {
    var optional = {
        axes : {
            xaxis : {
                tickOptions : {
                    formatString: '%b %#d, %y'
                }
            }
        }
    };
    var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable, options);
    var compulsory = {
        axes : {
            xaxis : {
                renderer : $.jqplot.DateAxisRenderer
            }
        }
    };
    $.extend(true, optional, opt, compulsory);
    return optional;
};

JQPlotTimelineChart.prototype.prepareData = function (dataTable) {
    var data = dataTable.getData(), row, d;
    var retData = [], retRow;
    for (var i = 0; i < data.length; i++) {
        row = data[i];
        d = row[0];
        for (var j = 1; j < row.length; j++) {
            retRow = retData[j - 1];
            if (retRow === undefined) {
                retRow = [];
                retData[j - 1] = retRow;
            }
            if (d !== null) {
                retRow.push([d.getTime(), row[j]]);
            }
        }
    }
    return retData;
};

/**
 * JQPlot area chart
 *
 * @param elementId
 *            id of the div element the chart is drawn in
 */
var JQPlotAreaChart = function (elementId) {
    JQPlotLineChart.call(this, elementId);
};
JQPlotAreaChart.prototype = new JQPlotLineChart();
JQPlotAreaChart.prototype.constructor = JQPlotAreaChart;

JQPlotAreaChart.prototype.populateOptions = function (dataTable, options) {
    var optional = {
        seriesDefaults : {
            fillToZero : true
        }
    };
    var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
            options);
    var compulsory = {
        seriesDefaults : {
            fill : true
        }
    };
    $.extend(true, optional, opt, compulsory);
    return optional;
};

/**
 * JQPlot column chart
 *
 * @param elementId
 *            id of the div element the chart is drawn in
 */
var JQPlotColumnChart = function (elementId) {
    JQPlotLineChart.call(this, elementId);
};
JQPlotColumnChart.prototype = new JQPlotLineChart();
JQPlotColumnChart.prototype.constructor = JQPlotColumnChart;

JQPlotColumnChart.prototype.populateOptions = function (dataTable, options) {
    var optional = {
        seriesDefaults : {
            fillToZero : true
        }
    };
    var opt = JQPlotLineChart.prototype.populateOptions.call(this, dataTable,
            options);
    var compulsory = {
        seriesDefaults : {
            renderer : $.jqplot.BarRenderer
        }
    };
    $.extend(true, optional, opt, compulsory);
    return optional;
};

/**
 * JQPlot bar chart
 *
 * @param elementId
 *            id of the div element the chart is drawn in
 */
var JQPlotBarChart = function (elementId) {
    JQPlotLineChart.call(this, elementId);
};
JQPlotBarChart.prototype = new JQPlotLineChart();
JQPlotBarChart.prototype.constructor = JQPlotBarChart;

JQPlotBarChart.prototype.populateOptions = function (dataTable, options) {
    var columns = dataTable.getColumns();
    var optional = {
        axes : {
            yaxis : {
                label : columns[0].name,
                labelRenderer : $.jqplot.CanvasAxisLabelRenderer,
                renderer : $.jqplot.CategoryAxisRenderer,
                ticks : []
            },
            xaxis : {
                label : (columns.length == 2 ? columns[1].name : 'Values'),
                labelRenderer : $.jqplot.CanvasAxisLabelRenderer
            }
        },
        highlighter: {
            show: true,
            tooltipAxes: 'x',
            formatString:'%d'
        },
        series : [],
        seriesDefaults : {
            fillToZero : true
        }
    };
    var compulsory = {
        seriesDefaults : {
            renderer : $.jqplot.BarRenderer,
            rendererOptions : {
                barDirection : 'horizontal'
            }
        }
    };
    $.extend(true, optional, options, compulsory);

    if (optional.axes.yaxis.ticks.length === 0) {
        var data = dataTable.getData();
        for (var i = 0; i < data.length; i++) {
            optional.axes.yaxis.ticks.push(data[i][0].toString());
        }
    }
    if (optional.series.length === 0) {
        for (var j = 1; j < columns.length; j++) {
            optional.series.push({
                label : columns[j].name.toString()
            });
        }
    }
    return optional;
};

/**
 * JQPlot pie chart
 *
 * @param elementId
 *            id of the div element the chart is drawn in
 */
var JQPlotPieChart = function (elementId) {
    JQPlotChart.call(this, elementId);
    this.validator = PieChart.prototype;
};
JQPlotPieChart.prototype = new JQPlotChart();
JQPlotPieChart.prototype.constructor = JQPlotPieChart;

JQPlotPieChart.prototype.populateOptions = function (dataTable, options) {
    var optional = {
        highlighter: {
            show: true,
            tooltipAxes: 'xy',
            formatString:'%s, %d',
            useAxesFormatters: false
        }
    };
    var compulsory = {
        seriesDefaults : {
            renderer : $.jqplot.PieRenderer
        }
    };
    $.extend(true, optional, options, compulsory);
    return optional;
};

JQPlotPieChart.prototype.prepareData = function (dataTable) {
    var data = dataTable.getData(), row;
    var retData = [];
    for (var i = 0; i < data.length; i++) {
        row = data[i];
        retData.push([ row[0], row[1] ]);
    }
    return [ retData ];
};

/**
 * Chart factory that returns JQPlotCharts
 */
var JQPlotChartFactory = function () {
};
JQPlotChartFactory.prototype = new ChartFactory();
JQPlotChartFactory.prototype.createChart = function (type, elementId) {
    var chart = null;
    switch (type) {
    case ChartType.LINE:
        chart = new JQPlotLineChart(elementId);
        break;
    case ChartType.SPLINE:
        chart = new JQPlotSplineChart(elementId);
        break;
    case ChartType.TIMELINE:
        chart = new JQPlotTimelineChart(elementId);
        break;
    case ChartType.AREA:
        chart = new JQPlotAreaChart(elementId);
        break;
    case ChartType.BAR:
        chart = new JQPlotBarChart(elementId);
        break;
    case ChartType.COLUMN:
        chart = new JQPlotColumnChart(elementId);
        break;
    case ChartType.PIE:
        chart = new JQPlotPieChart(elementId);
        break;
    case ChartType.SCATTER:
        chart = new JQPlotScatterChart(elementId);
        break;
    }

    return chart;
};