Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[charts] Harmonize charts TS #13366

Merged
merged 4 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ChartSeriesDefaultized, ChartSeriesType } from '../models/seriesType/co
import { AxisDefaultized } from '../models/axis';
import { ChartsTooltipClasses } from './chartsTooltipClasses';
import { DefaultChartsAxisTooltipContent } from './DefaultChartsAxisTooltipContent';
import { isCartesianSeriesType } from './utils';
import { isCartesianSeriesType } from '../internals/isCaterisan';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File name is wrong

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which one would you recomand?

I preferred to keep a single file for both isCartesianSeries and isCartesianSeriesType so I picked the common part that is not the exact name of a function

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I don't mind much, the issue is that isCaterisan is the wrong spelling. Should be isCartesian

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 🙏

I'm somewhat insensitive to letter order, if I don't focus on a specific word

import colorGetter from '../internals/colorGetter';
import { ZAxisContext } from '../context/ZAxisContextProvider';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import {
ChartsTooltipRow,
} from './ChartsTooltipTable';
import type { ChartsAxisContentProps } from './ChartsAxisTooltipContent';
import { isCartesianSeries, utcFormatter } from './utils';
import { utcFormatter } from './utils';
import { getLabel } from '../internals/getLabel';
import { isCartesianSeries } from '../internals/isCaterisan';

function DefaultChartsAxisTooltipContent(props: ChartsAxisContentProps) {
const { series, axis, dataIndex, axisValue, sx, classes } = props;
Expand Down
21 changes: 1 addition & 20 deletions packages/x-charts/src/ChartsTooltip/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import * as React from 'react';
import { AxisInteractionData, ItemInteractionData } from '../context/InteractionProvider';
import { SvgContext } from '../context/DrawingProvider';
import {
CartesianChartSeriesType,
ChartSeriesDefaultized,
ChartSeriesType,
} from '../models/seriesType/config';
import { ChartSeriesType } from '../models/seriesType/config';

export function generateVirtualElement(mousePosition: { x: number; y: number } | null) {
if (mousePosition === null) {
Expand Down Expand Up @@ -95,21 +91,6 @@ export function getTooltipHasData(
return hasAxisXData || hasAxisYData;
}

export function isCartesianSeriesType(seriesType: string): seriesType is CartesianChartSeriesType {
return ['bar', 'line', 'scatter'].includes(seriesType);
}

export function isCartesianSeries(
series: ChartSeriesDefaultized<ChartSeriesType> & { getColor: (dataIndex: number) => string },
): series is ChartSeriesDefaultized<CartesianChartSeriesType> & {
getColor: (dataIndex: number) => string;
};
export function isCartesianSeries(
series: ChartSeriesDefaultized<ChartSeriesType>,
): series is ChartSeriesDefaultized<CartesianChartSeriesType> {
return isCartesianSeriesType(series.type);
}

export function utcFormatter(v: string | number | Date): string {
if (v instanceof Date) {
return v.toUTCString();
Expand Down
28 changes: 28 additions & 0 deletions packages/x-charts/src/internals/configInit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ChartSeriesType } from '../models/seriesType/config';

let instance: undefined | Set<ChartSeriesType>;

class CartesianSeriesTypes {
types: Set<ChartSeriesType> = new Set();

constructor() {
if (instance) {
throw new Error('You can only create one instance!');
}
instance = this.types;
}

addType(value: ChartSeriesType) {
this.types.add(value);
}

getTypes() {
return this.types;
}
}

export const cartesianSeriesTypes = new CartesianSeriesTypes();

cartesianSeriesTypes.addType('bar');
cartesianSeriesTypes.addType('line');
cartesianSeriesTypes.addType('scatter');
JCQuintas marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/x-charts/src/internals/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './configInit';
21 changes: 21 additions & 0 deletions packages/x-charts/src/internals/isCaterisan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
ChartSeriesType,
CartesianChartSeriesType,
ChartSeriesDefaultized,
} from '../models/seriesType/config';
import { cartesianSeriesTypes } from './configInit';

export function isCartesianSeriesType(seriesType: string): seriesType is CartesianChartSeriesType {
return cartesianSeriesTypes.getTypes().has(seriesType as ChartSeriesType);
}

export function isCartesianSeries(
series: ChartSeriesDefaultized<ChartSeriesType> & { getColor: (dataIndex: number) => string },
): series is ChartSeriesDefaultized<CartesianChartSeriesType> & {
getColor: (dataIndex: number) => string;
};
export function isCartesianSeries(
series: ChartSeriesDefaultized<ChartSeriesType>,
): series is ChartSeriesDefaultized<CartesianChartSeriesType> {
return isCartesianSeriesType(series.type);
}
4 changes: 0 additions & 4 deletions packages/x-charts/src/models/seriesType/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ export type CartesianChartSeriesType = keyof Pick<
}[ChartSeriesType]
>;

export function isCartesianSeriesType(seriesType: string): seriesType is CartesianChartSeriesType {
return ['bar', 'line', 'scatter', 'heatmap'].includes(seriesType);
}

export type StackableChartSeriesType = keyof Pick<
ChartsSeriesConfig,
{
Expand Down