Ohm-Management - Projektarbeit B-ME
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.

VSparkline.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; // Mixins
  6. // Utilities
  7. var _colorable = require('../../mixins/colorable');
  8. var _colorable2 = _interopRequireDefault(_colorable);
  9. var _mixins = require('../../util/mixins');
  10. var _mixins2 = _interopRequireDefault(_mixins);
  11. var _core = require('./helpers/core');
  12. var _path = require('./helpers/path');
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. exports.default = (0, _mixins2.default)(_colorable2.default).extend({
  15. name: 'VSparkline',
  16. props: {
  17. autoDraw: Boolean,
  18. autoDrawDuration: {
  19. type: Number,
  20. default: 2000
  21. },
  22. autoDrawEasing: {
  23. type: String,
  24. default: 'ease'
  25. },
  26. autoLineWidth: {
  27. type: Boolean,
  28. default: false
  29. },
  30. color: {
  31. type: String,
  32. default: 'primary'
  33. },
  34. fill: {
  35. type: Boolean,
  36. default: false
  37. },
  38. gradient: {
  39. type: Array,
  40. default: function _default() {
  41. return [];
  42. }
  43. },
  44. gradientDirection: {
  45. type: String,
  46. validator: function validator(val) {
  47. return ['top', 'bottom', 'left', 'right'].includes(val);
  48. },
  49. default: 'top'
  50. },
  51. height: {
  52. type: [String, Number],
  53. default: 75
  54. },
  55. labels: {
  56. type: Array,
  57. default: function _default() {
  58. return [];
  59. }
  60. },
  61. lineWidth: {
  62. type: [String, Number],
  63. default: 4
  64. },
  65. padding: {
  66. type: [String, Number],
  67. default: 8
  68. },
  69. smooth: {
  70. type: [Boolean, Number, String],
  71. default: false
  72. },
  73. showLabels: Boolean,
  74. type: {
  75. type: String,
  76. default: 'trend',
  77. validator: function validator(val) {
  78. return ['trend', 'bar'].includes(val);
  79. }
  80. },
  81. value: {
  82. type: Array,
  83. default: function _default() {
  84. return [];
  85. }
  86. },
  87. width: {
  88. type: [Number, String],
  89. default: 300
  90. },
  91. labelSize: {
  92. type: [Number, String],
  93. default: 7
  94. }
  95. },
  96. data: function data() {
  97. return {
  98. lastLength: 0
  99. };
  100. },
  101. computed: {
  102. parsedPadding: function parsedPadding() {
  103. return Number(this.padding);
  104. },
  105. parsedWidth: function parsedWidth() {
  106. return Number(this.width);
  107. },
  108. totalBars: function totalBars() {
  109. return this.value.length;
  110. },
  111. _lineWidth: function _lineWidth() {
  112. if (this.autoLineWidth && this.type !== 'trend') {
  113. var totalPadding = this.parsedPadding * (this.totalBars + 1);
  114. return (this.parsedWidth - totalPadding) / this.totalBars;
  115. } else {
  116. return Number(this.lineWidth) || 4;
  117. }
  118. },
  119. boundary: function boundary() {
  120. var height = Number(this.height);
  121. return {
  122. minX: this.parsedPadding,
  123. minY: this.parsedPadding,
  124. maxX: this.parsedWidth - this.parsedPadding,
  125. maxY: height - this.parsedPadding
  126. };
  127. },
  128. hasLabels: function hasLabels() {
  129. return Boolean(this.showLabels || this.labels.length > 0 || this.$scopedSlots.label);
  130. },
  131. parsedLabels: function parsedLabels() {
  132. var labels = [];
  133. var points = this.points;
  134. var len = points.length;
  135. for (var i = 0; labels.length < len; i++) {
  136. var item = points[i];
  137. var value = this.labels[i];
  138. if (!value) {
  139. value = item === Object(item) ? item.value : item;
  140. }
  141. labels.push(_extends({}, item, {
  142. value: String(value)
  143. }));
  144. }
  145. return labels;
  146. },
  147. points: function points() {
  148. return (0, _core.genPoints)(this.value.slice(), this.boundary, this.type);
  149. },
  150. textY: function textY() {
  151. return this.boundary.maxY + 6;
  152. }
  153. },
  154. watch: {
  155. value: {
  156. immediate: true,
  157. handler: function handler() {
  158. var _this = this;
  159. this.$nextTick(function () {
  160. if (!_this.autoDraw || _this.type === 'bar') return;
  161. var path = _this.$refs.path;
  162. var length = path.getTotalLength();
  163. if (!_this.fill) {
  164. path.style.transition = 'none';
  165. path.style.strokeDasharray = length + ' ' + length;
  166. path.style.strokeDashoffset = Math.abs(length - (_this.lastLength || 0)).toString();
  167. path.getBoundingClientRect();
  168. path.style.transition = 'stroke-dashoffset ' + _this.autoDrawDuration + 'ms ' + _this.autoDrawEasing;
  169. path.style.strokeDashoffset = '0';
  170. } else {
  171. path.style.transformOrigin = 'bottom center';
  172. path.style.transition = 'none';
  173. path.style.transform = 'scaleY(0)';
  174. path.getBoundingClientRect();
  175. path.style.transition = 'transform ' + _this.autoDrawDuration + 'ms ' + _this.autoDrawEasing;
  176. path.style.transform = 'scaleY(1)';
  177. }
  178. _this.lastLength = length;
  179. });
  180. }
  181. }
  182. },
  183. methods: {
  184. genGradient: function genGradient() {
  185. var _this2 = this;
  186. var gradientDirection = this.gradientDirection;
  187. var gradient = this.gradient.slice();
  188. // Pushes empty string to force
  189. // a fallback to currentColor
  190. if (!gradient.length) gradient.push('');
  191. var len = Math.max(gradient.length - 1, 1);
  192. var stops = gradient.reverse().map(function (color, index) {
  193. return _this2.$createElement('stop', {
  194. attrs: {
  195. offset: index / len,
  196. 'stop-color': color || _this2.color || 'currentColor'
  197. }
  198. });
  199. });
  200. return this.$createElement('defs', [this.$createElement('linearGradient', {
  201. attrs: {
  202. id: this._uid,
  203. x1: +(gradientDirection === 'left'),
  204. y1: +(gradientDirection === 'top'),
  205. x2: +(gradientDirection === 'right'),
  206. y2: +(gradientDirection === 'bottom')
  207. }
  208. }, stops)]);
  209. },
  210. genG: function genG(children) {
  211. return this.$createElement('g', {
  212. style: {
  213. fontSize: '8',
  214. textAnchor: 'middle',
  215. dominantBaseline: 'mathematical',
  216. fill: this.color || 'currentColor'
  217. }
  218. }, children);
  219. },
  220. genLabels: function genLabels() {
  221. if (!this.hasLabels) return undefined;
  222. return this.genG(this.parsedLabels.map(this.genText));
  223. },
  224. genPath: function genPath() {
  225. var radius = this.smooth === true ? 8 : Number(this.smooth);
  226. return this.$createElement('path', {
  227. attrs: {
  228. id: this._uid,
  229. d: (0, _path.genPath)(this.points.slice(), radius, this.fill, Number(this.height)),
  230. fill: this.fill ? 'url(#' + this._uid + ')' : 'none',
  231. stroke: this.fill ? 'none' : 'url(#' + this._uid + ')'
  232. },
  233. ref: 'path'
  234. });
  235. },
  236. genText: function genText(item, index) {
  237. var children = this.$scopedSlots.label ? this.$scopedSlots.label({ index: index, value: item.value }) : item.value;
  238. return this.$createElement('text', {
  239. attrs: {
  240. x: item.x,
  241. y: this.textY
  242. }
  243. }, [children]);
  244. },
  245. genBar: function genBar() {
  246. if (!this.value || this.totalBars < 2) return undefined;
  247. var width = this.width,
  248. height = this.height,
  249. parsedPadding = this.parsedPadding,
  250. _lineWidth = this._lineWidth;
  251. var viewWidth = width || this.totalBars * parsedPadding * 2;
  252. var viewHeight = height || 75;
  253. var boundary = {
  254. minX: parsedPadding,
  255. minY: parsedPadding,
  256. maxX: Number(viewWidth) - parsedPadding,
  257. maxY: Number(viewHeight) - parsedPadding
  258. };
  259. var props = _extends({}, this.$props);
  260. props.points = (0, _core.genPoints)(this.value, boundary, this.type);
  261. var totalWidth = boundary.maxX / (props.points.length - 1);
  262. props.boundary = boundary;
  263. props.lineWidth = _lineWidth || totalWidth - Number(parsedPadding || 5);
  264. props.offsetX = 0;
  265. if (!this.autoLineWidth) {
  266. props.offsetX = boundary.maxX / this.totalBars / 2 - boundary.minX;
  267. }
  268. return this.$createElement('svg', {
  269. attrs: {
  270. width: '100%',
  271. height: '25%',
  272. viewBox: '0 0 ' + viewWidth + ' ' + viewHeight
  273. }
  274. }, [this.genGradient(), this.genClipPath(props.offsetX, props.lineWidth, 'sparkline-bar-' + this._uid), this.hasLabels ? this.genBarLabels(props) : undefined, this.$createElement('g', {
  275. attrs: {
  276. transform: 'scale(1,-1) translate(0,-' + boundary.maxY + ')',
  277. 'clip-path': 'url(#sparkline-bar-' + this._uid + '-clip)',
  278. fill: 'url(#' + this._uid + ')'
  279. }
  280. }, [this.$createElement('rect', {
  281. attrs: {
  282. x: 0,
  283. y: 0,
  284. width: viewWidth,
  285. height: viewHeight
  286. }
  287. })])]);
  288. },
  289. genClipPath: function genClipPath(offsetX, lineWidth, id) {
  290. var _this3 = this;
  291. var maxY = this.boundary.maxY;
  292. var rounding = typeof this.smooth === 'number' ? this.smooth : this.smooth ? 2 : 0;
  293. return this.$createElement('clipPath', {
  294. attrs: {
  295. id: id + '-clip'
  296. }
  297. }, this.points.map(function (item) {
  298. return _this3.$createElement('rect', {
  299. attrs: {
  300. x: item.x + offsetX,
  301. y: 0,
  302. width: lineWidth,
  303. height: Math.max(maxY - item.y, 0),
  304. rx: rounding,
  305. ry: rounding
  306. }
  307. }, [_this3.autoDraw ? _this3.$createElement('animate', {
  308. attrs: {
  309. attributeName: 'height',
  310. from: 0,
  311. to: maxY - item.y,
  312. dur: _this3.autoDrawDuration + 'ms',
  313. fill: 'freeze'
  314. }
  315. }) : undefined]);
  316. }));
  317. },
  318. genBarLabels: function genBarLabels(props) {
  319. var _this4 = this;
  320. var offsetX = props.offsetX || 0;
  321. var children = props.points.map(function (item) {
  322. return _this4.$createElement('text', {
  323. attrs: {
  324. x: item.x + offsetX + _this4._lineWidth / 2,
  325. y: props.boundary.maxY + (Number(_this4.labelSize) || 7),
  326. 'font-size': Number(_this4.labelSize) || 7
  327. }
  328. }, item.value.toString());
  329. });
  330. return this.genG(children);
  331. },
  332. genTrend: function genTrend() {
  333. return this.$createElement('svg', this.setTextColor(this.color, {
  334. attrs: {
  335. 'stroke-width': this._lineWidth || 1,
  336. width: '100%',
  337. height: '25%',
  338. viewBox: '0 0 ' + this.width + ' ' + this.height
  339. }
  340. }), [this.genGradient(), this.genLabels(), this.genPath()]);
  341. }
  342. },
  343. render: function render(h) {
  344. if (this.totalBars < 2) return undefined;
  345. return this.type === 'trend' ? this.genTrend() : this.genBar();
  346. }
  347. });
  348. //# sourceMappingURL=VSparkline.js.map