GPQAPP
179 строк · 5.9 Кб
1export default class Sparkline {2constructor(element, options = {}) {3this.element = element;4this.options = { ...Sparkline.options, ...options };5
6init: {7this.element.innerHTML = "<canvas></canvas>";8this.canvas = this.element.firstChild;9this.context = this.canvas.getContext("2d");10this.ratio = window.devicePixelRatio || 1;11
12if (this.options.tooltip) {13this.canvas.style.position = "relative";14this.canvas.addEventListener('mousemove', e => {15const x = e.offsetX || e.layerX || 0;16const delta = ((this.options.width - this.options.dotRadius * 2) / (this._points.length - 1));17const index = minmax(0, Math.round((x - this.options.dotRadius) / delta), this._points.length - 1);18
19this.canvas.title = this.options.tooltip(this._points[index], index, this._points);20}, false);21}22}23}24
25set points(points) {26this.draw(points);27}28
29get points() {30return this._points;31}32
33draw(points = []) {34this._points = points;35
36this.canvas.width = this.options.width * this.ratio;37this.canvas.style.width = `${this.options.width}px`;38
39const pxHeight = this.options.height || this.element.offsetHeight;40this.canvas.height = pxHeight * this.ratio;41this.canvas.style.height = `${pxHeight}px`;42
43const lineWidth = this.options.lineWidth * this.ratio;44const offsetX = Math.max(this.options.dotRadius * this.ratio, lineWidth / 2);45const offsetY = Math.max(this.options.dotRadius * this.ratio, lineWidth / 2);46const width = this.canvas.width - offsetX * 2;47const height = this.canvas.height - offsetY * 2;48
49const minValue = Math.min.apply(Math, points);50const maxValue = Math.max.apply(Math, points);51const bottomValue = this.options.minValue != undefined ? this.options.minValue : Math.min(minValue, this.options.maxMinValue != undefined ? this.options.maxMinValue : minValue);52const topValue = this.options.maxValue != undefined ? this.options.maxValue : Math.max(maxValue, this.options.minMaxValue != undefined ? this.options.minMaxValue : maxValue);53let minX = offsetX;54let maxX = offsetX;55
56let x = offsetX;57const y = index => (topValue === bottomValue)58? offsetY + height / 259: (offsetY + height) - ((points[index] - bottomValue) / (topValue - bottomValue)) * height;60const delta = width / (points.length - 1);61
62const line = (style, x, y) => {63if (!style) return;64
65this.context.save();66this.context.strokeStyle = style.color || 'black';67this.context.lineWidth = (style.width || 1) * this.ratio;68this.context.globalAlpha = style.alpha || 1;69this.context.beginPath();70this.context.moveTo(style.direction != 'right' ? offsetX : x, y);71this.context.lineTo(style.direction != 'left' ? width + offsetX : x, y);72this.context.stroke();73this.context.restore();74}75
76const dot = (color, lineStyle, x, y) => {77this.context.beginPath();78this.context.fillStyle = color;79this.context.arc(x, y, this.options.dotRadius * this.ratio, 0, Math.PI * 2, false);80this.context.fill();81line(lineStyle, x, y);82}83
84this.context.save();85
86this.context.strokeStyle = this.options.lineColor;87this.context.fillStyle = this.options.lineColor;88this.context.lineWidth = lineWidth;89this.context.lineCap = 'round';90this.context.lineJoin = 'round';91
92if (this.options.fillBelow && points.length > 1) {93this.context.save();94this.context.beginPath();95this.context.moveTo(x, y(0));96for (let i = 1; i < points.length; i++) {97x += delta;98
99minX = points[i] == minValue ? x : minX;100maxX = points[i] == maxValue ? x : maxX;101
102this.context.lineTo(x, y(i));103}104this.context.lineTo(width + offsetX, height + offsetY + lineWidth / 2);105this.context.lineTo(offsetX, height + offsetY + lineWidth / 2);106this.context.fill();107if (this.options.fillLighten > 0) {108this.context.fillStyle = 'white';109this.context.globalAlpha = this.options.fillLighten;110this.context.fill();111this.context.globalAlpha = 1;112} else if (this.options.fillLighten < 0) {113this.context.fillStyle = 'black';114this.context.globalAlpha = -this.options.fillLighten;115this.context.fill();116}117this.context.restore();118}119
120x = offsetX;121this.context.beginPath();122this.context.moveTo(x, y(0));123for (let i = 1; i < points.length; i++) {124x += delta;125this.context.lineTo(x, y(i));126}127this.context.stroke();128
129this.context.restore();130
131line(this.options.bottomLine, 0, offsetY);132line(this.options.topLine, 0, height + offsetY + lineWidth / 2);133
134dot(this.options.startColor, this.options.startLine, offsetX + (points.length == 1 ? width / 2 : 0), y(0));135dot(this.options.endColor, this.options.endLine, offsetX + (points.length == 1 ? width / 2 : width), y(points.length - 1));136dot(this.options.minColor, this.options.minLine, minX + (points.length == 1 ? width / 2 : 0), y(points.indexOf(minValue)));137dot(this.options.maxColor, this.options.maxLine, maxX + (points.length == 1 ? width / 2 : 0), y(points.indexOf(maxValue)));138}139
140static init(element, options) {141return new Sparkline(element, options);142}143
144static draw(element, points, options) {145const sparkline = new Sparkline(element, options);146sparkline.draw(points);147return sparkline;148}149}
150
151Sparkline.options = {152width: 100,153height: null,154lineColor: "black",155lineWidth: 1.5,156startColor: "transparent",157endColor: "black",158maxColor: "transparent",159minColor: "transparent",160minValue: null,161maxValue: null,162minMaxValue: null,163maxMinValue: null,164dotRadius: 2.5,165tooltip: null,166fillBelow: true,167fillLighten: 0.5,168startLine: false,169endLine: false,170minLine: false,171maxLine: false,172bottomLine: false,173topLine: false,174averageLine: false175};176
177function minmax(a, b, c) {178return Math.max(a, Math.min(b, c));179}
180