@hestia-earth/ui-components 0.33.15 → 0.33.16

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,15 +1,16 @@
1
1
  import Chart from 'chart.js';
2
2
  import { ChartPlugin } from './models';
3
3
  export declare const defaultBarDrawSettings: {
4
- xPosFn: (x: number, index: number, width: number, chart: Chart) => number;
5
- yPosFn: (y: number, index: number, height: number, chart: Chart) => number;
6
- colorFn: (m: any, index: number, chart: Chart) => string;
4
+ xPosFn: (x: number, index: number, width: number, chart: Chart, data?: number) => number;
5
+ yPosFn: (y: number, index: number, height: number, chart: Chart, data?: number) => number;
6
+ colorFn: (m: any, index: number, chart: Chart, data?: number) => string;
7
7
  textFn: (m: {
8
8
  label: string;
9
9
  data: number;
10
10
  }, index: number, chart: Chart) => string | [string, string];
11
11
  font: string;
12
12
  maxWidth: number;
13
+ emptyValueLabel: string;
13
14
  };
14
15
  export type BarDrawSettings = Partial<typeof defaultBarDrawSettings>;
15
16
  export declare const afterBarDrawPlugin: (settings?: BarDrawSettings) => ChartPlugin;
@@ -1074,19 +1074,23 @@ function isMouseInsideCircle(event, circleCenter, circleRadius) {
1074
1074
  }
1075
1075
 
1076
1076
  const defaultBarDrawSettings = {
1077
- xPosFn: (x, index, width, chart) => x + 10,
1078
- yPosFn: (y, index, height, chart) => y + 3,
1079
- colorFn: (m, index, chart) => 'black',
1077
+ xPosFn: (x, index, width, chart, data) => x + 10,
1078
+ yPosFn: (y, index, height, chart, data) => y + 3,
1079
+ colorFn: (m, index, chart, data) => 'black',
1080
1080
  textFn: (m, index, chart) => `${m.label} - ${toPrecision(m.data)} kg`,
1081
1081
  font: '14px Lato',
1082
- maxWidth: 90
1082
+ maxWidth: 90,
1083
+ emptyValueLabel: 'No data'
1083
1084
  };
1084
1085
  const afterBarDrawPlugin = settings => ({
1085
1086
  afterDraw: (chart) => {
1086
1087
  if (!chart.data.datasets?.length) {
1087
1088
  return;
1088
1089
  }
1089
- const { xPosFn, yPosFn, colorFn, textFn, maxWidth, font } = { ...defaultBarDrawSettings, ...(settings ?? {}) };
1090
+ const { xPosFn, yPosFn, colorFn, textFn, maxWidth, font, emptyValueLabel } = {
1091
+ ...defaultBarDrawSettings,
1092
+ ...(settings ?? {})
1093
+ };
1090
1094
  const { ctx } = chart;
1091
1095
  ctx.save();
1092
1096
  const height = chart.chartArea.bottom - chart.chartArea.top;
@@ -1098,21 +1102,19 @@ const afterBarDrawPlugin = settings => ({
1098
1102
  const { x, y } = _view;
1099
1103
  const label = _model.label;
1100
1104
  const data = chart.data.datasets[_datasetIndex].data[_index];
1101
- //data should be a number
1102
- if (typeof data !== 'number') {
1103
- return;
1104
- }
1105
- const xPos = xPosFn(x, index, width, chart);
1106
- const yPos = yPosFn(y, index, height, chart);
1107
- const text = textFn({ label, data }, index, chart);
1108
- ctx.font = font;
1109
- ctx.fillStyle = colorFn(_view, index, chart);
1110
- if (Array.isArray(text)) {
1111
- ctx.fillText(text[0], xPos, yPos - 5, maxWidth);
1112
- ctx.fillText(text[1], xPos, yPos + 5, maxWidth);
1113
- }
1114
- else {
1115
- ctx.fillText(text, xPos, yPos, maxWidth);
1105
+ const xPos = xPosFn(x, index, width, chart, data);
1106
+ const yPos = yPosFn(y, index, height, chart, data);
1107
+ const text = typeof data === 'number' ? textFn({ label, data }, index, chart) : emptyValueLabel;
1108
+ if (text) {
1109
+ ctx.font = font;
1110
+ ctx.fillStyle = colorFn(_view, index, chart, data);
1111
+ if (Array.isArray(text)) {
1112
+ ctx.fillText(text[0], xPos, yPos - 5, maxWidth);
1113
+ ctx.fillText(text[1], xPos, yPos + 5, maxWidth);
1114
+ }
1115
+ else {
1116
+ ctx.fillText(text, xPos, yPos, maxWidth);
1117
+ }
1116
1118
  }
1117
1119
  });
1118
1120
  }