Skip to content

React Chart Component

Github Issues Github Stars Github Forks Github Licence NPM Publish

GitHub repository

Online Demo

height-chart-girl-component

Use our Growth Chart React Component

The dGC React Component is the recommended way to display Digital Growth Charts to end users. We have built the component to be easy to integrate into existing web-based views, even if your stack does not currently use React. You can use the component as-is in a React app, or include it in plain HTML or any other JavaScript framework.

Displaying growth charts is a complex task, and we have built the component to make it as easy as possible for developers to display charts correctly. The component is designed to be customisable, so you can change the look and feel to match your app.

Background

React.js

React is a popular UI library for Javascript. It has endured well and remains a popular choice for developers. Importantly, unlike some other Javascript frameworks which are primarily designed for Single Page Applications, React doesn't expect to have the entire webpage to itself. It can be used as a small component in any other web page, even if the main framework being used is completely different.

Tell us what you think

Let us know what you think of our design decisions, on this or any other area of the dGC Project, by chatting to us on our dGC Forum , or our RCPCH Community Signal chat channel

What about other frameworks/UI libraries?

If you need us to develop a charting component in a different language or framework, we may be able to do this with you or your company. We would need to discuss the requirements and quote for this service. You should be aware that all such RCPCH-developed artefacts will also be open source. We ensure the licensing of open source components is compatible with commercial use.

Contact us

To contact us for this service, email commercial@rcpch.ac.uk.

Getting started

git clone the repo

git clone https://github.com.rcpch/rcpch/digital-growth-charts-react-component-library

Install dependencies

npm install

Run Storybook to view the component in isolation

npm run storybook

Running the Charts component locally

To run the Chart component locally alongside the React client, there are some extra steps. Since the Chart component (aka the chart itself) and the React client (aka the interactive form) both use React, the Charts will throw an error if you import them in the package.json of your app from a folder on your local machine.

We would advise having both the Component and Client open when working through this part of the documentation. If you are using Visual Studio code, you can do this by opening a 'Workspace'.

First, in the Client directory, ensure you have a version of the chart component in your node_modules. Look for node_modules/@rcpch/digital-growth-charts-react-component-library

Begin by opening a terminal in the client directory, and in the component directory. In both, execute the following command:

npm ls -g --depth=0 --link=true

The result printed out will show you any symlinks between the 2 directories, and if you have never before ran npm link, then this will likely be empty.

In both the client and component terminals, execute the following command:

nvm use node

This will use nvm (node version manager) to move to the latest version of node.

Next, in your client terminal, execute the following command:

npm link @rcpch/digital-growth-charts-react-component-library@7.0.0

Warning

You may receive a permission denied error. If this is the case, execute the above command again but place sudo at the start of it.

Then, execute the following command in the component terminal:

npm run build

And once you receive a message that the build has completed, go to the client terminal and enter:

s/deprecated/start-react-client

Warning

You may receive a hooks error and a blank screen. This would be due to a clash between react versions between the client and component. You should navigate to the Component node_modules folder, and delete the react and react-dom folders.

A url will now present itself in the terminal, and you should follow that, which will take you to the client and component working together side by side.

Making changes to the code, and visualising them

If you make changes to the component or the client, you should rerun the final 2 commands above, in the same order.

Structure

This library has been written in Typescript. The main component is RCPCHChart, which takes the following props. Note that each component will only render a single chart type, so if you wanted to render a weight and a height chart, these must be done as two separate instances of the component. We find that on modern screens you can render two charts side-by-side, but on smaller screens, you may wish to render one chart at a time, perhaps in tabs for height, weight, BMI, head circumference etc, as in our demo client.

RCPCHChart component

