jqplot.logAxisRenderer.js
21.1 KB
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
/**
* jqPlot
* Pure JavaScript plotting plugin using jQuery
*
* Version: 1.0.4
* Revision: 1121
*
* Copyright (c) 2009-2012 Chris Leonello
* jqPlot is currently available for use in all personal or commercial projects
* under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL
* version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can
* choose the license that best suits your project and use it accordingly.
*
* Although not required, the author would appreciate an email letting him
* know of any substantial use of jqPlot. You can reach the author at:
* chris at jqplot dot com or see http://www.jqplot.com/info.php .
*
* If you are feeling kind and generous, consider supporting the project by
* making a donation at: http://www.jqplot.com/donate.php .
*
* sprintf functions contained in jqplot.sprintf.js by Ash Searle:
*
* version 2007.04.27
* author Ash Searle
* http://hexmen.com/blog/2007/03/printf-sprintf/
* http://hexmen.com/js/sprintf.js
* The author (Ash Searle) has placed this code in the public domain:
* "This code is unrestricted: you are free to use it however you like."
*
*/
(function($) {
/**
* class: $.jqplot.LogAxisRenderer
* A plugin for a jqPlot to render a logarithmic axis.
*
* To use this renderer, include the plugin in your source
* > <script type="text/javascript" language="javascript" src="plugins/jqplot.logAxisRenderer.js"></script>
*
* and supply the appropriate options to your plot
*
* > {axes:{xaxis:{renderer:$.jqplot.LogAxisRenderer}}}
**/
$.jqplot.LogAxisRenderer = function() {
$.jqplot.LinearAxisRenderer.call(this);
// prop: axisDefaults
// Default properties which will be applied directly to the series.
//
// Group: Properties
//
// Properties
//
// base - the logarithmic base, commonly 2, 10 or Math.E
// tickDistribution - Deprecated. "power" distribution of ticks
// always used. Option has no effect.
this.axisDefaults = {
base : 10,
tickDistribution :'power'
};
};
$.jqplot.LogAxisRenderer.prototype = new $.jqplot.LinearAxisRenderer();
$.jqplot.LogAxisRenderer.prototype.constructor = $.jqplot.LogAxisRenderer;
$.jqplot.LogAxisRenderer.prototype.init = function(options) {
// prop: drawBaseline
// True to draw the axis baseline.
this.drawBaseline = true;
// prop: minorTicks
// Number of ticks to add between "major" ticks.
// Major ticks are ticks supplied by user or auto computed.
// Minor ticks cannot be created by user.
this.minorTicks = 'auto';
this._scalefact = 1.0;
$.extend(true, this, options);
this._autoFormatString = '%d';
this._overrideFormatString = false;
for (var d in this.renderer.axisDefaults) {
if (this[d] == null) {
this[d] = this.renderer.axisDefaults[d];
}
}
this.resetDataBounds();
};
$.jqplot.LogAxisRenderer.prototype.createTicks = function(plot) {
// we're are operating on an axis here
var ticks = this._ticks;
var userTicks = this.ticks;
var name = this.name;
var db = this._dataBounds;
var dim = (this.name.charAt(0) === 'x') ? this._plotDimensions.width : this._plotDimensions.height;
var interval;
var min, max;
var pos1, pos2;
var tt, i;
var threshold = 30;
// For some reason scalefactor is screwing up ticks.
this._scalefact = (Math.max(dim, threshold+1) - threshold)/300;
// if we already have ticks, use them.
// ticks must be in order of increasing value.
if (userTicks.length) {
// ticks could be 1D or 2D array of [val, val, ,,,] or [[val, label], [val, label], ...] or mixed
for (i=0; i<userTicks.length; i++){
var ut = userTicks[i];
var t = new this.tickRenderer(this.tickOptions);
if (ut.constructor == Array) {
t.value = ut[0];
t.label = ut[1];
if (!this.showTicks) {
t.showLabel = false;
t.showMark = false;
}
else if (!this.showTickMarks) {
t.showMark = false;
}
t.setTick(ut[0], this.name);
this._ticks.push(t);
}
else if ($.isPlainObject(ut)) {
$.extend(true, t, ut);
t.axis = this.name;
this._ticks.push(t);
}
else {
t.value = ut;
if (!this.showTicks) {
t.showLabel = false;
t.showMark = false;
}
else if (!this.showTickMarks) {
t.showMark = false;
}
t.setTick(ut, this.name);
this._ticks.push(t);
}
}
this.numberTicks = userTicks.length;
this.min = this._ticks[0].value;
this.max = this._ticks[this.numberTicks-1].value;
}
// we don't have any ticks yet, let's make some!
else if (this.min == null && this.max == null) {
min = db.min * (2 - this.padMin);
max = db.max * this.padMax;
// if min and max are same, space them out a bit
if (min == max) {
var adj = 0.05;
min = min*(1-adj);
max = max*(1+adj);
}
// perform some checks
if (this.min != null && this.min <= 0) {
throw('log axis minimum must be greater than 0');
}
if (this.max != null && this.max <= 0) {
throw('log axis maximum must be greater than 0');
}
function findCeil (val) {
var order = Math.pow(10, Math.floor(Math.log(val)/Math.LN10));
return Math.ceil(val/order) * order;
}
function findFloor(val) {
var order = Math.pow(10, Math.floor(Math.log(val)/Math.LN10));
return Math.floor(val/order) * order;
}
// var range = max - min;
var rmin, rmax;
// for power distribution, open up range to get a nice power of axis.renderer.base.
// power distribution won't respect the user's min/max settings.
rmin = Math.pow(this.base, Math.floor(Math.log(min)/Math.log(this.base)));
rmax = Math.pow(this.base, Math.ceil(Math.log(max)/Math.log(this.base)));
// // if min and max are same, space them out a bit
// if (rmin === rmax) {
// var adj = 0.05;
// rmin = rmin*(1-adj);
// rmax = rmax*(1+adj);
// }
var order = Math.round(Math.log(rmin)/Math.LN10);
if (this.tickOptions == null || !this.tickOptions.formatString) {
this._overrideFormatString = true;
}
this.min = rmin;
this.max = rmax;
var range = this.max - this.min;
var minorTicks = (this.minorTicks === 'auto') ? 0 : this.minorTicks;
var numberTicks;
if (this.numberTicks == null){
if (dim > 140) {
numberTicks = Math.round(Math.log(this.max/this.min)/Math.log(this.base) + 1);
if (numberTicks < 2) {
numberTicks = 2;
}
if (minorTicks === 0) {
var temp = dim/(numberTicks - 1);
if (temp < 100) {
minorTicks = 0;
}
else if (temp < 190) {
minorTicks = 1;
}
else if (temp < 250) {
minorTicks = 3;
}
else if (temp < 600) {
minorTicks = 4;
}
else {
minorTicks = 9;
}
}
}
else {
numberTicks = 2;
if (minorTicks === 0) {
minorTicks = 1;
}
minorTicks = 0;
}
}
else {
numberTicks = this.numberTicks;
}
if (order >= 0 && minorTicks !== 3) {
this._autoFormatString = '%d';
}
// Adjust format string for case with 3 ticks where we'll have like 1, 2.5, 5, 7.5, 10
else if (order <= 0 && minorTicks === 3) {
var temp = -(order - 1);
this._autoFormatString = '%.'+ Math.abs(order-1) + 'f';
}
// Adjust format string for values less than 1.
else if (order < 0) {
var temp = -order;
this._autoFormatString = '%.'+ Math.abs(order) + 'f';
}
else {
this._autoFormatString = '%d';
}
var to, t, val, tt1, spread, interval;
for (var i=0; i<numberTicks; i++){
tt = Math.pow(this.base, i - numberTicks + 1) * this.max;
t = new this.tickRenderer(this.tickOptions);
if (this._overrideFormatString) {
t.formatString = this._autoFormatString;
}
if (!this.showTicks) {
t.showLabel = false;
t.showMark = false;
}
else if (!this.showTickMarks) {
t.showMark = false;
}
t.setTick(tt, this.name);
this._ticks.push(t);
if (minorTicks && i<numberTicks-1) {
tt1 = Math.pow(this.base, i - numberTicks + 2) * this.max;
spread = tt1 - tt;
interval = tt1 / (minorTicks+1);
for (var j=minorTicks-1; j>=0; j--) {
val = tt1-interval*(j+1);
t = new this.tickRenderer(this.tickOptions);
if (this._overrideFormatString && this._autoFormatString != '') {
t.formatString = this._autoFormatString;
}
if (!this.showTicks) {
t.showLabel = false;
t.showMark = false;
}
else if (!this.showTickMarks) {
t.showMark = false;
}
t.setTick(val, this.name);
this._ticks.push(t);
}
}
}
}
// min and max are set as would be the case with zooming
else if (this.min != null && this.max != null) {
var opts = $.extend(true, {}, this.tickOptions, {name: this.name, value: null});
var nt, ti;
// don't have an interval yet, pick one that gives the most
// "round" ticks we can get.
if (this.numberTicks == null && this.tickInterval == null) {
// var threshold = 30;
var tdim = Math.max(dim, threshold+1);
var nttarget = Math.ceil((tdim-threshold)/35 + 1);
var ret = $.jqplot.LinearTickGenerator.bestConstrainedInterval(this.min, this.max, nttarget);
this._autoFormatString = ret[3];
nt = ret[2];
ti = ret[4];
for (var i=0; i<nt; i++) {
opts.value = this.min + i * ti;
t = new this.tickRenderer(opts);
if (this._overrideFormatString && this._autoFormatString != '') {
t.formatString = this._autoFormatString;
}
if (!this.showTicks) {
t.showLabel = false;
t.showMark = false;
}
else if (!this.showTickMarks) {
t.showMark = false;
}
this._ticks.push(t);
}
}
// for loose zoom, number ticks and interval are also set.
else if (this.numberTicks != null && this.tickInterval != null) {
nt = this.numberTicks;
for (var i=0; i<nt; i++) {
opts.value = this.min + i * this.tickInterval;
t = new this.tickRenderer(opts);
if (this._overrideFormatString && this._autoFormatString != '') {
t.formatString = this._autoFormatString;
}
if (!this.showTicks) {
t.showLabel = false;
t.showMark = false;
}
else if (!this.showTickMarks) {
t.showMark = false;
}
this._ticks.push(t);
}
}
}
};
$.jqplot.LogAxisRenderer.prototype.pack = function(pos, offsets) {
var lb = parseInt(this.base, 10);
var ticks = this._ticks;
var trans = function (v) { return Math.log(v)/Math.log(lb); };
var invtrans = function (v) { return Math.pow(Math.E, (Math.log(lb)*v)); };
var max = trans(this.max);
var min = trans(this.min);
var offmax = offsets.max;
var offmin = offsets.min;
var lshow = (this._label == null) ? false : this._label.show;
for (var p in pos) {
this._elem.css(p, pos[p]);
}
this._offsets = offsets;
// pixellength will be + for x axes and - for y axes becasue pixels always measured from top left.
var pixellength = offmax - offmin;
var unitlength = max - min;
// point to unit and unit to point conversions references to Plot DOM element top left corner.
this.p2u = function(p){
return invtrans((p - offmin) * unitlength / pixellength + min);
};
this.u2p = function(u){
return (trans(u) - min) * pixellength / unitlength + offmin;
};
if (this.name == 'xaxis' || this.name == 'x2axis'){
this.series_u2p = function(u){
return (trans(u) - min) * pixellength / unitlength;
};
this.series_p2u = function(p){
return invtrans(p * unitlength / pixellength + min);
};
}
// yaxis is max at top of canvas.
else {
this.series_u2p = function(u){
return (trans(u) - max) * pixellength / unitlength;
};
this.series_p2u = function(p){
return invtrans(p * unitlength / pixellength + max);
};
}
if (this.show) {
if (this.name == 'xaxis' || this.name == 'x2axis') {
for (var i=0; i<ticks.length; i++) {
var t = ticks[i];
if (t.show && t.showLabel) {
var shim;
if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
switch (t.labelPosition) {
case 'auto':
// position at end
if (t.angle < 0) {
shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
}
// position at start
else {
shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
}
break;
case 'end':
shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
break;
case 'start':
shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
break;
case 'middle':
shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
break;
default:
shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
break;
}
}
else {
shim = -t.getWidth()/2;
}
// var shim = t.getWidth()/2;
var val = this.u2p(t.value) + shim + 'px';
t._elem.css('left', val);
t.pack();
}
}
if (lshow) {
var w = this._label._elem.outerWidth(true);
this._label._elem.css('left', offmin + pixellength/2 - w/2 + 'px');
if (this.name == 'xaxis') {
this._label._elem.css('bottom', '0px');
}
else {
this._label._elem.css('top', '0px');
}
this._label.pack();
}
}
else {
for (var i=0; i<ticks.length; i++) {
var t = ticks[i];
if (t.show && t.showLabel) {
var shim;
if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
switch (t.labelPosition) {
case 'auto':
// position at end
case 'end':
if (t.angle < 0) {
shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
}
else {
shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
}
break;
case 'start':
if (t.angle > 0) {
shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
}
else {
shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
}
break;
case 'middle':
// if (t.angle > 0) {
// shim = -t.getHeight()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
// }
// else {
// shim = -t.getHeight()/2 - t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
// }
shim = -t.getHeight()/2;
break;
default:
shim = -t.getHeight()/2;
break;
}
}
else {
shim = -t.getHeight()/2;
}
var val = this.u2p(t.value) + shim + 'px';
t._elem.css('top', val);
t.pack();
}
}
if (lshow) {
var h = this._label._elem.outerHeight(true);
this._label._elem.css('top', offmax - pixellength/2 - h/2 + 'px');
if (this.name == 'yaxis') {
this._label._elem.css('left', '0px');
}
else {
this._label._elem.css('right', '0px');
}
this._label.pack();
}
}
}
};
})(jQuery);