3
* Pure JavaScript plotting plugin using jQuery
5
* Version: 1.0.0b2_r1012
7
* Copyright (c) 2009-2011 Chris Leonello
8
* jqPlot is currently available for use in all personal or commercial projects
9
* under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL
10
* version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can
11
* choose the license that best suits your project and use it accordingly.
13
* Although not required, the author would appreciate an email letting him
14
* know of any substantial use of jqPlot. You can reach the author at:
15
* chris at jqplot dot com or see http://www.jqplot.com/info.php .
17
* If you are feeling kind and generous, consider supporting the project by
18
* making a donation at: http://www.jqplot.com/donate.php .
20
* sprintf functions contained in jqplot.sprintf.js by Ash Searle:
24
* http://hexmen.com/blog/2007/03/printf-sprintf/
25
* http://hexmen.com/js/sprintf.js
26
* The author (Ash Searle) has placed this code in the public domain:
27
* "This code is unrestricted: you are free to use it however you like."
31
// class: $.jqplot.MekkoAxisRenderer
32
// An axis renderer for a Mekko chart.
33
// Should be used with a Mekko chart where the mekkoRenderer is used on the series.
34
// Displays the Y axis as a range from 0 to 1 (0 to 100%) and the x axis with a tick
35
// for each series scaled to the sum of all the y values.
36
$.jqplot.MekkoAxisRenderer = function() {
39
// called with scope of axis object.
40
$.jqplot.MekkoAxisRenderer.prototype.init = function(options){
42
// How to space the ticks on the axis.
43
// 'bar' will place a tick at the width of each bar.
44
// This is the default for the x axis.
45
// 'even' will place ticks at even intervals. This is
46
// the default for x2 axis and y axis. y axis cannot be changed.
48
// prop: barLabelRenderer
49
// renderer to use to draw labels under each bar.
50
this.barLabelRenderer = $.jqplot.AxisLabelRenderer;
52
// array of labels to put under each bar.
53
this.barLabels = this.barLabels || [];
54
// prop: barLabelOptions
55
// options object to pass to the bar label renderer.
56
this.barLabelOptions = {};
57
this.tickOptions = $.extend(true, {showGridline:false}, this.tickOptions);
59
$.extend(true, this, options);
60
if (this.name == 'yaxis') {
61
this.tickOptions.formatString = this.tickOptions.formatString || "%d\%";
63
var db = this._dataBounds;
65
// for y axes, scale always go from 0 to 1 (0 to 100%)
66
if (this.name == 'yaxis' || this.name == 'y2axis') {
68
this.tickMode = 'even';
70
// For x axes, scale goes from 0 to sum of all y values.
71
else if (this.name == 'xaxis'){
72
this.tickMode = (this.tickMode == null) ? 'bar' : this.tickMode;
73
for (var i=0; i<this._series.length; i++) {
74
db.max += this._series[i]._sumy;
77
else if (this.name == 'x2axis'){
78
this.tickMode = (this.tickMode == null) ? 'even' : this.tickMode;
79
for (var i=0; i<this._series.length; i++) {
80
db.max += this._series[i]._sumy;
85
// called with scope of axis
86
$.jqplot.MekkoAxisRenderer.prototype.draw = function(ctx, plot) {
88
// populate the axis label and value properties.
89
// createTicks is a method on the renderer, but
90
// call it within the scope of the axis.
91
this.renderer.createTicks.call(this);
92
// fill a div with axes labels in the right direction.
93
// Need to pregenerate each axis to get it's bounds and
94
// position it and the labels correctly on the plot.
98
var elem = document.createElement('div');
100
this._elem.addClass('jqplot-axis jqplot-'+this.name);
101
this._elem.css('position', 'absolute');
104
if (this.name == 'xaxis' || this.name == 'x2axis') {
105
this._elem.width(this._plotDimensions.width);
108
this._elem.height(this._plotDimensions.height);
111
// draw the axis label
112
// create a _label object.
113
this.labelOptions.axis = this.name;
114
this._label = new this.labelRenderer(this.labelOptions);
115
if (this._label.show) {
116
this._elem.append(this._label.draw(ctx));
120
if (this.showTicks) {
122
for (var i=0; i<t.length; i++) {
124
if (tick.showLabel && (!tick.isMinorTick || this.showMinorTicks)) {
125
this._elem.append(tick.draw(ctx));
130
// draw the series labels
131
for (i=0; i<this.barLabels.length; i++) {
132
this.barLabelOptions.axis = this.name;
133
this.barLabelOptions.label = this.barLabels[i];
134
this._barLabels.push(new this.barLabelRenderer(this.barLabelOptions));
135
if (this.tickMode != 'bar') {
136
this._barLabels[i].show = false;
138
if (this._barLabels[i].show) {
139
var elem = this._barLabels[i].draw(ctx, plot);
140
elem.removeClass('jqplot-'+this.name+'-label');
141
elem.addClass('jqplot-'+this.name+'-tick');
142
elem.addClass('jqplot-mekko-barLabel');
143
elem.appendTo(this._elem);
152
// called with scope of an axis
153
$.jqplot.MekkoAxisRenderer.prototype.reset = function() {
154
this.min = this._min;
155
this.max = this._max;
156
this.tickInterval = this._tickInterval;
157
this.numberTicks = this._numberTicks;
158
// this._ticks = this.__ticks;
161
// called with scope of axis
162
$.jqplot.MekkoAxisRenderer.prototype.set = function() {
167
var lshow = (this._label == null) ? false : this._label.show;
168
if (this.show && this.showTicks) {
170
for (var i=0; i<t.length; i++) {
172
if (tick.showLabel && (!tick.isMinorTick || this.showMinorTicks)) {
173
if (this.name == 'xaxis' || this.name == 'x2axis') {
174
temp = tick._elem.outerHeight(true);
177
temp = tick._elem.outerWidth(true);
186
w = this._label._elem.outerWidth(true);
187
h = this._label._elem.outerHeight(true);
189
if (this.name == 'xaxis') {
191
this._elem.css({'height':dim+'px', left:'0px', bottom:'0px'});
193
else if (this.name == 'x2axis') {
195
this._elem.css({'height':dim+'px', left:'0px', top:'0px'});
197
else if (this.name == 'yaxis') {
199
this._elem.css({'width':dim+'px', left:'0px', top:'0px'});
200
if (lshow && this._label.constructor == $.jqplot.AxisLabelRenderer) {
201
this._label._elem.css('width', w+'px');
206
this._elem.css({'width':dim+'px', right:'0px', top:'0px'});
207
if (lshow && this._label.constructor == $.jqplot.AxisLabelRenderer) {
208
this._label._elem.css('width', w+'px');
214
// called with scope of axis
215
$.jqplot.MekkoAxisRenderer.prototype.createTicks = function() {
216
// we're are operating on an axis here
217
var ticks = this._ticks;
218
var userTicks = this.ticks;
219
var name = this.name;
220
// databounds were set on axis initialization.
221
var db = this._dataBounds;
227
// if we already have ticks, use them.
228
// ticks must be in order of increasing value.
230
if (userTicks.length) {
231
// ticks could be 1D or 2D array of [val, val, ,,,] or [[val, label], [val, label], ...] or mixed
232
for (i=0; i<userTicks.length; i++){
233
var ut = userTicks[i];
234
var t = new this.tickRenderer(this.tickOptions);
235
if (ut.constructor == Array) {
238
if (!this.showTicks) {
242
else if (!this.showTickMarks) {
245
t.setTick(ut[0], this.name);
251
if (!this.showTicks) {
255
else if (!this.showTickMarks) {
258
t.setTick(ut, this.name);
262
this.numberTicks = userTicks.length;
263
this.min = this._ticks[0].value;
264
this.max = this._ticks[this.numberTicks-1].value;
265
this.tickInterval = (this.max - this.min) / (this.numberTicks - 1);
268
// we don't have any ticks yet, let's make some!
270
if (name == 'xaxis' || name == 'x2axis') {
271
dim = this._plotDimensions.width;
274
dim = this._plotDimensions.height;
277
// if min, max and number of ticks specified, user can't specify interval.
278
if (this.min != null && this.max != null && this.numberTicks != null) {
279
this.tickInterval = null;
282
min = (this.min != null) ? this.min : db.min;
283
max = (this.max != null) ? this.max : db.max;
285
// if min and max are same, space them out a bit.+
289
adj = Math.max(Math.log(min)/Math.LN10, 0.05);
295
var range = max - min;
297
var temp, prev, curr;
298
var ynumticks = [3,5,6,11,21];
300
// yaxis divide ticks in nice intervals from 0 to 1.
301
if (this.name == 'yaxis' || this.name == 'y2axis') {
304
// user didn't specify number of ticks.
305
if (!this.numberTicks){
306
if (this.tickInterval) {
307
this.numberTicks = 3 + Math.ceil(range / this.tickInterval);
310
temp = 2 + Math.ceil((dim-(this.tickSpacing-1))/this.tickSpacing);
311
for (i=0; i<ynumticks.length; i++) {
312
curr = temp/ynumticks[i];
314
this.numberTicks = ynumticks[i];
322
// was prev or is curr closer to one?
323
if (Math.abs(prev - 1) < Math.abs(curr - 1)) {
324
this.numberTicks = ynumticks[i-1];
328
this.numberTicks = ynumticks[i];
332
else if (i == ynumticks.length -1) {
333
this.numberTicks = ynumticks[i];
336
this.tickInterval = range / (this.numberTicks - 1);
340
// user did specify number of ticks.
342
this.tickInterval = range / (this.numberTicks - 1);
345
for (var i=0; i<this.numberTicks; i++){
346
tt = this.min + i * this.tickInterval;
347
t = new this.tickRenderer(this.tickOptions);
348
// var t = new $.jqplot.AxisTickRenderer(this.tickOptions);
349
if (!this.showTicks) {
353
else if (!this.showTickMarks) {
356
t.setTick(tt, this.name);
361
// for x axes, have number ot ticks equal to number of series and ticks placed
362
// at sum of y values for each series.
363
else if (this.tickMode == 'bar') {
365
this.numberTicks = this._series.length + 1;
366
t = new this.tickRenderer(this.tickOptions);
367
if (!this.showTicks) {
371
else if (!this.showTickMarks) {
374
t.setTick(0, this.name);
379
for (i=1; i<this.numberTicks; i++){
380
temp += this._series[i-1]._sumy;
381
t = new this.tickRenderer(this.tickOptions);
382
if (!this.showTicks) {
386
else if (!this.showTickMarks) {
389
t.setTick(temp, this.name);
392
this.max = this.max || temp;
394
// if user specified a max and it is greater than sum, add a tick
395
if (this.max > temp) {
396
t = new this.tickRenderer(this.tickOptions);
397
if (!this.showTicks) {
401
else if (!this.showTickMarks) {
404
t.setTick(this.max, this.name);
410
else if (this.tickMode == 'even') {
412
this.max = this.max || db.max;
413
// get a desired number of ticks
414
var nt = 2 + Math.ceil((dim-(this.tickSpacing-1))/this.tickSpacing);
415
range = this.max - this.min;
416
this.numberTicks = nt;
417
this.tickInterval = range / (this.numberTicks - 1);
419
for (i=0; i<this.numberTicks; i++){
420
tt = this.min + i * this.tickInterval;
421
t = new this.tickRenderer(this.tickOptions);
422
// var t = new $.jqplot.AxisTickRenderer(this.tickOptions);
423
if (!this.showTicks) {
427
else if (!this.showTickMarks) {
430
t.setTick(tt, this.name);
438
// called with scope of axis
439
$.jqplot.MekkoAxisRenderer.prototype.pack = function(pos, offsets) {
440
var ticks = this._ticks;
443
var offmax = offsets.max;
444
var offmin = offsets.min;
445
var lshow = (this._label == null) ? false : this._label.show;
448
this._elem.css(p, pos[p]);
451
this._offsets = offsets;
452
// pixellength will be + for x axes and - for y axes becasue pixels always measured from top left.
453
var pixellength = offmax - offmin;
454
var unitlength = max - min;
456
// point to unit and unit to point conversions references to Plot DOM element top left corner.
457
this.p2u = function(p){
458
return (p - offmin) * unitlength / pixellength + min;
461
this.u2p = function(u){
462
return (u - min) * pixellength / unitlength + offmin;
465
if (this.name == 'xaxis' || this.name == 'x2axis'){
466
this.series_u2p = function(u){
467
return (u - min) * pixellength / unitlength;
469
this.series_p2u = function(p){
470
return p * unitlength / pixellength + min;
475
this.series_u2p = function(u){
476
return (u - max) * pixellength / unitlength;
478
this.series_p2u = function(p){
479
return p * unitlength / pixellength + max;
484
if (this.name == 'xaxis' || this.name == 'x2axis') {
485
for (var i=0; i<ticks.length; i++) {
487
if (t.show && t.showLabel) {
490
if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
491
// will need to adjust auto positioning based on which axis this is.
492
var temp = (this.name == 'xaxis') ? 1 : -1;
493
switch (t.labelPosition) {
496
if (temp * t.angle < 0) {
497
shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
501
shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
505
shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
508
shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
511
shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
514
shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
519
shim = -t.getWidth()/2;
521
var val = this.u2p(t.value) + shim + 'px';
522
t._elem.css('left', val);
528
w = this._label._elem.outerWidth(true);
529
this._label._elem.css('left', offmin + pixellength/2 - w/2 + 'px');
530
if (this.name == 'xaxis') {
531
this._label._elem.css('bottom', '0px');
534
this._label._elem.css('top', '0px');
538
// now show the labels under the bars.
540
for (var i=0; i<this.barLabels.length; i++) {
541
b = this._barLabels[i];
544
l = this._ticks[i].getLeft() + this._ticks[i].getWidth();
545
r = this._ticks[i+1].getLeft();
546
b._elem.css('left', (r+l-w)/2+'px');
547
b._elem.css('top', this._ticks[i]._elem.css('top'));
553
for (var i=0; i<ticks.length; i++) {
555
if (t.show && t.showLabel) {
557
if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
558
// will need to adjust auto positioning based on which axis this is.
559
var temp = (this.name == 'yaxis') ? 1 : -1;
560
switch (t.labelPosition) {
564
if (temp * t.angle < 0) {
565
shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
568
shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
573
shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
576
shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
580
shim = -t.getHeight()/2;
583
shim = -t.getHeight()/2;
588
shim = -t.getHeight()/2;
591
var val = this.u2p(t.value) + shim + 'px';
592
t._elem.css('top', val);
597
var h = this._label._elem.outerHeight(true);
598
this._label._elem.css('top', offmax - pixellength/2 - h/2 + 'px');
599
if (this.name == 'yaxis') {
600
this._label._elem.css('left', '0px');
603
this._label._elem.css('right', '0px');