Dashboard sipadu mbip
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.

chartist-plugin-threshold.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. (function (root, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. // AMD. Register as an anonymous module.
  4. define([], function () {
  5. return (root.returnExportsGlobal = factory());
  6. });
  7. } else if (typeof exports === 'object') {
  8. // Node. Does not work with strict CommonJS, but
  9. // only CommonJS-like enviroments that support module.exports,
  10. // like Node.
  11. module.exports = factory();
  12. } else {
  13. root['Chartist.plugins.ctThreshold'] = factory();
  14. }
  15. }(this, function () {
  16. /**
  17. * Chartist.js plugin to display a data label on top of the points in a line chart.
  18. *
  19. */
  20. /* global Chartist */
  21. (function (window, document, Chartist) {
  22. 'use strict';
  23. var defaultOptions = {
  24. threshold: 0,
  25. classNames: {
  26. aboveThreshold: 'ct-threshold-above',
  27. belowThreshold: 'ct-threshold-below'
  28. },
  29. maskNames: {
  30. aboveThreshold: 'ct-threshold-mask-above',
  31. belowThreshold: 'ct-threshold-mask-below'
  32. }
  33. };
  34. function createMasks(data, options) {
  35. // Select the defs element within the chart or create a new one
  36. var defs = data.svg.querySelector('defs') || data.svg.elem('defs');
  37. // Project the threshold value on the chart Y axis
  38. var projectedThreshold = data.chartRect.height() - data.axisY.projectValue(options.threshold) + data.chartRect.y2;
  39. var width = data.svg.width();
  40. var height = data.svg.height();
  41. // Create mask for upper part above threshold
  42. defs
  43. .elem('mask', {
  44. x: 0,
  45. y: 0,
  46. width: width,
  47. height: height,
  48. id: options.maskNames.aboveThreshold
  49. })
  50. .elem('rect', {
  51. x: 0,
  52. y: 0,
  53. width: width,
  54. height: projectedThreshold,
  55. fill: 'white'
  56. });
  57. // Create mask for lower part below threshold
  58. defs
  59. .elem('mask', {
  60. x: 0,
  61. y: 0,
  62. width: width,
  63. height: height,
  64. id: options.maskNames.belowThreshold
  65. })
  66. .elem('rect', {
  67. x: 0,
  68. y: projectedThreshold,
  69. width: width,
  70. height: height - projectedThreshold,
  71. fill: 'white'
  72. });
  73. return defs;
  74. }
  75. Chartist.plugins = Chartist.plugins || {};
  76. Chartist.plugins.ctThreshold = function (options) {
  77. options = Chartist.extend({}, defaultOptions, options);
  78. return function ctThreshold(chart) {
  79. if (chart instanceof Chartist.Line || chart instanceof Chartist.Bar) {
  80. chart.on('draw', function (data) {
  81. if (data.type === 'point') {
  82. // For points we can just use the data value and compare against the threshold in order to determine
  83. // the appropriate class
  84. data.element.addClass(
  85. data.value.y >= options.threshold ? options.classNames.aboveThreshold : options.classNames.belowThreshold
  86. );
  87. } else if (data.type === 'line' || data.type === 'bar' || data.type === 'area') {
  88. // Cloning the original line path, mask it with the upper mask rect above the threshold and add the
  89. // class for above threshold
  90. data.element
  91. .parent()
  92. .elem(data.element._node.cloneNode(true))
  93. .attr({
  94. mask: 'url(#' + options.maskNames.aboveThreshold + ')'
  95. })
  96. .addClass(options.classNames.aboveThreshold);
  97. // Use the original line path, mask it with the lower mask rect below the threshold and add the class
  98. // for blow threshold
  99. data.element
  100. .attr({
  101. mask: 'url(#' + options.maskNames.belowThreshold + ')'
  102. })
  103. .addClass(options.classNames.belowThreshold);
  104. }
  105. });
  106. // On the created event, create the two mask definitions used to mask the line graphs
  107. chart.on('created', function (data) {
  108. createMasks(data, options);
  109. });
  110. }
  111. };
  112. }
  113. }(window, document, Chartist));
  114. return Chartist.plugins.ctThreshold;
  115. }));