@iyio/svg-charts 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,3 +3,4 @@ import { ChartData } from "./svg-charts-types";
3
3
  export declare const classNamePrefix = "svg-charts-";
4
4
  export declare const createEmptyChartData: () => ChartData;
5
5
  export declare const getViewBoxRect: (viewBox: string | SVGAnimatedRect | null | undefined) => Rect;
6
+ export declare const generateRandomChartId: () => string;
@@ -35,4 +35,8 @@ export const getViewBoxRect = (viewBox) => {
35
35
  };
36
36
  }
37
37
  };
38
+ let nextId = 1;
39
+ export const generateRandomChartId = () => {
40
+ return `svg-chart-${nextId++}-${Date.now()}-${Math.round(Math.random() * 999999)}`;
41
+ };
38
42
  //# sourceMappingURL=svg-charts-lib.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"svg-charts-lib.js","sourceRoot":"","sources":["../../../../../../packages/svg-charts/src/lib/svg-charts-lib.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,MAAM,eAAe,GAAC,aAAa,CAAC;AAE3C,MAAM,CAAC,MAAM,oBAAoB,GAAC,GAAY,EAAE,CAAA,CAAC;IAC7C,MAAM,EAAC,EAAE;IACT,MAAM,EAAC,EAAE;CACZ,CAAC,CAAA;AAEF,MAAM,CAAC,MAAM,cAAc,GAAC,CAAC,OAA6C,EAAM,EAAE;IAC9E,IAAG,CAAC,OAAO,EAAC;QACR,OAAO,EAAC,KAAK,EAAC,CAAC,EAAC,MAAM,EAAC,CAAC,EAAC,CAAC,EAAC,CAAC,EAAC,CAAC,EAAC,CAAC,EAAC,CAAA;KACpC;IAED,IAAG,OAAO,OAAO,KAAK,QAAQ,EAAC;QAC3B,MAAM,KAAK,GAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACjC,IAAG,KAAK,CAAC,MAAM,KAAG,CAAC,EAAC;YAChB,OAAO,EAAC,KAAK,EAAC,CAAC,EAAC,MAAM,EAAC,CAA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AAO,CAAC,OAAO,CAAC,CAAC;YACnB,CAAC,EAAC,OAAO,CAAC,OAAO,CAAC,CAAC;YACnB,KAAK,EAAC,OAAO,CAAC,OAAO,CAAC,KAAK;YAC3B,MAAM,EAAC,OAAO,CAAC,OAAO,CAAC,MAAM;SAChC,CAAA;KACJ;AACL,CAAC,CAAA;AAED,IAAI,MAAM,GAAC,CAAC,CAAC;AACb,MAAM,CAAC,MAAM,qBAAqB,GAAC,GAAE,EAAE;IACnC,OAAO,aAAa,MAAM,EAAE,IAAI,IAAI,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAC,MAAM,CAAC,EAAE,CAAA;AACpF,CAAC,CAAA"}
@@ -1,11 +1,14 @@
1
+ import { Sides } from "@iyio/common";
1
2
  export interface ChartScale {
2
3
  scale: number;
3
4
  step: number;
4
5
  }