RCPCHChart component props
    {
    title: string,
    subtitle: string,
    measurementMethod: 'height' | 'weight' | 'ofc' | 'bmi',
    sex: 'male' | 'female',
    measurementsArray: [Measurement],
    reference: 'uk-who' | 'turner' | 'trisomy-21',
    width: number,
    height: number,
    chartStyle: ChartStyle,
    axisStyle: AxisStyle,
    gridlineStyle: GridlineStyle,
    centileStyle: CentileStyle,
    sdsStyle?: SDSStyle;
    measurementStyle: MeasurementStyle
    midParentalHeightData?: MidParentalHeightObject,
    enableZoom?: boolean,
    chartType?: 'centile' | 'sds',
    enableExport: boolean,
    exportChartCallback: function(svg: any),
    clinicianFocus?: boolean;
    }

Measurement interface

The Measurement interface is structured to reflect the JSON Measurement object which is returned by the API. The RCPCHChart component uses the reference prop to determine which chart to render. So far, 3 references are supported: UK-WHO (uk-who), Turner Syndrome (turner) and Down Syndrome (trisomy-21). The reference data for the centiles are included in the library in plottable format in the chartdata folder.

Tip

You simply need to pass JSON from the dGC API directly in to the component as an array of Measurement JSON objects. The component 'knows' how to render this correctly. You don't need to parse, restructure, or even understand the JSON returned from the API: just pass it directly to the component inside an array containing one or more Measurement objects.

Styling

The styling components allow the user to customise elements of the chart. Chart styles control the chart and the tooltips.

Styling options available through ChartStyle

    interface ChartStyle{
        backgroundColour?: string,
        width?: number,
        height?: number,
        padding?: requires {left?: number, right?: number, top?: number, bottom?: number},
        titleStyle?: requires {name?: string, colour?: string, size?: number, weight?: 'bold' | 'italic' | 'regular'}
        subTitleStyle?: requires {name?: string, colour?: string, size?: number, weight?: 'bold' | 'italic' | 'regular'},,
        tooltipBackgroundColour?: string,
        tooltipStroke?: string,
        tooltipTextStyle?: requires {name?: string, colour?: string, size?: number, weight?: 'bold' | 'italic' | 'regular'}
        termFill?: string,
        termStroke?: string,
        infoBoxFill?: string,
        infoBoxStroke?: string
        infoBoxTextStyle?: requires {name?: string, colour?: string, size?: number, weight?: 'bold' | 'italic' | 'regular'}
        toggleButtonInactiveColour: string // relates to the toggle buttons present if age correction is necessary
        toggleButtonActiveColour: string
        toggleButtonTextColour: string
    }

Note for the tooltips and infobox text sizes, these are strokeWidths, not point sizes as the text here is SVG.

Axis Styles

Axis styles control axes and axis labels
    interface AxisStyle{
        axisStroke?: string,
        axisLabelTextStyle?: requires {name?: string, colour?: string, size?: number, weight?: 'bold' | 'italic' | 'regular'}
        tickLabelTextStyle?: requires {name?: string, colour?: string, size?: number, weight?: 'bold' | 'italic' | 'regular'}
    }

Gridline Styles

Gridline styles allow/hide gridlines and control line width, presence of dashes, colour
    interface GridlineStyle{
        gridlines?: boolean,
        stroke?: string,
        strokeWidth?: number,
        dashed?: boolean
    }

Centile Styles

Centile styles control the width and colour of the centile and SDS lines
    interface CentileStyle{
        sdsStroke?: string,
        sdsStrokeWidth?: string,
        centileStroke?: string,
        centileStrokeWidth?: number,
        delayedPubertyAreaFill?: string,
        midParentalCentileStroke?: number;
        midParentalCentileStrokeWidth?: number;
        midParentalAreaFill?: string;
    }

SDS Styles

SDS styles control the colour and width of the SDS lines. As all measurement methods are rendered on a single chart, the user is offered the option of different colours for each measurement method (height, weight, head circumference(OFC) and body mass index (BMI)). If no SDS style is supplied, the centile line colour is used with an opacity applied to each measurement.

SDS Styles
    interface SDSStyle {
        lineStrokeWidth?: number;
        heightStroke?: string;
        weightStroke?: string;
        ofcStroke?: string;
        bmiStroke?: string;
    }

