Pagination of date-based data
This demo shows how we can chunk up a multi-month data into “pages”, as well as create pagination controls to iterate between them.
Open in:New tab
/** * --------------------------------------- * This demo was created using amCharts 4. * * For more information visit: * https://www.amcharts.com/ * * Documentation is available at: * https://www.amcharts.com/docs/v4/ * --------------------------------------- */
// Add datavar data = [ { date: "2012-07-27", value: 13 }, { date: "2012-07-28", value: 11 }, { date: "2012-07-29", value: 15 }, { date: "2012-07-30", value: 16 }, { date: "2012-07-31", value: 18 }, { date: "2012-08-01", value: 13 }, { date: "2012-08-02", value: 22 }, { date: "2012-08-03", value: 23 }, { date: "2012-08-04", value: 20 }, { date: "2012-08-05", value: 17 }, { date: "2012-08-06", value: 16 }, { date: "2012-08-07", value: 18 }, { date: "2012-08-08", value: 21 }, { date: "2012-08-09", value: 26 }, { date: "2012-08-10", value: 24 }, { date: "2012-08-11", value: 29 }, { date: "2012-08-12", value: 32 }, { date: "2012-08-13", value: 18 }, { date: "2012-08-14", value: 24 }, { date: "2012-08-15", value: 22 }, { date: "2012-08-16", value: 18 }, { date: "2012-08-17", value: 19 }, { date: "2012-08-18", value: 14 }, { date: "2012-08-19", value: 15 }, { date: "2012-08-20", value: 12 }, { date: "2012-08-21", value: 8 }, { date: "2012-08-22", value: 9 }, { date: "2012-08-23", value: 8 }, { date: "2012-08-24", value: 7 }, { date: "2012-08-25", value: 5 }, { date: "2012-08-26", value: 11 }, { date: "2012-08-27", value: 13 }, { date: "2012-08-28", value: 18 }, { date: "2012-08-29", value: 20 }, { date: "2012-08-30", value: 29 }, { date: "2012-08-31", value: 33 }, { date: "2012-09-01", value: 42 }, { date: "2012-09-02", value: 35 }, { date: "2012-09-03", value: 31 }, { date: "2012-09-04", value: 47 }, { date: "2012-09-05", value: 52 }, { date: "2012-09-06", value: 46 }, { date: "2012-09-07", value: 41 }, { date: "2012-09-08", value: 43 }, { date: "2012-09-09", value: 40 }, { date: "2012-09-10", value: 39 }, { date: "2012-09-11", value: 34 }, { date: "2012-09-12", value: 29 }, { date: "2012-09-13", value: 34 }, { date: "2012-09-14", value: 37 }, { date: "2012-09-15", value: 42 }, { date: "2012-09-16", value: 49 }, { date: "2012-09-17", value: 46 }, { date: "2012-09-18", value: 47 }, { date: "2012-09-19", value: 55 }, { date: "2012-09-20", value: 59 }, { date: "2012-09-21", value: 58 }, { date: "2012-09-22", value: 57 }, { date: "2012-09-23", value: 61 }, { date: "2012-09-24", value: 59 }, { date: "2012-09-25", value: 67 }, { date: "2012-09-26", value: 65 }, { date: "2012-09-27", value: 61 }, { date: "2012-09-28", value: 66 }, { date: "2012-09-29", value: 69 }, { date: "2012-09-30", value: 71 }, { date: "2012-10-01", value: 67 }, { date: "2012-10-02", value: 63 }, { date: "2012-10-03", value: 46 }, { date: "2012-10-04", value: 32 }, { date: "2012-10-05", value: 21 }, { date: "2012-10-06", value: 18 }, { date: "2012-10-07", value: 21 }, { date: "2012-10-08", value: 28 }, { date: "2012-10-09", value: 27 }, { date: "2012-10-10", value: 36 }, { date: "2012-10-11", value: 33 }, { date: "2012-10-12", value: 31 }, { date: "2012-10-13", value: 30 }, { date: "2012-10-14", value: 34 }, { date: "2012-10-15", value: 38 }, { date: "2012-10-16", value: 37 }, { date: "2012-10-17", value: 44 }, { date: "2012-10-18", value: 49 }, { date: "2012-10-19", value: 53 }, { date: "2012-10-20", value: 57 }, { date: "2012-10-21", value: 60 }, { date: "2012-10-22", value: 61 }, { date: "2012-10-23", value: 69 }, { date: "2012-10-24", value: 67 }, { date: "2012-10-25", value: 72 }, { date: "2012-10-26", value: 77 }, { date: "2012-10-27", value: 75 }, { date: "2012-10-28", value: 70 }, { date: "2012-10-29", value: 72 }, { date: "2012-10-30", value: 70 }, { date: "2012-10-31", value: 72 }, { date: "2012-11-01", value: 73 }, { date: "2012-11-02", value: 67 }, { date: "2012-11-03", value: 68 }, { date: "2012-11-04", value: 65 }, { date: "2012-11-05", value: 71 }, { date: "2012-11-06", value: 75 }, { date: "2012-11-07", value: 74 }, { date: "2012-11-08", value: 71 }, { date: "2012-11-09", value: 76 }, { date: "2012-11-10", value: 77 }, { date: "2012-11-11", value: 81 }, { date: "2012-11-12", value: 83 }, { date: "2012-11-13", value: 80 }, { date: "2012-11-14", value: 81 }, { date: "2012-11-15", value: 87 }, { date: "2012-11-16", value: 82 }, { date: "2012-11-17", value: 86 }, { date: "2012-11-18", value: 80 }, { date: "2012-11-19", value: 87 }, { date: "2012-11-20", value: 83 }, { date: "2012-11-21", value: 85 }, { date: "2012-11-22", value: 84 }, { date: "2012-11-23", value: 82 }, { date: "2012-11-24", value: 73 }, { date: "2012-11-25", value: 71 }, { date: "2012-11-26", value: 75 }, { date: "2012-11-27", value: 79 }, { date: "2012-11-28", value: 70 }, { date: "2012-11-29", value: 73 }, { date: "2012-11-30", value: 61 }, { date: "2012-12-01", value: 62 }, { date: "2012-12-02", value: 66 }, { date: "2012-12-03", value: 65 }, { date: "2012-12-04", value: 73 }, { date: "2012-12-05", value: 79 }, { date: "2012-12-06", value: 78 }, { date: "2012-12-07", value: 78 }, { date: "2012-12-08", value: 78 }, { date: "2012-12-09", value: 74 }, { date: "2012-12-10", value: 73 }, { date: "2012-12-11", value: 75 }, { date: "2012-12-12", value: 70 }, { date: "2012-12-13", value: 77 }, { date: "2012-12-14", value: 67 }, { date: "2012-12-15", value: 62 }, { date: "2012-12-16", value: 64 }, { date: "2012-12-17", value: 61 }, { date: "2012-12-18", value: 59 }, { date: "2012-12-19", value: 53 }, { date: "2012-12-20", value: 54 }, { date: "2012-12-21", value: 56 }, { date: "2012-12-22", value: 59 }, { date: "2012-12-23", value: 58 }, { date: "2012-12-24", value: 55 }, { date: "2012-12-25", value: 52 }, { date: "2012-12-26", value: 54 }, { date: "2012-12-27", value: 50 }, { date: "2012-12-28", value: 50 }, { date: "2012-12-29", value: 51 }, { date: "2012-12-30", value: 52 }, { date: "2012-12-31", value: 58 }, { date: "2013-01-01", value: 60 }, { date: "2013-01-02", value: 67 }, { date: "2013-01-03", value: 64 }, { date: "2013-01-04", value: 66 }, { date: "2013-01-05", value: 60 }, { date: "2013-01-06", value: 63 }, { date: "2013-01-07", value: 61 }, { date: "2013-01-08", value: 60 }, { date: "2013-01-09", value: 65 }, { date: "2013-01-10", value: 75 }, { date: "2013-01-11", value: 77 }, { date: "2013-01-12", value: 78 }, { date: "2013-01-13", value: 70 }, { date: "2013-01-14", value: 70 }, { date: "2013-01-15", value: 73 }, { date: "2013-01-16", value: 71 }, { date: "2013-01-17", value: 74 }, { date: "2013-01-18", value: 78 }, { date: "2013-01-19", value: 85 }, { date: "2013-01-20", value: 82 }, { date: "2013-01-21", value: 83 }, { date: "2013-01-22", value: 88 }, { date: "2013-01-23", value: 85 }, { date: "2013-01-24", value: 85 }, { date: "2013-01-25", value: 80 }, { date: "2013-01-26", value: 87 }, { date: "2013-01-27", value: 84 }, { date: "2013-01-28", value: 83 }, { date: "2013-01-29", value: 84 }, { date: "2013-01-30", value: 81 }];
var firstYear;var firstMonth;var lastYear;var lastMonth;var currentYear;var currentMonth;function getMonthData() { var monthData = []; for (var i = 0; i < data.length; i++) { var date = chart.dateFormatter.parse(data[i].date); var y = date.getFullYear(); var m = date.getMonth(); lastYear = y; lastMonth = m; if (!currentYear) { currentYear = y; currentMonth = m; firstYear = y; firstMonth = m; } if (currentYear == y && currentMonth == m) { monthData.push(data[i]); } } title.text = chart.dateFormatter.format(new Date(currentYear, currentMonth, 1), "MMMM yyyy"); isFirstPage(); isLastPage(); return monthData;}
function prevPage() { if (!isFirstPage()) { currentMonth--; if (currentMonth < 0) { currentYear--; currentMonth = 11; } chart.data = getMonthData(); }}
function nextPage() { if (!isLastPage()) { currentMonth++; if (currentMonth > 11) { currentYear++; currentMonth = 0; } chart.data = getMonthData(); }}
function isFirstPage() { if (currentYear > firstYear || currentMonth > firstMonth) { prev.disabled = false; return false; } else { prev.disabled = true; return true; }}
function isLastPage() { if (currentYear < lastYear || currentMonth < lastMonth) { next.disabled = false; return false; } else { next.disabled = true; return true; }}
// Themesam4core.useTheme(am4themes_animated);
// Create chart instancevar chart = am4core.create("chartdiv", am4charts.XYChart);
// Set input format for the dateschart.dateFormatter.inputDateFormat = "yyyy-MM-dd";
// Titlevar title = chart.titles.create();title.fontSize = 30;title.marginBottom = 10;
// Create axesvar dateAxis = chart.xAxes.push(new am4charts.DateAxis());var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
// Create seriesvar series = chart.series.push(new am4charts.LineSeries());series.dataFields.valueY = "value";series.dataFields.dateX = "date";series.tooltipText = "{value}";series.strokeWidth = 2;series.minBulletDistance = 15;
// Drop-shaped tooltipsseries.tooltip.pointerOrientation = "vertical";
// Make bullets grow on hovervar bullet = series.bullets.push(new am4charts.CircleBullet());bullet.circle.strokeWidth = 2;bullet.circle.radius = 4;bullet.circle.fill = am4core.color("#fff");
var bullethover = bullet.states.create("hover");bullethover.properties.scale = 1.3;
// Make a panning cursorchart.cursor = new am4charts.XYCursor();chart.cursor.behavior = "panXY";chart.cursor.xAxis = dateAxis;chart.cursor.snapToSeries = series;
// Add pagination buttonsvar arrow = "M26,14H10l5.17-6.79A2,2,0,0,0,12,4.79l-7.62,10a2,2,0,0,0,0,2.42l7.62,10a2,2,0,0,0,3.18-2.42L10,18H26a2,2,0,0,0,0-4Z";var prev = chart.plotContainer.createChild(am4core.Button);prev.dy = -60;prev.icon = new am4core.Sprite();prev.icon.fill = am4core.color("#555");prev.icon.path = arrow;prev.events.on("hit", prevPage);
var next = chart.plotContainer.createChild(am4core.Button);next.dy = -60;next.align = "right";next.icon = new am4core.Sprite();next.icon.fill = am4core.color("#555");next.icon.rotation = 180;next.icon.path = arrow;next.events.on("hit", nextPage);
// Set datachart.data = getMonthData();<div id="chartdiv"></div>body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";}
#chartdiv { width: 100%; height: 500px;}paging-through-date-based-dataAnd another version, which loads data dynamically for new page on drag left or right.
Open in:New tab
/** * --------------------------------------- * This demo was created using amCharts 4. * * For more information visit: * https://www.amcharts.com/ * * Documentation is available at: * https://www.amcharts.com/docs/v4/ * --------------------------------------- */
// Start and end datesvar firstDate = new Date(2020, 0, 1);var lastDate = new Date(2020, 11, 31);var firstYear = firstDate.getFullYear();var firstMonth = firstDate.getMonth();var lastYear = lastDate.getFullYear();var lastMonth = lastDate.getMonth();var currentYear = lastYear;var currentMonth = lastMonth;
// Populate empty datavar data = [];var loadedData = {};var currentDate = new Date(firstDate);do { data.push({ date: new Date(currentDate), value: 0 });
currentDate.setDate(currentDate.getDate() + 1);} while (currentDate <= lastDate);
function loadMonthData() { // This is where actual loading would happen // We're just generate random data here for (var i = 0; i < data.length; i++) { var date = data[i].date; var y = date.getFullYear(); var m = date.getMonth(); if (currentYear == y && currentMonth == m) { data[i].value = Math.random() * 100; } }
loadedData[currentYear + "-" + currentMonth] = true; chart.data = getMonthData();}
function getMonthData() { // Is data for current month loaded? if (loadedData[currentYear + "-" + currentMonth] == undefined) { loadMonthData(); }
var monthData = []; for (var i = 0; i < data.length; i++) { var date = data[i].date; var y = date.getFullYear(); var m = date.getMonth(); if (currentYear == y && currentMonth == m) { monthData.push(data[i]); } } title.text = chart.dateFormatter.format(new Date(currentYear, currentMonth, 1), "MMMM yyyy"); isFirstPage(); isLastPage(); return monthData;}
function prevPage() { if (!isFirstPage()) { currentMonth--; if (currentMonth < 0) { currentYear--; currentMonth = 11; } chart.data = getMonthData(); }}
function nextPage() { if (!isLastPage()) { currentMonth++; if (currentMonth > 11) { currentYear++; currentMonth = 0; } chart.data = getMonthData(); }}
function isFirstPage() { if (currentYear > firstYear || currentMonth > firstMonth) { prev.disabled = false; return false; } else { prev.disabled = true; return true; }}
function isLastPage() { if (currentYear < lastYear || currentMonth < lastMonth) { next.disabled = false; return false; } else { next.disabled = true; return true; }}
// Themesam4core.useTheme(am4themes_animated);
// Create chart instancevar chart = am4core.create("chartdiv", am4charts.XYChart);
// Titlevar title = chart.titles.create();title.fontSize = 30;title.marginBottom = 10;
// Create axesvar dateAxis = chart.xAxes.push(new am4charts.DateAxis());var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
// Create seriesvar series = chart.series.push(new am4charts.LineSeries());series.dataFields.valueY = "value";series.dataFields.dateX = "date";series.tooltipText = "{value}";series.strokeWidth = 2;series.minBulletDistance = 15;
// Drop-shaped tooltipsseries.tooltip.pointerOrientation = "vertical";
// Make bullets grow on hovervar bullet = series.bullets.push(new am4charts.CircleBullet());bullet.circle.strokeWidth = 2;bullet.circle.radius = 4;bullet.circle.fill = am4core.color("#fff");
var bullethover = bullet.states.create("hover");bullethover.properties.scale = 1.3;
// Make a panning cursorchart.cursor = new am4charts.XYCursor();chart.cursor.behavior = "panX";chart.cursor.xAxis = dateAxis;chart.cursor.snapToSeries = series;
// Add pagination buttonsvar arrow = "M26,14H10l5.17-6.79A2,2,0,0,0,12,4.79l-7.62,10a2,2,0,0,0,0,2.42l7.62,10a2,2,0,0,0,3.18-2.42L10,18H26a2,2,0,0,0,0-4Z";var prev = chart.plotContainer.createChild(am4core.Button);prev.dy = -60;prev.icon = new am4core.Sprite();prev.icon.fill = am4core.color("#555");prev.icon.path = arrow;prev.events.on("hit", prevPage);
var next = chart.plotContainer.createChild(am4core.Button);next.dy = -60;next.align = "right";next.icon = new am4core.Sprite();next.icon.fill = am4core.color("#555");next.icon.rotation = 180;next.icon.path = arrow;next.events.on("hit", nextPage);
// Set datachart.data = getMonthData();
chart.plotContainer.events.on("down", function () { dateAxis.events.once("rangechangeended", function (ev) { if (dateAxis.start < 0) { prevPage(); } if (dateAxis.start > 0) { nextPage(); } });});<div id="chartdiv"></div>body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";}
#chartdiv { width: 100%; height: 500px;}paging-chart-data-with-incremental-data-loading