Skip to content

Commit

Permalink
feat(Yagr plugin): update lib, use legendColorKey from yagr lib
Browse files Browse the repository at this point in the history
  • Loading branch information
artemmufazalov committed Jan 30, 2025
1 parent 90b4a3e commit 46cc3e5
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 53 deletions.
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@gravity-ui/charts": "^0.5.0",
"@gravity-ui/date-utils": "^2.1.0",
"@gravity-ui/i18n": "^1.0.0",
"@gravity-ui/yagr": "^4.4.0",
"@gravity-ui/yagr": "^4.5.0",
"afterframe": "^1.0.2",
"lodash": "^4.17.21",
"tslib": "^2.6.2"
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/yagr/__stories__/Yagr.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, {useEffect, useState} from 'react';
import {dateTime} from '@gravity-ui/date-utils';
import {Button} from '@gravity-ui/uikit';
import placement from '@gravity-ui/yagr/dist/YagrCore/plugins/tooltip/placement';
import {Meta, Story} from '@storybook/react';
import {Meta, StoryFn} from '@storybook/react';

import {CustomTooltipProps, TooltipHandlerData, YagrPlugin} from '../';
import {ChartKit} from '../../../components/ChartKit';
Expand All @@ -19,7 +19,7 @@ export default {
component: ChartKit,
} as Meta;

const LineTemplate: Story<any> = () => {
const LineTemplate: StoryFn = () => {
const [shown, setShown] = React.useState(false);
const chartkitRef = React.useRef<ChartKitRef>();

Expand All @@ -35,7 +35,7 @@ const LineTemplate: Story<any> = () => {
);
};

const UpdatesTemplate: Story<any> = () => {
const UpdatesTemplate: StoryFn = () => {
const [shown, setShown] = React.useState(false);
const chartkitRef = React.useRef<ChartKitRef>();

Expand Down Expand Up @@ -107,7 +107,7 @@ function Tooltip({yagr}: CustomTooltipProps) {
);
}

const CustomTooltipImpl: Story<any> = () => {
const CustomTooltipImpl: StoryFn = () => {
const [shown, setShown] = React.useState(false);
const chartkitRef = React.useRef<ChartKitRef>();

Expand All @@ -132,7 +132,7 @@ const CustomTooltipImpl: Story<any> = () => {
);
};

const AreaTemplate: Story<any> = () => {
const AreaTemplate: StoryFn = () => {
const [shown, setShown] = React.useState(false);
const chartkitRef = React.useRef<ChartKitRef>();

Expand Down
8 changes: 5 additions & 3 deletions src/plugins/yagr/__stories__/mocks/line10.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {AreaSeriesOptions, YagrSeriesData, YagrWidgetData} from '../../types';
import type {AreaSeriesOptions, RawSerieData, YagrWidgetData} from '../../types';

export const line10: YagrWidgetData = {
data: {
Expand Down Expand Up @@ -118,7 +118,7 @@ function colorHexToRGBA(htmlColor: string, opacity: number) {
return `rgba(${[red, green, blue, opacity].join(',')})`;
}

const graphs: YagrSeriesData<AreaSeriesOptions>[] = [
const graphs: RawSerieData<AreaSeriesOptions>[] = [
{
id: '0',
name: 'Serie 1',
Expand Down Expand Up @@ -189,7 +189,9 @@ export const line10WithGrafanaStyle: YagrWidgetData = {
show: true,
tracking: 'sticky',
},
legend: {},
legend: {
show: true,
},
processing: {},
},
};
30 changes: 4 additions & 26 deletions src/plugins/yagr/renderer/tooltip/renderTooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,9 @@ const calcOption = <T>(d: T | {[key in string]: T} | undefined) => {
: d;
};

const getSeriesColorProperty = (args: {
data: TooltipRenderOptions;
userData: YagrWidgetData['data'];
row: TooltipRow;
rowIndex: number;
}) => {
const {data, userData, row, rowIndex} = args;
const userSeries = userData.graphs[rowIndex];
const lineColor = data.yagr.getSeriesById(row.id)?.lineColor;
let seriesColor = row.color;

switch (userSeries?.legendColorKey) {
case 'lineColor': {
if (lineColor) {
seriesColor = lineColor;
}
break;
}
case 'color':
default: {
seriesColor = row.color;
}
}

return seriesColor;
const getSeriesColorProperty = ({data, rowId}: {data: TooltipRenderOptions; rowId: string}) => {
const series = data.yagr.getSeriesById(rowId);
return data.yagr.getSerieLegendColor(series);
};

/*
Expand Down Expand Up @@ -88,7 +66,7 @@ export const getRenderTooltip =
({
...row,
seriesName: row.name || 'Serie ' + (i + 1),
seriesColor: getSeriesColorProperty({data, userData, row, rowIndex: i}),
seriesColor: getSeriesColorProperty({data, rowId: row.id}),
selectedSeries: row.active,
seriesIdx: row.seriesIdx,
percentValue:
Expand Down
15 changes: 2 additions & 13 deletions src/plugins/yagr/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {MinimalValidConfig, RawSerieData, SeriesOptions, YagrConfig} from '@gravity-ui/yagr';
import type {MinimalValidConfig, RawSerieData, YagrConfig} from '@gravity-ui/yagr';
import type Yagr from '@gravity-ui/yagr';

import {ChartKitProps} from 'src/types';
Expand All @@ -15,20 +15,9 @@ export type YagrWidgetProps = ChartKitProps<'yagr'> & {
id: string;
};

export type YagrSeriesData<T = Omit<SeriesOptions, 'type'>> = RawSerieData<T> & {
/**
* Determines what data value should be used to get a color for tooltip series. Does not work in case of using custom tooltip rendered via `tooltip` property.
* - `lineColor` indicates that lineColor property should be used
* - `color` indicates that color property should be used
*
* @default 'color'
*/
legendColorKey?: 'color' | 'lineColor';
};

export type YagrWidgetData = {
data: {
graphs: YagrSeriesData[];
graphs: RawSerieData[];
timeline: number[];
/**
* Allow to setup timezone for X axis and tooltip's header.
Expand Down

0 comments on commit 46cc3e5

Please sign in to comment.