jqplot.meterGaugeRenderer.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /**
  2. * jqPlot
  3. * Pure JavaScript plotting plugin using jQuery
  4. *
  5. * Version: @VERSION
  6. * Revision: @REVISION
  7. *
  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.
  13. *
  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 .
  17. *
  18. * If you are feeling kind and generous, consider supporting the project by
  19. * making a donation at: http://www.jqplot.com/donate.php .
  20. *
  21. * sprintf functions contained in jqplot.sprintf.js by Ash Searle:
  22. *
  23. * version 2007.04.27
  24. * author 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."
  29. *
  30. */
  31. (function($) {
  32. /**
  33. * Class: $.jqplot.MeterGaugeRenderer
  34. * Plugin renderer to draw a meter gauge chart.
  35. *
  36. * Data consists of a single series with 1 data point to position the gauge needle.
  37. *
  38. * To use this renderer, you need to include the
  39. * meter gauge renderer plugin, for example:
  40. *
  41. * > <script type="text/javascript" src="plugins/jqplot.meterGaugeRenderer.js"></script>
  42. *
  43. * Properties described here are passed into the $.jqplot function
  44. * as options on the series renderer. For example:
  45. *
  46. * > plot0 = $.jqplot('chart0',[[18]],{
  47. * > title: 'Network Speed',
  48. * > seriesDefaults: {
  49. * > renderer: $.jqplot.MeterGaugeRenderer,
  50. * > rendererOptions: {
  51. * > label: 'MB/s'
  52. * > }
  53. * > }
  54. * > });
  55. *
  56. * A meterGauge plot does not support events.
  57. */
  58. $.jqplot.MeterGaugeRenderer = function(){
  59. $.jqplot.LineRenderer.call(this);
  60. };
  61. $.jqplot.MeterGaugeRenderer.prototype = new $.jqplot.LineRenderer();
  62. $.jqplot.MeterGaugeRenderer.prototype.constructor = $.jqplot.MeterGaugeRenderer;
  63. // called with scope of a series
  64. $.jqplot.MeterGaugeRenderer.prototype.init = function(options) {
  65. // Group: Properties
  66. //
  67. // prop: diameter
  68. // Outer diameter of the meterGauge, auto computed by default
  69. this.diameter = null;
  70. // prop: padding
  71. // padding between the meterGauge and plot edges, auto
  72. // calculated by default.
  73. this.padding = null;
  74. // prop: shadowOffset
  75. // offset of the shadow from the gauge ring and offset of
  76. // each succesive stroke of the shadow from the last.
  77. this.shadowOffset = 2;
  78. // prop: shadowAlpha
  79. // transparency of the shadow (0 = transparent, 1 = opaque)
  80. this.shadowAlpha = 0.07;
  81. // prop: shadowDepth
  82. // number of strokes to apply to the shadow,
  83. // each stroke offset shadowOffset from the last.
  84. this.shadowDepth = 4;
  85. // prop: background
  86. // background color of the inside of the gauge.
  87. this.background = "#efefef";
  88. // prop: ringColor
  89. // color of the outer ring, hub, and needle of the gauge.
  90. this.ringColor = "#BBC6D0";
  91. // needle color not implemented yet.
  92. this.needleColor = "#C3D3E5";
  93. // prop: tickColor
  94. // color of the tick marks around the gauge.
  95. this.tickColor = "#989898";
  96. // prop: ringWidth
  97. // width of the ring around the gauge. Auto computed by default.
  98. this.ringWidth = null;
  99. // prop: min
  100. // Minimum value on the gauge. Auto computed by default
  101. this.min;
  102. // prop: max
  103. // Maximum value on the gauge. Auto computed by default
  104. this.max;
  105. // prop: ticks
  106. // Array of tick values. Auto computed by default.
  107. this.ticks = [];
  108. // prop: showTicks
  109. // true to show ticks around gauge.
  110. this.showTicks = true;
  111. // prop: showTickLabels
  112. // true to show tick labels next to ticks.
  113. this.showTickLabels = true;
  114. // prop: label
  115. // A gauge label like 'kph' or 'Volts'
  116. this.label = null;
  117. // prop: labelHeightAdjust
  118. // Number of Pixels to offset the label up (-) or down (+) from its default position.
  119. this.labelHeightAdjust = 0;
  120. // prop: labelPosition
  121. // Where to position the label, either 'inside' or 'bottom'.
  122. this.labelPosition = 'inside';
  123. // prop: intervals
  124. // Array of ranges to be drawn around the gauge.
  125. // Array of form:
  126. // > [value1, value2, ...]
  127. // indicating the values for the first, second, ... intervals.
  128. this.intervals = [];
  129. // prop: intervalColors
  130. // Array of colors to use for the intervals.
  131. this.intervalColors = [ "#4bb2c5", "#EAA228", "#c5b47f", "#579575", "#839557", "#958c12", "#953579", "#4b5de4", "#d8b83f", "#ff5800", "#0085cc", "#c747a3", "#cddf54", "#FBD178", "#26B4E3", "#bd70c7"];
  132. // prop: intervalInnerRadius
  133. // Radius of the inner circle of the interval ring.
  134. this.intervalInnerRadius = null;
  135. // prop: intervalOuterRadius
  136. // Radius of the outer circle of the interval ring.
  137. this.intervalOuterRadius = null;
  138. this.tickRenderer = $.jqplot.MeterGaugeTickRenderer;
  139. // ticks spaced every 1, 2, 2.5, 5, 10, 20, .1, .2, .25, .5, etc.
  140. this.tickPositions = [1, 2, 2.5, 5, 10];
  141. // prop: tickSpacing
  142. // Degrees between ticks. This is a target number, if
  143. // incompatible span and ticks are supplied, a suitable
  144. // spacing close to this value will be computed.
  145. this.tickSpacing = 30;
  146. this.numberMinorTicks = null;
  147. // prop: hubRadius
  148. // Radius of the hub at the bottom center of gauge which the needle attaches to.
  149. // Auto computed by default
  150. this.hubRadius = null;
  151. // prop: tickPadding
  152. // padding of the tick marks to the outer ring and the tick labels to marks.
  153. // Auto computed by default.
  154. this.tickPadding = null;
  155. // prop: needleThickness
  156. // Maximum thickness the needle. Auto computed by default.
  157. this.needleThickness = null;
  158. // prop: needlePad
  159. // Padding between needle and inner edge of the ring when the needle is at the min or max gauge value.
  160. this.needlePad = 6;
  161. // prop: pegNeedle
  162. // True will stop needle just below/above the min/max values if data is below/above min/max,
  163. // as if the meter is "pegged".
  164. this.pegNeedle = true;
  165. this._type = 'meterGauge';
  166. $.extend(true, this, options);
  167. this.type = null;
  168. this.numberTicks = null;
  169. this.tickInterval = null;
  170. // span, the sweep (in degrees) from min to max. This gauge is
  171. // a semi-circle.
  172. this.span = 180;
  173. // get rid of this nonsense
  174. // this.innerSpan = this.span;
  175. if (this.type == 'circular') {
  176. this.semiCircular = false;
  177. }
  178. else if (this.type != 'circular') {
  179. this.semiCircular = true;
  180. }
  181. else {
  182. this.semiCircular = (this.span <= 180) ? true : false;
  183. }
  184. this._tickPoints = [];
  185. // reference to label element.
  186. this._labelElem = null;
  187. // start the gauge at the beginning of the span
  188. this.startAngle = (90 + (360 - this.span)/2) * Math.PI/180;
  189. this.endAngle = (90 - (360 - this.span)/2) * Math.PI/180;
  190. this.setmin = !!(this.min == null);
  191. this.setmax = !!(this.max == null);
  192. // if given intervals and is an array of values, create labels and colors.
  193. if (this.intervals.length) {
  194. if (this.intervals[0].length == null || this.intervals.length == 1) {
  195. for (var i=0; i<this.intervals.length; i++) {
  196. this.intervals[i] = [this.intervals[i], this.intervals[i], this.intervalColors[i]];
  197. }
  198. }
  199. else if (this.intervals[0].length == 2) {
  200. for (i=0; i<this.intervals.length; i++) {
  201. this.intervals[i] = [this.intervals[i][0], this.intervals[i][1], this.intervalColors[i]];
  202. }
  203. }
  204. }
  205. // compute min, max and ticks if not supplied:
  206. if (this.ticks.length) {
  207. if (this.ticks[0].length == null || this.ticks[0].length == 1) {
  208. for (var i=0; i<this.ticks.length; i++) {
  209. this.ticks[i] = [this.ticks[i], this.ticks[i]];
  210. }
  211. }
  212. this.min = (this.min == null) ? this.ticks[0][0] : this.min;
  213. this.max = (this.max == null) ? this.ticks[this.ticks.length-1][0] : this.max;
  214. this.setmin = false;
  215. this.setmax = false;
  216. this.numberTicks = this.ticks.length;
  217. this.tickInterval = this.ticks[1][0] - this.ticks[0][0];
  218. this.tickFactor = Math.floor(parseFloat((Math.log(this.tickInterval)/Math.log(10)).toFixed(11)));
  219. // use the first interal to calculate minor ticks;
  220. this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor);
  221. if (!this.numberMinorTicks) {
  222. this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor-1);
  223. }
  224. if (!this.numberMinorTicks) {
  225. this.numberMinorTicks = 1;
  226. }
  227. }
  228. else if (this.intervals.length) {
  229. this.min = (this.min == null) ? 0 : this.min;
  230. this.setmin = false;
  231. if (this.max == null) {
  232. if (this.intervals[this.intervals.length-1][0] >= this.data[0][1]) {
  233. this.max = this.intervals[this.intervals.length-1][0];
  234. this.setmax = false;
  235. }
  236. }
  237. else {
  238. this.setmax = false;
  239. }
  240. }
  241. else {
  242. // no ticks and no intervals supplied, put needle in middle
  243. this.min = (this.min == null) ? 0 : this.min;
  244. this.setmin = false;
  245. if (this.max == null) {
  246. this.max = this.data[0][1] * 1.25;
  247. this.setmax = true;
  248. }
  249. else {
  250. this.setmax = false;
  251. }
  252. }
  253. };
  254. $.jqplot.MeterGaugeRenderer.prototype.setGridData = function(plot) {
  255. // set gridData property. This will hold angle in radians of each data point.
  256. var stack = [];
  257. var td = [];
  258. var sa = this.startAngle;
  259. for (var i=0; i<this.data.length; i++){
  260. stack.push(this.data[i][1]);
  261. td.push([this.data[i][0]]);
  262. if (i>0) {
  263. stack[i] += stack[i-1];
  264. }
  265. }
  266. var fact = Math.PI*2/stack[stack.length - 1];
  267. for (var i=0; i<stack.length; i++) {
  268. td[i][1] = stack[i] * fact;
  269. }
  270. this.gridData = td;
  271. };
  272. $.jqplot.MeterGaugeRenderer.prototype.makeGridData = function(data, plot) {
  273. var stack = [];
  274. var td = [];
  275. var sa = this.startAngle;
  276. for (var i=0; i<data.length; i++){
  277. stack.push(data[i][1]);
  278. td.push([data[i][0]]);
  279. if (i>0) {
  280. stack[i] += stack[i-1];
  281. }
  282. }
  283. var fact = Math.PI*2/stack[stack.length - 1];
  284. for (var i=0; i<stack.length; i++) {
  285. td[i][1] = stack[i] * fact;
  286. }
  287. return td;
  288. };
  289. function getnmt(pos, interval, fact) {
  290. var temp;
  291. for (var i=pos.length-1; i>=0; i--) {
  292. temp = interval/(pos[i] * Math.pow(10, fact));
  293. if (temp == 4 || temp == 5) {
  294. return temp - 1;
  295. }
  296. }
  297. return null;
  298. }
  299. // called with scope of series
  300. $.jqplot.MeterGaugeRenderer.prototype.draw = function (ctx, gd, options) {
  301. var i;
  302. var opts = (options != undefined) ? options : {};
  303. // offset and direction of offset due to legend placement
  304. var offx = 0;
  305. var offy = 0;
  306. var trans = 1;
  307. if (options.legendInfo && options.legendInfo.placement == 'inside') {
  308. var li = options.legendInfo;
  309. switch (li.location) {
  310. case 'nw':
  311. offx = li.width + li.xoffset;
  312. break;
  313. case 'w':
  314. offx = li.width + li.xoffset;
  315. break;
  316. case 'sw':
  317. offx = li.width + li.xoffset;
  318. break;
  319. case 'ne':
  320. offx = li.width + li.xoffset;
  321. trans = -1;
  322. break;
  323. case 'e':
  324. offx = li.width + li.xoffset;
  325. trans = -1;
  326. break;
  327. case 'se':
  328. offx = li.width + li.xoffset;
  329. trans = -1;
  330. break;
  331. case 'n':
  332. offy = li.height + li.yoffset;
  333. break;
  334. case 's':
  335. offy = li.height + li.yoffset;
  336. trans = -1;
  337. break;
  338. default:
  339. break;
  340. }
  341. }
  342. // pre-draw so can get its dimensions.
  343. if (this.label) {
  344. this._labelElem = $('<div class="jqplot-meterGauge-label" style="position:absolute;">'+this.label+'</div>');
  345. this.canvas._elem.after(this._labelElem);
  346. }
  347. var shadow = (opts.shadow != undefined) ? opts.shadow : this.shadow;
  348. var showLine = (opts.showLine != undefined) ? opts.showLine : this.showLine;
  349. var fill = (opts.fill != undefined) ? opts.fill : this.fill;
  350. var cw = ctx.canvas.width;
  351. var ch = ctx.canvas.height;
  352. if (this.padding == null) {
  353. this.padding = Math.round(Math.min(cw, ch)/30);
  354. }
  355. var w = cw - offx - 2 * this.padding;
  356. var h = ch - offy - 2 * this.padding;
  357. if (this.labelPosition == 'bottom' && this.label) {
  358. h -= this._labelElem.outerHeight(true);
  359. }
  360. var mindim = Math.min(w,h);
  361. var d = mindim;
  362. if (!this.diameter) {
  363. if (this.semiCircular) {
  364. if ( w >= 2*h) {
  365. if (!this.ringWidth) {
  366. this.ringWidth = 2*h/35;
  367. }
  368. this.needleThickness = this.needleThickness || 2+Math.pow(this.ringWidth, 0.8);
  369. this.innerPad = this.ringWidth/2 + this.needleThickness/2 + this.needlePad;
  370. this.diameter = 2 * (h - 2*this.innerPad);
  371. }
  372. else {
  373. if (!this.ringWidth) {
  374. this.ringWidth = w/35;
  375. }
  376. this.needleThickness = this.needleThickness || 2+Math.pow(this.ringWidth, 0.8);
  377. this.innerPad = this.ringWidth/2 + this.needleThickness/2 + this.needlePad;
  378. this.diameter = w - 2*this.innerPad - this.ringWidth - this.padding;
  379. }
  380. // center taking into account legend and over draw for gauge bottom below hub.
  381. // this will be center of hub.
  382. this._center = [(cw - trans * offx)/2 + trans * offx, (ch + trans*offy - this.padding - this.ringWidth - this.innerPad)];
  383. }
  384. else {
  385. if (!this.ringWidth) {
  386. this.ringWidth = d/35;
  387. }
  388. this.needleThickness = this.needleThickness || 2+Math.pow(this.ringWidth, 0.8);
  389. this.innerPad = 0;
  390. this.diameter = d - this.ringWidth;
  391. // center in middle of canvas taking into account legend.
  392. // will be center of hub.
  393. this._center = [(cw-trans*offx)/2 + trans * offx, (ch-trans*offy)/2 + trans * offy];
  394. }
  395. if (this._labelElem && this.labelPosition == 'bottom') {
  396. this._center[1] -= this._labelElem.outerHeight(true);
  397. }
  398. }
  399. this._radius = this.diameter/2;
  400. this.tickSpacing = 6000/this.diameter;
  401. if (!this.hubRadius) {
  402. this.hubRadius = this.diameter/18;
  403. }
  404. this.shadowOffset = 0.5 + this.ringWidth/9;
  405. this.shadowWidth = this.ringWidth*1;
  406. this.tickPadding = 3 + Math.pow(this.diameter/20, 0.7);
  407. this.tickOuterRadius = this._radius - this.ringWidth/2 - this.tickPadding;
  408. this.tickLength = (this.showTicks) ? this._radius/13 : 0;
  409. if (this.ticks.length == 0) {
  410. // no ticks, lets make some.
  411. var max = this.max,
  412. min = this.min,
  413. setmax = this.setmax,
  414. setmin = this.setmin,
  415. ti = (max - min) * this.tickSpacing / this.span;
  416. var tf = Math.floor(parseFloat((Math.log(ti)/Math.log(10)).toFixed(11)));
  417. var tp = (ti/Math.pow(10, tf));
  418. (tp > 2 && tp <= 2.5) ? tp = 2.5 : tp = Math.ceil(tp);
  419. var t = this.tickPositions;
  420. var tpindex, nt;
  421. for (i=0; i<t.length; i++) {
  422. if (tp == t[i] || i && t[i-1] < tp && tp < t[i]) {
  423. ti = t[i]*Math.pow(10, tf);
  424. tpindex = i;
  425. }
  426. }
  427. for (i=0; i<t.length; i++) {
  428. if (tp == t[i] || i && t[i-1] < tp && tp < t[i]) {
  429. ti = t[i]*Math.pow(10, tf);
  430. nt = Math.ceil((max - min) / ti);
  431. }
  432. }
  433. // both max and min are free
  434. if (setmax && setmin) {
  435. var tmin = (min > 0) ? min - min % ti : min - min % ti - ti;
  436. if (!this.forceZero) {
  437. var diff = Math.min(min - tmin, 0.8*ti);
  438. var ntp = Math.floor(diff/t[tpindex]);
  439. if (ntp > 1) {
  440. tmin = tmin + t[tpindex] * (ntp-1);
  441. if (parseInt(tmin, 10) != tmin && parseInt(tmin-t[tpindex], 10) == tmin-t[tpindex]) {
  442. tmin = tmin - t[tpindex];
  443. }
  444. }
  445. }
  446. if (min == tmin) {
  447. min -= ti;
  448. }
  449. else {
  450. // tmin should always be lower than dataMin
  451. if (min - tmin > 0.23*ti) {
  452. min = tmin;
  453. }
  454. else {
  455. min = tmin -ti;
  456. nt += 1;
  457. }
  458. }
  459. nt += 1;
  460. var tmax = min + (nt - 1) * ti;
  461. if (max >= tmax) {
  462. tmax += ti;
  463. nt += 1;
  464. }
  465. // now tmax should always be mroe than dataMax
  466. if (tmax - max < 0.23*ti) {
  467. tmax += ti;
  468. nt += 1;
  469. }
  470. this.max = max = tmax;
  471. this.min = min;
  472. this.tickInterval = ti;
  473. this.numberTicks = nt;
  474. var it;
  475. for (i=0; i<nt; i++) {
  476. it = parseFloat((min+i*ti).toFixed(11));
  477. this.ticks.push([it, it]);
  478. }
  479. this.max = this.ticks[nt-1][1];
  480. this.tickFactor = tf;
  481. // determine number of minor ticks
  482. this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor);
  483. if (!this.numberMinorTicks) {
  484. this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor-1);
  485. }
  486. }
  487. // max is free, min is fixed
  488. else if (setmax) {
  489. var tmax = min + (nt - 1) * ti;
  490. if (max >= tmax) {
  491. max = tmax + ti;
  492. nt += 1;
  493. }
  494. else {
  495. max = tmax;
  496. }
  497. this.tickInterval = this.tickInterval || ti;
  498. this.numberTicks = this.numberTicks || nt;
  499. var it;
  500. for (i=0; i<this.numberTicks; i++) {
  501. it = parseFloat((min+i*this.tickInterval).toFixed(11));
  502. this.ticks.push([it, it]);
  503. }
  504. this.max = this.ticks[this.numberTicks-1][1];
  505. this.tickFactor = tf;
  506. // determine number of minor ticks
  507. this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor);
  508. if (!this.numberMinorTicks) {
  509. this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor-1);
  510. }
  511. }
  512. // not setting max or min
  513. if (!setmax && !setmin) {
  514. var range = this.max - this.min;
  515. tf = Math.floor(parseFloat((Math.log(range)/Math.log(10)).toFixed(11))) - 1;
  516. var nticks = [5,6,4,7,3,8,9,10,2], res, numticks, nonSigDigits=0, sigRange;
  517. // check to see how many zeros are at the end of the range
  518. if (range > 1) {
  519. var rstr = String(range);
  520. if (rstr.search(/\./) == -1) {
  521. var pos = rstr.search(/0+$/);
  522. nonSigDigits = (pos > 0) ? rstr.length - pos - 1 : 0;
  523. }
  524. }
  525. sigRange = range/Math.pow(10, nonSigDigits);
  526. for (i=0; i<nticks.length; i++) {
  527. res = sigRange/(nticks[i]-1);
  528. if (res == parseInt(res, 10)) {
  529. this.numberTicks = nticks[i];
  530. this.tickInterval = range/(this.numberTicks-1);
  531. this.tickFactor = tf+1;
  532. break;
  533. }
  534. }
  535. var it;
  536. for (i=0; i<this.numberTicks; i++) {
  537. it = parseFloat((this.min+i*this.tickInterval).toFixed(11));
  538. this.ticks.push([it, it]);
  539. }
  540. // determine number of minor ticks
  541. this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor);
  542. if (!this.numberMinorTicks) {
  543. this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor-1);
  544. }
  545. if (!this.numberMinorTicks) {
  546. this.numberMinorTicks = 1;
  547. var nums = [4, 5, 3, 6, 2];
  548. for (i=0; i<5; i++) {
  549. var temp = this.tickInterval/nums[i];
  550. if (temp == parseInt(temp, 10)) {
  551. this.numberMinorTicks = nums[i]-1;
  552. break;
  553. }
  554. }
  555. }
  556. }
  557. }
  558. var r = this._radius,
  559. sa = this.startAngle,
  560. ea = this.endAngle,
  561. pi = Math.PI,
  562. hpi = Math.PI/2;
  563. if (this.semiCircular) {
  564. var overAngle = Math.atan(this.innerPad/r),
  565. outersa = this.outerStartAngle = sa - overAngle,
  566. outerea = this.outerEndAngle = ea + overAngle,
  567. hubsa = this.hubStartAngle = sa - Math.atan(this.innerPad/this.hubRadius*2),
  568. hubea = this.hubEndAngle = ea + Math.atan(this.innerPad/this.hubRadius*2);
  569. ctx.save();
  570. ctx.translate(this._center[0], this._center[1]);
  571. ctx.lineJoin = "round";
  572. ctx.lineCap = "round";
  573. // draw the innerbackground
  574. ctx.save();
  575. ctx.beginPath();
  576. ctx.fillStyle = this.background;
  577. ctx.arc(0, 0, r, outersa, outerea, false);
  578. ctx.closePath();
  579. ctx.fill();
  580. ctx.restore();
  581. // draw the shadow
  582. // the outer ring.
  583. var shadowColor = 'rgba(0,0,0,'+this.shadowAlpha+')';
  584. ctx.save();
  585. for (var i=0; i<this.shadowDepth; i++) {
  586. ctx.translate(this.shadowOffset*Math.cos(this.shadowAngle/180*Math.PI), this.shadowOffset*Math.sin(this.shadowAngle/180*Math.PI));
  587. ctx.beginPath();
  588. ctx.strokeStyle = shadowColor;
  589. ctx.lineWidth = this.shadowWidth;
  590. ctx.arc(0 ,0, r, outersa, outerea, false);
  591. ctx.closePath();
  592. ctx.stroke();
  593. }
  594. ctx.restore();
  595. // the inner hub.
  596. ctx.save();
  597. var tempd = parseInt((this.shadowDepth+1)/2, 10);
  598. for (var i=0; i<tempd; i++) {
  599. ctx.translate(this.shadowOffset*Math.cos(this.shadowAngle/180*Math.PI), this.shadowOffset*Math.sin(this.shadowAngle/180*Math.PI));
  600. ctx.beginPath();
  601. ctx.fillStyle = shadowColor;
  602. ctx.arc(0 ,0, this.hubRadius, hubsa, hubea, false);
  603. ctx.closePath();
  604. ctx.fill();
  605. }
  606. ctx.restore();
  607. // draw the outer ring.
  608. ctx.save();
  609. ctx.beginPath();
  610. ctx.strokeStyle = this.ringColor;
  611. ctx.lineWidth = this.ringWidth;
  612. ctx.arc(0 ,0, r, outersa, outerea, false);
  613. ctx.closePath();
  614. ctx.stroke();
  615. ctx.restore();
  616. // draw the hub
  617. ctx.save();
  618. ctx.beginPath();
  619. ctx.fillStyle = this.ringColor;
  620. ctx.arc(0 ,0, this.hubRadius,hubsa, hubea, false);
  621. ctx.closePath();
  622. ctx.fill();
  623. ctx.restore();
  624. // draw the ticks
  625. if (this.showTicks) {
  626. ctx.save();
  627. var orad = this.tickOuterRadius,
  628. tl = this.tickLength,
  629. mtl = tl/2,
  630. nmt = this.numberMinorTicks,
  631. ts = this.span * Math.PI / 180 / (this.ticks.length-1),
  632. mts = ts/(nmt + 1);
  633. for (i = 0; i<this.ticks.length; i++) {
  634. ctx.beginPath();
  635. ctx.lineWidth = 1.5 + this.diameter/360;
  636. ctx.strokeStyle = this.ringColor;
  637. var wps = ts*i+sa;
  638. ctx.moveTo(-orad * Math.cos(ts*i+sa), orad * Math.sin(ts*i+sa));
  639. ctx.lineTo(-(orad-tl) * Math.cos(ts*i+sa), (orad - tl) * Math.sin(ts*i+sa));
  640. this._tickPoints.push([(orad-tl) * Math.cos(ts*i+sa) + this._center[0] + this.canvas._offsets.left, (orad - tl) * Math.sin(ts*i+sa) + this._center[1] + this.canvas._offsets.top, ts*i+sa]);
  641. ctx.stroke();
  642. ctx.lineWidth = 1.0 + this.diameter/440;
  643. if (i<this.ticks.length-1) {
  644. for (var j=1; j<=nmt; j++) {
  645. ctx.beginPath();
  646. ctx.moveTo(-orad * Math.cos(ts*i+mts*j+sa), orad * Math.sin(ts*i+mts*j+sa));
  647. ctx.lineTo(-(orad-mtl) * Math.cos(ts*i+mts*j+sa), (orad-mtl) * Math.sin(ts*i+mts*j+sa));
  648. ctx.stroke();
  649. }
  650. }
  651. }
  652. ctx.restore();
  653. }
  654. // draw the tick labels
  655. if (this.showTickLabels) {
  656. var elem, l, t, ew, eh, dim, maxdim=0;
  657. var tp = this.tickPadding * (1 - 1/(this.diameter/80+1));
  658. for (i=0; i<this.ticks.length; i++) {
  659. elem = $('<div class="jqplot-meterGauge-tick" style="position:absolute;">'+this.ticks[i][1]+'</div>');
  660. this.canvas._elem.after(elem);
  661. ew = elem.outerWidth(true);
  662. eh = elem.outerHeight(true);
  663. l = this._tickPoints[i][0] - ew * (this._tickPoints[i][2]-Math.PI)/Math.PI - tp * Math.cos(this._tickPoints[i][2]);
  664. t = this._tickPoints[i][1] - eh/2 + eh/2 * Math.pow(Math.abs((Math.sin(this._tickPoints[i][2]))), 0.5) + tp/3 * Math.pow(Math.abs((Math.sin(this._tickPoints[i][2]))), 0.5) ;
  665. // t = this._tickPoints[i][1] - eh/2 - eh/2 * Math.sin(this._tickPoints[i][2]) - tp/2 * Math.sin(this._tickPoints[i][2]);
  666. elem.css({left:l, top:t, color: this.tickColor});
  667. dim = ew*Math.cos(this._tickPoints[i][2]) + eh*Math.sin(Math.PI/2+this._tickPoints[i][2]/2);
  668. maxdim = (dim > maxdim) ? dim : maxdim;
  669. }
  670. }
  671. // draw the gauge label
  672. if (this.label && this.labelPosition == 'inside') {
  673. var l = this._center[0] + this.canvas._offsets.left;
  674. var tp = this.tickPadding * (1 - 1/(this.diameter/80+1));
  675. var t = 0.5*(this._center[1] + this.canvas._offsets.top - this.hubRadius) + 0.5*(this._center[1] + this.canvas._offsets.top - this.tickOuterRadius + this.tickLength + tp) + this.labelHeightAdjust;
  676. // this._labelElem = $('<div class="jqplot-meterGauge-label" style="position:absolute;">'+this.label+'</div>');
  677. // this.canvas._elem.after(this._labelElem);
  678. l -= this._labelElem.outerWidth(true)/2;
  679. t -= this._labelElem.outerHeight(true)/2;
  680. this._labelElem.css({left:l, top:t});
  681. }
  682. else if (this.label && this.labelPosition == 'bottom') {
  683. var l = this._center[0] + this.canvas._offsets.left - this._labelElem.outerWidth(true)/2;
  684. var t = this._center[1] + this.canvas._offsets.top + this.innerPad + this.ringWidth + this.padding + this.labelHeightAdjust;
  685. this._labelElem.css({left:l, top:t});
  686. }
  687. // draw the intervals
  688. ctx.save();
  689. var inner = this.intervalInnerRadius || this.hubRadius * 1.5;
  690. if (this.intervalOuterRadius == null) {
  691. if (this.showTickLabels) {
  692. var outer = (this.tickOuterRadius - this.tickLength - this.tickPadding - this.diameter/8);
  693. }
  694. else {
  695. var outer = (this.tickOuterRadius - this.tickLength - this.diameter/16);
  696. }
  697. }
  698. else {
  699. var outer = this.intervalOuterRadius;
  700. }
  701. var range = this.max - this.min;
  702. var intrange = this.intervals[this.intervals.length-1] - this.min;
  703. var start, end, span = this.span*Math.PI/180;
  704. for (i=0; i<this.intervals.length; i++) {
  705. start = (i == 0) ? sa : sa + (this.intervals[i-1][0] - this.min)*span/range;
  706. if (start < 0) {
  707. start = 0;
  708. }
  709. end = sa + (this.intervals[i][0] - this.min)*span/range;
  710. if (end < 0) {
  711. end = 0;
  712. }
  713. ctx.beginPath();
  714. ctx.fillStyle = this.intervals[i][2];
  715. ctx.arc(0, 0, inner, start, end, false);
  716. ctx.lineTo(outer*Math.cos(end), outer*Math.sin(end));
  717. ctx.arc(0, 0, outer, end, start, true);
  718. ctx.lineTo(inner*Math.cos(start), inner*Math.sin(start));
  719. ctx.closePath();
  720. ctx.fill();
  721. }
  722. ctx.restore();
  723. // draw the needle
  724. var datapoint = this.data[0][1];
  725. var dataspan = this.max - this.min;
  726. if (this.pegNeedle) {
  727. if (this.data[0][1] > this.max + dataspan*3/this.span) {
  728. datapoint = this.max + dataspan*3/this.span;
  729. }
  730. if (this.data[0][1] < this.min - dataspan*3/this.span) {
  731. datapoint = this.min - dataspan*3/this.span;
  732. }
  733. }
  734. var dataang = (datapoint - this.min)/dataspan * this.span * Math.PI/180 + this.startAngle;
  735. ctx.save();
  736. ctx.beginPath();
  737. ctx.fillStyle = this.ringColor;
  738. ctx.strokeStyle = this.ringColor;
  739. this.needleLength = (this.tickOuterRadius - this.tickLength) * 0.85;
  740. this.needleThickness = (this.needleThickness < 2) ? 2 : this.needleThickness;
  741. var endwidth = this.needleThickness * 0.4;
  742. var dl = this.needleLength/10;
  743. var dt = (this.needleThickness - endwidth)/10;
  744. var templ;
  745. for (var i=0; i<10; i++) {
  746. templ = this.needleThickness - i*dt;
  747. ctx.moveTo(dl*i*Math.cos(dataang), dl*i*Math.sin(dataang));
  748. ctx.lineWidth = templ;
  749. ctx.lineTo(dl*(i+1)*Math.cos(dataang), dl*(i+1)*Math.sin(dataang));
  750. ctx.stroke();
  751. }
  752. ctx.restore();
  753. }
  754. else {
  755. this._center = [(cw - trans * offx)/2 + trans * offx, (ch - trans*offy)/2 + trans * offy];
  756. }
  757. };
  758. $.jqplot.MeterGaugeAxisRenderer = function() {
  759. $.jqplot.LinearAxisRenderer.call(this);
  760. };
  761. $.jqplot.MeterGaugeAxisRenderer.prototype = new $.jqplot.LinearAxisRenderer();
  762. $.jqplot.MeterGaugeAxisRenderer.prototype.constructor = $.jqplot.MeterGaugeAxisRenderer;
  763. // There are no traditional axes on a gauge chart. We just need to provide
  764. // dummy objects with properties so the plot will render.
  765. // called with scope of axis object.
  766. $.jqplot.MeterGaugeAxisRenderer.prototype.init = function(options){
  767. //
  768. this.tickRenderer = $.jqplot.MeterGaugeTickRenderer;
  769. $.extend(true, this, options);
  770. // I don't think I'm going to need _dataBounds here.
  771. // have to go Axis scaling in a way to fit chart onto plot area
  772. // and provide u2p and p2u functionality for mouse cursor, etc.
  773. // for convienence set _dataBounds to 0 and 100 and
  774. // set min/max to 0 and 100.
  775. this._dataBounds = {min:0, max:100};
  776. this.min = 0;
  777. this.max = 100;
  778. this.showTicks = false;
  779. this.ticks = [];
  780. this.showMark = false;
  781. this.show = false;
  782. };
  783. $.jqplot.MeterGaugeLegendRenderer = function(){
  784. $.jqplot.TableLegendRenderer.call(this);
  785. };
  786. $.jqplot.MeterGaugeLegendRenderer.prototype = new $.jqplot.TableLegendRenderer();
  787. $.jqplot.MeterGaugeLegendRenderer.prototype.constructor = $.jqplot.MeterGaugeLegendRenderer;
  788. /**
  789. * Class: $.jqplot.MeterGaugeLegendRenderer
  790. *Meter gauges don't typically have a legend, this overrides the default legend renderer.
  791. */
  792. $.jqplot.MeterGaugeLegendRenderer.prototype.init = function(options) {
  793. // Maximum number of rows in the legend. 0 or null for unlimited.
  794. this.numberRows = null;
  795. // Maximum number of columns in the legend. 0 or null for unlimited.
  796. this.numberColumns = null;
  797. $.extend(true, this, options);
  798. };
  799. // called with context of legend
  800. $.jqplot.MeterGaugeLegendRenderer.prototype.draw = function() {
  801. if (this.show) {
  802. var series = this._series;
  803. var ss = 'position:absolute;';
  804. ss += (this.background) ? 'background:'+this.background+';' : '';
  805. ss += (this.border) ? 'border:'+this.border+';' : '';
  806. ss += (this.fontSize) ? 'font-size:'+this.fontSize+';' : '';
  807. ss += (this.fontFamily) ? 'font-family:'+this.fontFamily+';' : '';
  808. ss += (this.textColor) ? 'color:'+this.textColor+';' : '';
  809. ss += (this.marginTop != null) ? 'margin-top:'+this.marginTop+';' : '';
  810. ss += (this.marginBottom != null) ? 'margin-bottom:'+this.marginBottom+';' : '';
  811. ss += (this.marginLeft != null) ? 'margin-left:'+this.marginLeft+';' : '';
  812. ss += (this.marginRight != null) ? 'margin-right:'+this.marginRight+';' : '';
  813. this._elem = $('<table class="jqplot-table-legend" style="'+ss+'"></table>');
  814. // MeterGauge charts legends don't go by number of series, but by number of data points
  815. // in the series. Refactor things here for that.
  816. var pad = false,
  817. reverse = false,
  818. nr, nc;
  819. var s = series[0];
  820. if (s.show) {
  821. var pd = s.data;
  822. if (this.numberRows) {
  823. nr = this.numberRows;
  824. if (!this.numberColumns){
  825. nc = Math.ceil(pd.length/nr);
  826. }
  827. else{
  828. nc = this.numberColumns;
  829. }
  830. }
  831. else if (this.numberColumns) {
  832. nc = this.numberColumns;
  833. nr = Math.ceil(pd.length/this.numberColumns);
  834. }
  835. else {
  836. nr = pd.length;
  837. nc = 1;
  838. }
  839. var i, j, tr, td1, td2, lt, rs, color;
  840. var idx = 0;
  841. for (i=0; i<nr; i++) {
  842. if (reverse){
  843. tr = $('<tr class="jqplot-table-legend"></tr>').prependTo(this._elem);
  844. }
  845. else{
  846. tr = $('<tr class="jqplot-table-legend"></tr>').appendTo(this._elem);
  847. }
  848. for (j=0; j<nc; j++) {
  849. if (idx < pd.length){
  850. // debugger
  851. lt = this.labels[idx] || pd[idx][0].toString();
  852. color = s.color;
  853. if (!reverse){
  854. if (i>0){
  855. pad = true;
  856. }
  857. else{
  858. pad = false;
  859. }
  860. }
  861. else{
  862. if (i == nr -1){
  863. pad = false;
  864. }
  865. else{
  866. pad = true;
  867. }
  868. }
  869. rs = (pad) ? this.rowSpacing : '0';
  870. td1 = $('<td class="jqplot-table-legend" style="text-align:center;padding-top:'+rs+';">'+
  871. '<div><div class="jqplot-table-legend-swatch" style="border-color:'+color+';"></div>'+
  872. '</div></td>');
  873. td2 = $('<td class="jqplot-table-legend" style="padding-top:'+rs+';"></td>');
  874. if (this.escapeHtml){
  875. td2.text(lt);
  876. }
  877. else {
  878. td2.html(lt);
  879. }
  880. if (reverse) {
  881. td2.prependTo(tr);
  882. td1.prependTo(tr);
  883. }
  884. else {
  885. td1.appendTo(tr);
  886. td2.appendTo(tr);
  887. }
  888. pad = true;
  889. }
  890. idx++;
  891. }
  892. }
  893. }
  894. }
  895. return this._elem;
  896. };
  897. // setup default renderers for axes and legend so user doesn't have to
  898. // called with scope of plot
  899. function preInit(target, data, options) {
  900. // debugger
  901. options = options || {};
  902. options.axesDefaults = options.axesDefaults || {};
  903. options.legend = options.legend || {};
  904. options.seriesDefaults = options.seriesDefaults || {};
  905. options.grid = options.grid || {};
  906. // only set these if there is a gauge series
  907. var setopts = false;
  908. if (options.seriesDefaults.renderer == $.jqplot.MeterGaugeRenderer) {
  909. setopts = true;
  910. }
  911. else if (options.series) {
  912. for (var i=0; i < options.series.length; i++) {
  913. if (options.series[i].renderer == $.jqplot.MeterGaugeRenderer) {
  914. setopts = true;
  915. }
  916. }
  917. }
  918. if (setopts) {
  919. options.axesDefaults.renderer = $.jqplot.MeterGaugeAxisRenderer;
  920. options.legend.renderer = $.jqplot.MeterGaugeLegendRenderer;
  921. options.legend.preDraw = true;
  922. options.grid.background = options.grid.background || 'white';
  923. options.grid.drawGridlines = false;
  924. options.grid.borderWidth = (options.grid.borderWidth != null) ? options.grid.borderWidth : 0;
  925. options.grid.shadow = (options.grid.shadow != null) ? options.grid.shadow : false;
  926. }
  927. }
  928. // called with scope of plot
  929. function postParseOptions(options) {
  930. //
  931. }
  932. $.jqplot.preInitHooks.push(preInit);
  933. $.jqplot.postParseOptionsHooks.push(postParseOptions);
  934. $.jqplot.MeterGaugeTickRenderer = function() {
  935. $.jqplot.AxisTickRenderer.call(this);
  936. };
  937. $.jqplot.MeterGaugeTickRenderer.prototype = new $.jqplot.AxisTickRenderer();
  938. $.jqplot.MeterGaugeTickRenderer.prototype.constructor = $.jqplot.MeterGaugeTickRenderer;
  939. })(jQuery);