一种解决方法是在数据列中使用不同的名称。例如,如果有两个名为“类别1”的类别,则可以将它们重命名为“类别1A”和“类别1B”,以便它们在数据列中有不同的名称。
另一种解决方法是使用唯一的ID作为每个类别的标识符。例如,可以使用类别的ID属性而不是名称属性来标识每个类别,以确保每个类别都是唯一的。
以下是使用ID属性的示例代码:
// 定义类别 const category1 = { id: "category1", name: "类别1" };
const category2 = { id: "category2", name: "类别2" };
// 在数据列中使用类别ID const chartData = [ { category: "category1", // 此处使用类别1的ID ...otherData }, { category: "category2", // 此处使用类别2的ID ...otherData }, ];
// 在甘特图中使用类别ID const categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis()); categoryAxis.dataFields.category = "id"; // 使用类别的ID属性作为数据类别 categoryAxis.title.text = "类别";
const valueAxis = chart.yAxes.push(new am4charts.ValueAxis()); valueAxis.title.text = "值";
const series = chart.series.push(new am4charts.ColumnSeries()); series.dataFields.categoryX = "category"; series.dataFields.valueY = "value"; series.name = "数据"; series.columns.template.tooltipText = "{categoryX}: [bold]{valueY}[/]"; series.columns.template.fillOpacity = 0.8;
// 更新数据 chart.data = chartData;
下一篇:Amchart文本换行