3 * Pure JavaScript plotting plugin using jQuery
8 * Copyright (c) 2009-2013 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 if (typeof labels[0] == "string") {
217 labels.sort(function(a,b) { return a - b; });
221 // keep a reference to these tick labels to use for redrawing plot (see bug #57)
224 // now bin the data values to the right lables.
225 for (var i=0; i<this._series.length; i++) {
226 var s = this._series[i];
227 for (var j=0; j<s.data.length; j++) {
228 if (this.name == 'xaxis' || this.name == 'x2axis') {
234 // for category axis, force the values into category bins.
235 // we should have the value in the label array now.
236 var idx = $.inArray(val, labels)+1;
237 if (this.name == 'xaxis' || this.name == 'x2axis') {
246 // adjust with blanks if we have groups
247 if (this.groups > 1 && !this._grouped) {
248 var l = labels.length;
249 var skip = parseInt(l/this.groups, 10);
251 for (var i=skip; i<l; i+=skip+1) {
254 this._grouped = true;
258 if (this.numberTicks == null) {
259 this.numberTicks = 2*numcats + 1;
262 var range = max - min;
267 // todo: adjust this so more ticks displayed.
268 var maxVisibleTicks = parseInt(3+dim/10, 10);
269 var skip = parseInt(numcats/maxVisibleTicks, 10);
271 if (this.tickInterval == null) {
273 this.tickInterval = range / (this.numberTicks-1);
276 // if tickInterval is specified, we will ignore any computed maximum.
277 for (var i=0; i<this.numberTicks; i++){
278 tt = this.min + i * this.tickInterval;
279 var t = new this.tickRenderer(this.tickOptions);
280 // if even tick, it isn't a category, it's a divider
281 if (i/2 == parseInt(i/2, 10)) {
286 if (skip>0 && track<skip) {
294 t.label = t.formatter(t.formatString, labels[(i-1)/2]);
296 t.showGridline = false;
298 t.setTick(tt, this.name);
305 // called with scope of axis
306 $.jqplot.CategoryAxisRenderer.prototype.draw = function(ctx, plot) {
308 // populate the axis label and value properties.
309 // createTicks is a method on the renderer, but
310 // call it within the scope of the axis.
311 this.renderer.createTicks.call(this);
312 // fill a div with axes labels in the right direction.
313 // Need to pregenerate each axis to get its bounds and
314 // position it and the labels correctly on the plot.
317 // Added for theming.
319 // this._elem.empty();
320 // Memory Leaks patch
321 this._elem.emptyForce();
324 this._elem = this._elem || $('<div class="jqplot-axis jqplot-'+this.name+'" style="position:absolute;"></div>');
326 if (this.name == 'xaxis' || this.name == 'x2axis') {
327 this._elem.width(this._plotDimensions.width);
330 this._elem.height(this._plotDimensions.height);
333 // create a _label object.
334 this.labelOptions.axis = this.name;
335 this._label = new this.labelRenderer(this.labelOptions);
336 if (this._label.show) {
337 var elem = this._label.draw(ctx, plot);
338 elem.appendTo(this._elem);
342 for (var i=0; i<t.length; i++) {
344 if (tick.showLabel && (!tick.isMinorTick || this.showMinorTicks)) {
345 var elem = tick.draw(ctx, plot);
346 elem.appendTo(this._elem);
350 this._groupLabels = [];
351 // now make group labels
352 for (var i=0; i<this.groupLabels.length; i++)
354 var elem = $('<div style="position:absolute;" class="jqplot-'+this.name+'-groupLabel"></div>');
355 elem.html(this.groupLabels[i]);
356 this._groupLabels.push(elem);
357 elem.appendTo(this._elem);
363 // called with scope of axis
364 $.jqplot.CategoryAxisRenderer.prototype.set = function() {
369 var lshow = (this._label == null) ? false : this._label.show;
372 for (var i=0; i<t.length; i++) {
374 if (tick.showLabel && (!tick.isMinorTick || this.showMinorTicks)) {
375 if (this.name == 'xaxis' || this.name == 'x2axis') {
376 temp = tick._elem.outerHeight(true);
379 temp = tick._elem.outerWidth(true);
388 for (var i=0; i<this._groupLabels.length; i++) {
389 var l = this._groupLabels[i];
390 if (this.name == 'xaxis' || this.name == 'x2axis') {
391 temp = l.outerHeight(true);
394 temp = l.outerWidth(true);
402 w = this._label._elem.outerWidth(true);
403 h = this._label._elem.outerHeight(true);
405 if (this.name == 'xaxis') {
407 this._elem.css({'height':dim+'px', left:'0px', bottom:'0px'});
409 else if (this.name == 'x2axis') {
411 this._elem.css({'height':dim+'px', left:'0px', top:'0px'});
413 else if (this.name == 'yaxis') {
415 this._elem.css({'width':dim+'px', left:'0px', top:'0px'});
416 if (lshow && this._label.constructor == $.jqplot.AxisLabelRenderer) {
417 this._label._elem.css('width', w+'px');
422 this._elem.css({'width':dim+'px', right:'0px', top:'0px'});
423 if (lshow && this._label.constructor == $.jqplot.AxisLabelRenderer) {
424 this._label._elem.css('width', w+'px');
430 // called with scope of axis
431 $.jqplot.CategoryAxisRenderer.prototype.pack = function(pos, offsets) {
432 var ticks = this._ticks;
435 var offmax = offsets.max;
436 var offmin = offsets.min;
437 var lshow = (this._label == null) ? false : this._label.show;
441 this._elem.css(p, pos[p]);
444 this._offsets = offsets;
445 // pixellength will be + for x axes and - for y axes becasue pixels always measured from top left.
446 var pixellength = offmax - offmin;
447 var unitlength = max - min;
450 // point to unit and unit to point conversions references to Plot DOM element top left corner.
452 this.u2p = function(u){
453 return (u - min) * pixellength / unitlength + offmin;
456 this.p2u = function(p){
457 return (p - offmin) * unitlength / pixellength + min;
460 if (this.name == 'xaxis' || this.name == 'x2axis'){
461 this.series_u2p = function(u){
462 return (u - min) * pixellength / unitlength;
464 this.series_p2u = function(p){
465 return p * unitlength / pixellength + min;
470 this.series_u2p = function(u){
471 return (u - max) * pixellength / unitlength;
473 this.series_p2u = function(p){
474 return p * unitlength / pixellength + max;
480 // point to unit and unit to point conversions references to Plot DOM element top left corner.
482 this.u2p = function(u){
483 return offmin + (max - u) * pixellength / unitlength;
486 this.p2u = function(p){
487 return min + (p - offmin) * unitlength / pixellength;
490 if (this.name == 'xaxis' || this.name == 'x2axis'){
491 this.series_u2p = function(u){
492 return (max - u) * pixellength / unitlength;
494 this.series_p2u = function(p){
495 return p * unitlength / pixellength + max;
500 this.series_u2p = function(u){
501 return (min - u) * pixellength / unitlength;
503 this.series_p2u = function(p){
504 return p * unitlength / pixellength + min;
512 if (this.name == 'xaxis' || this.name == 'x2axis') {
513 for (i=0; i<ticks.length; i++) {
515 if (t.show && t.showLabel) {
518 if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
519 // will need to adjust auto positioning based on which axis this is.
520 var temp = (this.name == 'xaxis') ? 1 : -1;
521 switch (t.labelPosition) {
524 if (temp * t.angle < 0) {
525 shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
529 shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
533 shim = -t.getWidth() + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
536 shim = -t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
539 shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
542 shim = -t.getWidth()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
547 shim = -t.getWidth()/2;
549 var val = this.u2p(t.value) + shim + 'px';
550 t._elem.css('left', val);
555 var labeledge=['bottom', 0];
557 var w = this._label._elem.outerWidth(true);
558 this._label._elem.css('left', offmin + pixellength/2 - w/2 + 'px');
559 if (this.name == 'xaxis') {
560 this._label._elem.css('bottom', '0px');
561 labeledge = ['bottom', this._label._elem.outerHeight(true)];
564 this._label._elem.css('top', '0px');
565 labeledge = ['top', this._label._elem.outerHeight(true)];
570 // draw the group labels
571 var step = parseInt(this._ticks.length/this.groups, 10) + 1;
572 for (i=0; i<this._groupLabels.length; i++) {
575 for (var j=i*step; j<(i+1)*step; j++) {
576 if (j >= this._ticks.length-1) continue; // the last tick does not exist as there is no other group in order to have an empty one.
577 if (this._ticks[j]._elem && this._ticks[j].label != " ") {
578 var t = this._ticks[j]._elem;
579 var p = t.position();
580 mid += p.left + t.outerWidth(true)/2;
585 this._groupLabels[i].css({'left':(mid - this._groupLabels[i].outerWidth(true)/2)});
586 this._groupLabels[i].css(labeledge[0], labeledge[1]);
590 for (i=0; i<ticks.length; i++) {
592 if (t.show && t.showLabel) {
594 if (t.constructor == $.jqplot.CanvasAxisTickRenderer && t.angle) {
595 // will need to adjust auto positioning based on which axis this is.
596 var temp = (this.name == 'yaxis') ? 1 : -1;
597 switch (t.labelPosition) {
601 if (temp * t.angle < 0) {
602 shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
605 shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
610 shim = -t._textRenderer.height * Math.cos(-t._textRenderer.angle) / 2;
613 shim = -t.getHeight() + t._textRenderer.height * Math.cos(t._textRenderer.angle) / 2;
617 // if (t.angle > 0) {
618 // shim = -t.getHeight()/2 + t._textRenderer.height * Math.sin(-t._textRenderer.angle) / 2;
621 // shim = -t.getHeight()/2 - t._textRenderer.height * Math.sin(t._textRenderer.angle) / 2;
623 shim = -t.getHeight()/2;
626 shim = -t.getHeight()/2;
631 shim = -t.getHeight()/2;
634 var val = this.u2p(t.value) + shim + 'px';
635 t._elem.css('top', val);
640 var labeledge=['left', 0];
642 var h = this._label._elem.outerHeight(true);
643 this._label._elem.css('top', offmax - pixellength/2 - h/2 + 'px');
644 if (this.name == 'yaxis') {
645 this._label._elem.css('left', '0px');
646 labeledge = ['left', this._label._elem.outerWidth(true)];
649 this._label._elem.css('right', '0px');
650 labeledge = ['right', this._label._elem.outerWidth(true)];
655 // draw the group labels, position top here, do left after label position.
656 var step = parseInt(this._ticks.length/this.groups, 10) + 1; // step is one more than before as we don't want to have overlaps in loops
657 for (i=0; i<this._groupLabels.length; i++) {
660 for (var j=i*step; j<(i+1)*step; j++) { // j must never reach (i+1)*step as we don't want to have overlap between loops
661 if (j >= this._ticks.length-1) continue; // the last tick does not exist as there is no other group in order to have an empty one.
662 if (this._ticks[j]._elem && this._ticks[j].label != " ") {
663 var t = this._ticks[j]._elem;
664 var p = t.position();
665 mid += p.top + t.outerHeight()/2;
670 this._groupLabels[i].css({'top':mid - this._groupLabels[i].outerHeight()/2});
671 this._groupLabels[i].css(labeledge[0], labeledge[1]);