要在AmCharts中的类别之间添加刻度线,可以使用调整类别轴的gridIntervals属性。以下是一个代码示例:
// 创建图表实例
var chart = am4core.create("chartdiv", am4charts.XYChart);
// 创建类别轴
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "category";
categoryAxis.renderer.grid.template.disabled = true; // 禁用默认的网格线
// 在类别之间添加刻度线
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.grid.template.strokeOpacity = 0.5;
categoryAxis.renderer.grid.template.stroke = am4core.color("#000000");
categoryAxis.renderer.grid.template.strokeWidth = 1;
// 添加数据
chart.data = [
{ category: "Category 1", value: 10 },
{ category: "Category 2", value: 20 },
{ category: "Category 3", value: 15 }
];
// 创建值轴
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
// 创建柱状图系列
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = "value";
series.dataFields.categoryX = "category";
// 更新图表布局
chart.leftAxesContainer.layout = "absolute";
chart.leftAxesContainer.width = am4core.percent(100);
chart.plotContainer.width = am4core.percent(80);
chart.plotContainer.marginLeft = am4core.percent(20);
// 刷新图表
chart.invalidateData();
在这个示例中,我们首先创建一个类别轴,并禁用默认的网格线。然后,我们使用grid.template.location属性将刻度线位置设置为0,使用grid.template.strokeOpacity和grid.template.stroke属性来设置刻度线的样式,以及grid.template.strokeWidth属性来设置刻度线的宽度。
最后,我们创建一个柱状图系列,并将数据绑定到类别轴和值轴上。我们还通过调整图表的布局来使类别轴的刻度线在图表的左侧可见,并刷新图表以显示更改。