mdt-charts 1.14.0 → 1.14.1

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.
@@ -90,7 +90,7 @@ export class Legend {
90
90
  .attr('class', options.itemsOptions.labelClass)
91
91
  .text(d => d.textContent);
92
92
  if (options.shouldCropLabels)
93
- LegendDomHelper.cropRowLabels(foreignObject, itemWrappers);
93
+ LegendDomHelper.decreaseRowLabels(foreignObject, itemWrappers, options.blockModel.static);
94
94
  return itemWrappers;
95
95
  }
96
96
  getObject(block) {
@@ -13,7 +13,9 @@ function getNewWidths(collection, wrapper) {
13
13
  let avgExtra = extra / biggerThanAvg.length;
14
14
  biggerThanAvg.forEach((item, index) => {
15
15
  const avgDiff = item.getCurrentWidth() - avgWidth;
16
- const decreaseBy = avgDiff < avgExtra ? avgDiff : avgExtra;
16
+ const decreaseBy = index === biggerThanAvg.length - 1
17
+ ? extra
18
+ : (avgDiff < avgExtra ? avgDiff : avgExtra);
17
19
  item.decreaseBy(decreaseBy);
18
20
  extra -= decreaseBy;
19
21
  avgExtra = extra / (biggerThanAvg.length - index - 1);
@@ -25,7 +27,7 @@ function fixWidthsByLinePos(collection, wrapper) {
25
27
  let currentRow = [];
26
28
  collection.items.forEach((item, i) => {
27
29
  const currentRowSum = currentRow.reduce((acc, i) => acc + i.getCurrentTotalWidth(), 0);
28
- if (currentRowSum + item.getWidthWithoutRightMargin() <= wrapper.getWidthOfOneLine()) {
30
+ if (currentRowSum + item.getCurrentTotalWidth() <= wrapper.getWidthOfOneLine()) {
29
31
  currentRow.push(item);
30
32
  }
31
33
  else {
@@ -43,11 +45,13 @@ function fixWidthsByLinePos(collection, wrapper) {
43
45
  const bottom = byRows[i + 1];
44
46
  const topFreeSpace = wrapper.getWidthOfOneLine() - top.getTotalWidth();
45
47
  const firstOfBottom = bottom.getFirstItem();
46
- if (topFreeSpace / firstOfBottom.getCurrentTotalWidth() > 0.51 || i === byRows.length - 2) {
48
+ if (i === byRows.length - 2)
49
+ top.pushMany(bottom.shiftAll());
50
+ else if (topFreeSpace / firstOfBottom.getCurrentTotalWidth() > 0.5) {
47
51
  const fromBottom = bottom.shift();
48
52
  top.push(fromBottom);
49
- result.push(...getNewWidths(top, new LegendWrapper({ maxRowsAmount: 1, width: wrapper.getWidthOfOneLine() })));
50
53
  }
54
+ result.push(...getNewWidths(top, new LegendWrapper({ maxRowsAmount: 1, width: wrapper.getWidthOfOneLine() })));
51
55
  }
52
56
  return result;
53
57
  }
@@ -80,11 +84,14 @@ class LegendItem {
80
84
  this.width -= by;
81
85
  }
82
86
  getCurrentWidth() {
83
- return this.width;
87
+ return Math.round(this.width * 100) / 100;
84
88
  }
85
89
  getTotalMarginSize() {
86
90
  return this.config.marginLeft + this.config.marginRight;
87
91
  }
92
+ resetWidthToOriginal() {
93
+ this.width = this.config.width;
94
+ }
88
95
  }
89
96
  class LegendItemCollection {
90
97
  constructor(items) {
@@ -113,7 +120,17 @@ class LegendItemCollection {
113
120
  shift() {
114
121
  return this.items.shift();
115
122
  }
123
+ shiftAll() {
124
+ return this.items.splice(0, this.items.length);
125
+ }
116
126
  push(item) {
117
127
  this.items.push(item);
118
128
  }
129
+ pushMany(items) {
130
+ this.items.push(...items);
131
+ }
132
+ resetItemsToOriginalWidth() {
133
+ this.items.forEach(item => item.resetWidthToOriginal());
134
+ return this;
135
+ }
119
136
  }
@@ -5,6 +5,7 @@ import { MIN_DONUT_BLOCK_SIZE, PolarModel } from "../notations/polar/polarModel"
5
5
  import { DataManagerModelService } from "./dataManagerModelService";
6
6
  import { LegendPolarMarginCalculator } from "../featuresModel/legendModel/polarMarginCalculator";
7
7
  import { CardsDataManagerModel } from "./notations/cardsDataManagerModel";
8
+ import { styledElementValues } from "../modelBuilder";
8
9
  export class DataManagerModel {
9
10
  static getPreparedData(data, allowableKeys, config) {
10
11
  const scopedData = config.options.type !== "card" ? this.getScopedData(data, allowableKeys, config.options.data) : data;
@@ -83,11 +84,16 @@ export class DataManagerModel {
83
84
  }
84
85
  //TODO: position type
85
86
  static getLegendDataParams(position, keys, legendCanvas, canvasModel, legendBlock) {
87
+ const legendItemContentOptions = keys.map(k => ({
88
+ text: k,
89
+ markerSize: styledElementValues.defaultLegendMarkerSizes,
90
+ wrapperSize: { marginRightPx: styledElementValues.legend.inlineDynamicItemWrapperMarginRightPx }
91
+ }));
86
92
  if (position === 'right') {
87
- return LegendCanvasModel.findElementsAmountByLegendSize(keys, position, this.polarMarginCalculator.getMaxLegendWidth(legendCanvas, canvasModel.getBlockSize().width), canvasModel.getChartBlockHeight() - legendBlock.coordinate.bottom.margin.bottom);
93
+ return LegendCanvasModel.findElementsAmountByLegendSize(legendItemContentOptions, position, this.polarMarginCalculator.getMaxLegendWidth(legendCanvas, canvasModel.getBlockSize().width), canvasModel.getChartBlockHeight() - legendBlock.coordinate.bottom.margin.bottom);
88
94
  }
89
95
  else {
90
- return LegendCanvasModel.findElementsAmountByLegendSize(keys, position, canvasModel.getChartBlockWidth() - legendBlock.coordinate.bottom.margin.left - legendBlock.coordinate.bottom.margin.right, canvasModel.getChartBlockHeight() - legendBlock.coordinate.bottom.margin.bottom - legendBlock.coordinate.bottom.margin.top - MIN_DONUT_BLOCK_SIZE);
96
+ return LegendCanvasModel.findElementsAmountByLegendSize(legendItemContentOptions, position, canvasModel.getChartBlockWidth() - legendBlock.coordinate.bottom.margin.left - legendBlock.coordinate.bottom.margin.right, canvasModel.getChartBlockHeight() - legendBlock.coordinate.bottom.margin.bottom - legendBlock.coordinate.bottom.margin.top - MIN_DONUT_BLOCK_SIZE);
91
97
  }
92
98
  }
93
99
  static getMaximumPossibleScope(keys, dataModel) {
@@ -1,8 +1,19 @@
1
1
  import { DataLegendParams } from "../../dataManagerModel/dataManagerModel";
2
2
  import { LegendPosition } from "../../model";
3
3
  export declare type LegendItemsDirection = 'row' | 'column';
4
+ export interface LegendItemContentOptions {
5
+ text: string;
6
+ markerSize: {
7
+ widthPx: number;
8
+ heightPx: number;
9
+ marginRightPx: number;
10
+ };
11
+ wrapperSize: {
12
+ marginRightPx: number;
13
+ };
14
+ }
4
15
  export declare class LegendCanvasModel {
5
16
  static getLegendItemWidth(text: string): number;
6
- static findElementsAmountByLegendSize(texts: string[], position: LegendPosition, legendBlockWidth: number, legendBlockHeight: number): DataLegendParams;
17
+ static findElementsAmountByLegendSize(items: LegendItemContentOptions[], position: LegendPosition, legendBlockWidth: number, legendBlockHeight: number): DataLegendParams;
7
18
  private static getLegendWrapperEl;
8
19
  }
@@ -18,31 +18,37 @@ export class LegendCanvasModel {
18
18
  itemWrapper.remove();
19
19
  return sumWidth;
20
20
  }
21
- static findElementsAmountByLegendSize(texts, position, legendBlockWidth, legendBlockHeight) {
21
+ //TODO: find better solution
22
+ static findElementsAmountByLegendSize(items, position, legendBlockWidth, legendBlockHeight) {
22
23
  const legendWrapper = this.getLegendWrapperEl(legendBlockWidth, position === "right" ? "column" : "row");
23
24
  document.body.append(legendWrapper);
24
25
  let amount = 0;
25
- for (let i = 0; i < texts.length; i++) {
26
+ for (let i = 0; i < items.length; i++) {
26
27
  const itemWrapper = document.createElement('div');
27
28
  const colorBlock = document.createElement('span');
28
29
  const textBlock = document.createElement('span');
29
30
  itemWrapper.classList.add("legend-item");
30
- if (position === 'bottom') {
31
+ if (position === 'bottom' || position === 'top') {
31
32
  itemWrapper.classList.add('legend-item-inline');
32
33
  textBlock.classList.add('legend-label-nowrap');
33
34
  }
34
35
  else {
35
36
  itemWrapper.classList.add('legend-item-row');
36
37
  }
37
- colorBlock.classList.add(CLASSES.legendColor);
38
+ // colorBlock.classList.add(CLASSES.legendColor);
39
+ colorBlock.style.display = "inline-block";
40
+ colorBlock.style.width = `${items[i].markerSize.widthPx}px`;
41
+ colorBlock.style.height = `${items[i].markerSize.heightPx}px`;
42
+ colorBlock.style.marginRight = `${items[i].markerSize.marginRightPx}px`;
38
43
  textBlock.classList.add(CLASSES.legendLabel);
39
- textBlock.textContent = texts[i];
44
+ textBlock.textContent = items[i].text;
45
+ itemWrapper.style.marginRight = `${items[i].wrapperSize.marginRightPx}px`;
40
46
  itemWrapper.append(colorBlock, textBlock);
41
47
  legendWrapper.append(itemWrapper);
42
48
  amount++;
43
49
  if (legendWrapper.offsetHeight > legendBlockHeight) {
44
50
  itemWrapper.remove();
45
- if (legendBlockHeight - legendWrapper.offsetHeight >= 15 && position !== 'bottom')
51
+ if (legendBlockHeight - legendWrapper.offsetHeight >= 15 && position !== 'bottom' && position !== 'top')
46
52
  amount = amount; //TODO: remove
47
53
  else
48
54
  amount -= 1;
@@ -1,8 +1,11 @@
1
1
  import { Legend } from "../../../config/config";
2
2
  import { LegendBlockModel } from "../../model";
3
+ import { TwoDimConfigReader } from "../../modelInstance/configReader";
3
4
  import { ModelInstance } from "../../modelInstance/modelInstance";
4
5
  export declare class TwoDimLegendModel {
6
+ private configReader;
7
+ constructor(configReader: TwoDimConfigReader);
5
8
  recalcMarginWith2DLegend(modelInstance: ModelInstance, legendBlockModel: LegendBlockModel, legendOptions: Legend): void;
6
- private getLegendSize;
9
+ private getLegendSizeLegacy;
7
10
  private getLegendModel;
8
11
  }
@@ -1,18 +1,29 @@
1
+ import { styledElementValues } from "../../modelBuilder";
2
+ import { getWidthOfLegendMarkerByType } from "../../notations/twoDimensional/styles";
3
+ import { LegendCanvasModel } from "./legendCanvasModel";
1
4
  import { LegendModel } from "./legendModel";
2
5
  export class TwoDimLegendModel {
6
+ constructor(configReader) {
7
+ this.configReader = configReader;
8
+ }
3
9
  recalcMarginWith2DLegend(modelInstance, legendBlockModel, legendOptions) {
4
10
  const canvasModel = modelInstance.canvasModel;
5
11
  const legendPosition = this.getLegendModel(legendOptions).position;
6
12
  modelInstance.canvasModel.legendCanvas.setPosition(legendPosition);
7
13
  if (legendPosition !== 'off') {
8
- const legendSize = this.getLegendSize();
14
+ const legendItemInfo = this.configReader.getLegendItemInfo();
15
+ const legendSize = LegendCanvasModel.findElementsAmountByLegendSize(legendItemInfo.map(i => ({
16
+ text: i.text,
17
+ markerSize: Object.assign(Object.assign({}, styledElementValues.defaultLegendMarkerSizes), { widthPx: getWidthOfLegendMarkerByType(i.chartType) }),
18
+ wrapperSize: { marginRightPx: styledElementValues.legend.inlineItemWrapperMarginRightPx }
19
+ })), 'top', modelInstance.canvasModel.getBlockSize().width, legendBlockModel.static.maxLinesAmount * styledElementValues.legend.inlineLegendOneLineHeightPx).size.height;
9
20
  canvasModel.increaseMarginSide(legendPosition, legendSize);
10
21
  if (legendSize !== 0)
11
22
  LegendModel.appendToGlobalMarginValuesLegendMargin(canvasModel, legendPosition, legendBlockModel);
12
23
  legendBlockModel.coordinate[legendPosition].size = legendSize;
13
24
  }
14
25
  }
15
- getLegendSize() {
26
+ getLegendSizeLegacy() {
16
27
  const heightOfLegendItemWithoutWordWrapping = 20;
17
28
  return heightOfLegendItemWithoutWordWrapping;
18
29
  }
@@ -9,7 +9,7 @@ export class TwoDimMarginModel {
9
9
  constructor(designerConfig, configReader) {
10
10
  this.designerConfig = designerConfig;
11
11
  this.configReader = configReader;
12
- this.twoDimLegendModel = new TwoDimLegendModel();
12
+ this.twoDimLegendModel = new TwoDimLegendModel(this.configReader);
13
13
  }
14
14
  recalcMargin(otherComponents, modelInstance) {
15
15
  const canvasModel = modelInstance.canvasModel;
@@ -11,5 +11,17 @@ export declare const CLASSES: {
11
11
  legendColor: string;
12
12
  legendItem: string;
13
13
  };
14
+ export declare const styledElementValues: {
15
+ defaultLegendMarkerSizes: {
16
+ widthPx: number;
17
+ heightPx: number;
18
+ marginRightPx: number;
19
+ };
20
+ legend: {
21
+ inlineLegendOneLineHeightPx: number;
22
+ inlineItemWrapperMarginRightPx: number;
23
+ inlineDynamicItemWrapperMarginRightPx: number;
24
+ };
25
+ };
14
26
  export declare function assembleModel(config: MdtChartsConfig, data: MdtChartsDataSource, designerConfig: DesignerConfig): Model;
15
27
  export declare function getPreparedData(model: Model, data: MdtChartsDataSource, config: MdtChartsConfig): MdtChartsDataSource;
@@ -19,6 +19,18 @@ export const CLASSES = {
19
19
  legendColor: 'legend-circle',
20
20
  legendItem: 'legend-item',
21
21
  };
22
+ export const styledElementValues = {
23
+ defaultLegendMarkerSizes: {
24
+ widthPx: 11,
25
+ heightPx: 11,
26
+ marginRightPx: 6
27
+ },
28
+ legend: {
29
+ inlineLegendOneLineHeightPx: 21,
30
+ inlineItemWrapperMarginRightPx: 12,
31
+ inlineDynamicItemWrapperMarginRightPx: 2
32
+ }
33
+ };
22
34
  function getBlockCanvas(config, modelInstance) {
23
35
  const emptyBlockParams = { width: 0, height: 0 };
24
36
  const size = ConfigValidator.validateCanvasSize(modelInstance.canvasModel.getBlockSize()) ? Object.assign({}, modelInstance.canvasModel.getBlockSize()) : emptyBlockParams;
@@ -1,4 +1,4 @@
1
- import { AxisLabelFormatter, MdtChartsConfig, MdtChartsField, MdtChartsTwoDimensionalOptions } from "../../config/config";
1
+ import { AxisLabelFormatter, MdtChartsConfig, MdtChartsField, MdtChartsTwoDimensionalOptions, TwoDimensionalChartType } from "../../config/config";
2
2
  import { DesignerConfig } from "../../designer/designerConfig";
3
3
  interface BaseConfigReader {
4
4
  getValueFields(): MdtChartsField[];
@@ -10,6 +10,10 @@ export declare class TwoDimConfigReader implements BaseConfigReader {
10
10
  constructor(config: MdtChartsConfig, designerConfig: DesignerConfig);
11
11
  getValueFields(): MdtChartsField[];
12
12
  getAxisLabelFormatter(): AxisLabelFormatter;
13
+ getLegendItemInfo(): {
14
+ text: string;
15
+ chartType: TwoDimensionalChartType;
16
+ }[];
13
17
  }
14
18
  export declare class PolarConfigReader implements BaseConfigReader {
15
19
  private options;
@@ -24,6 +24,16 @@ export class TwoDimConfigReader {
24
24
  const valueFieldFormat = this.options.charts[0].data.valueFields[0].format;
25
25
  return (v) => this.designerConfig.dataFormat.formatters(v, { type: valueFieldFormat });
26
26
  }
27
+ getLegendItemInfo() {
28
+ const info = [];
29
+ this.options.charts.forEach(c => {
30
+ c.data.valueFields.forEach(vf => {
31
+ var _a;
32
+ info.push({ text: (_a = vf.title) !== null && _a !== void 0 ? _a : vf.name, chartType: c.type });
33
+ });
34
+ });
35
+ return info;
36
+ }
27
37
  }
28
38
  export class PolarConfigReader {
29
39
  constructor(config) {
@@ -1,6 +1,7 @@
1
1
  import { ChartLegendModel, LineLikeChartDashOptions, LineLikeChartShapeOptions } from "../../model";
2
- import { ChartOrientation, MdtChartsLineLikeChartDashedStyles, MdtChartsTwoDimensionalChart } from "../../../config/config";
2
+ import { ChartOrientation, MdtChartsLineLikeChartDashedStyles, MdtChartsTwoDimensionalChart, TwoDimensionalChartType } from "../../../config/config";
3
3
  import { MdtChartsLineLikeChartShape } from "../../../designer/designerConfig";
4
4
  export declare function parseShape(chartOrientation: ChartOrientation, configOptions?: MdtChartsLineLikeChartShape): LineLikeChartShapeOptions;
5
5
  export declare function parseDashStyles(configOptions?: MdtChartsLineLikeChartDashedStyles): LineLikeChartDashOptions;
6
6
  export declare function getLegendMarkerOptions(chart: MdtChartsTwoDimensionalChart): ChartLegendModel;
7
+ export declare function getWidthOfLegendMarkerByType(chartType: TwoDimensionalChartType): number;
@@ -1,4 +1,5 @@
1
1
  import { LineCurveType } from "../../model";
2
+ import { styledElementValues } from "../../modelBuilder";
2
3
  export function parseShape(chartOrientation, configOptions) {
3
4
  var _a;
4
5
  const curveType = (_a = configOptions === null || configOptions === void 0 ? void 0 : configOptions.curve) === null || _a === void 0 ? void 0 : _a.type;
@@ -35,7 +36,15 @@ export function getLegendMarkerOptions(chart) {
35
36
  };
36
37
  return {
37
38
  markerShape: shapeByType[chart.type],
38
- barViewOptions: { hatch: { on: (_c = (_b = (_a = chart.barStyles) === null || _a === void 0 ? void 0 : _a.hatch) === null || _b === void 0 ? void 0 : _b.on) !== null && _c !== void 0 ? _c : false }, width: 10 },
39
- lineViewOptions: { dashedStyles: parseDashStyles((_d = chart.lineStyles) === null || _d === void 0 ? void 0 : _d.dash), width: 30 }
39
+ barViewOptions: { hatch: { on: (_c = (_b = (_a = chart.barStyles) === null || _a === void 0 ? void 0 : _a.hatch) === null || _b === void 0 ? void 0 : _b.on) !== null && _c !== void 0 ? _c : false }, width: getWidthOfLegendMarkerByType("bar") },
40
+ lineViewOptions: { dashedStyles: parseDashStyles((_d = chart.lineStyles) === null || _d === void 0 ? void 0 : _d.dash), width: getWidthOfLegendMarkerByType("line") }
40
41
  };
41
42
  }
43
+ export function getWidthOfLegendMarkerByType(chartType) {
44
+ if (chartType === "bar")
45
+ return 10;
46
+ if (chartType === "line")
47
+ return 30;
48
+ if (chartType === "area")
49
+ return styledElementValues.defaultLegendMarkerSizes.widthPx;
50
+ }
@@ -69,8 +69,8 @@
69
69
  overflow: hidden;
70
70
  text-overflow: ellipsis;
71
71
  }
72
- .legend-wrapper-without-wrap .legend-item-inline:not(:first-of-type) {
73
- margin-left: 20px;
72
+ .legend-wrapper-without-wrap .legend-item-inline:not(:last-of-type) {
73
+ margin-right: 12px
74
74
  }
75
75
  .legend-wrapper-with-wrap .legend-item-inline {
76
76
  margin: 2px;
@@ -98,7 +98,7 @@
98
98
  text-overflow: ellipsis;
99
99
  }
100
100
  .legend-marker {
101
- margin-right: 8px;
101
+ margin-right: 6px;
102
102
  }
103
103
  .legend-circle {
104
104
  position: relative;
@@ -107,7 +107,7 @@
107
107
  width: 11px;
108
108
  height: 11px;
109
109
  border-radius: 50%;
110
- margin-right: 8px;
110
+ margin-right: 6px;
111
111
  }
112
112
 
113
113
 
@@ -69,8 +69,8 @@
69
69
  overflow: hidden;
70
70
  text-overflow: ellipsis;
71
71
  }
72
- .legend-wrapper-without-wrap .legend-item-inline:not(:first-of-type) {
73
- margin-left: 20px;
72
+ .legend-wrapper-without-wrap .legend-item-inline:not(:last-of-type) {
73
+ margin-right: 12px
74
74
  }
75
75
  .legend-wrapper-with-wrap .legend-item-inline {
76
76
  margin: 2px;
@@ -98,7 +98,7 @@
98
98
  text-overflow: ellipsis;
99
99
  }
100
100
  .legend-marker {
101
- margin-right: 8px;
101
+ margin-right: 6px;
102
102
  }
103
103
  .legend-circle {
104
104
  position: relative;
@@ -107,7 +107,7 @@
107
107
  width: 11px;
108
108
  height: 11px;
109
109
  border-radius: 50%;
110
- margin-right: 8px;
110
+ margin-right: 6px;
111
111
  }
112
112
 
113
113
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mdt-charts",
3
- "version": "1.14.0",
3
+ "version": "1.14.1",
4
4
  "description": "",
5
5
  "main": "lib/main.js",
6
6
  "scripts": {