Reputation: 114
If you can see on X axis There are increment of time 6 hour Now, I want to change its scale to 2 hour
I just learn this amchart yesterday and really don't know so much on how to use it.
here is the code
<script type="text/javascript">
//am4core.useTheme(am4themes_kelly);
am4core.useTheme(am4themes_animated);
// Themes end
// Create chart
var chart = am4core.create("chartdiv", am4charts.XYChart);
chart.paddingRight = 20;
chart.data = generateChartData();
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.baseInterval = {
"timeUnit": "minute",
"count": 1
};
dateAxis.tooltipDateFormat = "HH:mm, d MMMM";
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.tooltip.disabled = true;
valueAxis.title.text = "Fin 1";
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.dateX = "date";
series.dataFields.valueY = "visits";
series.tooltipText = "Stacker: [bold]{valueY}[/]";
series.fillOpacity = 0.3;
chart.cursor = new am4charts.XYCursor();
chart.cursor.lineY.opacity = 0;
chart.scrollbarX = new am4charts.XYChartScrollbar();
chart.scrollbarX.series.push(series);
chart.events.on("datavalidated", function () {
dateAxis.zoom({start:0, end:1});
});
function generateChartData() {
var chartData = [];
// current date
var firstDate = new Date();
// now set 12:00 am
firstDate.setHours(0,0,0,0);
// and generate 500 data items
var visits = 0;
for (var i = 0; i < 1442; i++) {
var newDate = new Date(firstDate);
// each time we add one minute
newDate.setMinutes(newDate.getMinutes() + i);
chartData.push({
date: newDate,
visits: visits
});
}
return chartData;
}
</script>
I have edit the code and available to edit here: https://codepen.io/muhammad-syazani/pen/wLrbQE
Upvotes: 1
Views: 1551
Reputation: 3655
Yeah working with the Date Axis can be tricky. Even if you've been working with amCharts v4 for a while, it may not be clear how to tame the axis the way you want. I highly recommend reading our guide on the Date Axis in full, that will really help. The chart will basically try and figure out how it can fit a frequency nicely into the chart.
The first thing you want to adjust is the dateAxis.renderer.minGridDistance
. By default, its 120
pixels. The sample chart has 24 hours, with a minimum pixel interval of 120px for every 2 hours, your chart.plotContainer
area would have to be 120 * (24/2)
, or at least 1440px, your browser window would have to be even wider than that. So if we cut that down, we're more likely to be able to fit a 2-hour interval grid.
The next thing, and this is the main ingredient, is the dateAxis.gridIntervals
. If you check out the generated documentation, you'll see a list of intervals the chart will choose from. There's hourly, or every 3 hours, but not every 2 hours. If we replace that list with one that has a 2-hour interval, if it can fit, the chart will use that.
You'll want to do the same on the scrollBarX
's date axis as well.
Relevant code:
dateAxis.renderer.minGridDistance = 75; // 75 * 24/2 == 900 minimum plotContainer width requisite
var gridIntervals = [
{ timeUnit: "millisecond", count: 1 },
{ timeUnit: "millisecond", count: 5 },
{ timeUnit: "millisecond", count: 10 },
{ timeUnit: "millisecond", count: 50 },
{ timeUnit: "millisecond", count: 100 },
{ timeUnit: "millisecond", count: 500 },
{ timeUnit: "second", count: 1 },
{ timeUnit: "second", count: 5 },
{ timeUnit: "second", count: 10 },
{ timeUnit: "second", count: 30 },
{ timeUnit: "minute", count: 1 },
{ timeUnit: "minute", count: 5 },
{ timeUnit: "minute", count: 10 },
{ timeUnit: "minute", count: 30 },
{ timeUnit: "hour", count: 1 },
{ timeUnit: "hour", count: 2 }, // This is the 2-hour interval
{ timeUnit: "hour", count: 3 },
{ timeUnit: "hour", count: 6 },
{ timeUnit: "hour", count: 12 },
{ timeUnit: "day", count: 1 },
{ timeUnit: "day", count: 2 },
{ timeUnit: "day", count: 3 },
{ timeUnit: "day", count: 4 },
{ timeUnit: "day", count: 5 },
{ timeUnit: "week", count: 1 },
{ timeUnit: "month", count: 1 },
{ timeUnit: "month", count: 2 },
{ timeUnit: "month", count: 3 },
{ timeUnit: "month", count: 6 },
{ timeUnit: "year", count: 1 },
{ timeUnit: "year", count: 2 },
{ timeUnit: "year", count: 5 },
{ timeUnit: "year", count: 10 },
{ timeUnit: "year", count: 50 },
{ timeUnit: "year", count: 100 }
];
dateAxis.gridIntervals.setAll(gridIntervals);
chart.scrollbarX.events.on("validated", function() {
chart.scrollbarX.scrollbarChart.xAxes.getIndex(0).gridIntervals.setAll(gridIntervals);
});
Fork:
https://codepen.io/team/amcharts/pen/9ba62a5bb5239fee149c2d46bf1e4b81
Hope this helps!
(Btw - Salaams! I like the username, is that a Fairy Tail reference?)
Upvotes: 2