Backend API
import React, { useCallback } from 'react';
import SelectBox, { type SelectBoxTypes } from 'devextreme-react/select-box';
import { DataSource, CustomStore } from 'devextreme-react/common/data';
import RowTemplate from './RowTemplate.tsx';
const months = [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
const years = ['2021', '2022', '2023'];
const monthLabel = { 'aria-label': 'Month' };
const dataSource = new DataSource({
store: new CustomStore({
load: () => fetch('../../../../data/resourceData.json')
.then((e) => e.json())
.catch(() => { throw new Error('Data Loading Error'); }),
loadMode: 'raw',
}),
filter: ['month', '<=', '12'],
paginate: false,
});
function App() {
const onValueChanged = useCallback((e: SelectBoxTypes.ValueChangedEvent) => {
dataSource.filter(['month', '<=', e.value]);
dataSource.load();
}, []);
return (
<>
<div className="long-title"><h3>Monthly Prices of Copper, Nickel and Palladium</h3></div>
<div id="chart-demo">
<table
className="demo-table"
style={{ border: 1 }}
>
<tbody>
<tr>
<th><div className="dx-screen-reader-only">Year</div></th>
<th>Copper (USD/ton)</th>
<th>Nickel (USD/ton)</th>
<th>Palladium (USD/troy ounce)</th>
</tr>
{
years.map((year, index) => <RowTemplate
key={index}
year={year}
source={dataSource} />)
}
</tbody>
</table>
<div className="action">
<div className="label">Choose a number of months:</div>
<SelectBox
id="choose-months"
dataSource={months}
inputAttr={monthLabel}
defaultValue={months[0]}
onValueChanged={onValueChanged}
/>
</div>
</div>
</>
);
}
export default App;
import React, { useCallback } from 'react';
import SelectBox from 'devextreme-react/select-box';
import { DataSource, CustomStore } from 'devextreme-react/common/data';
import RowTemplate from './RowTemplate.js';
const months = [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
const years = ['2021', '2022', '2023'];
const monthLabel = { 'aria-label': 'Month' };
const dataSource = new DataSource({
store: new CustomStore({
load: () =>
fetch('../../../../data/resourceData.json')
.then((e) => e.json())
.catch(() => {
throw new Error('Data Loading Error');
}),
loadMode: 'raw',
}),
filter: ['month', '<=', '12'],
paginate: false,
});
function App() {
const onValueChanged = useCallback((e) => {
dataSource.filter(['month', '<=', e.value]);
dataSource.load();
}, []);
return (
<>
<div className="long-title">
<h3>Monthly Prices of Copper, Nickel and Palladium</h3>
</div>
<div id="chart-demo">
<table
className="demo-table"
style={{ border: 1 }}
>
<tbody>
<tr>
<th>
<div className="dx-screen-reader-only">Year</div>
</th>
<th>Copper (USD/ton)</th>
<th>Nickel (USD/ton)</th>
<th>Palladium (USD/troy ounce)</th>
</tr>
{years.map((year, index) => (
<RowTemplate
key={index}
year={year}
source={dataSource}
/>
))}
</tbody>
</table>
<div className="action">
<div className="label">Choose a number of months:</div>
<SelectBox
id="choose-months"
dataSource={months}
inputAttr={monthLabel}
defaultValue={months[0]}
onValueChanged={onValueChanged}
/>
</div>
</div>
</>
);
}
export default App;
import React from 'react';
import Sparkline, {
Tooltip,
} from 'devextreme-react/sparkline';
import { DataSource } from 'devextreme-react/common/data';
interface RowTemplateProps {
key: number;
year: string;
source: DataSource;
}
export default function RowTemplate(props: RowTemplateProps) {
return (
<tr>
<th>{props.year}</th>
<td>
<Sparkline
dataSource={props.source}
showMinMax={true}
className="sparkline"
argumentField="month"
valueField={`copper${props.year}`}
type="bar"
>
<Tooltip format={{ type: 'currency', precision: 2 }} />
</Sparkline>
</td>
<td>
<Sparkline
dataSource={props.source}
showMinMax={true}
showFirstLast={false}
className="sparkline"
argumentField="month"
valueField={`nickel${props.year}`}
type="bar"
barPositiveColor="#6babac"
minColor="#9ab57e"
maxColor="#8076bb"
>
<Tooltip format={{ type: 'currency', precision: 2 }} />
</Sparkline>
</td>
<td>
<Sparkline
dataSource={props.source}
className="sparkline"
argumentField="month"
valueField={`palladium${props.year}`}
type="bar"
barPositiveColor="#e55253"
firstLastColor="#ebdd8f"
pointColor="#e8c267"
>
<Tooltip format={{ type: 'currency', precision: 2 }} />
</Sparkline>
</td>
</tr>
);
}
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.tsx';
ReactDOM.render(
<App />,
document.getElementById('app'),
);
import React from 'react';
import Sparkline, { Tooltip } from 'devextreme-react/sparkline';
export default function RowTemplate(props) {
return (
<tr>
<th>{props.year}</th>
<td>
<Sparkline
dataSource={props.source}
showMinMax={true}
className="sparkline"
argumentField="month"
valueField={`copper${props.year}`}
type="bar"
>
<Tooltip format={{ type: 'currency', precision: 2 }} />
</Sparkline>
</td>
<td>
<Sparkline
dataSource={props.source}
showMinMax={true}
showFirstLast={false}
className="sparkline"
argumentField="month"
valueField={`nickel${props.year}`}
type="bar"
barPositiveColor="#6babac"
minColor="#9ab57e"
maxColor="#8076bb"
>
<Tooltip format={{ type: 'currency', precision: 2 }} />
</Sparkline>
</td>
<td>
<Sparkline
dataSource={props.source}
className="sparkline"
argumentField="month"
valueField={`palladium${props.year}`}
type="bar"
barPositiveColor="#e55253"
firstLastColor="#ebdd8f"
pointColor="#e8c267"
>
<Tooltip format={{ type: 'currency', precision: 2 }} />
</Sparkline>
</td>
</tr>
);
}
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.js';
ReactDOM.render(<App />, document.getElementById('app'));
[
{ "month": 1, "copper2021": 7970.50, "copper2022": 9775.93, "copper2023": 8999.79, "nickel2021": 17847.68, "nickel2022": 22355.40, "nickel2023": 28194.61, "palladium2021": 2391.20, "palladium2022": 2030.86, "palladium2023": 1734.27 },
{ "month": 2, "copper2021": 8460.25, "copper2022": 9941.35, "copper2023": 8955.20, "nickel2021": 18595.30, "nickel2022": 24015.55, "nickel2023": 26727.95, "palladium2021": 2342.33, "palladium2022": 2350.10, "palladium2023": 1543.90 },
{ "month": 3, "copper2021": 9004.98, "copper2022": 10237.59, "copper2023": 8835.72, "nickel2021": 16460.83, "nickel2022": 33924.18, "nickel2023": 23288.61, "palladium2021": 2525.61, "palladium2022": 2582.78, "palladium2023": 1426.00 },
{ "month": 4, "copper2021": 9335.55, "copper2022": 10183.13, "copper2023": 8814.00, "nickel2021": 16480.91, "nickel2022": 33132.74, "nickel2023": 23894.56, "palladium2021": 2776.45, "palladium2022": 2330.67, "palladium2023": 1508.00 },
{ "month": 5, "copper2021": 10183.97, "copper2022": 9362.81, "copper2023": 8234.28, "nickel2021": 17552.45, "nickel2022": 28062.55, "nickel2023": 21970.39, "palladium2021": 2900.52, "palladium2022": 2060.05, "palladium2023": 1482.61 },
{ "month": 6, "copper2021": 9612.43, "copper2022": 9033.13, "copper2023": 8386.23, "nickel2021": 17942.32, "nickel2022": 25658.63, "nickel2023": 21233.28, "palladium2021": 2752.48, "palladium2022": 1913.05, "palladium2023": 1348.09 },
{ "month": 7, "copper2021": 9433.59, "copper2022": 7529.79, "copper2023": 8445.26, "nickel2021": 18819.36, "nickel2022": 21481.89, "nickel2023": 21091.26, "palladium2021": 2754.76, "palladium2022": 1973.10, "palladium2023": 1264.48 },
{ "month": 8, "copper2021": 9357.19, "copper2022": 7960.98, "copper2023": 8351.77, "nickel2021": 19160.23, "nickel2022": 22057.39, "nickel2023": 20438.65, "palladium2021": 2544.73, "palladium2022": 2135.52, "palladium2023": 1247.30 },
{ "month": 9, "copper2021": 9324.07, "copper2022": 7734.70, "copper2023": 8270.86, "nickel2021": 19398.23, "nickel2022": 22773.97, "nickel2023": 19644.64, "palladium2021": 2130.32, "palladium2022": 2113.95, "palladium2023": 1239.29 },
{ "month": 10, "copper2021": 9778.50, "copper2022": 7621.21, "copper2023": 7939.66, "nickel2021": 19420.52, "nickel2022": 22032.89, "nickel2023": 18281.23, "palladium2021": 2058.48, "palladium2022": 2078.86, "palladium2023": 1142.09 },
{ "month": 11, "copper2021": 9765.48, "copper2022": 8029.95, "copper2023": 8173.95, "nickel2021": 19970.43, "nickel2022": 25562.70, "nickel2023": 17027.36, "palladium2021": 2036.32, "palladium2022": 1911.55, "palladium2023": 1050.27 },
{ "month": 12, "copper2021": 9550.31, "copper2022": 8367.23, "copper2023": 8394.11, "nickel2021": 20015.55, "nickel2022": 28946.81, "nickel2023": 16460.84, "palladium2021": 1810.52, "palladium2022": 1808.36, "palladium2023": 1087.76 }
]
<!DOCTYPE html>
<html lang="en">
<head>
<title>DevExtreme Demo</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0" />
<link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/26.1.5/css/dx.light.css" />
<link rel="stylesheet" type="text/css" href="./bundle.css" />
</head>
<body class="dx-viewport">
<div class="demo-container">
<div id="app"></div>
</div>
<script src="bundles/vendor/react.vendor.js"></script>
<script src="./bundle.js"></script>
</body>
</html>
#chart-demo {
height: 440px;
}
.demo-table {
width: 100%;
border: 1px solid #c2c2c2;
border-collapse: collapse;
}
.demo-table th,
.demo-table td {
font-weight: 400;
width: 200px;
padding: 25px 10px 5px;
border: 1px solid #c2c2c2;
}
.demo-table th {
padding: 25px 15px 20px;
border: 1px solid #c2c2c2;
}
.demo-table tr:nth-child(2) td {
border-top: 1px solid #c2c2c2;
}
.demo-table td:first-of-type {
border-left: 1px solid #c2c2c2;
}
.demo-table .sparkline {
width: 200px;
height: 30px;
}
.action {
width: 300px;
margin-top: 20px;
display: flex;
align-items: center;
justify-content: space-between;
}
.action .dx-selectbox {
width: 90px;
}
.long-title h3 {
font-weight: 200;
font-size: 28px;
text-align: center;
margin-bottom: 20px;
margin-top: 0;
}