我正在使用 amCharts,并且正在制作包含多个系列的 XY 图表,工具提示仅在 X 轴类型为 DateAxis
时显示,但在 X 轴类型为 ValueAxis
时则不起作用>
var dateAxis = chart5.xAxes.push(new am4charts.DateAxis());
series.dataFields.dateX = "time";
带有工具提示的 amChart:
现在,当我将这两行更改为值轴时,它不起作用
var dateAxis = chart5.xAxes.push(new am4charts.ValueAxis());
series.dataFields.valueX = "time";
不带工具提示的 amChart:
最佳答案
我遇到了同样的问题,并深入研究了 library code ,我意识到 ValueAxis 没有实现 getSeriesDataItem方法( DateAxis 和 CategoryAxis 执行)。所以,我的解决方案是实现该方法。基于其他轴实现,我得出的代码是:
am4charts.ValueAxis.prototype.getSeriesDataItem = function (series, position) {
var key = this.axisFieldName + this.axisLetter;
var value = this.positionToValue(position);
return series.dataItems.getIndex(series.dataItems.findClosestIndex(value, function(x) {
return x[key];
}, "any"));
}
添加此原型(prototype)后,使用 ValueAxis 时工具提示可以正常工作:
/********** FIX: Add getSeriesDataItem method to ValueAxis ************/
am4charts.ValueAxis.prototype.getSeriesDataItem = function (series, position) {
var key = this.axisFieldName + this.axisLetter;
var value = this.positionToValue(position);
return series.dataItems.getIndex(series.dataItems.findClosestIndex(value, function(x) {
return x[key];
}, "any"));
}
/**********************************************************************/
/* Create chart instance */
var chart = am4core.create("chartdiv", am4charts.XYChart);
/* Add data */
chart.data = [{
"xValue": 1000,
"yValue": 1587
}, {
"xValue": 1100,
"yValue": 1567
}, {
"xValue": 1250,
"yValue": 1617
}, {
"xValue": 1400,
"yValue": 1630
}, {
"xValue": 1700,
"yValue": 1660
}, {
"xValue": 1754,
"yValue": 1683
}, {
"xValue": 2255,
"yValue": 1691
}, {
"xValue": 3004,
"yValue": 1298
}];
/* Create axes */
var theXAxis = chart.xAxes.push(new am4charts.ValueAxis());
/* Create value axis */
var theYAxis = chart.yAxes.push(new am4charts.ValueAxis());
/* Create series */
var series1 = chart.series.push(new am4charts.LineSeries());
series1.dataFields.valueY = "yValue";
series1.dataFields.valueX = "xValue";
series1.name = "The X Axis";
series1.bullets.push(new am4charts.CircleBullet());
series1.tooltipText = "{valueY}";
/* Add legend */
chart.legend = new am4charts.Legend();
/* Create a cursor */
chart.cursor = new am4charts.XYCursor();
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
#chartdiv {
width: 100%;
height: 300px;
}
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<div id="chartdiv"></div>
关于javascript - amCharts 工具提示仅在 DateAxis 上显示,但在 ValueAxis 上不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59466110/