5
6
  export interface SvgChartCtrlOptions {
7
+ id?: string;
6
8
  data?: ChartData;
7
- style?: ChartStyle;
9
+ seriesOptions?: Partial<SeriesOptions>[];
8
10
  hLines?: boolean;
11
+ vLinePadding?: number;
9
12
  hLinesFullWidth?: boolean;
10
13
  vLines?: boolean;
11
14
  hLineSpacing?: number;
@@ -15,16 +18,24 @@ export interface SvgChartCtrlOptions {
15
18
  showLabelLabels?: boolean;
16
19
  labelHAlignment?: 'left' | 'right';
17
20
  labelVAlignment?: 'top' | 'bottom';
21
+ autoResize?: boolean;
22
+ removeElementsOnDispose?: boolean;
23
+ /**
24
+ * For bar charts if true series values are stacked on top of each other
25
+ */
26
+ stack?: boolean;
27
+ css?: string;
28
+ className?: string | null;
18
29
  }
19
30
  export interface ChartData {
20
31
  labels: string[];
21
32
  series: number[][];
22
33
  }
23
- export interface ChartStyle {
24
- series?: Partial<SeriesStyle>[];
25
- }
26
- export interface SeriesStyle {
34
+ export interface SeriesOptions {
27
35
  smoothness: number;
36
+ thickness: number;
37
+ margin: number;
38
+ cornerRadius: number;
28
39
  }
29
40
  export interface ChartRenderOptions {
30
41
  min: number;
@@ -32,14 +43,21 @@ export interface ChartRenderOptions {
32
43
  diff: number;
33
44
  width: number;
34
45
  height: number;
35
- hLineSpacing: number;
36
- hLineCount: number;
37
- valueCount: number;
38
- valueStep: number;
39
46
  left: number;
40
47
  right: number;
41
48
  top: number;
42
49
  bottom: number;
50
+ hLineSpacing: number;
51
+ hLineCount: number;
52
+ valueCount: number;
53
+ valueStep: number;
43
54
  viewBoxWidth: number;
44
55
  viewBoxHeight: number;
56
+ renderPadding: Sides;
57
+ canvasWidth: number;
58
+ canvasHeight: number;
59
+ canvasLeft: number;
60
+ canvasRight: number;
61
+ canvasTop: number;
62
+ canvasBottom: number;
45
63
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@iyio/svg-charts",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "main": "./src/index.js",
5
5
  "types": "./src/index.d.ts",
6
6
  "dependencies": {},
7
7
  "peerDependencies": {
8
- "@iyio/common": "^0.2.7",
8
+ "@iyio/common": "^0.2.10",
9
9
  "rxjs": ">=7.5.7",
10
10
  "date-fns": ">=2.29.3",
11
11
  "uuid": ">=9.0.0",
package/src/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './lib/svg-charts-lib';
2
2
  export * from './lib/svg-charts-types';
3
+ export * from './lib/SvgBarChartCtrl';
3
4
  export * from './lib/SvgBaseChartCtrl';
4
5
  export * from './lib/SvgLineChartCtrl';
package/src/index.js CHANGED
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./lib/svg-charts-lib"), exports);
5
5
  tslib_1.__exportStar(require("./lib/svg-charts-types"), exports);
6
+ tslib_1.__exportStar(require("./lib/SvgBarChartCtrl"), exports);
6
7
  tslib_1.__exportStar(require("./lib/SvgBaseChartCtrl"), exports);
7
8
  tslib_1.__exportStar(require("./lib/SvgLineChartCtrl"), exports);
8
9
  //# sourceMappingURL=index.js.map
package/src/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/svg-charts/src/index.ts"],"names":[],"mappings":";;;AAAA,+DAAqC;AACrC,iEAAuC;AACvC,iEAAuC;AACvC,iEAAuC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/svg-charts/src/index.ts"],"names":[],"mappings":";;;AAAA,+DAAqC;AACrC,iEAAuC;AACvC,gEAAsC;AACtC,iEAAuC;AACvC,iEAAuC"}
@@ -0,0 +1,11 @@
1
+ import { Sides } from "@iyio/common";
2
+ import { SvgChartCtrlOptions } from "./svg-charts-types";
3
+ import { SvgBaseChartCtrl } from "./SvgBaseChartCtrl";
4
+ export declare class SvgBarChartCtrl extends SvgBaseChartCtrl {
5
+ constructor(options?: SvgChartCtrlOptions | null, svg?: SVGSVGElement, skipRender?: boolean);
6
+ private readonly lines;
7
+ protected renderData(): void;
8
+ private updateBar;
9
+ private removeLine;
10
+ protected getRenderPadding(preCanvasWidth: number): Sides;
11
+ }
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SvgBarChartCtrl = void 0;
4
+ const common_1 = require("@iyio/common");
5
+ const svg_charts_lib_1 = require("./svg-charts-lib");
6
+ const SvgBaseChartCtrl_1 = require("./SvgBaseChartCtrl");
7
+ class SvgBarChartCtrl extends SvgBaseChartCtrl_1.SvgBaseChartCtrl {
8
+ constructor(options, svg, skipRender) {
9
+ super(options, svg, true);
10
+ this.lines = [];
11
+ if (!skipRender) {
12
+ this.render();
13
+ }
14
+ }
15
+ renderData() {
16
+ while (this.lines.length > this.renderOptions.valueCount) {
17
+ this.removeLine(this.lines[this.lines.length - 1]);
18
+ }
19
+ for (let i = 0; i < this.data.series.length; i++) {
20
+ const data = this.data.series[i];
21
+ if (this.lines.length <= i) {
22
+ const group = document.createElementNS('http://www.w3.org/2000/svg', 'g');
23
+ const classes = {
24
+ [`${svg_charts_lib_1.classNamePrefix}odd`]: i % 2 ? false : true,
25
+ [`${svg_charts_lib_1.classNamePrefix}even`]: i % 2 ? true : false,
26
+ [`${svg_charts_lib_1.classNamePrefix}${i + 1}`]: true,
27
+ };
28
+ group.setAttribute('class', (0, common_1.cn)(`${svg_charts_lib_1.classNamePrefix}bar`, classes));
29
+ const line = {
30
+ group,
31
+ data,
32
+ index: i
33
+ };
34
+ this.canvas.appendChild(group);
35
+ this.lines.push(line);
36
+ }
37
+ this.updateBar(this.lines[i]);
38
+ }
39
+ }
40
+ updateBar(bar) {
41
+ var _a, _b;
42
+ const style = this.getSeriesStyle(bar.index);
43
+ const count = ((_b = (_a = this.data.series[0]) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) - 1;
44
+ const { width, height, min, diff } = this.renderOptions;
45
+ const dist = width / count;
46
+ while (bar.group.children.length > bar.data.length) {
47
+ bar.group.children[bar.group.children.length - 1].remove();
48
+ }
49
+ for (let i = 0; i < bar.data.length; i++) {
50
+ let rect = bar.group.children[i];
51
+ if (!rect) {
52
+ rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
53
+ rect.setAttribute('class', 'svg-charts-fill');
54
+ bar.group.appendChild(rect);
55
+ }
56
+ const v = bar.data[i];
57
+ const x = dist * i;
58
+ const y = height - ((v - min) / diff * height);
59
+ rect.setAttribute('x', (x - style.thickness / 2).toString());
60
+ rect.setAttribute('y', y.toString());
61
+ rect.setAttribute('width', style.thickness.toString());
62
+ rect.setAttribute('height', (height - y).toString());
63
+ rect.setAttribute('rx', style.cornerRadius.toString());
64
+ }
65
+ }
66
+ removeLine(line) {
67
+ line.group.remove();
68
+ (0, common_1.aryRemoveItem)(this.lines, line);
69
+ }
70
+ getRenderPadding(preCanvasWidth) {
71
+ const count = this.data.labels.length;
72
+ const m = count < 2 ? preCanvasWidth / 2 : (preCanvasWidth / (count - 1) / 2);
73
+ return {
74
+ left: m,
75
+ right: m,
76
+ top: 0,
77
+ bottom: 0,
78
+ };
79
+ }
80
+ }
81
+ exports.SvgBarChartCtrl = SvgBarChartCtrl;
82
+ //# sourceMappingURL=SvgBarChartCtrl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SvgBarChartCtrl.js","sourceRoot":"","sources":["../../../../../packages/svg-charts/src/lib/SvgBarChartCtrl.ts"],"names":[],"mappings":";;;AAAA,yCAAwD;AACxD,qDAAmD;AAEnD,yDAAsD;AAStD,MAAa,eAAgB,SAAQ,mCAAgB;IAGjD,YAAmB,OAAiC,EAAE,GAAkB,EAAE,UAAmB;QAEzF,KAAK,CAAC,OAAO,EAAC,GAAG,EAAC,IAAI,CAAC,CAAC;QAOX,UAAK,GAAO,EAAE,CAAC;QAL5B,IAAG,CAAC,UAAU,EAAC;YACX,IAAI,CAAC,MAAM,EAAE,CAAC;SACjB;IACL,CAAC;IAGS,UAAU;QAEhB,OAAM,IAAI,CAAC,KAAK,CAAC,MAAM,GAAC,IAAI,CAAC,aAAa,CAAC,UAAU,EAAC;YAClD,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,GAAC,CAAC,CAAC,CAAC,CAAC;SACpD;QAED,KAAI,IAAI,CAAC,GAAC,CAAC,EAAC,CAAC,GAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAC,CAAC,EAAE,EAAC;YACtC,MAAM,IAAI,GAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;YAC/B,IAAG,IAAI,CAAC,KAAK,CAAC,MAAM,IAAE,CAAC,EAAC;gBACpB,MAAM,KAAK,GAAC,QAAQ,CAAC,eAAe,CAAC,4BAA4B,EAAC,GAAG,CAAC,CAAC;gBACvE,MAAM,OAAO,GAAC;oBACV,CAAC,GAAG,gCAAe,KAAK,CAAC,EAAC,CAAC,GAAC,CAAC,CAAA,CAAC,CAAA,KAAK,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I,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;QACpB,IAAA,sBAAa,EAAC,IAAI,CAAC,KAAK,EAAC,IAAI,CAAC,CAAC;IACnC,CAAC;IAEkB,gBAAgB,CAAC,cAAqB;QACrD,MAAM,KAAK,GAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;QACpC,MAAM,CAAC,GAAC,KAAK,GAAC,CAAC,CAAA,CAAC,CAAA,cAAc,GAAC,CAAC,CAAA,CAAC,CAAA,CAAC,cAAc,GAAC,CAAC,KAAK,GAAC,CAAC,CAAC,GAAC,CAAC,CAAC,CAAA;QAC7D,OAAO;YACH,IAAI,EAAC,CAAC;YACN,KAAK,EAAC,CAAC;YACP,GAAG,EAAC,CAAC;YACL,MAAM,EAAC,CAAC;SACX,CAAA;IACL,CAAC;CACJ;AA1FD,0CA0FC"}
@@ -1,5 +1,5 @@
1
1
  import { Sides, Size } from "@iyio/common";
2
- import { ChartData, ChartRenderOptions, ChartStyle, SeriesStyle, SvgChartCtrlOptions } from "./svg-charts-types";
2
+ import { ChartData, ChartRenderOptions, SeriesOptions, SvgChartCtrlOptions } from "./svg-charts-types";
3
3
  export declare abstract class SvgBaseChartCtrl {
4
4
  readonly svg: SVGSVGElement;
5
5
  /**
@@ -11,25 +11,37 @@ export declare abstract class SvgBaseChartCtrl {
11
11
  * drawn around the canvas.
12
12
  */
13
13
  readonly canvas: SVGGElement;
14
+ private readonly styleElem;
14
15
  private _data;
15
16
  get data(): ChartData;
16
17
  set data(value: ChartData);
17
- private _style;
18
- get style(): ChartStyle;
19
- set style(value: ChartStyle);
18
+ private _seriesOptions;
19
+ get seriesOptions(): Partial<SeriesOptions>[];
20
+ set seriesOptions(value: Partial<SeriesOptions>[]);
20
21
  private _options;
22
+ get options(): Partial<SvgChartCtrlOptions>;
23
+ set options(value: Partial<SvgChartCtrlOptions>);
24
+ private parseCss;
21
25
  private _canvasPadding;
22
26
  get canvasPadding(): Sides;
23
27
  set canvasPadding(value: Sides);
28
+ get id(): string;
24
29
  private _viewBox;
25
30
  get viewBox(): Size;
26
31
  private _renderOptions;
27
32
  get renderOptions(): ChartRenderOptions;
28
- constructor(options?: SvgChartCtrlOptions | null, svg?: SVGSVGElement);
33
+ constructor(options?: SvgChartCtrlOptions | null, svg?: SVGSVGElement, skipRender?: boolean);
34
+ private readonly resizeObserver;
35
+ private resizeObserved;
29
36
  private _isDisposed;
30
37
  get isDisposed(): boolean;
31
38
  dispose(): void;
39
+ private readonly onResize;
40
+ private prevClassName;
41
+ private updateCss;
42
+ private setOptions;
32
43
  protected getRenderOptions(): ChartRenderOptions;
44
+ private pauseRender;
33
45
  render(): void;
34
46
  protected abstract renderData(): void;
35
47
  private hLines;
@@ -44,8 +56,9 @@ export declare abstract class SvgBaseChartCtrl {
44
56
  private vLabelGroup;
45
57
  private vLabels;
46
58
  protected renderLabelLabels(): void;
47
- private seriesStyle;
48
- protected fillSeriesStyle(style: Partial<SeriesStyle>): SeriesStyle;
49
- protected getDefaultSeriesStyle(): SeriesStyle;
50
- getSeriesStyle(index: number): SeriesStyle;
59
+ private filledSeriesOptions;
60
+ protected fillSeriesStyle(style: Partial<SeriesOptions>): SeriesOptions;
61
+ protected getDefaultSeriesStyle(): SeriesOptions;
62
+ getSeriesStyle(index: number): SeriesOptions;
63
+ protected getRenderPadding(preCanvasWidth: number, preCanvasHeight: number): Sides;
51
64
  }
@@ -5,11 +5,20 @@ const common_1 = require("@iyio/common");
5
5
  const svg_charts_lib_1 = require("./svg-charts-lib");
6
6
  const steps = [0.001, 0.01, 0.1, 1, 2, 5, 10, 20, 50, 100];
7
7
  class SvgBaseChartCtrl {
8
- constructor(options, svg) {
9
- var _a, _b, _c;
8
+ constructor(options, svg, skipRender) {
9
+ var _a, _b, _c, _d;
10
+ this._seriesOptions = [];
10
11
  this._canvasPadding = { left: 60, right: 35, top: 35, bottom: 35 };
11
12
  this._viewBox = { width: 1, height: 1 };
13
+ this.resizeObserved = false;
12
14
  this._isDisposed = false;
15
+ this.onResize = () => {
16
+ if (this._options.autoResize) {
17
+ this.render();
18
+ }
19
+ };
20
+ this.prevClassName = null;
21
+ this.pauseRender = false;
13
22
  this.hLines = [];
14
23
  this.hLineGroup = null;
15
24
  this.vLines = [];
@@ -18,7 +27,7 @@ class SvgBaseChartCtrl {
18
27
  this.hLabels = [];
19
28
  this.vLabelGroup = null;
20
29
  this.vLabels = [];
21
- this.seriesStyle = [];
30
+ this.filledSeriesOptions = [];
22
31
  if (!svg) {
23
32
  svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
24
33
  }
@@ -29,11 +38,34 @@ class SvgBaseChartCtrl {
29
38
  this.canvas = document.createElementNS('http://www.w3.org/2000/svg', 'g');
30
39
  this.root.classList.add(svg_charts_lib_1.classNamePrefix + 'canvas');
31
40
  this.root.appendChild(this.canvas);
41
+ this.styleElem = document.createElement('style');
42
+ this.svg.appendChild(this.styleElem);
32
43
  this._data = (_a = options === null || options === void 0 ? void 0 : options.data) !== null && _a !== void 0 ? _a : (0, svg_charts_lib_1.createEmptyChartData)();
33
- this._style = (_b = options === null || options === void 0 ? void 0 : options.style) !== null && _b !== void 0 ? _b : {};
34
- this.seriesStyle = ((_c = this._style.series) !== null && _c !== void 0 ? _c : []).map(s => this.fillSeriesStyle(s));
35
- this._options = Object.assign(Object.assign({ hLines: true, hLinesFullWidth: false, vLines: true, hLineSpacing: 50, labelHAlignment: 'left', labelVAlignment: 'bottom', zoom: 1, viewBox: null, showLabelLabels: true, showValueLabels: true }, (options !== null && options !== void 0 ? options : {})), { data: this._data, style: this.style });
44
+ this.seriesOptions = (_b = options === null || options === void 0 ? void 0 : options.seriesOptions) !== null && _b !== void 0 ? _b : [];
45
+ this._options = Object.assign({ id: (_c = options === null || options === void 0 ? void 0 : options.id) !== null && _c !== void 0 ? _c : (this.svg.id || (0, svg_charts_lib_1.generateRandomChartId)()), hLines: true, hLinesFullWidth: false, vLines: true, hLineSpacing: 50, labelHAlignment: 'left', labelVAlignment: 'bottom', zoom: 1, viewBox: null, showLabelLabels: true, showValueLabels: true, stack: false, vLinePadding: 0, css: '', className: (_d = options === null || options === void 0 ? void 0 : options.className) !== null && _d !== void 0 ? _d : null, autoResize: true, removeElementsOnDispose: true }, (options !== null && options !== void 0 ? options : {}));
36
46
  this._renderOptions = this.getRenderOptions();
47
+ this.updateCss();
48
+ if (!skipRender) {
49
+ this.render();
50
+ }
51
+ window.addEventListener('resize', this.onResize);
52
+ if (window.ResizeObserver) {
53
+ let size = { width: this.svg.clientWidth, height: this.svg.clientHeight };
54
+ this.resizeObserver = new ResizeObserver(() => {
55
+ if (!this.resizeObserved) {
56
+ this.resizeObserved = true;
57
+ window.removeEventListener('resize', this.onResize);
58
+ }
59
+ if (size.width !== this.svg.clientWidth || size.height !== this.svg.clientHeight) {
60
+ size = { width: this.svg.clientWidth, height: this.svg.clientHeight };
61
+ this.render();
62
+ }
63
+ });
64
+ this.resizeObserver.observe(this.svg);
65
+ }
66
+ else {
67
+ this.resizeObserver = null;
68
+ }
37
69
  }
38
70
  get data() { return this._data; }
39
71
  set data(value) {
@@ -43,16 +75,25 @@ class SvgBaseChartCtrl {
43
75
  this._data = value;
44
76
  this.render();
45
77
  }
46
- get style() { return this._style; }
47
- set style(value) {
48
- var _a;
49
- if (this._style === value) {
78
+ get seriesOptions() { return this._seriesOptions; }
79
+ set seriesOptions(value) {
80
+ if (this._seriesOptions === value) {
50
81
  return;
51
82
  }
52
- this._style = value;
53
- this.seriesStyle = ((_a = value.series) !== null && _a !== void 0 ? _a : []).map(s => this.fillSeriesStyle(s));
83
+ this._seriesOptions = value;
84
+ this.filledSeriesOptions = value.map(s => this.fillSeriesStyle(s));
54
85
  this.render();
55
86
  }
87
+ get options() { return Object.assign({}, this._options); }
88
+ set options(value) {
89
+ this.setOptions(value);
90
+ }
91
+ parseCss(css, id) {
92
+ if (id) {
93
+ id = '#' + id;
94
+ }
95
+ return css.split('@@').join(id);
96
+ }
56
97
  get canvasPadding() { return this._canvasPadding; }
57
98
  set canvasPadding(value) {
58
99
  if (this._canvasPadding === value) {
@@ -61,21 +102,102 @@ class SvgBaseChartCtrl {
61
102
  this._canvasPadding = value;
62
103
  this.render();
63
104
  }
105
+ get id() { return this._options.id; }
64
106
  get viewBox() { return this._viewBox; }
65
107
  get renderOptions() { return this._renderOptions; }
66
108
  get isDisposed() { return this._isDisposed; }
67
109
  dispose() {
110
+ var _a, _b;
68
111
  if (this._isDisposed) {
69
112
  return;
70
113
  }
71
114
  this._isDisposed = true;
72
- this.root.remove();
115
+ window.removeEventListener('resize', this.onResize);
116
+ (_a = this.resizeObserver) === null || _a === void 0 ? void 0 : _a.unobserve(this.svg);
117
+ (_b = this.resizeObserver) === null || _b === void 0 ? void 0 : _b.disconnect();
118
+ if (this._options.removeElementsOnDispose) {
119
+ this.root.remove();
120
+ this.styleElem.remove();
121
+ }
122
+ }
123
+ updateCss() {
124
+ this.styleElem.innerHTML = this.parseCss(this._options.css, this._options.id);
125
+ this.svg.id = this._options.id;
126
+ if (this.prevClassName !== this._options.className) {
127
+ if (this.prevClassName) {
128
+ this.svg.classList.remove(this.prevClassName);
129
+ }
130
+ if (this._options.className) {
131
+ this.svg.classList.add(this._options.className);
132
+ }
133
+ }
134
+ }
135
+ setOptions(value, skipRender = false) {
136
+ if (!value) {
137
+ return;
138
+ }
139
+ let changed = false;
140
+ let updateCss = false;
141
+ this.pauseRender = true;
142
+ try {
143
+ for (const e in value) {
144
+ const v = value[e];
145
+ let _continue = true;
146
+ switch (e) {
147
+ case 'data':
148
+ if (v !== this._data) {
149
+ changed = true;
150
+ this.data = v;
151
+ }
152
+ break;
153
+ case 'seriesOptions': {
154
+ const current = this.filledSeriesOptions;
155
+ this.seriesOptions = v;
156
+ if (!(0, common_1.deepCompare)(current, this.filledSeriesOptions)) {
157
+ changed = true;
158
+ }
159
+ break;
160
+ }
161
+ case 'className':
162
+ case 'id':
163
+ case 'css':
164
+ if (value.id !== this._options.id || value.css !== this._options.css) {
165
+ updateCss = true;
166
+ }
167
+ break;
168
+ default:
169
+ _continue = false;
170
+ break;
171
+ }
172
+ if (_continue) {
173
+ this._options[e] = v;
174
+ continue;
175
+ }
176
+ if (e === undefined || this._options[e] === v) {
177
+ continue;
178
+ }
179
+ changed = true;
180
+ this._options[e] = v;
181
+ }
182
+ }
183
+ finally {
184
+ this.pauseRender = false;
185
+ }
186
+ if (updateCss) {
187
+ this.updateCss();
188
+ }
189
+ if (changed && !skipRender) {
190
+ this.render();
191
+ }
73
192
  }
74
193
  getRenderOptions() {
75
194
  var _a, _b, _c, _d;
76
195
  const valueCount = (_d = (_c = (_b = (_a = this.data) === null || _a === void 0 ? void 0 : _a.series) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0;
77
- const width = Math.max(0, this._viewBox.width - this._canvasPadding.left - this._canvasPadding.right) || 1;
78
- const height = Math.max(0, this._viewBox.height - this._canvasPadding.top - this._canvasPadding.bottom) || 1;
196
+ const preCanvasWidth = Math.max(0, this._viewBox.width - this._canvasPadding.left - this._canvasPadding.right) || 1;
197
+ const preCanvasHeight = Math.max(0, this._viewBox.height - this._canvasPadding.top - this._canvasPadding.bottom) || 1;
198
+ const renderPadding = this.getRenderPadding(preCanvasWidth, preCanvasHeight);
199
+ const width = Math.max(0, this._viewBox.width - this._canvasPadding.left - this._canvasPadding.right - renderPadding.left - renderPadding.right) || 1;
200
+ const height = Math.max(0, this._viewBox.height - this._canvasPadding.top - this._canvasPadding.bottom - renderPadding.top - renderPadding.bottom) || 1;
79
201
  let min = Number.MAX_VALUE;
80
202
  let max = Number.MIN_VALUE;
81
203
  for (const series of this.data.series) {
@@ -93,8 +215,6 @@ class SvgBaseChartCtrl {
93
215
  const layoutScale = (this.svg.clientHeight || 1) / this._viewBox.height;
94
216
  const canvasHeightPx = height * layoutScale;
95
217
  let hLineCount = Math.ceil(canvasHeightPx / hLineSpacingPx);
96
- console.log('----');
97
- console.log({ hLineCount, height, layoutScale }, this.viewBox);
98
218
  let valueStep = -1;
99
219
  let diff = max - min;
100
220
  if (diff >= 2) {
@@ -138,16 +258,10 @@ class SvgBaseChartCtrl {
138
258
  if (valueStep === -1) {
139
259
  valueStep = diff / (hLineCount - 1);
140
260
  }
141
- console.log({ diff, valueStep, hLineCount, hLineSpacing });
142
- console.log({ min, max, height, canvasHeightPx, viewBoxHeight: this._viewBox.height });
143
- // for(let i=1;i<steps.length;i++){
144
- // const spacing=hLineCount*steps[i];
145
- // if(spacing>=height){
146
- // console.log({spacing},steps[i])
147
- // hLineSpacing=steps[i];
148
- // break;
149
- // }
150
- // }
261
+ const left = this._canvasPadding.left + renderPadding.left;
262
+ const top = this._canvasPadding.top + renderPadding.top;
263
+ const right = this._viewBox.width - this.canvasPadding.right - renderPadding.right;
264
+ const bottom = this._viewBox.height - this.canvasPadding.bottom - renderPadding.bottom;
151
265
  return {
152
266
  min,
153
267
  max,
@@ -157,16 +271,26 @@ class SvgBaseChartCtrl {
157
271
  hLineSpacing,
158
272
  valueCount,
159
273
  hLineCount,
160
- left: this._canvasPadding.left,
161
- top: this._canvasPadding.top,
162
- right: this._viewBox.width - this.canvasPadding.right,
163
- bottom: this._viewBox.height - this.canvasPadding.bottom,
274
+ left,
275
+ top,
276
+ right,
277
+ bottom,
164
278
  viewBoxWidth: this._viewBox.width,
165
279
  viewBoxHeight: this._viewBox.height,
166
- valueStep
280
+ valueStep,
281
+ renderPadding,
282
+ canvasWidth: width + renderPadding.left + renderPadding.right,
283
+ canvasHeight: height + renderPadding.top + renderPadding.bottom,
284
+ canvasLeft: left - renderPadding.left,
285
+ canvasRight: right + renderPadding.right,
286
+ canvasTop: top - renderPadding.top,
287
+ canvasBottom: bottom + renderPadding.bottom,
167
288
  };
168
289
  }
169
290
  render() {
291
+ if (this.pauseRender || !this._options) {
292
+ return;
293
+ }
170
294
  if (this._options.viewBox) {
171
295
  this.svg.setAttribute('viewBox', this._options.viewBox);
172
296
  }
@@ -179,7 +303,7 @@ class SvgBaseChartCtrl {
179
303
  const rect = (0, svg_charts_lib_1.getViewBoxRect)(this.svg.viewBox);
180
304
  this._viewBox = { width: rect.width || 1, height: rect.height || 1 };
181
305
  this._renderOptions = this.getRenderOptions();
182
- this.canvas.setAttribute('transform', `translate(${this._canvasPadding.left},${this._canvasPadding.top})`);
306
+ this.canvas.setAttribute('transform', `translate(${this._renderOptions.left},${this._renderOptions.top})`);
183
307
  this.renderValueLines();
184
308
  this.renderLabelLines();
185
309
  this.renderValueLabels();
@@ -188,7 +312,7 @@ class SvgBaseChartCtrl {
188
312
  }
189
313
  renderValueLines() {
190
314
  var _a;
191
- const { hLineCount, hLineSpacing, bottom, left, right, } = this.renderOptions;
315
+ const { hLineCount, hLineSpacing, bottom, canvasLeft: left, canvasRight: right, } = this.renderOptions;
192
316
  const { hLines, hLinesFullWidth } = this._options;
193
317
  if (hLines) {
194
318
  if (!this.hLineGroup) {
@@ -257,7 +381,7 @@ class SvgBaseChartCtrl {
257
381
  }
258
382
  renderValueLabels() {
259
383
  var _a;
260
- const { hLineCount, hLineSpacing, bottom, left, min, valueStep } = this.renderOptions;
384
+ const { hLineCount, hLineSpacing, canvasBottom: bottom, canvasLeft: left, min, valueStep } = this.renderOptions;
261
385
  const { hLines, } = this._options;
262
386
  if (hLines) {
263
387
  if (!this.hLabelGroup) {
@@ -305,7 +429,7 @@ class SvgBaseChartCtrl {
305
429
  }
306
430
  renderLabelLabels() {
307
431
  var _a, _b;
308
- const { left, valueCount, width, viewBoxHeight, bottom } = this.renderOptions;
432
+ const { left, valueCount, width, viewBoxHeight, bottom, renderPadding } = this.renderOptions;
309
433
  const { showLabelLabels, } = this._options;
310
434
  if (showLabelLabels) {
311
435
  if (!this.vLabelGroup) {
@@ -317,8 +441,8 @@ class SvgBaseChartCtrl {
317
441
  (_a = this.vLabels.pop()) === null || _a === void 0 ? void 0 : _a.remove();
318
442
  }
319
443
  for (let i = 0; i < valueCount; i++) {
320
- const isFirst = i === 0;
321
- const isLast = i === valueCount - 1;
444
+ const isFirst = i === 0 && renderPadding.left < 20;
445
+ const isLast = i === valueCount - 1 && renderPadding.right < 20;
322
446
  if (this.vLabels.length <= i) {
323
447
  const newLine = document.createElementNS('http://www.w3.org/2000/svg', 'foreignObject');
324
448
  newLine.setAttribute('class', `${svg_charts_lib_1.classNamePrefix}label-label`);
@@ -355,11 +479,18 @@ class SvgBaseChartCtrl {
355
479
  getDefaultSeriesStyle() {
356
480
  return {
357
481
  smoothness: 0,
482
+ thickness: 30,
483
+ margin: 20,
484
+ cornerRadius: 0,
358
485
  };
359
486
  }
360
487
  getSeriesStyle(index) {
361
488
  var _a;
362
- return (_a = this.seriesStyle[index % this.seriesStyle.length]) !== null && _a !== void 0 ? _a : this.fillSeriesStyle({});
489
+ return (_a = this.filledSeriesOptions[index % this.filledSeriesOptions.length]) !== null && _a !== void 0 ? _a : this.fillSeriesStyle({});
490
+ }
491
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
492
+ getRenderPadding(preCanvasWidth, preCanvasHeight) {
493
+ return { left: 0, right: 0, top: 0, bottom: 0 };
363
494
  }
364
495
  }
365
496
  exports.SvgBaseChartCtrl = SvgBaseChartCtrl;