[9062] | 1 | /** |
---|
| 2 | * jqPlot |
---|
| 3 | * Pure JavaScript plotting plugin using jQuery |
---|
| 4 | * |
---|
| 5 | * Version: @VERSION |
---|
| 6 | * |
---|
| 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. |
---|
| 12 | * |
---|
| 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 . |
---|
| 16 | * |
---|
| 17 | * If you are feeling kind and generous, consider supporting the project by |
---|
| 18 | * making a donation at: http://www.jqplot.com/donate.php . |
---|
| 19 | * |
---|
| 20 | * sprintf functions contained in jqplot.sprintf.js by Ash Searle: |
---|
| 21 | * |
---|
| 22 | * version 2007.04.27 |
---|
| 23 | * author 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." |
---|
| 28 | * |
---|
| 29 | */ |
---|
| 30 | (function($) { |
---|
| 31 | /** |
---|
| 32 | * The following code was generaously given to me a while back by Scott Prahl. |
---|
| 33 | * He did a good job at computing axes min, max and number of ticks for the |
---|
| 34 | * case where the user has not set any scale related parameters (tickInterval, |
---|
| 35 | * numberTicks, min or max). I had ignored this use case for a long time, |
---|
| 36 | * focusing on the more difficult case where user has set some option controlling |
---|
| 37 | * tick generation. Anyway, about time I got this into jqPlot. |
---|
| 38 | * Thanks Scott!! |
---|
| 39 | */ |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * Copyright (c) 2010 Scott Prahl |
---|
| 43 | * The next three routines are currently available for use in all personal |
---|
| 44 | * or commercial projects under both the MIT and GPL version 2.0 licenses. |
---|
| 45 | * This means that you can choose the license that best suits your project |
---|
| 46 | * and use it accordingly. |
---|
| 47 | */ |
---|
| 48 | |
---|
| 49 | // A good format string depends on the interval. If the interval is greater |
---|
| 50 | // than 1 then there is no need to show any decimal digits. If it is < 1.0, then |
---|
| 51 | // use the magnitude of the interval to determine the number of digits to show. |
---|
| 52 | function bestFormatString (interval) |
---|
| 53 | { |
---|
| 54 | interval = Math.abs(interval); |
---|
| 55 | if (interval > 1) {return '%d';} |
---|
| 56 | |
---|
| 57 | var expv = -Math.floor(Math.log(interval)/Math.LN10); |
---|
| 58 | return '%.' + expv + 'f'; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | // This will return an interval of form 2 * 10^n, 5 * 10^n or 10 * 10^n |
---|
| 62 | function bestLinearInterval(range, scalefact) { |
---|
| 63 | var expv = Math.floor(Math.log(range)/Math.LN10); |
---|
| 64 | var magnitude = Math.pow(10, expv); |
---|
| 65 | // 0 < f < 10 |
---|
| 66 | var f = range / magnitude; |
---|
| 67 | // console.log('f: %s, scaled: %s ', f, f/scalefact); |
---|
| 68 | // for large plots, scalefact will decrease f and increase number of ticks. |
---|
| 69 | // for small plots, scalefact will increase f and decrease number of ticks. |
---|
| 70 | f = f/scalefact; |
---|
| 71 | |
---|
| 72 | // for large plots, smaller interval, more ticks. |
---|
| 73 | if (f<=0.38) {return 0.1*magnitude;} |
---|
| 74 | if (f<=1.6) {return 0.2*magnitude;} |
---|
| 75 | if (f<=4.0) {return 0.5*magnitude;} |
---|
| 76 | if (f<=8.0) {return magnitude;} |
---|
| 77 | // for very small plots, larger interval, less ticks in number ticks |
---|
| 78 | if (f<=16.0) {return 2*magnitude;} |
---|
| 79 | return 5*magnitude; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | // Given the min and max for a dataset, return suitable endpoints |
---|
| 83 | // for the graphing, a good number for the number of ticks, and a |
---|
| 84 | // format string so that extraneous digits are not displayed. |
---|
| 85 | // returned is an array containing [min, max, nTicks, format] |
---|
| 86 | $.jqplot.LinearTickGenerator = function(axis_min, axis_max, scalefact) { |
---|
| 87 | // if endpoints are equal try to include zero otherwise include one |
---|
| 88 | if (axis_min == axis_max) { |
---|
| 89 | axis_max = (axis_max) ? 0 : 1; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | scalefact = scalefact || 1.0; |
---|
| 93 | |
---|
| 94 | // make sure range is positive |
---|
| 95 | if (axis_max < axis_min) { |
---|
| 96 | var a = axis_max; |
---|
| 97 | axis_max = axis_min; |
---|
| 98 | axis_min = a; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | var ss = bestLinearInterval(axis_max - axis_min, scalefact); |
---|
| 102 | var r = []; |
---|
| 103 | |
---|
| 104 | // Figure out the axis min, max and number of ticks |
---|
| 105 | // the min and max will be some multiple of the tick interval, |
---|
| 106 | // 1*10^n, 2*10^n or 5*10^n. This gaurantees that, if the |
---|
| 107 | // axis min is negative, 0 will be a tick. |
---|
| 108 | r[0] = Math.floor(axis_min / ss) * ss; // min |
---|
| 109 | r[1] = Math.ceil(axis_max / ss) * ss; // max |
---|
| 110 | r[2] = Math.round((r[1]-r[0])/ss+1.0); // number of ticks |
---|
| 111 | r[3] = bestFormatString(ss); // format string |
---|
| 112 | r[4] = ss; // tick Interval |
---|
| 113 | return r; |
---|
| 114 | }; |
---|
| 115 | |
---|
| 116 | })(jQuery); |
---|