Reputation: 81
I am trying to see how to have a scrollable AMCharts line graph exactly like this
Here is a JSFiddle of what I am talking about. https://jsfiddle.net/6nchebyu/1/
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/serial.js"></script>
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>
var chartData = generateChartData();
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"theme": "light",
"marginRight": 80,
"autoMarginOffset": 20,
"marginTop": 7,
"dataProvider": chartData,
"valueAxes": [{
"axisAlpha": 0.2,
"dashLength": 1,
"position": "left"
}],
"mouseWheelZoomEnabled": true,
"graphs": [{
"id": "g1",
"balloonText": "[[value]]",
"bullet": "round",
"bulletBorderAlpha": 1,
"bulletColor": "#FFFFFF",
"hideBulletsCount": 50,
"title": "red line",
"valueField": "visits",
"useLineColorForBulletBorder": true,
"balloon":{
"drop":true
}
}],
"chartScrollbar": {
"autoGridCount": true,
"graph": "g1",
"scrollbarHeight": 40
},
"chartCursor": {
"limitToGraph":"g1"
},
"categoryField": "date",
"categoryAxis": {
"parseDates": true,
"axisColor": "#DADADA",
"dashLength": 1,
"minorGridEnabled": true
},
"export": {
"enabled": true
}
});
chart.addListener("rendered", zoomChart);
zoomChart();
// this method is called when chart is first inited as we listen for "rendered" event
function zoomChart() {
// different zoom methods can be used - zoomToIndexes, zoomToDates, zoomToCategoryValues
chart.zoomToIndexes(chartData.length - 40, chartData.length - 1);
}
// generate some random data, quite different range
function generateChartData() {
var chartData = [];
var firstDate = new Date();
firstDate.setDate(firstDate.getDate() - 5);
for (var i = 0; i < 1000; i++) {
// we create date objects here. In your data, you can have date strings
// and then set format of your dates using chart.dataDateFormat property,
// however when possible, use date objects, as this will speed up chart rendering.
var newDate = new Date(firstDate);
newDate.setDate(newDate.getDate() + i);
var visits = Math.round(Math.random() * (40 + i / 5)) + 20 + i;
chartData.push({
date: newDate,
visits: visits
});
}
return chartData;
}
But I want it to update every minute and not "Reset" the view port if you are looking at older data (Zooming in or out). Maybe have a button to reset it back to the front and then keep showing the current data coming in? Is this possible to have all of that?
Upvotes: 0
Views: 298
Reputation: 81
I figured it out a little after creating this topic.
chart.zoomOutOnDataUpdate = false;
var j = 101;
setInterval(function () {
var x = j++,
y = (Math.random() * 50) + 50;
chart.dataProvider.shift();
chart.
dataProvider.push({
date: x,
visits: y
});
// chart.dataChanged();
chart.validateData();
}, 1000);
The chart.zoomOutOnDataUpdate = false; Makes it so that it does not keep zooming in.
chart.dataProvider.shift() pushes the first datapoint off the screen. At this point i just need to add a custom button to set zoomOutOnDataUpdate = true when I click on it. And also maybe change it to false when I zoom out or zoom in.
Upvotes: 1