@gravity-ui/charts 1.13.1 → 1.13.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.
@@ -29,39 +29,25 @@ function getYScaleRange(args) {
29
29
  case 'linear':
30
30
  case 'logarithmic': {
31
31
  let range = [boundsHeight, boundsHeight * axis.maxPadding];
32
- switch (axis.order) {
33
- case 'sortDesc':
34
- case 'reverse': {
35
- range.reverse();
36
- }
37
- }
38
32
  const barYSeries = series.filter((s) => s.type === SeriesType.BarY);
39
33
  if (barYSeries.length) {
40
34
  const groupedData = groupBarYDataByYValue(barYSeries, [axis]);
41
- const { barSize, dataLength } = getBarYLayoutForNumericScale({
42
- plotHeight: boundsHeight - boundsHeight * axis.maxPadding,
43
- groupedData,
44
- seriesOptions: seriesOptions,
45
- });
46
- if (dataLength > 1) {
47
- const alreadyCountedStackingIds = new Set();
48
- const offsetMultiplier = barYSeries.reduce((acc, s) => {
49
- let count = 0;
50
- if (s.stackId) {
51
- if (!alreadyCountedStackingIds.has(s.stackId)) {
52
- alreadyCountedStackingIds.add(s.stackId);
53
- count = 1;
54
- }
55
- }
56
- else {
57
- count = 1;
58
- }
59
- return acc + count;
60
- }, 0);
61
- const offset = (barSize * Math.max(offsetMultiplier, 1)) / 2;
35
+ if (Object.keys(groupedData).length > 1) {
36
+ const { bandSize } = getBarYLayoutForNumericScale({
37
+ plotHeight: boundsHeight - boundsHeight * axis.maxPadding,
38
+ groupedData,
39
+ seriesOptions: seriesOptions,
40
+ });
41
+ const offset = bandSize / 2;
62
42
  range = [range[0] - offset, range[1] + offset];
63
43
  }
64
44
  }
45
+ switch (axis.order) {
46
+ case 'sortDesc':
47
+ case 'reverse': {
48
+ range.reverse();
49
+ }
50
+ }
65
51
  return range;
66
52
  }
67
53
  case 'category': {
@@ -10,7 +10,8 @@ export const prepareBarYData = async (args) => {
10
10
  const stackGap = seriesOptions['bar-y'].stackGap;
11
11
  const xLinearScale = xScale;
12
12
  const yLinearScale = yScale;
13
- const plotHeight = yLinearScale(yLinearScale.domain()[0]);
13
+ const yScaleRange = yLinearScale.range();
14
+ const plotHeight = Math.abs(yScaleRange[0] - yScaleRange[1]);
14
15
  const sortingOptions = get(seriesOptions, 'bar-y.dataSorting');
15
16
  const comparator = (sortingOptions === null || sortingOptions === void 0 ? void 0 : sortingOptions.direction) === 'desc' ? descending : ascending;
16
17
  const sortKey = (() => {
@@ -40,7 +41,7 @@ export const prepareBarYData = async (args) => {
40
41
  const stacks = Object.values(val);
41
42
  const currentBarHeight = barSize * stacks.length + barGap * (stacks.length - 1);
42
43
  stacks.forEach((measureValues, groupItemIndex) => {
43
- const base = xLinearScale(0 - measureValues[0].series.borderWidth);
44
+ const base = xLinearScale(0) - measureValues[0].series.borderWidth;
44
45
  let stackSum = base;
45
46
  const stackItems = [];
46
47
  const sortedData = sortKey
@@ -85,7 +86,7 @@ export const prepareBarYData = async (args) => {
85
86
  height: barSize,
86
87
  color: data.color || s.color,
87
88
  borderColor: s.borderColor,
88
- borderWidth: s.borderWidth,
89
+ borderWidth: barSize > s.borderWidth * 2 ? s.borderWidth : 0,
89
90
  opacity: get(data, 'opacity', null),
90
91
  data,
91
92
  series: s,
@@ -14,7 +14,6 @@ export declare function getBarYLayoutForNumericScale(args: {
14
14
  bandSize: number;
15
15
  barGap: number;
16
16
  barSize: number;
17
- dataLength: number;
18
17
  };
19
18
  export declare function getBarYLayoutForCategoryScale(args: {
20
19
  groupedData: ReturnType<typeof groupBarYDataByYValue>;
@@ -32,13 +32,14 @@ export function getBarYLayoutForNumericScale(args) {
32
32
  const barMaxWidth = get(seriesOptions, 'bar-y.barMaxWidth');
33
33
  const barPadding = get(seriesOptions, 'bar-y.barPadding');
34
34
  const groupPadding = get(seriesOptions, 'bar-y.groupPadding');
35
- const dataLength = Object.values(groupedData).reduce((sum, items) => sum + Object.keys(items).length, 0);
36
- const bandSize = plotHeight / dataLength;
35
+ const groups = Object.values(groupedData);
36
+ const maxGroupItemCount = groups.reduce((acc, items) => Math.max(acc, Object.keys(items).length), 0);
37
+ const bandSize = plotHeight / groups.length;
37
38
  const groupGap = Math.max(bandSize * groupPadding, MIN_BAR_GROUP_GAP);
38
39
  const groupSize = bandSize - groupGap;
39
40
  const barGap = Math.max(bandSize * barPadding, MIN_BAR_GAP);
40
- const barSize = Math.max(MIN_BAR_WIDTH, Math.min(groupSize - barGap, barMaxWidth));
41
- return { bandSize, barGap, barSize, dataLength };
41
+ const barSize = Math.max(MIN_BAR_WIDTH, Math.min((groupSize - barGap) / maxGroupItemCount, barMaxWidth));
42
+ return { bandSize, barGap, barSize };
42
43
  }
43
44
  export function getBarYLayoutForCategoryScale(args) {
44
45
  const { groupedData, seriesOptions, yScale } = args;
@@ -29,39 +29,25 @@ function getYScaleRange(args) {
29
29
  case 'linear':
30
30
  case 'logarithmic': {
31
31
  let range = [boundsHeight, boundsHeight * axis.maxPadding];
32
- switch (axis.order) {
33
- case 'sortDesc':
34
- case 'reverse': {
35
- range.reverse();
36
- }
37
- }
38
32
  const barYSeries = series.filter((s) => s.type === SeriesType.BarY);
39
33
  if (barYSeries.length) {
40
34
  const groupedData = groupBarYDataByYValue(barYSeries, [axis]);
41
- const { barSize, dataLength } = getBarYLayoutForNumericScale({
42
- plotHeight: boundsHeight - boundsHeight * axis.maxPadding,
43
- groupedData,
44
- seriesOptions: seriesOptions,
45
- });
46
- if (dataLength > 1) {
47
- const alreadyCountedStackingIds = new Set();
48
- const offsetMultiplier = barYSeries.reduce((acc, s) => {
49
- let count = 0;
50
- if (s.stackId) {
51
- if (!alreadyCountedStackingIds.has(s.stackId)) {
52
- alreadyCountedStackingIds.add(s.stackId);
53
- count = 1;
54
- }
55
- }
56
- else {
57
- count = 1;
58
- }
59
- return acc + count;
60
- }, 0);
61
- const offset = (barSize * Math.max(offsetMultiplier, 1)) / 2;
35
+ if (Object.keys(groupedData).length > 1) {
36
+ const { bandSize } = getBarYLayoutForNumericScale({
37
+ plotHeight: boundsHeight - boundsHeight * axis.maxPadding,
38
+ groupedData,
39
+ seriesOptions: seriesOptions,
40
+ });
41
+ const offset = bandSize / 2;
62
42
  range = [range[0] - offset, range[1] + offset];
63
43
  }
64
44
  }
45
+ switch (axis.order) {
46
+ case 'sortDesc':
47
+ case 'reverse': {
48
+ range.reverse();
49
+ }
50
+ }
65
51
  return range;
66
52
  }
67
53
  case 'category': {
@@ -10,7 +10,8 @@ export const prepareBarYData = async (args) => {
10
10
  const stackGap = seriesOptions['bar-y'].stackGap;
11
11
  const xLinearScale = xScale;
12
12
  const yLinearScale = yScale;
13
- const plotHeight = yLinearScale(yLinearScale.domain()[0]);
13
+ const yScaleRange = yLinearScale.range();
14
+ const plotHeight = Math.abs(yScaleRange[0] - yScaleRange[1]);
14
15
  const sortingOptions = get(seriesOptions, 'bar-y.dataSorting');
15
16
  const comparator = (sortingOptions === null || sortingOptions === void 0 ? void 0 : sortingOptions.direction) === 'desc' ? descending : ascending;
16
17
  const sortKey = (() => {
@@ -40,7 +41,7 @@ export const prepareBarYData = async (args) => {
40
41
  const stacks = Object.values(val);
41
42
  const currentBarHeight = barSize * stacks.length + barGap * (stacks.length - 1);
42
43
  stacks.forEach((measureValues, groupItemIndex) => {
43
- const base = xLinearScale(0 - measureValues[0].series.borderWidth);
44
+ const base = xLinearScale(0) - measureValues[0].series.borderWidth;
44
45
  let stackSum = base;
45
46
  const stackItems = [];
46
47
  const sortedData = sortKey
@@ -85,7 +86,7 @@ export const prepareBarYData = async (args) => {
85
86
  height: barSize,
86
87
  color: data.color || s.color,
87
88
  borderColor: s.borderColor,
88
- borderWidth: s.borderWidth,
89
+ borderWidth: barSize > s.borderWidth * 2 ? s.borderWidth : 0,
89
90
  opacity: get(data, 'opacity', null),
90
91
  data,
91
92
  series: s,
@@ -14,7 +14,6 @@ export declare function getBarYLayoutForNumericScale(args: {
14
14
  bandSize: number;
15
15
  barGap: number;
16
16
  barSize: number;
17
- dataLength: number;
18
17
  };
19
18
  export declare function getBarYLayoutForCategoryScale(args: {
20
19
  groupedData: ReturnType<typeof groupBarYDataByYValue>;
@@ -32,13 +32,14 @@ export function getBarYLayoutForNumericScale(args) {
32
32
  const barMaxWidth = get(seriesOptions, 'bar-y.barMaxWidth');
33
33
  const barPadding = get(seriesOptions, 'bar-y.barPadding');
34
34
  const groupPadding = get(seriesOptions, 'bar-y.groupPadding');
35
- const dataLength = Object.values(groupedData).reduce((sum, items) => sum + Object.keys(items).length, 0);
36
- const bandSize = plotHeight / dataLength;
35
+ const groups = Object.values(groupedData);
36
+ const maxGroupItemCount = groups.reduce((acc, items) => Math.max(acc, Object.keys(items).length), 0);
37
+ const bandSize = plotHeight / groups.length;
37
38
  const groupGap = Math.max(bandSize * groupPadding, MIN_BAR_GROUP_GAP);
38
39
  const groupSize = bandSize - groupGap;
39
40
  const barGap = Math.max(bandSize * barPadding, MIN_BAR_GAP);
40
- const barSize = Math.max(MIN_BAR_WIDTH, Math.min(groupSize - barGap, barMaxWidth));
41
- return { bandSize, barGap, barSize, dataLength };
41
+ const barSize = Math.max(MIN_BAR_WIDTH, Math.min((groupSize - barGap) / maxGroupItemCount, barMaxWidth));
42
+ return { bandSize, barGap, barSize };
42
43
  }
43
44
  export function getBarYLayoutForCategoryScale(args) {
44
45
  const { groupedData, seriesOptions, yScale } = args;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/charts",
3
- "version": "1.13.1",
3
+ "version": "1.13.2",
4
4
  "description": "React component used to render charts",
5
5
  "license": "MIT",
6
6
  "main": "dist/cjs/index.js",