RangeSelector - Chart as Background

React RangeSelector: Chart on Background

Chart as background in React RangeSelector.

A live React example of jqxRangeSelector, the jQWidgets RangeSelector component, with its complete source code below. It is one of 12 jqxRangeSelector demos for React.

jqxRangeSelector API used in this demo

Source code

To run it in your own app, install the package with npm install jqwidgets-scripts, add these files to a Vite React and TypeScript project, and import jqx.base.css and a theme in main.tsx. Getting started with jqxRangeSelector shows the setup step by step.

App.tsx

import type { JSX } from 'react';
import { useRef, useState } from 'react';
import JqxChart, { type IChartProps, jqx } from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxchart';
import JqxRangeSelector, { type IRangeSelectorProps } from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxrangeselector';

interface WeatherRecord {
    month: string;
    min: number;
    max: number;
}

function App(): JSX.Element {
    const recordsArray = useRef<WeatherRecord[]>([]);
    const [source] = useState(() => {
        const dataSource = {
            datafields: [
                { name: 'month' },
                { name: 'min' },
                { name: 'max' }
            ],
            datatype: 'json',
            url: 'weather_geneva.txt'
        };
        return new jqx.dataAdapter(dataSource, {
            async: false,
            autoBind: true,
            loadComplete: (
                _data: unknown,
                _status: unknown,
                _xhr: XMLHttpRequest,
                records: WeatherRecord[]
            ): void => {
                recordsArray.current = records;
            },
            loadError: (_xhr: XMLHttpRequest, _status: string, error: string) => {
                window.alert('Error loading "' + dataSource.url + '" : ' + error);
            }
        });
    });

    const labelsFormatFunction: IRangeSelectorProps['labelsFormatFunction'] = (data: number): string => {
        if (typeof data === 'number') {
            if (recordsArray.current[data]) {
                return "1 " + recordsArray.current[data].month;
            } else if (recordsArray.current[data - 1]) {
                return "31 " + recordsArray.current[data - 1].month;
            }
        }
        return "";
    };

    const markersFormatFunction: IRangeSelectorProps['markersFormatFunction'] = (data: number): string => {
        if (typeof data === 'number') {
            if (recordsArray.current[data]) {
                return "1 " + recordsArray.current[data].month;
            } else if (recordsArray.current[data - 1]) {
                return "31 " + recordsArray.current[data - 1].month;
            }
        }
        return "";
    };

    const padding: IChartProps['padding'] = { left: 5, top: 0, right: 0, bottom: 0 };
    const titlePadding: IChartProps['titlePadding'] = { left: 0, top: 0, right: 0, bottom: 0 };
    const seriesGroups: IChartProps['seriesGroups'] = [
        {
            series: [
                { dataField: 'max', lineColor: 'green', displayText: 'Max Temperature' },
                { dataField: 'min', lineColor: 'red', displayText: 'Min Temperature' }
            ],
            showLabels: false,
            type: 'line',
            valueAxis: {
                visible: false
            }
        }
    ];
    const xAxis: IChartProps['xAxis'] = {
        visible: false
    };

    return (
        <div>
            <label style={{ marginLeft: '250px', fontSize: '18px', fontWeight: 'bold' }}>Weather in Geneva, Switzerland</label>
            <br />
            <label style={{ marginLeft: '250px', fontSize: '14px', fontWeight: 'bold' }}>Climatological Information about Geneva</label>
            <JqxRangeSelector
                theme="tailwind-blue-dark"
                width={750}
                height={200}
                min={0}
                max={12}
                range={{ from: 3, to: 9 }}
                majorTicksInterval={1}
                minorTicksInterval={1}
                labelsOnTicks={false}
                labelsFormatFunction={labelsFormatFunction}
                markersFormatFunction={markersFormatFunction}
            >
                <JqxChart
                    theme="tailwind-blue-dark"
                    style={{ width: '750px', height: '200px' }}
                    title=""
                    description=""
                    showBorderLine={false}
                    showLegend={false}
                    enableAnimations={true}
                    padding={padding}
                    titlePadding={titlePadding}
                    source={source}
                    xAxis={xAxis}
                    colorScheme="scheme05"
                    seriesGroups={seriesGroups}
                />
            </JqxRangeSelector>
        </div>
    );
}

export default App;

Learn more

More React RangeSelector demos