You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1033 line
42 KiB

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