$(() => {
const rangeOptions = {
dataSource,
chart: {
commonSeriesSettings: {
argumentField: 'country',
type: 'bar',
},
series: [
{ valueField: 'copper', name: 'Copper' },
],
},
title: 'Copper Production in 2013',
onValueChanged(e) {
const data = e.component.option('dataSource');
let total = 0;
let startIndex;
let endIndex;
$.each(data, (i, item) => {
if (item.country === e.value[0]) {
startIndex = i;
} else if (item.country === e.value[1]) {
endIndex = i;
}
});
if (endIndex) {
data
.slice(startIndex, endIndex + 1)
.forEach((item) => {
total += item.copper;
});
} else {
total = data[startIndex].copper;
}
$('#total').text(formatNumber(total));
},
};
const formatNumber = new Intl.NumberFormat('en-US', { minimumFractionDigits: 0 }).format;
$('#range-selector').dxRangeSelector(rangeOptions);
$('#total').text('12,809,000');
});
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<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=1.0" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>window.jQuery || document.write(decodeURIComponent('%3Cscript src="js/jquery.min.js"%3E%3C/script%3E'))</script>
<link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/22.2.6/css/dx.light.css" />
<script src="https://cdn3.devexpress.com/jslib/22.2.6/js/dx.all.js"></script>
<script src="data.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css" />
<script src="index.js"></script>
</head>
<body class="dx-viewport">
<div class="demo-container">
<div id="range-selector-demo">
<div id="range-selector"></div>
<h2>Total: <span id="total"></span> tons</h2>
</div>
</div>
</body>
</html>
#range-selector-demo {
text-align: center;
}
#range-selector {
margin-bottom: 20px;
height: 200px;
}
const dataSource = [
{ country: 'Chile', copper: 5700000 },
{ country: 'United States', copper: 1220000 },
{ country: 'Peru', copper: 1300000 },
{ country: 'China', copper: 1650000 },
{ country: 'Australia', copper: 990000 },
{ country: 'Russia', copper: 930000 },
{ country: 'DR Congo', copper: 900000 },
{ country: 'Finland', copper: 119000 },
];