我正在使用Amchart V4中的“ variable-radius-radar”。我想在类别名称的悬停上显示工具提示,但找不到任何解决方案。需要工具提示,因为名称可能很长。我打算在图表上仅显示类别的前10个字符。工具提示将具有全名。
在https://codepen.io/pzombade/pen/RExzbj?editors=0010上创建了一个页面
代码:
var chart = am4core.create("chartdiv", am4charts.RadarChart);
chart.hiddenState.properties.opacity = 0; // this creates initial fade-in
var label = chart.createChild(am4core.Label);
label.text = "Drag slider to change radius";
label.exportable = false;
chart.data = [
{
category: "One",
value1: 8,
value2: 2,
value3: 4,
value4: 3
},
{
category: "Two",
value1: 11,
value2: 4,
value3: 2,
value4: 4
},
{
category: "Three",
value1: 7,
value2: 6,
value3: 6,
value4: 2
},
{
category: "Four",
value1: 13,
value2: 8,
value3: 3,
value4: 2
}
];
chart.radius = am4core.percent(95);
chart.startAngle = 270 - 180;
chart.endAngle = 270 + 180;
chart.innerRadius = am4core.percent(60);
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "category";
categoryAxis.renderer.labels.template.location = 0.5;
categoryAxis.renderer.grid.template.strokeOpacity = 0.1;
categoryAxis.renderer.axisFills.template.disabled = true;
categoryAxis.mouseEnabled = true;
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.tooltip.disabled = false;
valueAxis.renderer.grid.template.strokeOpacity = 0.05;
valueAxis.renderer.axisFills.template.disabled = true;
valueAxis.renderer.axisAngle = 260;
valueAxis.renderer.labels.template.horizontalCenter = "right";
valueAxis.min = 0;
var series1 = chart.series.push(new am4charts.RadarColumnSeries());
series1.columns.template.radarColumn.strokeOpacity = 1;
series1.name = "Series 1";
series1.dataFields.categoryX = "category";
series1.columns.template.tooltipText = "{name}: {valueY.value}";
series1.dataFields.valueY = "value1";
series1.stacked = true;
var series2 = chart.series.push(new am4charts.RadarColumnSeries());
series2.columns.template.radarColumn.strokeOpacity = 1;
series2.columns.template.tooltipText = "{name}: {valueY.value}";
series2.name = "Series 2";
series2.dataFields.categoryX = "category";
series2.dataFields.valueY = "value2";
series2.stacked = true;
var series3 = chart.series.push(new am4charts.RadarColumnSeries());
series3.columns.template.radarColumn.strokeOpacity = 1;
series3.columns.template.tooltipText = "{name}: {valueY.value}";
series3.name = "Series 3";
series3.dataFields.categoryX = "category";
series3.dataFields.valueY = "value3";
series3.stacked = true;
chart.seriesContainer.zIndex = -1;
var slider = chart.createChild(am4core.Slider);
slider.start = 0.5;
slider.exportable = false;
slider.events.on("rangechanged", () => {
var start = slider.start;
chart.startAngle = 270 - start * 179 - 1;
chart.endAngle = 270 + start * 179 + 1;
valueAxis.renderer.axisAngle = chart.startAngle;
});
答案 0 :(得分:2)
已启用工具提示,以便在正确的位置为tooltipText
提供非空字符串/值。在这种情况下,将其设置在轴标签上将在悬停时触发轴工具提示(与其他工具提示略有不同,例如,默认情况下为黑色,并紧贴图表的周长或在此情况下紧贴图表的周长): / p>
categoryAxis.renderer.labels.template.tooltipText = "{category}";
要截断轴标签的文本,可以在其textOutput
上使用adapter(在text
上执行此操作将为您"{category}"
提供实际生成的标签,在这种情况下不是很有用):
var categoryCharacterLimit = 10;
categoryAxis.renderer.labels.template.adapter.add("textOutput", function(text) {
if (text && text.length > categoryCharacterLimit) {
return text.substr(0, categoryCharacterLimit) + "\u2026"; // unicode for ellipsis
}
return text;
});
鉴于默认情况下轴工具提示的宽度将与其文本一样,这是一个快速修复方法,用于限制其宽度,环绕并居中显示文本:
categoryAxis.tooltip.maxWidth = 200;
categoryAxis.tooltip.label.wrap = true;
categoryAxis.tooltip.label.textAlign = "middle";
如果您不希望为已经足够短的轴标签显示工具提示(请参见下面的演示中的“一个”),也可以在此处设置适配器以返回空字符串({ {1}})(如果有文本,则会触发工具提示,当重置为空字符串时,将不再满足触发条件):
""
这是一个演示,其中包含所有内容:
https://codepen.io/team/amcharts/pen/64e5da963cc3822fc95bac15a8f56b09
如果您需要自定义轴工具提示的外观,请参见以下附加信息:
https://www.amcharts.com/docs/v4/concepts/axes/axis-tooltips/#Modifying_tooltip_appearance https://www.amcharts.com/docs/v4/concepts/series/#Tooltip_colors