Skip to content

Commit

Permalink
Add linecap option, fix story
Browse files Browse the repository at this point in the history
  • Loading branch information
artemipanchuk committed Dec 25, 2023
1 parent 081fade commit e59e7db
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 38 deletions.
48 changes: 17 additions & 31 deletions src/plugins/d3/__stories__/line/Shapes.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,29 @@ import {StoryObj} from '@storybook/react';
import {Button} from '@gravity-ui/uikit';
import {settings} from '../../../../libs';
import {D3Plugin} from '../..';
import {ChartKitWidgetData, LineSeriesData} from '../../../../types';
import {ChartKitWidgetData, LineSeriesData, LineSeries, DashStyle} from '../../../../types';
import {ChartKit} from '../../../../components/ChartKit';
import nintendoGames from '../../examples/nintendoGames';
import {HighchartsPlugin} from '../../../highcharts';

enum LineShapeType {
Solid = 'Solid',
ShortDash = 'ShortDash',
ShortDot = 'ShortDot',
ShortDashDot = 'ShortDashDot',
ShortDashDotDot = 'ShortDashDotDot',
Dot = 'Dot',
Dash = 'Dash',
LongDash = 'LongDash',
DashDot = 'DashDot',
LongDashDot = 'LongDashDot',
LongDashDotDot = 'LongDashDotDot',
}
const SHAPES_ORDER = {
[LineShapeType.Solid]: 1,
[LineShapeType.Dash]: 2,
[LineShapeType.Dot]: 3,
[LineShapeType.ShortDashDot]: 4,
[LineShapeType.LongDash]: 5,
[LineShapeType.LongDashDot]: 6,
[LineShapeType.ShortDot]: 7,
[LineShapeType.LongDashDotDot]: 8,
[LineShapeType.ShortDash]: 9,
[LineShapeType.DashDot]: 10,
[LineShapeType.ShortDashDotDot]: 11,
const SHAPES = {
[DashStyle.Solid]: 1,
[DashStyle.Dash]: 2,
[DashStyle.Dot]: 3,
[DashStyle.ShortDashDot]: 4,
[DashStyle.LongDash]: 5,
[DashStyle.LongDashDot]: 6,
[DashStyle.ShortDot]: 7,
[DashStyle.LongDashDotDot]: 8,
[DashStyle.ShortDash]: 9,
[DashStyle.DashDot]: 10,
[DashStyle.ShortDashDotDot]: 11,
};

const selectShapes = (): LineShapeType[] => Object.values(LineShapeType);
const getServerShapesOrder = () => selectShapes().sort((a, b) => SHAPES_ORDER[a] - SHAPES_ORDER[b]);
const selectShapes = (): DashStyle[] => Object.values(DashStyle);
const getShapesOrder = () => selectShapes().sort((a, b) => SHAPES[a] - SHAPES[b]);

const SHAPES_IN_ORDER = getServerShapesOrder();
const SHAPES_IN_ORDER = getShapesOrder();

function prepareData(): ChartKitWidgetData {
const games = nintendoGames.filter((d) => {
Expand Down Expand Up @@ -127,8 +114,7 @@ const ChartStory = ({data}: {data: ChartKitWidgetData}) => {
return <Button onClick={() => setShown(true)}>Show chart</Button>;
}

data.series.data.forEach((graph, i) => {
// @ts-ignore
(data.series.data as LineSeries[]).forEach((graph, i) => {
graph.dashStyle = SHAPES_IN_ORDER[i % SHAPES_IN_ORDER.length];
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ChartKitWidgetSeries,
ChartKitWidgetSeriesOptions,
DashStyle,
LineCap,
LineSeries,
RectLegendSymbolOptions,
} from '../../../../../types';
Expand Down Expand Up @@ -41,6 +42,10 @@ function prepareDashStyle(series: LineSeries) {
return series.dashStyle || DashStyle.Solid;
}

function prepareLinecap(series: LineSeries) {
return series.linecap || LineCap.None;
}

function prepareLineLegendSymbol(
series: ChartKitWidgetSeries,
seriesOptions?: ChartKitWidgetSeriesOptions,
Expand Down Expand Up @@ -109,6 +114,7 @@ export function prepareLineSeries(args: PrepareLineSeriesArgs) {
},
marker: prepareMarker(series, seriesOptions),
dashStyle: prepareDashStyle(series),
linecap: prepareLinecap(series),
};

return prepared;
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/d3/renderer/hooks/useSeries/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ConnectorCurve,
PathLegendSymbolOptions,
DashStyle,
LineCap,
AreaSeries,
AreaSeriesData,
} from '../../../../../types';
Expand Down Expand Up @@ -157,6 +158,7 @@ export type PreparedLineSeries = {
};
};
dashStyle: DashStyle;
linecap: LineCap;
} & BasePreparedSeries;

export type PreparedAreaSeries = {
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/d3/renderer/hooks/useShapes/line/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ export const LineSeriesShapes = (args: Args) => {
.attr('fill', 'none')
.attr('stroke', (d) => d.color)
.attr('stroke-width', (d) => d.width)
.attr('stroke-linejoin', (d) => d.linecap)
.attr('stroke-linecap', (d) => d.linecap)
.attr('stroke-dasharray', (d) => getLineDashArray(d.dashStyle, d.width));

let dataLabels = preparedData.reduce((acc, d) => {
Expand Down
6 changes: 2 additions & 4 deletions src/plugins/d3/renderer/hooks/useShapes/line/prepare-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ export const prepareLineData = (args: {
hovered: false,
active: true,
id: s.id,
dashStyle: s.dashStyle,
linecap: s.linecap,
};

if (s.dashStyle) {
result.dashStyle = s.dashStyle;
}

acc.push(result);

return acc;
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/d3/renderer/hooks/useShapes/line/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {PreparedLineSeries} from '../../useSeries/types';
import {DashStyle, LineSeriesData} from '../../../../../../types';
import {DashStyle, LineCap, LineSeriesData} from '../../../../../../types';
import {LabelData} from '../../../types';

export type PointData = {
Expand All @@ -26,4 +26,5 @@ export type PreparedLineData = {
active: boolean;
labels: LabelData[];
dashStyle: DashStyle;
linecap: LineCap;
};
3 changes: 2 additions & 1 deletion src/types/widget-data/line.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {BaseSeries, BaseSeriesData} from './base';
import type {ChartKitWidgetLegend, RectLegendSymbolOptions} from './legend';
import type {PointMarkerOptions} from './marker';
import type {DashStyle} from './series';
import type {DashStyle, LineCap} from './series';

export type LineSeriesData<T = any> = BaseSeriesData<T> & {
/**
Expand Down Expand Up @@ -47,4 +47,5 @@ export type LineSeries<T = any> = BaseSeries & {
/** Options for the point markers of line series */
marker?: LineMarkerOptions;
dashStyle?: DashStyle;
linecap?: LineCap;
};
13 changes: 12 additions & 1 deletion src/types/widget-data/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ export enum DashStyle {
Solid = 'Solid',
}

export enum LineCap {}
export enum LineCap {
Butt = 'butt',
Round = 'round',
Square = 'square',
None = 'none',
}

type BasicHoverState = {
/**
Expand Down Expand Up @@ -195,6 +200,12 @@ export type ChartKitWidgetSeriesOptions = {
* @default 'Solid'
* */
dashStyle?: DashStyle;

/** Options for line cap style
*
* @default 'round'
* */
linecap?: LineCap;
};
area?: {
/** Pixel width of the graph line.
Expand Down

0 comments on commit e59e7db

Please sign in to comment.