3 * Pure JavaScript plotting plugin using jQuery
8 * Copyright (c) 2009-2012 Chris Leonello
9 * jqPlot is currently available for use in all personal or commercial projects
10 * under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL
11 * version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can
12 * choose the license that best suits your project and use it accordingly.
14 * Although not required, the author would appreciate an email letting him
15 * know of any substantial use of jqPlot. You can reach the author at:
16 * chris at jqplot dot com or see http://www.jqplot.com/info.php .
18 * If you are feeling kind and generous, consider supporting the project by
19 * making a donation at: http://www.jqplot.com/donate.php .
21 * sprintf functions contained in jqplot.sprintf.js by Ash Searle:
25 * http://hexmen.com/blog/2007/03/printf-sprintf/
26 * http://hexmen.com/js/sprintf.js
27 * The author (Ash Searle) has placed this code in the public domain:
28 * "This code is unrestricted: you are free to use it however you like."
33 * class: $.jqplot.CategoryAxisRenderer
34 * A plugin for jqPlot to render a category style axis, with equal pixel spacing between y data values of a series.
36 * To use this renderer, include the plugin in your source
37 * > <script type="text/javascript" language="javascript" src="plugins/jqplot.categoryAxisRenderer.js"></script>
39 * and supply the appropriate options to your plot
41 * > {axes:{xaxis:{renderer:$.jqplot.CategoryAxisRenderer}}}
43 $.jqplot.CategoryAxisRenderer = function(options) {
44 $.jqplot.LinearAxisRenderer.call(this);
45 // prop: sortMergedLabels
46 // True to sort tick labels when labels are created by merging
47 // x axis values from multiple series. That is, say you have
49 // > line1 = [[2006, 4], [2008, 9], [2009, 16]];
50 // > line2 = [[2006, 3], [2007, 7], [2008, 6]];
51 // If no label array is specified, tick labels will be collected
52 // from the x values of the series. With sortMergedLabels
53 // set to true, tick labels will be:
54 // > [2006, 2007, 2008, 2009]
55 // With sortMergedLabels set to false, tick labels will be:
56 // > [2006, 2008, 2009, 2007]
58 // Note, this property is specified on the renderOptions for the
59 // axes when creating a plot:
60 // > axes:{xaxis:{renderer:$.jqplot.CategoryAxisRenderer, rendererOptions:{sortMergedLabels:true}}}
61 this.sortMergedLabels = false;
64 $.jqplot.CategoryAxisRenderer.prototype = new $.jqplot.LinearAxisRenderer();
65 $.jqplot.CategoryAxisRenderer.prototype.constructor = $.jqplot.CategoryAxisRenderer;
67 $.jqplot.CategoryAxisRenderer.prototype.init = function(options){
69 this.groupLabels = [];
70 this._groupLabels = [];
71 this._grouped = false;
72 this._barsPerGroup = null;
75 // A class of a rendering engine for creating the ticks labels displayed on the plot,
76 // See <$.jqplot.AxisTickRenderer>.
77 // this.tickRenderer = $.jqplot.AxisTickRenderer;
78 // this.labelRenderer = $.jqplot.AxisLabelRenderer;
79 $.extend(true, this, {tickOptions:{formatString:'%d'}}, options);
80 var db = this._dataBounds;
81 // Go through all the series attached to this axis and find
82 // the min/max bounds for this axis.
83 for (var i=0; i<this._series.length; i++) {
84 var s = this._series[i];
86 this.groups = s.groups;
90 for (var j=0; j<d.length; j++) {
91 if (this.name == 'xaxis' || this.name == 'x2axis') {
92 if (d[j][0] < db.min || db.min == null) {
95 if (d[j][0] > db.max || db.max == null) {
100 if (d[j][1] < db.min || db.min == null) {
103 if (d[j][1] > db.max || db.max == null) {
110 if (this.groupLabels.length) {
111 this.groups = this.groupLabels.length;
116 $.jqplot.CategoryAxisRenderer.prototype.createTicks = function() {
117 // we're are operating on an axis here
118 var ticks = this._ticks;
119 var userTicks = this.ticks;
120 var name = this.name;
121 // databounds were set on axis initialization.
122 var db = this._dataBounds;
128 // if we already have ticks, use them.
129 if (userTicks.length) {
130 // adjust with blanks if we have groups
131 if (this.groups > 1 && !this._grouped) {
132 var l = userTicks.length;
133 var skip = parseInt(l/this.groups, 10);
135 for (var i=skip; i<l; i+=skip) {
136 userTicks.splice(i+count, 0, ' ');
139 this._grouped = true;
142 this.max = userTicks.length + 0.5;
143 var range = this.max - this.min;
144 this.numberTicks = 2*userTicks.length + 1;
145 for (i=0; i<userTicks.length; i++){
146 tt = this.min + 2 * i * range / (this.numberTicks-1);
147 // need a marker before and after the tick
148 var t = new this.tickRenderer(this.tickOptions);
150 // t.showMark = true;
151 t.setTick(tt, this.name);
153 var t = new this.tickRenderer(this.tickOptions);
154 t.label = userTicks[i];
155 // t.showLabel = true;
157 t.showGridline = false;
158 t.setTick(tt+0.5, this.name);
161 // now add the last tick at the end
162 var t = new this.tickRenderer(this.tickOptions);
164 // t.showMark = true;
165 t.setTick(tt+1, this.name);
169 // we don't have any ticks yet, let's make some!
171 if (name == 'xaxis' || name == 'x2axis') {
172 dim = this._plotDimensions.width;
175 dim = this._plotDimensions.height;
178 // if min, max and number of ticks specified, user can't specify interval.
179 if (this.min != null && this.max != null && this.numberTicks != null) {
180 this.tickInterval = null;
183 // if max, min, and interval specified and interval won't fit, ignore interval.
184 if (this.min != null && this.max != null && this.tickInterval != null) {
185 if (parseInt((this.max-this.min)/this.tickInterval, 10) != (this.max-this.min)/this.tickInterval) {
186 this.tickInterval = null;
190 // find out how many categories are in the lines and collect labels
195 var isMerged = false;
196 for (var i=0; i<this._series.length; i++) {
197 var s = this._series[i];
198 for (var j=0; j<s.data.length; j++) {
199 if (this.name == 'xaxis' || this.name == 'x2axis') {
205 if ($.inArray(val, labels) == -1) {
213 if (isMerged && this.sortMergedLabels) {
214 labels.sort(function(a,b) { return a - b; });
217 // keep a reference to these tick labels to use for redrawing plot (see bug #57)
220 // now bin the data values to the right lables.
221 for (var i=0; i<this._series.length; i++) {
222 var s = this._series[i];
223 for (var j=0; j<s.data.length; j++) {
224 if (this.name == 'xaxis' || this.name == 'x2axis') {
230 // for category axis, force the values into category bins.
231 // we should have the value in the label array now.
232 var idx = $.inArray(val, labels)+1;
233 if (this.name == 'xaxis' || this.name == 'x2axis') {
242 // adjust with blanks if we have groups
243 if (this.groups > 1 && !this._grouped) {
244 var l = labels.length;
245 var skip = parseInt(l/this.groups, 10);
247 for (var i=skip; i<l; i+=skip+1) {
250 this._grouped = true;
254 if (this.numberTicks == null) {
255 this.numberTicks = 2*numcats + 1;
258 var range = max - min;
263 // todo: adjust this so more ticks displayed.
264 var maxVisibleTicks = parseInt(3+dim/10, 10);
265 var skip = parseInt(numcats/maxVisibleTicks, 10);
267 if (this.tickInterval == null) {
269 this.tickInterval = range / (this.numberTicks-1);
272 // if tickInterval is specified, we will ignore any computed maximum.
273 for (var i=0; i<this.numberTicks; i++){
274 tt = this.min + i * this.tickInterval;
275 var t = new this.tickRenderer(this.tickOptions);
276 // if even tick, it isn't a category, it's a divider
277 if (i/2 == parseInt(i/2, 10)) {
282 if (skip>0 && track<skip) {
290 t.label = t.formatter(t.formatString, labels[(i-1)/2]);
292 t.showGridline = false;
294 t.setTick(tt, this.name);
301 // called with scope of axis
302 $.jqplot.CategoryAxisRenderer.prototype.draw = function(ctx, plot) {
304 // populate the axis label and value properties.
305 // createTicks is a method on the renderer, but
306 // call it within the scope of the axis.
307 this.renderer.createTicks.call(this);
308 // fill a div with axes labels in the right direction.
309 // Need to pregenerate each axis to get it's bounds and
310 // position it and the labels correctly on the plot.
313 // Added for theming.
315 // this._elem.empty();
316 // Memory Leaks patch
317 this._elem.emptyForce();
320 this._elem = this._elem || $('<div class="jqplot-axis jqplot-'+this.name+'" style="position:absolute;"></div>');
322 if (this.name == 'xaxis' || this.name == 'x2axis') {
323 this._elem.width(this._plotDimensions.width);
326 this._elem.height(this._plotDimensions.height);
329 // create a _label object.
330 this.labelOptions.axis = this.name;
331 this._label = new this.labelRenderer(this.labelOptions);
332 if (this._label.show) {
333 var elem = this._label.draw(ctx, plot);
334 elem.appendTo(this._elem);
338 for (var i=0; i<t.length; i++) {
340 if (tick.showLabel && (!tick.isMinorTick || this.showMinorTicks)) {
341 var elem = tick.draw(ctx, plot);
342 elem.appendTo(this._elem);
346 this._groupLabels = [];
347 // now make group labels
348 for (var i=0; i<this.groupLabels.length; i++)
350 var elem = $('<div style="position:absolute;" class="jqplot-'+this.name+'-groupLabel"></div>');
351 elem.html(this.groupLabels[i]);
352 this._groupLabels.push(elem);
353 elem.appendTo(this._elem);
359 // called with scope of axis
360 $.jqplot.CategoryAxisRenderer.prototype.set = function() {
365 var lshow = (this._label == null) ? false : this._label.show;
368 for (var i=0; i<t.length; i++) {
370 if (tick.showLabel && (!tick.isMinorTick || this.showMinorTicks)) {
371 if (this.name == 'xaxis' || this.name == 'x2axis') {
372 temp = tick._elem.outerHeight(true);
375 temp = tick._elem.outerWidth(true);
384 for (var i=0; i<this._groupLabels.length; i++) {
385 var l = this._groupLabels[i];
386 if (this.name == 'xaxis' || this.name == 'x2axis') {
387 temp = l.outerHeight(true);
390 temp = l.outerWidth(true);
398 w = this._label._elem.outerWidth(true);
399 h = this._label._elem.outerHeight(true);
401 if (this.name == 'xaxis') {
403 this._elem.css({'height':dim+'px', left:'0px', bottom:'0px'});
405 else if (this.name == 'x2axis') {
407 this._elem.css({'height':dim+'px', left:'0px', top:'0px'});
409 else if (this.name == 'yaxis') {
411 this._elem.css({'width':dim+'px', left:'0px', top:'0px'});
412 if (lshow && this._label.constructor == $.jqplot.AxisLabelRenderer) {
413 this._label._elem.css('width', w+'px');
418 this._elem.css({'width':dim+'px', right:'0px', top:'0px'});
419 if (lshow && this._label.constructor == $.jqplot.AxisLabelRenderer) {
420 this._label._elem.css('width', w+'px');
426 // called with scope of axis
427 $.jqplot.CategoryAxisRenderer.prototype.pack = function(pos, offsets) {
428 var ticks = this._ticks;
431 var offmax = offsets.max;
432 var offmin = offsets.min;
433 var lshow = (this._label == null) ? false : this._label.show;
437 this._elem.css(p, pos[p]);
440 this._offsets = offsets;
441 // pixellength will be + for x axes and - for y axes becasue pixels always measured from top left.
442 var pixellength = offmax - offmin;
443 var unitlength = max - min;
446 // point to unit and unit to point conversions references to Plot DOM element top left corner.
448 this.u2p = function(u){
449 return (u - min) * pixellength / unitlength + offmin;
452 this.p2u = function(p){
453 return (p - offmin) * unitlength / pixellength + min;
456 if (this.name == 'xaxis' || this.name == 'x2axis'){
457 this.series_u2p = function(u){
458 return (u - min) * pixellength / unitlength;
460 this.series_p2u = function(p){
461 return p * unitlength / pixellength + min;
466 this.series_u2p = function(u){
467 return (u - max) * pixellength / unitlength;
469 this.series_p2u = function(p){
470 return p * unitlength / pixellength + max;
476 // point to unit and unit to point conversions references to Plot DOM element top left corner.
478 this.u2p = function(u){
479 return offmin + (max - u) * pixellength / unitlength;
482 this.p2u = function(p){
483 return min + (p - offmin) * unitlength / pixellength;
486 if (this.name == 'xaxis' || this.name == 'x2axis'){
487 this.series_u2p = function(u){
488 return (max - u) * pixellength / unitlength;
490 this.series_p2u = function(p){
491 return p * unitlength / pixellength + max;
496 this.series_u2p = function(u){
497 return (min - u) * pixellength / unitlength;
499 this.series_p2u = function(p){
500 return p * unitlength / pixellength + min;
508 if (this.name == 'xaxis' || this.name == 'x2axis') {
509 for (i=0; i<ticks.length; i++) {
511 if (t.show && t.showLabel) {
514 if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
515 // will need to adjust auto positioning based on which axis this is.
516 var temp = (this.name == 'xaxis') ? 1 : -1;
517 switch (t.labelPosition) {
520 if (temp * t.angle < 0) {
521 shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
525 shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
529 shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
532 shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
535 shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
538 shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
543 shim = -t.getWidth()/2;
545 var val = this.u2p(t.value) + shim + 'px';
546 t._elem.css('left', val);
551 var labeledge=['bottom', 0];
553 var w = this._label._elem.outerWidth(true);
554 this._label._elem.css('left', offmin + pixellength/2 - w/2 + 'px');
555 if (this.name == 'xaxis') {
556 this._label._elem.css('bottom', '0px');
557 labeledge = ['bottom', this._label._elem.outerHeight(true)];
560 this._label._elem.css('top', '0px');
561 labeledge = ['top', this._label._elem.outerHeight(true)];
566 // draw the group labels
567 var step = parseInt(this._ticks.length/this.groups, 10);
568 for (i=0; i<this._groupLabels.length; i++) {
571 for (var j=i*step; j<=(i+1)*step; j++) {
572 if (this._ticks[j]._elem && this._ticks[j].label != " ") {
573 var t = this._ticks[j]._elem;
574 var p = t.position();
575 mid += p.left + t.outerWidth(true)/2;
580 this._groupLabels[i].css({'left':(mid - this._groupLabels[i].outerWidth(true)/2)});
581 this._groupLabels[i].css(labeledge[0], labeledge[1]);
585 for (i=0; i<ticks.length; i++) {
587 if (t.show && t.showLabel) {
589 if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
590 // will need to adjust auto positioning based on which axis this is.
591 var temp = (this.name == 'yaxis') ? 1 : -1;
592 switch (t.labelPosition) {
596 if (temp * t.angle < 0) {
597 shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
600 shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
605 shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
608 shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
612 // if (t.angle > 0) {
613 // shim = -t.getHeight()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
616 // shim = -t.getHeight()/2 - t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
618 shim = -t.getHeight()/2;
621 shim = -t.getHeight()/2;
626 shim = -t.getHeight()/2;
629 var val = this.u2p(t.value) + shim + 'px';
630 t._elem.css('top', val);
635 var labeledge=['left', 0];
637 var h = this._label._elem.outerHeight(true);
638 this._label._elem.css('top', offmax - pixellength/2 - h/2 + 'px');
639 if (this.name == 'yaxis') {
640 this._label._elem.css('left', '0px');
641 labeledge = ['left', this._label._elem.outerWidth(true)];
644 this._label._elem.css('right', '0px');
645 labeledge = ['right', this._label._elem.outerWidth(true)];
650 // draw the group labels, position top here, do left after label position.
651 var step = parseInt(this._ticks.length/this.groups, 10);
652 for (i=0; i<this._groupLabels.length; i++) {
655 for (var j=i*step; j<=(i+1)*step; j++) {
656 if (this._ticks[j]._elem && this._ticks[j].label != " ") {
657 var t = this._ticks[j]._elem;
658 var p = t.position();
659 mid += p.top + t.outerHeight()/2;
664 this._groupLabels[i].css({'top':mid - this._groupLabels[i].outerHeight()/2});
665 this._groupLabels[i].css(labeledge[0], labeledge[1]);