jquery.flot.gauge.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. /*!
  2. * jquery.flot.gauge v1.1.0 *
  3. *
  4. * Flot plugin for rendering gauge charts.
  5. *
  6. * Copyright (c) 2015 @toyoty99.
  7. * Licensed under the MIT license.
  8. */
  9. /**
  10. * @module flot.gauge
  11. */
  12. (function($) {
  13. /**
  14. * Gauge class
  15. *
  16. * @class Gauge
  17. */
  18. var Gauge = (function() {
  19. /**
  20. * context of canvas
  21. *
  22. * @property context
  23. * @type Object
  24. */
  25. var context;
  26. /**
  27. * placeholder of canvas
  28. *
  29. * @property placeholder
  30. * @type Object
  31. */
  32. var placeholder;
  33. /**
  34. * options of plot
  35. *
  36. * @property options
  37. * @type Object
  38. */
  39. var options;
  40. /**
  41. * options of gauge
  42. *
  43. * @property gaugeOptions
  44. * @type Object
  45. */
  46. var gaugeOptions;
  47. /**
  48. * data series
  49. *
  50. * @property series
  51. * @type Array
  52. */
  53. var series;
  54. /**
  55. * logger
  56. *
  57. * @property logger
  58. * @type Object
  59. */
  60. var logger;
  61. /**
  62. * constructor
  63. *
  64. * @class Gauge
  65. * @constructor
  66. * @param {Object} gaugeOptions gauge options
  67. */
  68. var Gauge = function(plot, ctx) {
  69. context = ctx;
  70. placeholder = plot.getPlaceholder();
  71. options = plot.getOptions();
  72. gaugeOptions = options.series.gauges;
  73. series = plot.getData();
  74. logger = getLogger(gaugeOptions.debug);
  75. }
  76. /**
  77. * calculate layout
  78. *
  79. * @method calculateLayout
  80. * @return the calculated layout properties
  81. */
  82. Gauge.prototype.calculateLayout = function() {
  83. var canvasWidth = placeholder.width();
  84. var canvasHeight = placeholder.height();
  85. // calculate cell size
  86. var columns = Math.min(series.length, gaugeOptions.layout.columns);
  87. var rows = Math.ceil(series.length / columns);
  88. var margin = gaugeOptions.layout.margin;
  89. var hMargin = gaugeOptions.layout.hMargin;
  90. var vMargin = gaugeOptions.layout.vMargin;
  91. var cellWidth = (canvasWidth - (margin * 2) - (hMargin * (columns - 1))) / columns;
  92. var cellHeight = (canvasHeight - (margin * 2) - (vMargin * (rows - 1))) / rows;
  93. if (gaugeOptions.layout.square) {
  94. var cell = Math.min(cellWidth, cellHeight);
  95. cellWidth = cell;
  96. cellHeight = cell;
  97. }
  98. // calculate 'auto' values
  99. calculateAutoValues(gaugeOptions, cellWidth);
  100. // calculate maximum radius
  101. var cellMargin = gaugeOptions.cell.margin;
  102. var labelMargin = 0;
  103. var labelFontSize = 0;
  104. if (gaugeOptions.label.show) {
  105. labelMargin = gaugeOptions.label.margin;
  106. labelFontSize = gaugeOptions.label.font.size;
  107. }
  108. var valueMargin = 0;
  109. var valueFontSize = 0;
  110. if (gaugeOptions.value.show) {
  111. valueMargin = gaugeOptions.value.margin;
  112. valueFontSize = gaugeOptions.value.font.size;
  113. }
  114. var thresholdWidth = 0;
  115. if (gaugeOptions.threshold.show) {
  116. thresholdWidth = gaugeOptions.threshold.width;
  117. }
  118. var thresholdLabelMargin = 0;
  119. var thresholdLabelFontSize = 0;
  120. if (gaugeOptions.threshold.label.show) {
  121. thresholdLabelMargin = gaugeOptions.threshold.label.margin;
  122. thresholdLabelFontSize = gaugeOptions.threshold.label.font.size;
  123. }
  124. var maxRadiusH = (cellWidth / 2) - cellMargin - thresholdWidth - (thresholdLabelMargin * 2) - thresholdLabelFontSize;
  125. var startAngle = gaugeOptions.gauge.startAngle;
  126. var endAngle = gaugeOptions.gauge.endAngle;
  127. var dAngle = (endAngle - startAngle) / 100;
  128. var heightRatioV = -1;
  129. for (var a = startAngle; a < endAngle; a += dAngle) {
  130. heightRatioV = Math.max(heightRatioV, Math.sin(toRad(a)));
  131. }
  132. heightRatioV = Math.max(heightRatioV, Math.sin(toRad(endAngle)));
  133. var outerRadiusV = (cellHeight - (cellMargin * 2) - (labelMargin * 2) - labelFontSize) / (1 + heightRatioV);
  134. if (outerRadiusV * heightRatioV < valueMargin + (valueFontSize / 2)) {
  135. outerRadiusV = cellHeight - (cellMargin * 2) - (labelMargin * 2) - labelFontSize - valueMargin - (valueFontSize / 2);
  136. }
  137. var maxRadiusV = outerRadiusV - (thresholdLabelMargin * 2) - thresholdLabelFontSize - thresholdWidth;
  138. var radius = Math.min(maxRadiusH, maxRadiusV);
  139. var width = gaugeOptions.gauge.width;
  140. if (width >= radius) {
  141. width = Math.max(3, radius / 3);
  142. }
  143. var outerRadius = (thresholdLabelMargin * 2) + thresholdLabelFontSize + thresholdWidth + radius;
  144. var gaugeOuterHeight = Math.max(outerRadius * (1 + heightRatioV), outerRadius + valueMargin + (valueFontSize / 2));
  145. return {
  146. canvasWidth: canvasWidth,
  147. canvasHeight: canvasHeight,
  148. margin: margin,
  149. hMargin: hMargin,
  150. vMargin: vMargin,
  151. columns: columns,
  152. rows: rows,
  153. cellWidth: cellWidth,
  154. cellHeight: cellHeight,
  155. cellMargin: cellMargin,
  156. labelMargin: labelMargin,
  157. labelFontSize: labelFontSize,
  158. valueMargin: valueMargin,
  159. valueFontSize: valueFontSize,
  160. width: width,
  161. radius: radius,
  162. thresholdWidth: thresholdWidth,
  163. thresholdLabelMargin: thresholdLabelMargin,
  164. thresholdLabelFontSize: thresholdLabelFontSize,
  165. gaugeOuterHeight: gaugeOuterHeight
  166. };
  167. }
  168. /**
  169. * calculate the values which are set as 'auto'
  170. *
  171. * @method calculateAutoValues
  172. * @param {Object} gaugeOptionsi the options of the gauge
  173. * @param {Number} cellWidth the width of cell
  174. */
  175. function calculateAutoValues(gaugeOptionsi, cellWidth) {
  176. if (gaugeOptionsi.gauge.width === "auto") {
  177. gaugeOptionsi.gauge.width = Math.max(5, cellWidth / 8);
  178. }
  179. if (gaugeOptionsi.label.margin === "auto") {
  180. gaugeOptionsi.label.margin = Math.max(1, cellWidth / 20);
  181. }
  182. if (gaugeOptionsi.label.font.size === "auto") {
  183. gaugeOptionsi.label.font.size = Math.max(5, cellWidth / 8);
  184. }
  185. if (gaugeOptionsi.value.margin === "auto") {
  186. gaugeOptionsi.value.margin = Math.max(1, cellWidth / 30);
  187. }
  188. if (gaugeOptionsi.value.font.size === "auto") {
  189. gaugeOptionsi.value.font.size = Math.max(5, cellWidth / 9);
  190. }
  191. if (gaugeOptionsi.threshold.width === "auto") {
  192. gaugeOptionsi.threshold.width = Math.max(3, cellWidth / 100);
  193. }
  194. if (gaugeOptionsi.threshold.label.margin === "auto") {
  195. gaugeOptionsi.threshold.label.margin = Math.max(3, cellWidth / 40);
  196. }
  197. if (gaugeOptionsi.threshold.label.font.size === "auto") {
  198. gaugeOptionsi.threshold.label.font.size = Math.max(5, cellWidth / 15);
  199. }
  200. }
  201. Gauge.prototype.calculateAutoValues = calculateAutoValues;
  202. /**
  203. * calculate the layout of the cell inside
  204. *
  205. * @method calculateCellLayout
  206. * @param {Object} gaugeOptionsi the options of the gauge
  207. * @param {Number} cellWidth the width of cell
  208. * @param {Number} i the index of the series
  209. * @return the calculated cell layout properties
  210. */
  211. Gauge.prototype.calculateCellLayout = function(gaugeOptionsi, layout, i) {
  212. // calculate top, left and center
  213. var c = col(layout.columns, i);
  214. var r = row(layout.columns, i);
  215. var x = layout.margin + (layout.cellWidth + layout.hMargin) * c;
  216. var y = layout.margin + (layout.cellHeight + layout.vMargin) * r;
  217. var cx = x + (layout.cellWidth / 2);
  218. var cy = y + layout.cellMargin + (layout.labelMargin * 2) + layout.labelFontSize + layout.thresholdWidth
  219. + layout.thresholdLabelFontSize + (layout.thresholdLabelMargin * 2) + layout.radius;
  220. var blank = layout.cellHeight - (layout.cellMargin * 2) - (layout.labelMargin * 2) - layout.labelFontSize - layout.gaugeOuterHeight;
  221. var offsetY = 0;
  222. if (gaugeOptionsi.cell.vAlign === "middle") {
  223. offsetY = (blank / 2);
  224. } else if (gaugeOptionsi.cell.vAlign === "bottom") {
  225. offsetY = blank;
  226. }
  227. cy += offsetY;
  228. return {
  229. col: c,
  230. row: r,
  231. x: x,
  232. y: y,
  233. offsetY: offsetY,
  234. cellWidth: layout.cellWidth,
  235. cellHeight: layout.cellHeight,
  236. cellMargin: layout.cellMargin,
  237. cx: cx,
  238. cy: cy
  239. }
  240. }
  241. /**
  242. * draw the background of chart
  243. *
  244. * @method drawBackground
  245. * @param {Object} layout the layout properties
  246. */
  247. Gauge.prototype.drawBackground = function(layout) {
  248. if (!gaugeOptions.frame.show) {
  249. return;
  250. }
  251. context.save();
  252. context.strokeStyle = options.grid.borderColor;
  253. context.lineWidth = options.grid.borderWidth;
  254. context.strokeRect(0, 0, layout.canvasWidth, layout.canvasHeight);
  255. if (options.grid.backgroundColor) {
  256. context.fillStyle = options.grid.backgroundColor;
  257. context.fillRect(0, 0, layout.canvasWidth, layout.canvasHeight);
  258. }
  259. context.restore();
  260. }
  261. /**
  262. * draw the background of cell
  263. *
  264. * @method drawCellBackground
  265. * @param {Object} gaugeOptionsi the options of the gauge
  266. * @param {Object} cellLayout the cell layout properties
  267. */
  268. Gauge.prototype.drawCellBackground = function(gaugeOptionsi, cellLayout) {
  269. context.save();
  270. if (gaugeOptionsi.cell.border && gaugeOptionsi.cell.border.show && gaugeOptionsi.cell.border.color && gaugeOptionsi.cell.border.width) {
  271. context.strokeStyle = gaugeOptionsi.cell.border.color;
  272. context.lineWidth = gaugeOptionsi.cell.border.width;
  273. context.strokeRect(cellLayout.x, cellLayout.y, cellLayout.cellWidth, cellLayout.cellHeight);
  274. }
  275. if (gaugeOptionsi.cell.background && gaugeOptionsi.cell.background.color) {
  276. context.fillStyle = gaugeOptionsi.cell.background.color;
  277. context.fillRect(cellLayout.x, cellLayout.y, cellLayout.cellWidth, cellLayout.cellHeight);
  278. }
  279. context.restore();
  280. }
  281. /**
  282. * draw the gauge
  283. *
  284. * @method drawGauge
  285. * @param {Object} gaugeOptionsi the options of the gauge
  286. * @param {Object} layout the layout properties
  287. * @param {Object} cellLayout the cell layout properties
  288. * @param {String} label the label of data
  289. * @param {Number} data the value of the gauge
  290. */
  291. Gauge.prototype.drawGauge = function(gaugeOptionsi, layout, cellLayout, label, data) {
  292. var blur = gaugeOptionsi.gauge.shadow.show ? gaugeOptionsi.gauge.shadow.blur : 0;
  293. // draw gauge frame
  294. drawArcWithShadow(
  295. cellLayout.cx, // center x
  296. cellLayout.cy, // center y
  297. layout.radius,
  298. layout.width,
  299. toRad(gaugeOptionsi.gauge.startAngle),
  300. toRad(gaugeOptionsi.gauge.endAngle),
  301. gaugeOptionsi.gauge.border.color, // line color
  302. gaugeOptionsi.gauge.border.width, // line width
  303. gaugeOptionsi.gauge.background.color, // fill color
  304. blur);
  305. // draw gauge
  306. var c1 = getColor(gaugeOptionsi, data);
  307. var a2 = calculateAngle(gaugeOptionsi, layout, data);
  308. drawArcWithShadow(
  309. cellLayout.cx, // center x
  310. cellLayout.cy, // center y
  311. layout.radius - 1,
  312. layout.width - 2,
  313. toRad(gaugeOptionsi.gauge.startAngle),
  314. toRad(a2),
  315. c1, // line color
  316. 1, // line width
  317. c1, // fill color
  318. blur);
  319. }
  320. /**
  321. * decide the color of the data from the threshold options
  322. *
  323. * @method getColor
  324. * @private
  325. * @param {Object} gaugeOptionsi the options of the gauge
  326. * @param {Number} data the value of the gauge
  327. */
  328. function getColor(gaugeOptionsi, data) {
  329. var color;
  330. for (var i = 0; i < gaugeOptionsi.threshold.values.length; i++) {
  331. var threshold = gaugeOptionsi.threshold.values[i];
  332. color = threshold.color;
  333. if (data < threshold.value) {
  334. break;
  335. }
  336. }
  337. return color;
  338. }
  339. /**
  340. * calculate the angle of the data
  341. *
  342. * @method calculateAngle
  343. * @private
  344. * @param {Object} gaugeOptionsi the options of the gauge
  345. * @param {Object} layout the layout properties
  346. * @param {Number} data the value of the gauge
  347. */
  348. function calculateAngle(gaugeOptionsi, layout, data) {
  349. var a =
  350. gaugeOptionsi.gauge.startAngle
  351. + (gaugeOptionsi.gauge.endAngle - gaugeOptionsi.gauge.startAngle)
  352. * ((data - gaugeOptionsi.gauge.min) / (gaugeOptionsi.gauge.max - gaugeOptionsi.gauge.min));
  353. if (a < gaugeOptionsi.gauge.startAngle) {
  354. a = gaugeOptionsi.gauge.startAngle;
  355. } else if (a > gaugeOptionsi.gauge.endAngle) {
  356. a = gaugeOptionsi.gauge.endAngle;
  357. }
  358. return a;
  359. }
  360. /**
  361. * draw the arc of the threshold
  362. *
  363. * @method drawThreshold
  364. * @param {Object} gaugeOptionsi the options of the gauge
  365. * @param {Object} layout the layout properties
  366. * @param {Object} cellLayout the cell layout properties
  367. */
  368. Gauge.prototype.drawThreshold = function(gaugeOptionsi, layout, cellLayout) {
  369. var a1 = gaugeOptionsi.gauge.startAngle;
  370. for (var i = 0; i < gaugeOptionsi.threshold.values.length; i++) {
  371. var threshold = gaugeOptionsi.threshold.values[i];
  372. c1 = threshold.color;
  373. a2 = calculateAngle(gaugeOptionsi, layout, threshold.value);
  374. drawArc(
  375. context,
  376. cellLayout.cx, // center x
  377. cellLayout.cy, // center y
  378. layout.radius + layout.thresholdWidth,
  379. layout.thresholdWidth - 2,
  380. toRad(a1),
  381. toRad(a2),
  382. c1, // line color
  383. 1, // line width
  384. c1); // fill color
  385. a1 = a2;
  386. }
  387. }
  388. /**
  389. * draw an arc with a shadow
  390. *
  391. * @method drawArcWithShadow
  392. * @private
  393. * @param {Number} cx the x position of the center
  394. * @param {Number} cy the y position of the center
  395. * @param {Number} r the radius of an arc
  396. * @param {Number} w the width of an arc
  397. * @param {Number} rd1 the start angle of an arc in radians
  398. * @param {Number} rd2 the end angle of an arc in radians
  399. * @param {String} lc the color of a line
  400. * @param {Number} lw the widht of a line
  401. * @param {String} fc the fill color of an arc
  402. * @param {Number} blur the shdow blur
  403. */
  404. function drawArcWithShadow(cx, cy, r, w, rd1, rd2, lc, lw, fc, blur) {
  405. if (rd1 === rd2) {
  406. return;
  407. }
  408. context.save();
  409. drawArc(context, cx, cy, r, w, rd1, rd2, lc, lw, fc);
  410. if (blur) {
  411. drawArc(context, cx, cy, r, w, rd1, rd2);
  412. context.clip();
  413. context.shadowOffsetX = 0;
  414. context.shadowOffsetY = 0;
  415. context.shadowBlur = 10;
  416. context.shadowColor = "gray";
  417. drawArc(context, cx, cy, r + 1, w + 2, rd1, rd2, lc, 1);
  418. }
  419. context.restore();
  420. }
  421. /**
  422. * draw the label of the gauge
  423. *
  424. * @method drawLable
  425. * @param {Object} gaugeOptionsi the options of the gauge
  426. * @param {Object} layout the layout properties
  427. * @param {Object} cellLayout the cell layout properties
  428. * @param {Number} i the index of the series
  429. * @param {Object} item the item of the series
  430. */
  431. Gauge.prototype.drawLable = function(gaugeOptionsi, layout, cellLayout, i, item) {
  432. drawText(
  433. cellLayout.cx,
  434. cellLayout.y + cellLayout.cellMargin + layout.labelMargin + cellLayout.offsetY,
  435. "flotGaugeLabel" + i,
  436. gaugeOptionsi.label.formatter ? gaugeOptionsi.label.formatter(item.label, item.data[0][1]) : text,
  437. gaugeOptionsi.label);
  438. }
  439. /**
  440. * draw the value of the gauge
  441. *
  442. * @method drawValue
  443. * @param {Object} gaugeOptionsi the options of the gauge
  444. * @param {Object} layout the layout properties
  445. * @param {Object} cellLayout the cell layout properties
  446. * @param {Number} i the index of the series
  447. * @param {Object} item the item of the series
  448. */
  449. Gauge.prototype.drawValue = function(gaugeOptionsi, layout, cellLayout, i, item) {
  450. drawText(
  451. cellLayout.cx,
  452. cellLayout.cy - (gaugeOptionsi.value.font.size / 2),
  453. "flotGaugeValue" + i,
  454. gaugeOptionsi.value.formatter ? gaugeOptionsi.value.formatter(item.label, item.data[0][1]) : text,
  455. gaugeOptionsi.value);
  456. }
  457. /**
  458. * draw the values of the threshold
  459. *
  460. * @method drawThresholdValues
  461. * @param {Object} gaugeOptionsi the options of the gauge
  462. * @param {Object} layout the layout properties
  463. * @param {Object} cellLayout the cell layout properties
  464. * @param {Number} i the index of the series
  465. */
  466. Gauge.prototype.drawThresholdValues = function(gaugeOptionsi, layout, cellLayout, i) {
  467. // min, max
  468. drawThresholdValue(gaugeOptionsi, layout, cellLayout, "Min" + i, gaugeOptionsi.gauge.min, gaugeOptionsi.gauge.startAngle);
  469. drawThresholdValue(gaugeOptionsi, layout, cellLayout, "Max" + i, gaugeOptionsi.gauge.max, gaugeOptionsi.gauge.endAngle);
  470. // threshold values
  471. for (var j = 0; j < gaugeOptionsi.threshold.values.length; j++) {
  472. var threshold = gaugeOptionsi.threshold.values[j];
  473. if (threshold.value > gaugeOptionsi.gauge.min && threshold.value < gaugeOptionsi.gauge.max) {
  474. var a = calculateAngle(gaugeOptionsi, layout, threshold.value);
  475. drawThresholdValue(gaugeOptionsi, layout, cellLayout, i + "_" + j, threshold.value, a);
  476. }
  477. }
  478. }
  479. /**
  480. * draw the value of the threshold
  481. *
  482. * @method drawThresholdValue
  483. * @param {Object} gaugeOptionsi the options of the gauge
  484. * @param {Object} layout the layout properties
  485. * @param {Object} cellLayout the cell layout properties
  486. * @param {Number} i the index of the series
  487. * @param {Number} value the value of the threshold
  488. * @param {Number} a the angle of the value drawn
  489. */
  490. function drawThresholdValue(gaugeOptionsi, layout, cellLayout, i, value, a) {
  491. drawText(
  492. cellLayout.cx
  493. + ((layout.thresholdLabelMargin + (layout.thresholdLabelFontSize / 2) + layout.radius)
  494. * Math.cos(toRad(a))),
  495. cellLayout.cy
  496. + ((layout.thresholdLabelMargin + (layout.thresholdLabelFontSize / 2) + layout.radius)
  497. * Math.sin(toRad(a))),
  498. "flotGaugeThresholdValue" + i,
  499. gaugeOptionsi.threshold.label.formatter ? gaugeOptionsi.threshold.label.formatter(value) : value,
  500. gaugeOptionsi.threshold.label,
  501. a);
  502. }
  503. /**
  504. * draw a text
  505. *
  506. * the textOptions is assumed as follows:
  507. *
  508. * textOptions: {
  509. * background: {
  510. * color: null,
  511. * opacity: 0
  512. * },
  513. * font: {
  514. * size: "auto"
  515. * family: "\"MS ゴシック\",sans-serif"
  516. * },
  517. * color: null
  518. * }
  519. *
  520. * @method drawText
  521. * @private
  522. * @param {Number} x the x position of the text drawn (left top)
  523. * @param {Number} y the y position of the text drawn (left top)
  524. * @param {String} id the id of the dom element
  525. * @param {String} text the text drawn
  526. * @param {Object} textOptions the option of the text
  527. * @param {Number} [a] the angle of the value drawn
  528. */
  529. function drawText(x, y, id, text, textOptions, a) {
  530. var span = $(placeholder).find("#" + id);
  531. var exists = span.length;
  532. if (!exists) {
  533. span = $("<span></span>")
  534. span.attr("id", id);
  535. span.attr("class", "flot-temp-elem");
  536. placeholder.append(span);
  537. }
  538. span.css("position", "absolute");
  539. span.css("top", y + "px");
  540. span.css("white-space", "nowrap");
  541. if (textOptions.font.size) {
  542. span.css("font-size", textOptions.font.size + "px");
  543. }
  544. if (textOptions.font.family) {
  545. span.css("font-family", textOptions.font.family);
  546. }
  547. if (textOptions.color) {
  548. span.css("color", textOptions.color);
  549. }
  550. if (textOptions.background.color) {
  551. span.css("background-color", textOptions.background.color);
  552. }
  553. if (textOptions.background.opacity) {
  554. span.css("opacity", textOptions.background.opacity);
  555. }
  556. span.text(text);
  557. // after append, readjust the left position
  558. span.css("left", x + "px"); // for redraw, resetting the left position is needed here
  559. span.css("left", (parseInt(span.css("left")) - (span.width()/ 2)) + "px");
  560. // at last, set angle
  561. if (!exists && a) {
  562. span.css("top", (parseInt(span.css("top")) - (span.height()/ 2)) + "px");
  563. span.css("transform", "rotate(" + ((180 * a) + 90) + "deg)"); // not supported for ie8
  564. }
  565. }
  566. return Gauge;
  567. })();
  568. /**
  569. * get a instance of Logger
  570. *
  571. * @method getLogger
  572. * @for flot.gauge
  573. * @private
  574. * @param {Object} debugOptions the options of debug
  575. */
  576. function getLogger(debugOptions) {
  577. return typeof Logger !== "undefined" ? new Logger(debugOptions) : null;
  578. }
  579. /**
  580. * calculate the index of columns for the specified data
  581. *
  582. * @method col
  583. * @for flot.gauge
  584. * @param {Number} columns the number of columns
  585. * @param {Number} i the index of the series
  586. * @return the index of columns
  587. */
  588. function col(columns, i) {
  589. return i % columns;
  590. }
  591. /**
  592. * calculate the index of rows for the specified data
  593. *
  594. * @method row
  595. * @for flot.gauge
  596. * @param {Number} columns the number of rows
  597. * @param {Number} i the index of the series
  598. * @return the index of rows
  599. */
  600. function row(columns, i) {
  601. return Math.floor(i / columns);
  602. }
  603. /**
  604. * calculate the angle in radians
  605. *
  606. * internally, use a number without PI (0 - 2).
  607. * so, in this function, multiply PI
  608. *
  609. * @method toRad
  610. * @for flot.gauge
  611. * @param {Number} a the number of angle without PI
  612. * @return the angle in radians
  613. */
  614. function toRad(a) {
  615. return a * Math.PI;
  616. }
  617. /**
  618. * draw an arc
  619. *
  620. * @method drawArc
  621. * @for flot.gauge
  622. * @param {Object} context the context of canvas
  623. * @param {Number} cx the x position of the center
  624. * @param {Number} cy the y position of the center
  625. * @param {Number} r the radius of an arc
  626. * @param {Number} w the width of an arc
  627. * @param {Number} rd1 the start angle of an arc in radians
  628. * @param {Number} rd2 the end angle of an arc in radians
  629. * @param {String} lc the color of a line
  630. * @param {Number} lw the widht of a line
  631. * @param {String} fc the fill color of an arc
  632. */
  633. function drawArc(context, cx, cy, r, w, rd1, rd2, lc, lw, fc) {
  634. if (rd1 === rd2) {
  635. return;
  636. }
  637. var counterClockwise = false;
  638. context.save();
  639. context.beginPath();
  640. context.arc(cx, cy, r, rd1, rd2, counterClockwise);
  641. context.lineTo(cx + (r - w) * Math.cos(rd2),
  642. cy + (r - w) * Math.sin(rd2));
  643. context.arc(cx, cy, r - w, rd2, rd1, !counterClockwise);
  644. context.closePath();
  645. if (lw) {
  646. context.lineWidth = lw;
  647. }
  648. if (lc) {
  649. context.strokeStyle = lc;
  650. context.stroke();
  651. }
  652. if (fc) {
  653. context.fillStyle = fc;
  654. context.fill();
  655. }
  656. context.restore();
  657. }
  658. /**
  659. * initialize plugin
  660. *
  661. * @method init
  662. * @for flot.gauge
  663. * @private
  664. * @param {Object} plot a instance of plot
  665. */
  666. function init (plot) {
  667. // add processOptions hook
  668. plot.hooks.processOptions.push(function(plot, options) {
  669. var logger = getLogger(options.series.gauges.debug);
  670. // turn 'grid' and 'legend' off
  671. if (options.series.gauges.show) {
  672. options.grid.show = false;
  673. options.legend.show = false;
  674. }
  675. // sort threshold
  676. var thresholds = options.series.gauges.threshold.values;
  677. thresholds.sort(function(a, b) {
  678. if (a.value < b.value) {
  679. return -1;
  680. } else if (a.value > b.value) {
  681. return 1;
  682. } else {
  683. return 0;
  684. }
  685. });
  686. });
  687. // add draw hook
  688. plot.hooks.draw.push(function(plot, context) {
  689. var options = plot.getOptions();
  690. var gaugeOptions = options.series.gauges;
  691. var logger = getLogger(gaugeOptions.debug);
  692. if (!gaugeOptions.show) {
  693. return;
  694. }
  695. var series = plot.getData();
  696. if (!series || !series.length) {
  697. return; // if no series were passed
  698. }
  699. var gauge = new Gauge(plot, context);
  700. // calculate layout
  701. var layout = gauge.calculateLayout();
  702. // debug layout
  703. if (gaugeOptions.debug.layout) {
  704. }
  705. // draw background
  706. gauge.drawBackground(layout)
  707. // draw cells (label, gauge, value, threshold)
  708. for (var i = 0; i < series.length; i++) {
  709. var item = series[i];
  710. var gaugeOptionsi = $.extend({}, gaugeOptions, item.gauges);
  711. if (item.gauges) {
  712. // re-calculate 'auto' values
  713. gauge.calculateAutoValues(gaugeOptionsi, layout.cellWidth);
  714. }
  715. // calculate cell layout
  716. var cellLayout = gauge.calculateCellLayout(gaugeOptionsi, layout, i);
  717. // draw cell background
  718. gauge.drawCellBackground(gaugeOptionsi, cellLayout)
  719. // debug layout
  720. if (gaugeOptionsi.debug.layout) {
  721. }
  722. // draw label
  723. if (gaugeOptionsi.label.show) {
  724. gauge.drawLable(gaugeOptionsi, layout, cellLayout, i, item);
  725. }
  726. // draw gauge
  727. gauge.drawGauge(gaugeOptionsi, layout, cellLayout, item.label, item.data[0][1]);
  728. // draw threshold
  729. if (gaugeOptionsi.threshold.show) {
  730. gauge.drawThreshold(gaugeOptionsi, layout, cellLayout);
  731. }
  732. if (gaugeOptionsi.threshold.label.show) {
  733. gauge.drawThresholdValues(gaugeOptionsi, layout, cellLayout, i)
  734. }
  735. // draw value
  736. if (gaugeOptionsi.value.show) {
  737. gauge.drawValue(gaugeOptionsi, layout, cellLayout, i, item);
  738. }
  739. }
  740. });
  741. }
  742. /**
  743. * [defaults description]
  744. *
  745. * @property defaults
  746. * @type {Object}
  747. */
  748. var defaults = {
  749. series: {
  750. gauges: {
  751. debug: {
  752. log: false,
  753. layout: false,
  754. alert: false
  755. },
  756. show: false,
  757. layout: {
  758. margin: 5,
  759. columns: 3,
  760. hMargin: 5,
  761. vMargin: 5,
  762. square: false
  763. },
  764. frame: {
  765. show: true
  766. },
  767. cell: {
  768. background: {
  769. color: null
  770. },
  771. border: {
  772. show: true,
  773. color: "black",
  774. width: 1
  775. },
  776. margin: 5,
  777. vAlign: "middle" // 'top' or 'middle' or 'bottom'
  778. },
  779. gauge: {
  780. width: "auto", // a specified number, or 'auto'
  781. startAngle: 0.9, // 0 - 2 factor of the radians
  782. endAngle: 2.1, // 0 - 2 factor of the radians
  783. min: 0,
  784. max: 100,
  785. background: {
  786. color: "white"
  787. },
  788. border: {
  789. color: "lightgray",
  790. width: 2
  791. },
  792. shadow: {
  793. show: true,
  794. blur: 5
  795. }
  796. },
  797. label: {
  798. show: true,
  799. margin: "auto", // a specified number, or 'auto'
  800. background: {
  801. color: null,
  802. opacity: 0
  803. },
  804. font: {
  805. size: "auto", // a specified number, or 'auto'
  806. family: "sans-serif"
  807. },
  808. color: null,
  809. formatter: function(label, value) {
  810. return label;
  811. }
  812. },
  813. value: {
  814. show: true,
  815. margin: "auto", // a specified number, or 'auto'
  816. background: {
  817. color: null,
  818. opacity: 0
  819. },
  820. font: {
  821. size: "auto", // a specified number, or 'auto'
  822. family: "sans-serif"
  823. },
  824. color: null,
  825. formatter: function(label, value) {
  826. return parseInt(value);
  827. }
  828. },
  829. threshold: {
  830. show: true,
  831. width: "auto", // a specified number, or 'auto'
  832. label: {
  833. show: true,
  834. margin: "auto", // a specified number, or 'auto'
  835. background: {
  836. color: null,
  837. opacity: 0
  838. },
  839. font: {
  840. size: "auto", // a specified number, or 'auto'
  841. family: ",sans-serif"
  842. },
  843. color: null,
  844. formatter: function(value) {
  845. return value;
  846. }
  847. },
  848. values: [
  849. ]
  850. }
  851. }
  852. }
  853. };
  854. // register the gauge plugin
  855. $.plot.plugins.push({
  856. init: init,
  857. options: defaults,
  858. name: "gauge",
  859. version: "1.1.0"
  860. });
  861. })(jQuery);