Measurement Styles

Measurement styles control the plotted data points: colour, size and shape. Corrected ages are always rendered as crosses. Circles for chronological ages are preferred. On the SDS charts, measurement points are grey by default, with the measurement method in focus highlighted by rendering as a line. Points which are not highlighted can be emphasised on mouse hover, with the highlighted colour being set by the highlightedMeasurementFill prop.

Measurement Styles
    interface MeasurementStyle{
        measurementFill?: string,
        highLightedMeasurementFill?: string;
    }

Mid-Parental Height

midParentalHeightData: This is the return value from the RCPCH API and takes the structure:

midParentalHeightData
    export interface MidParentalHeightObject {
        mid_parental_height?: number;
        mid_parental_height_sds?: number;
        mid_parental_height_centile?: number;
        mid_parental_height_centile_data?: Reference[]
        mid_parental_height_upper_centile_data?: Reference[]
        mid_parental_height_lower_centile_data?: Reference[]
        mid_parental_height_lower_value?: number
        mid_parental_height_upper_value?: number
    }

This returns a mid-parental height, mid-parental SDS and centile, along with the centile data if the user wishes to plot a mid-parental centile. The structure of the Reference and Centile interfaces is:

Reference and Centile interface structures
export interface Reference {
[name: string]: ISexChoice
}

    export interface ICentile {
        centile: number,
        data: IPlottedCentileMeasurement[],
        sds: number
    }

    export interface IPlottedCentileMeasurement {
        "l": string | number,
        "x": number,
        "y": number
    }

    export interface ISexChoice {
        male: IMeasurementMethod,
        female: IMeasurementMethod
    }

    export interface IMeasurementMethod{
        height?: ICentile[],
        weight?: ICentile[],
        bmi?: ICentile[],
        ofc?: ICentile[],
    }

Centile data are returned from the RCPCH API in this same structure, though no API call is made from this component - all the centile data for all the references is included.

enableZoom

enableZoom: a boolean optional prop which defaults to false. If true, the user can press and mouse click to zoom in or out once measurements are being displayed. A reset zoom button also appears.

chartType

chartType: a string mandatory prop and must be one of 'centile' | 'sds'. It toggles between centile and SDS charts.

enableExport

enableExport: a boolean optional prop, defaults to false. If true, exportChartCallback must be implemented and a copy-paste button is rendered below the chart.

exportChartCallBack

exportChartCallback callback function implemented if enableExport is true. It receives an SVG element. This can be saved in the client to clipboard by converting to canvas using HTML5. An example implementation of this is here in our demo client.

clinicianFocus

clinicianFocus: a boolean optional prop which defaults to false. If true, the advice strings that are reported to users in tooltips are more technical and aimed at clinicians familiar with centile charts. If false, the advice strings will be less technical and more suitable for parents, guardians, carers or other laypersons.

Requests for additional functionality in props

In time, more props can be added if users request them. If you have requests, please post issues on our GitHub or get involved to contribute as below.

Troubleshooting

Circular import errors

Victory Charts are a dependency (see below), built on top of D3.js. On build, it is likely you will get an error relating to circular dependencies for some files in the d3-interpolate module. This issue is logged here.

Contributing

See Contributing for information on how to get involved in the project.

You can get in touch with the primary developers to talk about the project using any of the methods on our contact page.

Acknowledgements

This Typescript library was built from the starter created by Harvey Delaney

The charts are built using Victory Charts for React. We tried several chart packages for React, but we chose Victory because of their documentation and their ability to customise components.

Licensing

This chart component software is is subject to copyright and is owned by the RCPCH, but is released under the MIT license. License: MIT

There is important chart line rendering data bundled in the component, which subject to copyright and is owned by the RCPCH. It is specifically excluded from the MIT license mentioned above. If you wish to use this software, please contact the RCPCH so we can ensure you have the correct license for use. Subscribers to the Digital Growth Charts API will automatically be assigned licenses for the chart plotting data.