当我使用DurationAxis()时,我一直试图在amcharts4中使用工具提示。这可能是一个错误,因为我得到的工具提示有时会卡在左上角。
示例在这里:https://jsfiddle.net/jamiegau/fpye3bkv/25/
am4core.useTheme(am4themes_animated);
// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);
// Add data
chart.data = [{
"seconds": 1,
"valueM": 12.1
}, {
"seconds": 2,
"valueM": 4.2
}, {
"seconds": 4,
"valueM": 7.3
}, {
"seconds": 6,
"valueM": 8.4
}, {
"seconds": 9,
"valueM": 4.5
}, {
"seconds": 104,
"valueM": 10.7
}];
var durationAxis = chart.xAxes.push(new am4charts.DurationAxis());
durationAxis.baseUnit = 'second';
durationAxis.title.text = 'Duration';
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
// Add cursor
chart.cursor = new am4charts.XYCursor();
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.valueY = 'valueM';
series.dataFields.valueX = 'seconds';
series.yAxis = valueAxis;
series.name = 'Momentary';
series.strokeWidth = 2;
series.minBulletDistance = 10;
series.tooltipText = 'M: {valueY}';
我以前使用过此代码,但使用了DateAxis()却没有问题。当我尝试使用DurationAxis()时,所有有关工具提示的信息都会陷入困境。
在我的真实代码中,我有4个不同的系列。但是我只用1个系列尝试过。几乎都尝试了所有方法。甚至回到DateAxis,但这对我也不起作用,因为我需要不到一秒的精度,而当我使用DateAxes时,将其四舍五入到一秒钟,看起来很糟糕。
在valueAxis和DurationAxis上也是如此。我已经对此进行了更多研究,而在此类弹药上似乎无法在这些类的轴上使用POINT工具提示。
如果我错了,请这样回答。
答案 0 :(得分:1)
我遇到了同样的问题。幸运的是,我在那里找到了答案:https://stackoverflow.com/a/64914695/3324314。
然而,该答案是关于 ValueAxis
,而不是 DurationAxis
,但概念是相同的。似乎这些类没有定义必需的方法,因此您必须修补它们的原型。以下代码段解决了我的问题:
am4charts.DurationAxis.prototype.getSeriesDataItem = function (
series,
position
) {
const key = this.axisFieldName + this.axisLetter
const value = this.positionToValue(position)
return series.dataItems.getIndex(
series.dataItems.findClosestIndex(
value,
function (x) {
return x[key]
},
'any'
)
)
}