在Amchart中禁用自然时间缩放的方法是通过设置categoryAxis的minPeriod属性为对应的时间单位来实现的。下面是一个示例代码:
var chart = am4core.create("chartdiv", am4charts.XYChart);
// 创建categoryAxis
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "date";
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.minGridDistance = 30;
categoryAxis.startLocation = 0.5;
categoryAxis.endLocation = 0.5;
// 禁用自然时间缩放
categoryAxis.dateFormats.setKey("day", "yyyy-MM-dd");
categoryAxis.periodChangeDateFormats.setKey("day", "yyyy-MM-dd");
// 添加数据
chart.data = [{
"date": "2021-01-01",
"value": 100
}, {
"date": "2021-01-02",
"value": 200
}, {
"date": "2021-01-03",
"value": 150
}, {
"date": "2021-01-04",
"value": 120
}];
// 创建valueAxis
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
// 创建折线图系列
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.valueY = "value";
series.dataFields.categoryX = "date";
// 添加折线图系列到chart
chart.series.push(series);
在上面的代码中,我们通过设置categoryAxis的dateFormats和periodChangeDateFormats属性,将时间单位设置为"day",这样就禁用了自然时间缩放。你可以根据需要修改时间单位和日期格式。