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

tweak(tupaiaWeb): RN-249: getting x-Name in viz export #5886

Merged
merged 7 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -23,8 +23,11 @@ interface VerticalTickProps {
export const VerticalTick = ({ x, y, payload }: VerticalTickProps) => {
const stringVal =
payload.value !== undefined && payload.value !== null ? String(payload.value) : '';
const marginX = 10;
const marginY = 5;

return (
<g transform={`translate(${x - 5},${y + 3})`}>
<g transform={`translate(${x + marginX},${y - marginY})`}>
<text
fontSize="13px"
transform="rotate(305)"
Expand Down
12 changes: 7 additions & 5 deletions packages/ui-chart-components/src/components/Axes/XAxis.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Tupaia
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*/

import React from 'react';
Expand Down Expand Up @@ -41,13 +41,13 @@ const renderXAxisLabel = (
label: string | undefined,
fillColor: string | undefined,
isEnlarged: boolean,
isExporting: boolean,
isExporting: boolean
): LabelProps | undefined => {
if (label && isEnlarged && !isExporting) {
if (label && isEnlarged) {
return {
value: label,
fill: fillColor,
offset: -5,
offset: isExporting ? -50 : -5,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is intended to prevent overlapping between tick labels and the x-axis. Given the tick labels are restricted to a maximum number of characters (using ellipses for longer text) this approach should resolve the issue.

position: 'insideBottom',
};
}
Expand Down Expand Up @@ -76,6 +76,7 @@ interface XAxisProps {

export const XAxis = ({ config, report, isExporting = false, isEnlarged = false }: XAxisProps) => {
const fillColor = isExporting ? DARK_BLUE : getContrastTextColor();
const tickMargin = isExporting ? 20 : 0;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the space between the x-axis label and tick label.

const { Bar, Composed } = ChartType;
const { chartType, chartConfig } = config;
const { data = [] } = report;
Expand Down Expand Up @@ -154,6 +155,7 @@ export const XAxis = ({ config, report, isExporting = false, isEnlarged = false
return { left: 0, right: 10 };
};


const renderVerticalTick = (tickProps: TickProps & { x: number; y: number }) => {
const { payload, x, y } = tickProps;

Expand All @@ -177,7 +179,7 @@ export const XAxis = ({ config, report, isExporting = false, isEnlarged = false
tick={getXAxisTickMethod()}
tickFormatter={formatXAxisTick}
padding={getXAxisPadding()}
tickSize={6}
tickMargin={tickMargin}
{...(isTimeSeries ? AXIS_TIME_PROPS : { dataKey: 'name' })}
/>
);
Expand Down
12 changes: 6 additions & 6 deletions packages/ui-chart-components/src/components/CartesianChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import {
LineChart as LineChartComponent,
AreaChart as AreaChartComponent,
} from './Charts';
import { getCartesianLegend, ReferenceLines, ChartTooltip as CustomTooltip } from './Reference';
import { ReferenceLines, ChartTooltip as CustomTooltip } from './Reference';
import { getCartesianChartLegend } from './Legend';
import { XAxis as XAxisComponent, YAxes } from './Axes';

const { Area, Bar, Composed, Line } = ChartType;
Expand Down Expand Up @@ -74,9 +75,9 @@ const getRealDataKeys = (chartConfig: CartesianChartConfig['chartConfig'] | {})
const getLegendAlignment = (
legendPosition: LegendPosition,
isExporting: boolean,
): Pick<LegendProps, 'align' | 'verticalAlign' | 'layout'> => {
): Pick<LegendProps, 'align' | 'verticalAlign'> => {
if (isExporting) {
return { verticalAlign: 'top', align: 'right', layout: 'vertical' };
return { verticalAlign: 'top', align: 'right' };
}
if (legendPosition === 'bottom') {
return { verticalAlign: 'bottom', align: 'center' };
Expand Down Expand Up @@ -218,7 +219,7 @@ export const CartesianChart = ({
const aspect = !isEnlarged && !isMobileSize && !isExporting ? 1.6 : undefined;
const height = getHeight(isExporting, isEnlarged, hasLegend, isMobileSize);

const { verticalAlign, align, layout } = getLegendAlignment(legendPosition, isExporting);
const { verticalAlign, align } = getLegendAlignment(legendPosition, isExporting);

const presentationOptions = 'presentationOptions' in config ? config.presentationOptions : {};

Expand Down Expand Up @@ -252,8 +253,7 @@ export const CartesianChart = ({
<Legend
verticalAlign={verticalAlign}
align={align}
layout={layout}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The 'layout' property (whether 'horizontal' or 'vertical') does not affect the custom component as expected. This is because the component already has its own styling, including alignment and flex properties. Therefore, the layout property is not needed in this case.

content={getCartesianLegend({
content={getCartesianChartLegend({
chartConfig,
getIsActiveKey,
isExporting,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import { ChartReport, PieChartConfig, PieChartSegmentConfig } from '@tupaia/type
import { CHART_COLOR_PALETTE, OFF_WHITE } from '../../constants';
import { isMobile } from '../../utils';
import { LegendPosition } from '../../types';
import { getPieLegend, TooltipContainer } from '../Reference';
import { TooltipContainer } from '../Reference';
import { getPieChartLegend } from '../Legend';

const Heading = styled(Typography)`
font-weight: 500;
Expand Down Expand Up @@ -202,7 +203,7 @@ export const PieChart = ({
layout={layout as LegendProps['layout']}
verticalAlign={verticalAlign as LegendProps['verticalAlign']}
align={align as LegendProps['align']}
content={getPieLegend({
content={getPieChartLegend({
isEnlarged,
isExporting,
legendPosition,
Expand Down
49 changes: 49 additions & 0 deletions packages/ui-chart-components/src/components/Legend/LegendItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Tupaia
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*/

import React from 'react';
import styled from 'styled-components';
import MuiButton from '@material-ui/core/Button';

const getLegendTextColor = (theme: any, isExporting: boolean) => {
if (isExporting) {
return '#2c3236';
}

if (theme.palette.type === 'light') {
return theme.palette.text.primary;
}
return 'white';
};

export const LegendItem = styled(({ isExporting, ...props }) => <MuiButton {...props} />)`
text-align: left;
font-size: 0.75rem;
padding-bottom: 0;
padding-top: 0;
margin-right: 1.2rem;

.MuiButton-label {
display: flex;
white-space: nowrap;
align-items: center;
color: ${({ theme, isExporting }) => getLegendTextColor(theme, isExporting)};
> div {
width: ${isExporting => (isExporting ? '100%' : '')};
}
}

&.Mui-disabled {
color: ${({ theme, isExporting }) => getLegendTextColor(theme, isExporting)};
}

// small styles
&.small {
font-size: 0.5rem;
padding-bottom: 0;
padding-top: 0;
margin-right: 0;
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Tupaia
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*/

import React from 'react';
import { CartesianChartConfig } from '@tupaia/types';
import styled from 'styled-components';
import { TooltipPayload } from 'recharts';
import Tooltip from '@material-ui/core/Tooltip';
import { LegendPosition } from '../../types';
import { isMobile } from '../../utils';
import { LegendItem } from './LegendItem';

const LegendContainer = styled.div<{
$position?: LegendPosition;
}>`
display: flex;
flex-wrap: wrap;
justify-content: center;
flex-direction: row;
// Add more padding at the bottom for exports
padding: ${props => (props.$position === 'bottom' ? '1em 0 0 3.5em' : '0 0 3em 0')};
`;

const TooltipContainer = styled.div`
display: flex;
align-items: center;
justify-content: flex-start;
padding-bottom: 0.3rem;
padding-top: 0.3rem;
`;

const Box = styled.span`
display: block;
width: 1.25rem;
height: 1.25rem;
margin-right: 0.625rem;
border-radius: 3px;

// small styles
&.small {
width: 0.8rem;
min-width: 0.8rem;
height: 0.8rem;
margin-right: 0.4rem;
}
`;

const Text = styled.span`
line-height: 1.4;
`;

interface CartesianLegendProps {
chartConfig: CartesianChartConfig['chartConfig'];
onClick: Function;
getIsActiveKey: Function;
isExporting?: boolean;
legendPosition?: LegendPosition;
}

export const getCartesianChartLegend =
({ chartConfig, onClick, getIsActiveKey, isExporting, legendPosition }: CartesianLegendProps) =>
({ payload }: any) => {
const isMobileSize = isMobile(isExporting);
return (
<LegendContainer $position={isExporting ? 'top' : legendPosition}>
{payload.map(({ color, value, dataKey }: TooltipPayload) => {
// check the type here because according to TooltipPayload, value can be a number or a readonly string | number array
const displayValue = (typeof value === 'string' && chartConfig?.[value]?.label) || value;

return (
<LegendItem
// parse to a string because the key can't be an array
key={String(value)}
onClick={() => onClick(dataKey)}
isExporting={isExporting}
className={isMobileSize ? 'small' : 'enlarged'}
style={{ textDecoration: getIsActiveKey(value) ? '' : 'line-through' }}
>
<Tooltip title="Click to filter data" placement="top" arrow>
<TooltipContainer>
<Box
className={isMobileSize ? 'small' : 'enlarged'}
style={{ background: color }}
/>
<Text>{displayValue}</Text>
</TooltipContainer>
</Tooltip>
</LegendItem>
);
})}
</LegendContainer>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Tupaia
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*/

import React from 'react';
import { PieChartConfig } from '@tupaia/types';
import { TooltipPayload } from 'recharts';
import { formatDataValueByType } from '@tupaia/utils';
import { LegendPosition } from '../../types';
import { isMobile } from '../../utils';
import { LegendItem } from './LegendItem';
import styled from 'styled-components';

const PieLegendContainer = styled.div<{
$position?: LegendPosition;
$isExporting?: boolean;
}>`
display: flex;
flex-wrap: ${props => (props.$isExporting ? 'nowrap' : 'wrap')};
justify-content: ${props => (props.$position === 'bottom' ? 'center' : 'flex-start')};
flex-direction: ${props => (props.$isExporting ? 'column' : 'row')};
padding: 0;
`;

const TooltipContainer = styled.div`
display: flex;
align-items: center;
justify-content: flex-start;
padding-bottom: 0.3rem;
padding-top: 0.3rem;
`;

const Box = styled.span`
display: block;
width: 1.25rem;
height: 1.25rem;
margin-right: 0.625rem;
border-radius: 3px;

// small styles
&.small {
width: 0.8rem;
min-width: 0.8rem;
height: 0.8rem;
margin-right: 0.4rem;
}
`;

const Text = styled.span`
line-height: 1.4;
`;

interface PieLegendProps {
isEnlarged?: boolean;
isExporting?: boolean;
legendPosition?: LegendPosition;
config: PieChartConfig;
}

const getPieLegendDisplayValue = (
value: TooltipPayload['value'],
item: any,
config: PieLegendProps['config'],
isEnlarged?: PieLegendProps['isEnlarged'],
isMobileSize?: boolean,
) => {
const metadata = item[`${value}_metadata`];
const labelSuffix = formatDataValueByType({ value: item.value, metadata }, config.valueType);

// on mobile the legend will show the actual formatDataValueByType after the label value
return isMobileSize && isEnlarged ? `${value} ${labelSuffix}` : value;
};

export const getPieChartLegend =
({ isEnlarged, isExporting, legendPosition, config }: PieLegendProps) =>
({ payload }: any) => {
const isMobileSize = isMobile(isExporting);
return (
<PieLegendContainer $position={legendPosition} $isExporting={isExporting}>
{payload.map(({ color, value, payload: item }: TooltipPayload) => {
const displayValue = getPieLegendDisplayValue(
value,
item,
config,
isEnlarged,
isMobileSize,
);

return (
<LegendItem
key={String(value)}
isExporting={isExporting}
className={isEnlarged && !isMobileSize ? 'enlarged' : 'small'}
disabled
>
<TooltipContainer>
<Box
className={isEnlarged && !isMobileSize ? 'enlarged' : 'small'}
style={{ background: color }}
/>
<Text>{displayValue}</Text>
</TooltipContainer>
</LegendItem>
);
})}
</PieLegendContainer>
);
};
7 changes: 7 additions & 0 deletions packages/ui-chart-components/src/components/Legend/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Tupaia
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*/

export { getCartesianChartLegend } from './getCartesianChartLegend';
export { getPieChartLegend } from './getPieChartLegend';
Loading