@triptease/tt-bar-chart 0.2.0 → 0.2.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.
@@ -1,4 +1,5 @@
1
1
  import { LitElement } from 'lit';
2
+ import * as echarts from 'echarts';
2
3
  import { Dataset } from './types.js';
3
4
  export declare class TtBarChart extends LitElement {
4
5
  datasets: Dataset[];
@@ -10,9 +11,12 @@ export declare class TtBarChart extends LitElement {
10
11
  showLegend: boolean;
11
12
  showDataLabels: boolean;
12
13
  showTooltip: boolean;
14
+ stacked: boolean;
15
+ chart: echarts.ECharts | null;
13
16
  private getSeriesColor;
14
17
  render(): import("lit-html").TemplateResult<1>;
15
18
  protected firstUpdated(): void;
19
+ protected updated(): void;
16
20
  }
17
21
  declare global {
18
22
  interface HTMLElementTagNameMap {
@@ -7,11 +7,11 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  import { html, LitElement } from 'lit';
8
8
  // @ts-expect-error Known error - https://github.com/apache/echarts/issues/21250
9
9
  import * as echarts from 'echarts';
10
- import { customElement, property } from 'lit/decorators.js';
11
- import { formatter, jsonConvertor } from './utils.js';
10
+ import { property, state } from 'lit/decorators.js';
11
+ import { axesFormatter, dataLabelsFormatter, jsonConvertor } from './utils.js';
12
12
  // @ts-expect-error Known error - https://github.com/apache/echarts/issues/21250
13
13
  import { AriaComponent } from 'echarts/components';
14
- let TtBarChart = class TtBarChart extends LitElement {
14
+ export class TtBarChart extends LitElement {
15
15
  constructor() {
16
16
  super(...arguments);
17
17
  this.datasets = [];
@@ -21,6 +21,8 @@ let TtBarChart = class TtBarChart extends LitElement {
21
21
  this.showLegend = false;
22
22
  this.showDataLabels = false;
23
23
  this.showTooltip = false;
24
+ this.stacked = false;
25
+ this.chart = null;
24
26
  this.getSeriesColor = (dataset) => {
25
27
  if (this.datasets && this.datasets.length > 1)
26
28
  return undefined;
@@ -33,26 +35,31 @@ let TtBarChart = class TtBarChart extends LitElement {
33
35
  return html ` <div id=${this.id} style="width: 100%; height:100%;"></div> `;
34
36
  }
35
37
  firstUpdated() {
36
- const divElement = this.renderRoot.querySelector(`#${this.id}`);
37
- if (!divElement) {
38
- throw new Error('No div element');
38
+ if (this.chart === null) {
39
+ echarts.use([AriaComponent]);
40
+ const divElement = this.renderRoot.querySelector(`#${this.id}`);
41
+ if (!divElement) {
42
+ throw new Error('No div element');
43
+ }
44
+ this.chart = echarts.init(divElement);
39
45
  }
40
- const chart = echarts.init(divElement);
41
- echarts.use([AriaComponent]);
46
+ }
47
+ updated() {
42
48
  const labelsRaw = this.datasets.flatMap((dataset) => dataset.data.map((datapoint) => datapoint.label));
43
49
  const labels = [...new Set(labelsRaw)];
44
50
  const series = this.datasets.map((dataset) => ({
45
51
  name: dataset.label,
46
52
  type: 'bar',
47
- data: labels.map(label => {
48
- const point = dataset.data.find(dp => dp.label === label);
53
+ data: labels.map((label) => {
54
+ const point = dataset.data.find((dp) => dp.label === label);
49
55
  return point?.value ?? null;
50
56
  }),
57
+ stack: this.stacked ? "A" : undefined,
51
58
  color: this.getSeriesColor(dataset),
52
59
  label: {
53
60
  show: this.showDataLabels,
54
61
  position: 'right',
55
- formatter: ({ value }) => formatter.format(value),
62
+ formatter: ({ value }) => dataLabelsFormatter.format(value),
56
63
  },
57
64
  cursor: 'default',
58
65
  }));
@@ -68,14 +75,20 @@ let TtBarChart = class TtBarChart extends LitElement {
68
75
  nameLocation: 'center',
69
76
  type: this.direction === 'horizontal' ? 'value' : 'category',
70
77
  data: this.direction === 'horizontal' ? undefined : labels,
71
- splitLine: { show: this.showHorizontalGrid },
78
+ axisLabel: {
79
+ formatter: axesFormatter,
80
+ },
81
+ splitLine: { show: this.showVerticalGrid },
72
82
  },
73
83
  yAxis: {
74
84
  name: this.yAxisTitle,
75
85
  nameLocation: 'center',
76
86
  type: this.direction === 'horizontal' ? 'category' : 'value',
77
87
  data: this.direction === 'horizontal' ? labels : undefined,
78
- splitLine: { show: this.showVerticalGrid },
88
+ axisLabel: {
89
+ formatter: axesFormatter,
90
+ },
91
+ splitLine: { show: this.showHorizontalGrid },
79
92
  },
80
93
  grid: {
81
94
  left: 0,
@@ -93,9 +106,9 @@ let TtBarChart = class TtBarChart extends LitElement {
93
106
  }),
94
107
  series,
95
108
  };
96
- chart.setOption(option);
109
+ this.chart.setOption(option);
97
110
  }
98
- };
111
+ }
99
112
  __decorate([
100
113
  property({ type: Array, converter: jsonConvertor })
101
114
  ], TtBarChart.prototype, "datasets", void 0);
@@ -123,8 +136,10 @@ __decorate([
123
136
  __decorate([
124
137
  property({ type: Boolean, attribute: 'show-tooltip' })
125
138
  ], TtBarChart.prototype, "showTooltip", void 0);
126
- TtBarChart = __decorate([
127
- customElement('tt-bar-chart')
128
- ], TtBarChart);
129
- export { TtBarChart };
139
+ __decorate([
140
+ property({ type: Boolean, attribute: 'stacked' })
141
+ ], TtBarChart.prototype, "stacked", void 0);
142
+ __decorate([
143
+ state()
144
+ ], TtBarChart.prototype, "chart", void 0);
130
145
  //# sourceMappingURL=TtBarChart.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"TtBarChart.js","sourceRoot":"","sources":["../../src/TtBarChart.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AACvC,gFAAgF;AAChF,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE5D,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACtD,gFAAgF;AAChF,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAG5C,IAAM,UAAU,GAAhB,MAAM,UAAW,SAAQ,UAAU;IAAnC;;QAEL,aAAQ,GAAc,EAAE,CAAC;QAGzB,cAAS,GAA8B,YAAY,CAAC;QASpD,uBAAkB,GAAY,KAAK,CAAC;QAGpC,qBAAgB,GAAY,K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CAC1B;AAG5B;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,kBAAkB,EAAE,CAAC;kDAC3B;AAGhC;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,CAAC;+CAC1B;AA1BlB,UAAU;IADtB,aAAa,CAAC,cAAc,CAAC;GACjB,UAAU,CA4GtB","sourcesContent":["import { html, LitElement } from 'lit';\n// @ts-expect-error Known error - https://github.com/apache/echarts/issues/21250\nimport * as echarts from 'echarts';\nimport { customElement, property } from 'lit/decorators.js';\nimport { Dataset } from './types.js';\nimport { formatter, jsonConvertor } from './utils.js';\n// @ts-expect-error Known error - https://github.com/apache/echarts/issues/21250\nimport { AriaComponent } from 'echarts/components';\n\n@customElement('tt-bar-chart')\nexport class TtBarChart extends LitElement {\n @property({ type: Array, converter: jsonConvertor })\n datasets: Dataset[] = [];\n\n @property({ type: String })\n direction: 'horizontal' | 'vertical' = 'horizontal';\n\n @property({ type: String, attribute: 'x-axis-title' })\n xAxisTitle?: string;\n\n @property({ type: String, attribute: 'y-axis-title' })\n yAxisTitle?: string;\n\n @property({ type: Boolean, attribute: 'show-horizontal-grid' })\n showHorizontalGrid: boolean = false;\n\n @property({ type: Boolean, attribute: 'show-vertical-grid' })\n showVerticalGrid: boolean = false;\n\n @property({ type: Boolean, attribute: 'show-legend' })\n showLegend: boolean = false;\n\n @property({ type: Boolean, attribute: 'show-data-labels' })\n showDataLabels: boolean = false;\n\n @property({ type: Boolean, attribute: 'show-tooltip' })\n showTooltip: boolean = false;\n\n private getSeriesColor = (dataset: Dataset) => {\n if (this.datasets && this.datasets.length > 1) return undefined;\n if (dataset.color) return dataset.color;\n\n return '#4d35a1';\n };\n\n render() {\n return html` <div id=${this.id} style=\"width: 100%; height:100%;\"></div> `;\n }\n\n protected firstUpdated() {\n const divElement = this.renderRoot.querySelector(`#${this.id}`) as HTMLDivElement;\n\n if (!divElement) {\n throw new Error('No div element');\n }\n\n const chart = echarts.init(divElement);\n echarts.use([AriaComponent]);\n\n const labelsRaw = this.datasets.flatMap((dataset) => dataset.data.map((datapoint) => datapoint.label));\n const labels = [...new Set(labelsRaw)];\n\n const series: echarts.SeriesOption[] = this.datasets.map((dataset) => ({\n name: dataset.label,\n type: 'bar',\n data: labels.map(label => {\n const point = dataset.data.find(dp => dp.label === label);\n return point?.value ?? null;\n }),\n color: this.getSeriesColor(dataset),\n label: {\n show: this.showDataLabels,\n position: 'right',\n formatter: ({ value }: { value: number }) => formatter.format(value),\n },\n cursor: 'default',\n }));\n\n const option: echarts.EChartsOption = {\n aria: {\n show: true,\n },\n legend: {\n show: this.showLegend,\n },\n xAxis: {\n name: this.xAxisTitle,\n nameLocation: 'center',\n type: this.direction === 'horizontal' ? 'value' : 'category',\n data: this.direction === 'horizontal' ? undefined : labels,\n splitLine: { show: this.showHorizontalGrid },\n },\n yAxis: {\n name: this.yAxisTitle,\n nameLocation: 'center',\n type: this.direction === 'horizontal' ? 'category' : 'value',\n data: this.direction === 'horizontal' ? labels : undefined,\n splitLine: { show: this.showVerticalGrid },\n },\n grid: {\n left: 0,\n right: 0,\n top: 0,\n ...(this.showLegend ? {} : { bottom: 0 }),\n },\n ...(this.showTooltip && {\n tooltip: {\n trigger: 'axis',\n axisPointer: {\n type: 'shadow',\n },\n },\n }),\n series,\n };\n\n chart.setOption(option);\n }\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'tt-bar-chart': TtBarChart;\n }\n}\n"]}
1
+ {"version":3,"file":"TtBarChart.js","sourceRoot":"","sources":["../../src/TtBarChart.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AACvC,gFAAgF;AAChF,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAEpD,OAAO,EAAE,aAAa,EAAE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B,CAAC;CACF;AA3HC;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,CAAC;4CAC3B;AAGzB;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;6CACyB;AAGpD;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,cAAc,EAAE,CAAC;8CAClC;AAGpB;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,cAAc,EAAE,CAAC;8CAClC;AAGpB;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,sBAAsB,EAAE,CAAC;sDAC3B;AAGpC;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,oBAAoB,EAAE,CAAC;oDAC3B;AAGlC;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,CAAC;8CAC1B;AAG5B;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,kBAAkB,EAAE,CAAC;kDAC3B;AAGhC;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,CAAC;+CAC1B;AAG7B;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC;2CACzB;AAGzB;IADC,KAAK,EAAE;yCAC6B","sourcesContent":["import { html, LitElement } from 'lit';\n// @ts-expect-error Known error - https://github.com/apache/echarts/issues/21250\nimport * as echarts from 'echarts';\nimport { property, state } from 'lit/decorators.js';\nimport { Dataset } from './types.js';\nimport { axesFormatter, dataLabelsFormatter, jsonConvertor } from './utils.js';\n// @ts-expect-error Known error - https://github.com/apache/echarts/issues/21250\nimport { AriaComponent } from 'echarts/components';\n\nexport class TtBarChart extends LitElement {\n @property({ type: Array, converter: jsonConvertor })\n datasets: Dataset[] = [];\n\n @property({ type: String })\n direction: 'horizontal' | 'vertical' = 'horizontal';\n\n @property({ type: String, attribute: 'x-axis-title' })\n xAxisTitle?: string;\n\n @property({ type: String, attribute: 'y-axis-title' })\n yAxisTitle?: string;\n\n @property({ type: Boolean, attribute: 'show-horizontal-grid' })\n showHorizontalGrid: boolean = false;\n\n @property({ type: Boolean, attribute: 'show-vertical-grid' })\n showVerticalGrid: boolean = false;\n\n @property({ type: Boolean, attribute: 'show-legend' })\n showLegend: boolean = false;\n\n @property({ type: Boolean, attribute: 'show-data-labels' })\n showDataLabels: boolean = false;\n\n @property({ type: Boolean, attribute: 'show-tooltip' })\n showTooltip: boolean = false;\n\n @property({ type: Boolean, attribute: 'stacked' })\n stacked: boolean = false;\n\n @state()\n chart: echarts.ECharts | null = null;\n\n private getSeriesColor = (dataset: Dataset) => {\n if (this.datasets && this.datasets.length > 1) return undefined;\n if (dataset.color) return dataset.color;\n\n return '#4d35a1';\n };\n\n render() {\n return html` <div id=${this.id} style=\"width: 100%; height:100%;\"></div> `;\n }\n\n protected firstUpdated() {\n if (this.chart === null) {\n echarts.use([AriaComponent]);\n const divElement = this.renderRoot.querySelector(`#${this.id}`) as HTMLDivElement;\n\n if (!divElement) {\n throw new Error('No div element');\n }\n\n this.chart = echarts.init(divElement);\n }\n }\n\n protected updated() {\n const labelsRaw = this.datasets.flatMap((dataset) => dataset.data.map((datapoint) => datapoint.label));\n const labels = [...new Set(labelsRaw)];\n\n const series: echarts.SeriesOption[] = this.datasets.map((dataset) => ({\n name: dataset.label,\n type: 'bar',\n data: labels.map((label) => {\n const point = dataset.data.find((dp) => dp.label === label);\n return point?.value ?? null;\n }),\n stack: this.stacked ? \"A\" : undefined,\n color: this.getSeriesColor(dataset),\n label: {\n show: this.showDataLabels,\n position: 'right',\n formatter: ({ value }: { value: number }) => dataLabelsFormatter.format(value),\n },\n cursor: 'default',\n }));\n\n const option: echarts.EChartsOption = {\n aria: {\n show: true,\n },\n legend: {\n show: this.showLegend,\n },\n xAxis: {\n name: this.xAxisTitle,\n nameLocation: 'center',\n type: this.direction === 'horizontal' ? 'value' : 'category',\n data: this.direction === 'horizontal' ? undefined : labels,\n axisLabel: {\n formatter: axesFormatter,\n },\n splitLine: { show: this.showVerticalGrid },\n },\n yAxis: {\n name: this.yAxisTitle,\n nameLocation: 'center',\n type: this.direction === 'horizontal' ? 'category' : 'value',\n data: this.direction === 'horizontal' ? labels : undefined,\n axisLabel: {\n formatter: axesFormatter,\n },\n splitLine: { show: this.showHorizontalGrid },\n },\n grid: {\n left: 0,\n right: 0,\n top: 0,\n ...(this.showLegend ? {} : { bottom: 0 }),\n },\n ...(this.showTooltip && {\n tooltip: {\n trigger: 'axis',\n axisPointer: {\n type: 'shadow',\n },\n },\n }),\n series,\n };\n\n this.chart.setOption(option);\n }\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'tt-bar-chart': TtBarChart;\n }\n}\n"]}
@@ -22,6 +22,7 @@ interface TTBarChartAttributes {
22
22
  'show-legend'?: boolean;
23
23
  'show-data-labels'?: boolean;
24
24
  'show-tooltip'?: boolean;
25
+ stacked?: boolean;
25
26
  }
26
27
  declare global {
27
28
  namespace JSX {
@@ -42,13 +43,4 @@ declare global {
42
43
  }
43
44
  }
44
45
  }
45
- declare module '@triptease/html-jsx' {
46
- namespace JSX {
47
- interface IntrinsicElements {
48
- 'tt-bar-chart': TTBarChartAttributes & {
49
- class?: string;
50
- };
51
- }
52
- }
53
- }
54
46
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import React from 'react';\nimport type {} from '@triptease/html-jsx'; // Stops declare module from erroring\n\ninterface DataPoint {\n label: string;\n value: number;\n}\n\nexport interface Dataset {\n label: string;\n data: DataPoint[];\n color?: string;\n}\n\ninterface TTBarChartAttributes {\n id?: string;\n style?: React.CSSProperties;\n [key: `data-${string}`]: string | undefined;\n datasets?: string;\n direction?: 'horizontal' | 'vertical';\n 'x-axis-title'?: string;\n 'y-axis-title'?: string;\n color?: string;\n 'show-horizontal-grid'?: boolean;\n 'show-vertical-grid'?: boolean;\n 'show-legend'?: boolean;\n 'show-data-labels'?: boolean;\n 'show-tooltip'?: boolean;\n}\n\ndeclare global {\n namespace JSX {\n interface IntrinsicElements {\n 'tt-bar-chart': TTBarChartAttributes & { class?: string };\n }\n }\n\n namespace React {\n namespace JSX {\n interface IntrinsicElements {\n 'tt-bar-chart': TTBarChartAttributes & { className?: string; ref?: React.Ref<unknown> };\n }\n }\n }\n}\n\ndeclare module '@triptease/html-jsx' {\n namespace JSX {\n interface IntrinsicElements {\n 'tt-bar-chart': TTBarChartAttributes & { class?: string };\n }\n }\n}\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import React from 'react';\n\ninterface DataPoint {\n label: string;\n value: number;\n}\n\nexport interface Dataset {\n label: string;\n data: DataPoint[];\n color?: string;\n}\n\ninterface TTBarChartAttributes {\n id?: string;\n style?: React.CSSProperties;\n [key: `data-${string}`]: string | undefined;\n datasets?: string;\n direction?: 'horizontal' | 'vertical';\n 'x-axis-title'?: string;\n 'y-axis-title'?: string;\n color?: string;\n 'show-horizontal-grid'?: boolean;\n 'show-vertical-grid'?: boolean;\n 'show-legend'?: boolean;\n 'show-data-labels'?: boolean;\n 'show-tooltip'?: boolean;\n stacked?: boolean;\n}\n\ndeclare global {\n namespace JSX {\n interface IntrinsicElements {\n 'tt-bar-chart': TTBarChartAttributes & { class?: string };\n }\n }\n\n namespace React {\n namespace JSX {\n interface IntrinsicElements {\n 'tt-bar-chart': TTBarChartAttributes & { className?: string; ref?: React.Ref<unknown> };\n }\n }\n }\n}\n"]}
@@ -1,2 +1,3 @@
1
1
  export declare const jsonConvertor: (value: string | null) => any;
2
- export declare const formatter: Intl.NumberFormat;
2
+ export declare const dataLabelsFormatter: Intl.NumberFormat;
3
+ export declare const axesFormatter: (value: number | string) => string;
package/dist/src/utils.js CHANGED
@@ -1,3 +1,10 @@
1
1
  export const jsonConvertor = (value) => (value ? JSON.parse(value) : undefined);
2
- export const formatter = new Intl.NumberFormat();
2
+ export const dataLabelsFormatter = new Intl.NumberFormat(undefined);
3
+ const numericalAxesFormatter = new Intl.NumberFormat(undefined, { notation: 'compact', maximumFractionDigits: 2 });
4
+ export const axesFormatter = (value) => {
5
+ if (typeof value === 'number') {
6
+ return numericalAxesFormatter.format(value);
7
+ }
8
+ return value;
9
+ };
3
10
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,KAAoB,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;AAC/F,MAAM,CAAC,MAAM,SAAS,GAAG,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC","sourcesContent":["export const jsonConvertor = (value: string | null) => (value ? JSON.parse(value) : undefined);\nexport const formatter = new Intl.NumberFormat();\n"]}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,KAAoB,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;AAC/F,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;AACpE,MAAM,sBAAsB,GAAG,IAAI,IAAI,CAAC,YAAY,CAAC,SAAS,EAAE,EAAE,QAAQ,EAAE,SAAS,EAAE,qBAAqB,EAAE,CAAC,EAAE,CAAC,CAAC;AAEnH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,KAAsB,EAAE,EAAE;IACtD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,sBAAsB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC9C,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC,CAAC","sourcesContent":["export const jsonConvertor = (value: string | null) => (value ? JSON.parse(value) : undefined);\nexport const dataLabelsFormatter = new Intl.NumberFormat(undefined);\nconst numericalAxesFormatter = new Intl.NumberFormat(undefined, { notation: 'compact', maximumFractionDigits: 2 });\n\nexport const axesFormatter = (value: number | string) => {\n if (typeof value === 'number') {\n return numericalAxesFormatter.format(value);\n }\n\n return value;\n};\n"]}
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Webcomponent tt-bar-chart following open-wc recommendations",
4
4
  "license": "MIT",
5
5
  "author": "tt-bar-chart",
6
- "version": "0.2.0",
6
+ "version": "0.2.2",
7
7
  "type": "module",
8
8
  "main": "dist/src/index.js",
9
9
  "module": "dist/src/index.js",