@gravity-ui/chartkit 5.8.0 → 5.9.0

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.
@@ -0,0 +1,2 @@
1
+ import React from 'react';
2
+ export declare const LineWithLogarithmicAxis: () => React.JSX.Element;
@@ -0,0 +1,38 @@
1
+ import React from 'react';
2
+ import { Col, Container, Row, Text } from '@gravity-ui/uikit';
3
+ import { randomNormal } from 'd3';
4
+ import { ChartKit } from '../../../../components/ChartKit';
5
+ import { ExampleWrapper } from '../ExampleWrapper';
6
+ export const LineWithLogarithmicAxis = () => {
7
+ const randomY = randomNormal(0, 100);
8
+ const widgetData = {
9
+ series: {
10
+ data: [
11
+ {
12
+ type: 'line',
13
+ name: 'Line series',
14
+ data: new Array(25).fill(null).map((_, index) => ({
15
+ x: index,
16
+ y: Math.abs(randomY()),
17
+ })),
18
+ },
19
+ ],
20
+ },
21
+ };
22
+ const lineWidgetData = Object.assign(Object.assign({}, widgetData), { title: { text: 'line' } });
23
+ const logarithmicWidgetData = Object.assign(Object.assign({}, widgetData), { title: { text: 'logarithmic' }, yAxis: [
24
+ {
25
+ type: 'logarithmic',
26
+ },
27
+ ] });
28
+ return (React.createElement(Container, { spaceRow: 5 },
29
+ React.createElement(Row, { space: 1 },
30
+ React.createElement(Text, { variant: "header-2" }, "logarithmic VS line")),
31
+ React.createElement(Row, { space: 3 },
32
+ React.createElement(Col, { s: 12, m: 6 },
33
+ React.createElement(ExampleWrapper, null,
34
+ React.createElement(ChartKit, { type: "d3", data: lineWidgetData }))),
35
+ React.createElement(Col, { s: 12, m: 6 },
36
+ React.createElement(ExampleWrapper, null,
37
+ React.createElement(ChartKit, { type: "d3", data: logarithmicWidgetData }))))));
38
+ };
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import { extent, scaleBand, scaleLinear, scaleUtc } from 'd3';
2
+ import { extent, scaleBand, scaleLinear, scaleLog, scaleUtc } from 'd3';
3
3
  import get from 'lodash/get';
4
4
  import { DEFAULT_AXIS_TYPE } from '../../constants';
5
5
  import { CHART_SERIES_WITH_VOLUME_ON_Y_AXIS, getAxisHeight, getDataCategoryValue, getDefaultMaxXAxisValue, getDomainDataXBySeries, getDomainDataYBySeries, getOnlyVisibleSeries, isAxisRelatedSeries, isSeriesWithCategoryValues, } from '../../utils';
