Skip to content

Commit 507c9fd

Browse files
authored
Merge pull request #112 from rcpch/eatyourpeas/issue101
aspect-ratio
2 parents 5c6b367 + a9670c2 commit 507c9fd

11 files changed

+69
-27
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rcpch/digital-growth-charts-react-component-library",
3-
"version": "7.0.12",
3+
"version": "7.0.13",
44
"description": "A React component library for the RCPCH digital growth charts using Rollup, TypeScript and Styled-Components",
55
"main": "build/index.js",
66
"module": "build/esm.index.js",

src/CentileChart/CentileChart.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
VictoryLabel,
1313
VictoryArea,
1414
DomainPropType,
15-
VictoryPortal,
1615
} from 'victory';
1716

1817
// helper functions
@@ -89,6 +88,9 @@ function CentileChart({
8988
midParentalHeightData,
9089
enableZoom,
9190
styles,
91+
height,
92+
width,
93+
textScaleFactor,
9294
enableExport,
9395
exportChartCallback,
9496
clinicianFocus
@@ -277,8 +279,8 @@ function CentileChart({
277279
{/* Tooltips are here as it is the parent component. More information of tooltips in centiles below. */}
278280

279281
<VictoryChart
280-
width={1000}
281-
height={800}
282+
width={width}
283+
height={height}
282284
style={styles.chartMisc}
283285
domain={computedDomains}
284286
containerComponent={
@@ -314,7 +316,7 @@ function CentileChart({
314316
cornerRadius={0}
315317
flyoutHeight={(datum) => {
316318
const numberOfLines = datum.text.length;
317-
return numberOfLines * 18; // 18 is the line height
319+
return numberOfLines * 18 * textScaleFactor; // 18 is the line height
318320
}}
319321
flyoutStyle={{
320322
...styles.toolTipFlyout,

src/CentileChart/CentileChart.types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export interface CentileChartProps {
2828
midParentalHeightData?: MidParentalHeightObject | null;
2929
enableZoom?: boolean;
3030
styles: { [key: string]: any };
31+
width?: number;
32+
height?: number;
33+
textScaleFactor?: number;
3134
enableExport?: boolean;
3235
exportChartCallback(svg: any): any;
3336
clinicianFocus?: boolean | undefined | null;

src/RCPCHChart/RCPCHChart.mdx

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Other props are:
2828
- `exportChartCallback(svg?: any): any;` Names the function within the client to return the exported SVG to.
2929
- `clinicianFocus?: boolean | undefined | null;` Toggles tooltip advice between those aimed at clinicians and those more appropriate for patients/lay people.
3030
- `theme?: 'monochrome' | 'traditional' | 'tanner1' | 'tanner2' | 'tanner3' | 'custom'`
31+
- `height?: number` : defaults to 800px
32+
- `width?: number`: defaults to 1000px
3133
- `customThemeStyles?`: discussed below
3234

3335
### `measurements`

src/RCPCHChart/RCPCHChart.stories.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ export const CentileChart: Story = {
6060
enableExport: false,
6161
exportChartCallback: ()=>{},
6262
theme: 'tanner2',
63+
height: 800,
64+
width: 1000,
6365
customThemeStyles: {}
6466
},
6567
};

src/RCPCHChart/RCPCHChart.tsx

+28-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// packages/libraries
22
import * as React from 'react';
33

4-
import { createGlobalStyle } from 'styled-components';
4+
import { styled } from 'styled-components';
55

66
// props and interfaces
77
import { RCPCHChartProps } from './RCPCHChart.types';
@@ -27,7 +27,7 @@ import { montserratItalic } from '../fonts/montserrat-italic-b64';
2727
// const VERSION_LOG = '[VI]Version: {version} - built on {date}[/VI]';
2828
const VERSION = '[VI]v{version}[/VI]'; // uses version injector plugin to Rollup to report package.json version
2929

30-
const GlobalStyle = createGlobalStyle`
30+
const GlobalStyle = styled.div`
3131
@font-face {
3232
font-family: 'Montserrat';
3333
src: url(${montserratRegular}) format('truetype'),
@@ -70,7 +70,9 @@ const RCPCHChart: React.FC<RCPCHChartProps> = ({
7070
exportChartCallback,
7171
clinicianFocus,
7272
theme,
73-
customThemeStyles
73+
customThemeStyles,
74+
height,
75+
width
7476
}) => {
7577

7678
clinicianFocus = defineNonStylePropDefaults('clinicianFocus', clinicianFocus);
@@ -96,8 +98,20 @@ const RCPCHChart: React.FC<RCPCHChartProps> = ({
9698
// spread styles into individual objects
9799
const { chartStyle, axisStyle, gridlineStyle, centileStyle, sdsStyle, measurementStyle } = all_styles
98100

101+
// use height and width if provided to set text size also - text in SVG does not scale with the chart so we need to adjust it
102+
const referenceWidth = 1000;
103+
const referenceHeight = 800;
104+
const referenceGeometricMean = Math.sqrt(referenceWidth * referenceHeight);
105+
let textScaleFactor = 1;
106+
if (height != undefined && width != undefined){
107+
// Calculate the geometric mean of width and height
108+
const geometricMean = Math.sqrt(width * height);
109+
// Use the geometric mean to create a scaling factor
110+
textScaleFactor = geometricMean / referenceGeometricMean;
111+
}
112+
99113
// make granular styles to pass into charts
100-
const styles = makeAllStyles(chartStyle, axisStyle, gridlineStyle, centileStyle, sdsStyle, measurementStyle);
114+
const styles = makeAllStyles(chartStyle, axisStyle, gridlineStyle, centileStyle, sdsStyle, measurementStyle, textScaleFactor);
101115

102116

103117
// uncomment in development
@@ -118,7 +132,7 @@ const RCPCHChart: React.FC<RCPCHChartProps> = ({
118132

119133
return (
120134
<ErrorBoundary styles={styles}>
121-
<GlobalStyle />
135+
<GlobalStyle>
122136
<CentileChart
123137
chartsVersion={VERSION}
124138
reference={reference}
@@ -130,10 +144,14 @@ const RCPCHChart: React.FC<RCPCHChartProps> = ({
130144
sex={sex}
131145
enableZoom={enableZoom}
132146
styles={styles}
147+
height={height ?? 800}
148+
width={width ?? 1000}
149+
textScaleFactor={textScaleFactor}
133150
enableExport={enableExport}
134151
exportChartCallback={exportChartCallback}
135152
clinicianFocus={clinicianFocus}
136153
/>
154+
</GlobalStyle>
137155
</ErrorBoundary>
138156
);
139157
} else {
@@ -150,7 +168,7 @@ const RCPCHChart: React.FC<RCPCHChartProps> = ({
150168

151169
return (
152170
<ErrorBoundary styles={styles}>
153-
<GlobalStyle />
171+
<GlobalStyle>
154172
<SDSChart
155173
chartsVersion={VERSION}
156174
reference={reference}
@@ -162,10 +180,14 @@ const RCPCHChart: React.FC<RCPCHChartProps> = ({
162180
sex={sex}
163181
enableZoom={enableZoom}
164182
styles={styles}
183+
height={height ?? 800}
184+
width={width ?? 1000}
185+
textScaleFactor={textScaleFactor}
165186
enableExport={enableExport}
166187
exportChartCallback={exportChartCallback}
167188
clinicianFocus={clinicianFocus}
168189
/>
190+
</GlobalStyle>
169191
</ErrorBoundary>
170192
);
171193
}

src/RCPCHChart/RCPCHChart.types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export interface RCPCHChartProps {
1616
exportChartCallback(svg?: any): any;
1717
clinicianFocus?: boolean | undefined | null;
1818
theme?: 'monochrome' | 'traditional' | 'tanner1' | 'tanner2' | 'tanner3' | 'custom';
19+
height?: number
20+
width?: number
1921
customThemeStyles?: {
2022
chartStyle?: ChartStyle
2123
axisStyle?: AxisStyle

src/SDSChart/SDSChart.tsx

+11-3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ const SDSChart: React.FC<SDSChartProps> = (
6868
sex,
6969
enableZoom,
7070
styles,
71+
height,
72+
width,
73+
textScaleFactor,
7174
enableExport,
7275
exportChartCallback
7376
}
@@ -257,8 +260,8 @@ const SDSChart: React.FC<SDSChartProps> = (
257260
{/* It has an animation object and the domains are the thresholds of ages rendered. This is calculated from the child data supplied by the user. */}
258261
{/* Tooltips are here as it is the parent component. More information of tooltips in centiles below. */}
259262
<VictoryChart
260-
width={1000}
261-
height={800}
263+
width={width}
264+
height={height}
262265
style={styles.chartMisc}
263266
containerComponent={
264267
<VictoryVoronoiContainer
@@ -485,7 +488,12 @@ const SDSChart: React.FC<SDSChartProps> = (
485488
fill: "#FFFFFF"
486489
},
487490
title: {
488-
fontSize: 12,
491+
fontSize: 12*textScaleFactor,
492+
fontFamily: 'Montserrat',
493+
fontStyle: 'italic'
494+
},
495+
labels: {
496+
fontSize: 10*textScaleFactor,
489497
fontFamily: 'Montserrat',
490498
fontStyle: 'italic'
491499
}

src/SDSChart/SDSChart.types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ClientMeasurementObject } from '../interfaces/ClientMeasurementObject';
2-
import { ClientStyle } from '../interfaces/ClientStyleObjects';
32
import { MidParentalHeightObject } from '../interfaces/MidParentalHeightObject';
43
export interface SDSChartProps {
54
chartsVersion: string;
@@ -12,6 +11,9 @@ export interface SDSChartProps {
1211
sex: 'male' | 'female';
1312
enableZoom: boolean;
1413
styles: { [key: string]: any };
14+
height?: number;
15+
width?: number;
16+
textScaleFactor?: number;
1517
enableExport: boolean;
1618
exportChartCallback(svg: any): any;
1719
clinicianFocus?: boolean | undefined | null;

src/functions/buildListOfMeasurementMethods.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { VictoryLegendProps } from "victory";
21
import { ClientMeasurementObject } from "../interfaces/ClientMeasurementObject";
3-
import { ClientStyle } from "../interfaces/ClientStyleObjects";
42
import { nameForMeasurementMethod } from "./nameForMeasurementMethod";
53
import { symbolForMeasurementType } from "./symbolForMeasurementType";
64
import { VictoryLegendDatum } from "../interfaces/VictoryLegendData";

src/functions/makeAllStyles.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function makeAllStyles(
3333
centileStyle?: CentileStyle,
3434
sdsStyle?: SDSStyle,
3535
measurementStyle?: MeasurementStyle,
36+
textMultiplier?: number // this is used to scale text size based on the aspect ratio of the chart using the height and width. Default is 1
3637
) {
3738

3839
let newGridlineStyle = {
@@ -57,14 +58,14 @@ function makeAllStyles(
5758
chartMisc: {
5859
background: {
5960
fill: chartStyle?.backgroundColour ?? white,
60-
},
61+
}
6162
},
6263
toolTipFlyout: {
6364
stroke: chartStyle?.tooltipStroke ?? midGrey, // tooltip border colour
6465
fill: chartStyle?.tooltipBackgroundColour ?? midGrey, // tooltip backgroundcolour
6566
},
6667
toolTipMain: {
67-
fontSize: chartStyle?.tooltipTextStyle?.size ?? 14,
68+
fontSize: (chartStyle?.tooltipTextStyle?.size ?? 14) * (textMultiplier ?? 1),
6869
fill: chartStyle?.tooltipTextStyle?.colour ?? black,
6970
fontFamily: chartStyle?.tooltipTextStyle?.name ?? 'Montserrat',
7071
fontStyle: chartStyle?.tooltipTextStyle?.style ?? 'normal',
@@ -89,7 +90,7 @@ function makeAllStyles(
8990
strokeWidth: 1.0,
9091
},
9192
axisLabel: {
92-
fontSize: axisStyle?.axisLabelTextStyle?.size ?? 10,
93+
fontSize: (axisStyle?.axisLabelTextStyle?.size ?? 10) * (textMultiplier ?? 1),
9394
padding: 20,
9495
fill: axisStyle?.axisLabelTextStyle?.colour ?? black,
9596
fontFamily: axisStyle?.axisLabelTextStyle?.name ?? 'Arial',
@@ -99,7 +100,7 @@ function makeAllStyles(
99100
stroke: axisStyle?.tickLabelTextStyle?.colour ?? black,
100101
},
101102
tickLabels: {
102-
fontSize: axisStyle?.tickLabelTextStyle?.size ?? 8,
103+
fontSize: (axisStyle?.tickLabelTextStyle?.size ?? 8) * (textMultiplier ?? 1),
103104
padding: 5,
104105
fill: axisStyle?.tickLabelTextStyle?.colour ?? black,
105106
color: axisStyle?.tickLabelTextStyle?.colour ?? black,
@@ -112,7 +113,7 @@ function makeAllStyles(
112113
},
113114
xTicklabel: {
114115
fill: axisStyle?.tickLabelTextStyle?.colour ?? black,
115-
fontSize: axisStyle?.tickLabelTextStyle?.size ?? 8,
116+
fontSize: (axisStyle?.tickLabelTextStyle?.size ?? 8) * (textMultiplier ?? 1),
116117
fontFamily: axisStyle?.tickLabelTextStyle?.name ?? 'Arial',
117118
fontStyle: axisStyle?.axisLabelTextStyle?.style ?? 'normal',
118119
},
@@ -122,7 +123,7 @@ function makeAllStyles(
122123
strokeWidth: 1.0,
123124
},
124125
axisLabel: {
125-
fontSize: axisStyle?.axisLabelTextStyle?.size ?? 10,
126+
fontSize: (axisStyle?.axisLabelTextStyle?.size ?? 10) * (textMultiplier ?? 1),
126127
padding: 25,
127128
fill: axisStyle?.axisLabelTextStyle?.colour ?? black,
128129
fontFamily: axisStyle?.axisLabelTextStyle?.name ?? 'Arial',
@@ -132,7 +133,7 @@ function makeAllStyles(
132133
stroke: axisStyle?.tickLabelTextStyle?.colour ?? black,
133134
},
134135
tickLabels: {
135-
fontSize: axisStyle?.tickLabelTextStyle?.size ?? 8,
136+
fontSize: (axisStyle?.tickLabelTextStyle?.size ?? 8) * (textMultiplier ?? 1),
136137
padding: 5,
137138
fill: axisStyle?.tickLabelTextStyle?.colour ?? black,
138139
fontFamily: axisStyle?.axisLabelTextStyle?.name ?? 'Arial',
@@ -156,7 +157,7 @@ function makeAllStyles(
156157
},
157158
},
158159
delayedPubertyThresholdLabel: {
159-
fontSize: 9,
160+
fontSize: (9) * (textMultiplier ?? 1),
160161
fill: axisStyle?.axisLabelTextStyle?.colour ?? black,
161162
fontFamily: axisStyle?.axisLabelTextStyle?.name ?? 'Arial',
162163
textAlign: 'start',
@@ -185,7 +186,7 @@ function makeAllStyles(
185186
},
186187
},
187188
centileLabel: {
188-
fontSize: 6,
189+
fontSize: (6) * (textMultiplier ?? 1),
189190
fontFamily: 'Montserrat',
190191
fill: centileStyle?.centileStroke ?? black
191192
},
@@ -257,7 +258,7 @@ function makeAllStyles(
257258
}
258259
},
259260
eventTextStyle: {
260-
size: measurementStyle?.eventTextStyle?.size ?? 14,
261+
size: (measurementStyle?.eventTextStyle?.size ?? 14) * (textMultiplier ?? 1),
261262
name: measurementStyle?.eventTextStyle?.name ?? 'Montserrat',
262263
colour: measurementStyle?.eventTextStyle?.colour ?? black,
263264
style: measurementStyle?.eventTextStyle?.style ?? 'normal'

0 commit comments

Comments
 (0)