Me again,
I came up with another solution that seems to be working exactly as I wanted it to.
On load all list data is pulled into the chart, and year filters are populated dynamically based on available data.
Page loaded
Filtere applied
To achieve this i did the following. Custom List, 3 Columns(City, Spending, Date)
// DATA SOURCE - ADVANCED
// REQUEST SUCCESS
availableYears = [];
handlers.requestSuccess = function (data, query, logger, el) {
for (var i = 0; i < data.items.length; i++) {
var year = data.items[i].Date.getFullYear();
if (availableYears.indexOf(year) === -1) {
availableYears.push(year);
}
}
// logger.debug('array: ', availableYears);
availableYears.sort();
return true;
}
// FINISH
handlers.finish = function (query, data, logger, processor) {
$('body').on('click', '.yearSelectors', function () {
var selectedYear = $(this).text();
// Refresh Function
refresh(query, data, logger, selectedYear);
});
return true;
}
// FUNCTION
function refresh(query, data, logger, selectedYear) {
// Get Chart
var chart = $("[id*='chartg']").data("kendoChart");
// Get all data;
var series = query;
// Create Date Range
var startDate = new Date("01/01/" + selectedYear);
var endDate = new Date("12/31/" + selectedYear);
// Filter by Date Range
var filteredSeries = series.items.filter(row => row.Date >= startDate && row.Date <= endDate);
// Apply filtered Data Source
var dataSource = new kendo.data.DataSource({
data: filteredSeries
});
chart.setDataSource(dataSource);
}
// DASHBOARD - ADVANCED
// STYLE
var css = '.yearSelectors { background: #eee; color: #464646; font-size: 17px; border: 1px solid #464646; padding: 5px 20px; margin: 2px; cursor: pointer;}\
.yearSelectors:hover{ background: #c32032; color: white;}\
.yearSelectorsSelected { background: #464646 !important; color: #fff;}',
head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style');
head.appendChild(style);
style.type = 'text/css';
style.appendChild(document.createTextNode(css));
// SCRIPT
var handlers = {};
handlers.preRender = function (config, logger, processor, el) {
logger.debug('Configuration: ', config);
el.parent().prepend('<p id="buttons" style="text-align: center;">Filter by Year: </p>');
// Create Buttons
for (var i = 0; i < availableYears.length; i++) {
var btn = document.createElement("span");
btn.setAttribute("class", "yearSelectors");
var t = document.createTextNode(availableYears[i]);
btn.appendChild(t);
btn.onclick = filterdYear;
document.getElementById('buttons').appendChild(btn);
}
function filterdYear() {
$(this).addClass('yearSelectorsSelected');
$('.yearSelectors').not($(this)).removeClass('yearSelectorsSelected');
}
return true;
}
Again, this works perfectly fine, but I have one last problem that I can't seem to wrap my head around. I put this together in Chrome and it works just fine (same for Edge) but i'm getting the following error in IE. (Yes, we do need to support IE in our company ).
Pre-rendering failed: ReferenceError: 'availableYears' is undefined
I remember having to add
window.data = data;
But this didn't work this time, or i set t up incorrectly.
Do you happen to know how to fix this?
Thank you for everything!