Name | Description |
---|---|
public constructor()
|
Create a new instance of the BarChart class.
Parameters:
|
public reload()
|
Redraw the chart.
Return:
|
Name | Description |
---|---|
public instance: ApexCharts|undefined
|
Read-only ApexCharts instance. |
<!--begin::Wrapper-->
<div class="d-flex flex-stack flex-wrap mb-5">
<!--begin::Search-->
<div class="d-flex align-items-center position-relative my-1 mb-2 mb-md-0">
<!--begin::Svg Icon | path: icons/duotune/general/gen021.svg-->
<span class="svg-icon svg-icon-1 position-absolute ms-4">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect opacity="0.5" x="17.0365" y="15.1223" width="8.15546" height="2" rx="1" transform="rotate(45 17.0365 15.1223)" fill="currentColor" />
<path d="M11 19C6.55556 19 3 15.4444 3 11C3 6.55556 6.55556 3 11 3C15.4444 3 19 6.55556 19 11C19 15.4444 15.4444 19 11 19ZM11 5C7.53333 5 5 7.53333 5 11C5 14.4667 7.53333 17 11 17C14.4667 17 17 14.4667 17 11C17 7.53333 14.4667 5 11 5Z" fill="currentColor" />
</svg>
</span>
<!--end::Svg Icon-->
<input id="horizontalBarChartKeyword" type="text" class="form-control form-control-solid w-300px ps-15" placeholder="Search by name" maxlength="20" />
</div>
<!--end::Search-->
</div>
<!--end::Wrapper-->
<!--begin::Chart-->
<div id="horizontalBarChart"></div>
<!--end::Chart-->
import {components} from 'metronic-extension';
const initHorizontalBarChart = () => {
return new components.BarChart(document.getElementById('horizontalBarChart'), {
horizontal: true,
ajax: {
url: 'json/barchart.json',
dataSrc: data => {
// Filter chart data by categories matching the entered keywords.
// Normally, use the ajax.data option to set the keywords as request parameters and perform the filtering on the server side.
const keyword = horizontalBarChartKeyword.value.replace(/^[\s ]+|[\s ]+$/g, '');
if (!keyword)
// If the keyword is empty, all data is displayed.
return data;
// Return only data with keywords in the name.
return data.filter(item => item.category.includes(keyword));
},
},
xAxisFormatter: value => {
// Comma-separate the values.
return Number(value).toLocaleString();
},
dataLabelFormatter: value => {
// Comma-separate the values and add units.
return `${Number(value).toLocaleString()} items`;
},
tooltipDataFormatter: value => {
// Comma-separate the values and add units.
return `${Number(value).toLocaleString()} items`;
},
});
}
const initHorizontalBarChartSearchForm = () => {
// Timer to prevent duplicate filtering runs.
let searchTimer;
horizontalBarChartKeyword.addEventListener('input', () => {
// Cancel a reserved filtering request.
if (searchTimer)
clearTimeout(searchTimer);
// Scheduled filtering runs.
searchTimer = setTimeout(() => {
// Filtering chart data.
horizontalBarChart.reload();
}, 1000);
});
}
// Search keyword input field.
const horizontalBarChartKeyword = document.getElementById('horizontalBarChartKeyword');
// Initialize the component and set up event listeners.
const horizontalBarChart = initHorizontalBarChart();
initHorizontalBarChartSearchForm();
[
{"name": "Series1", "category": "Category1", "data": 400, "color": "#50CD89"},
{"name": "Series1", "category": "Category2", "data": 448, "color": "#50CD89"},
{"name": "Series1", "category": "Category3", "data": 540, "color": "#50CD89"},
{"name": "Series2", "category": "Category1", "data": 430, "color": "#7239EA"},
{"name": "Series2", "category": "Category2", "data": 470, "color": "#7239EA"},
{"name": "Series2", "category": "Category3", "data": 580, "color": "#7239EA"}
]
<!--begin::Wrapper-->
<div class="d-flex flex-stack flex-wrap mb-5">
<!--begin::Search-->
<div class="d-flex align-items-center position-relative my-1 mb-2 mb-md-0">
<!--begin::Svg Icon | path: icons/duotune/general/gen021.svg-->
<span class="svg-icon svg-icon-1 position-absolute ms-4">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect opacity="0.5" x="17.0365" y="15.1223" width="8.15546" height="2" rx="1" transform="rotate(45 17.0365 15.1223)" fill="currentColor" />
<path d="M11 19C6.55556 19 3 15.4444 3 11C3 6.55556 6.55556 3 11 3C15.4444 3 19 6.55556 19 11C19 15.4444 15.4444 19 11 19ZM11 5C7.53333 5 5 7.53333 5 11C5 14.4667 7.53333 17 11 17C14.4667 17 17 14.4667 17 11C17 7.53333 14.4667 5 11 5Z" fill="currentColor" />
</svg>
</span>
<!--end::Svg Icon-->
<input id="verticalBarChartKeyword" type="text" class="form-control form-control-solid w-300px ps-15" placeholder="Search by name" maxlength="20" />
</div>
<!--end::Search-->
</div>
<!--end::Wrapper-->
<!--begin::Chart-->
<div id="verticalBarChart" class="h-325px"></div>
<!--end::Chart-->
import {components} from 'metronic-extension';
const initVerticalBarChart = () => {
return new components.BarChart(document.getElementById('verticalBarChart'), {
horizontal: false,
ajax: {
url: 'json/barchart.json',
dataSrc: data => {
// Filter chart data by categories matching the entered keywords.
// Normally, use the ajax.data option to set the keywords as request parameters and perform the filtering on the server side.
const keyword = verticalBarChartKeyword.value.replace(/^[\s ]+|[\s ]+$/g, '');
if (!keyword)
// If the keyword is empty, all data is displayed.
return data;
// Return only data with keywords in the name.
return data.filter(item => item.category.includes(keyword));
},
},
yAxisFormatter: value => {
// Comma-separate the values.
return Number(value).toLocaleString();
},
dataLabelFormatter: value => {
// Comma-separate the values.
return Number(value).toLocaleString();
},
tooltipDataFormatter: value => {
// Comma-separate the values.
return `${Number(value).toLocaleString()} items`;
},
});
}
const initVerticalBarChartSearchForm = () => {
// Timer to prevent duplicate filtering runs.
let searchTimer;
verticalBarChartKeyword.addEventListener('input', () => {
// Cancel a reserved filtering request.
if (searchTimer)
clearTimeout(searchTimer);
// Scheduled filtering runs.
searchTimer = setTimeout(() => {
// Filtering chart data.
verticalBarChart.reload();
}, 1000);
});
}
// Search keyword input field.
const verticalBarChartKeyword = document.getElementById('verticalBarChartKeyword');
// Initialize the component and set up event listeners.
const verticalBarChart = initVerticalBarChart();
initVerticalBarChartSearchForm();
[
{"name": "Series1", "category": "Category1", "data": 400, "color": "#50CD89"},
{"name": "Series1", "category": "Category2", "data": 448, "color": "#50CD89"},
{"name": "Series1", "category": "Category3", "data": 540, "color": "#50CD89"},
{"name": "Series2", "category": "Category1", "data": 430, "color": "#7239EA"},
{"name": "Series2", "category": "Category2", "data": 470, "color": "#7239EA"},
{"name": "Series2", "category": "Category3", "data": 580, "color": "#7239EA"}
]