@@ -24,7 +24,8 @@ export function createYScale(axis, series, boundsHeight) {
24
24
  const yCategories = get(axis, 'categories');
25
25
  const yTimestamps = get(axis, 'timestamps');
26
26
  switch (yType) {
27
- case 'linear': {
27
+ case 'linear':
28
+ case 'logarithmic': {
28
29
  const domain = getDomainDataYBySeries(series);
29
30
  const range = [boundsHeight, boundsHeight * axis.maxPadding];
30
31
  if (isNumericalArrayData(domain)) {
@@ -34,7 +35,8 @@ export function createYScale(axis, series, boundsHeight) {
34
35
  if (series.some((s) => CHART_SERIES_WITH_VOLUME_ON_Y_AXIS.includes(s.type))) {
35
36
  yMaxValue = Math.max(yMaxValue, 0);
36
37
  }
37
- return scaleLinear().domain([yMinValue, yMaxValue]).range(range).nice();
38
+ const scaleFn = yType === 'logarithmic' ? scaleLog : scaleLinear;
39
+ return scaleFn().domain([yMinValue, yMaxValue]).range(range).nice();
38
40
  }
39
41
  break;
40
42
  }
@@ -94,13 +96,15 @@ export function createXScale(axis, series, boundsWidth) {
94
96
  const xAxisMinPadding = boundsWidth * maxPadding + calculateXAxisPadding(series);
95
97
  const xRange = [0, boundsWidth - xAxisMinPadding];
96
98
  switch (xType) {
97
- case 'linear': {
99
+ case 'linear':
100
+ case 'logarithmic': {
98
101
  const domain = getDomainDataXBySeries(series);
99
102
  if (isNumericalArrayData(domain)) {
100
103
  const [domainXMin, domainXMax] = extent(domain);
101
104
  const xMinValue = typeof xMin === 'number' ? xMin : domainXMin;
102
105
  const xMaxValue = typeof xMax === 'number' ? Math.max(xMax, domainXMax) : domainXMax;
103
- return scaleLinear().domain([xMinValue, xMaxValue]).range(xRange).nice();
106
+ const scaleFn = xType === 'logarithmic' ? scaleLog : scaleLinear;
107
+ return scaleFn().domain([xMinValue, xMaxValue]).range(xRange).nice();
104
108
  }
105
109
  break;
106
110
  }
@@ -1,5 +1,5 @@
1
1
  import get from 'lodash/get';
2
- import { DEFAULT_AXIS_LABEL_FONT_SIZE, axisLabelsDefaults, yAxisTitleDefaults, } from '../../constants';
2
+ import { DEFAULT_AXIS_LABEL_FONT_SIZE, DEFAULT_AXIS_TYPE, axisLabelsDefaults, yAxisTitleDefaults, } from '../../constants';
3
3
  import { CHART_SERIES_WITH_VOLUME_ON_Y_AXIS, formatAxisTickLabel, getClosestPointsRange, getHorisontalSvgTextHeight, getLabelsSize, getScaleTicks, getWaterfallPointSubtotal, } from '../../utils';
4
4
  import { createYScale } from '../useAxisScales';
5
5
  const getAxisLabelMaxWidth = (args) => {
@@ -60,7 +60,7 @@ export const getPreparedYAxis = ({ series, yAxis, }) => {
60
60
  const titleStyle = {
61
61
  fontSize: get(axisItem, 'title.style.fontSize', yAxisTitleDefaults.fontSize),
62
62
  };
63
- const axisType = get(axisItem, 'type', 'linear');
63
+ const axisType = get(axisItem, 'type', DEFAULT_AXIS_TYPE);
64
64
  const preparedAxis = {
65
65
  type: axisType,
66
66
  labels: {
@@ -379,9 +379,19 @@ function validateCellManipulationConfig(tooltipOptions, property, item) {
379
379
  item[property] = tooltipOptions[property];
380
380
  }
381
381
  }
382
+ function getSeriesTypeFromTooltipContext() {
383
+ var _a, _b;
384
+ if (this.series) {
385
+ return this.series.type;
386
+ }
387
+ if (Array.isArray(this.points)) {
388
+ return (_b = (_a = this.points[0]) === null || _a === void 0 ? void 0 : _a.series) === null || _b === void 0 ? void 0 : _b.type;
389
+ }
390
+ return '';
391
+ }
382
392
  function getTooltip(tooltip, options, comments, holidays) {
383
393
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
384
- const serieType = (this.series && this.series.type) || tooltip.chart.options.chart.type;
394
+ const serieType = getSeriesTypeFromTooltipContext.call(this) || tooltip.chart.options.chart.type;
385
395
  const chart = tooltip.chart;
386
396
  const xAxis = chart.xAxis[0];
387
397
  const isDatetimeXAxis = xAxis.options.type === 'datetime';
@@ -1,6 +1,6 @@
1
1
  import type { FormatNumberOptions } from '../../plugins/shared';
2
2
  import type { BaseTextStyle } from './base';
3
- export type ChartKitWidgetAxisType = 'category' | 'datetime' | 'linear';
3
+ export type ChartKitWidgetAxisType = 'category' | 'datetime' | 'linear' | 'logarithmic';
4
4
  export type ChartKitWidgetAxisLabels = {
5
5
  /** Enable or disable the axis labels. */
6
6
  enabled?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/chartkit",
3
- "version": "5.8.0",
3
+ "version": "5.9.0",
4
4
  "description": "React component used to render charts based on any sources you need",
5
5
  "license": "MIT",
6
6
  "repository": "git@github.com:gravity-ui/ChartKit.git",