amCharts 工具提示仅显示在 DateAxis 而不是 ValueAxis [英] amCharts tooltip is showing only on DateAxis but not ValueAxis
问题描述
我正在使用 amCharts 并且我正在制作具有多个系列的 XY 图表,当 X 轴类型为 DateAxis
时,工具提示仅显示,但在 ValueAxis
时不起作用>
I'm using amCharts and I'm making a XY Chart with multiple series, the tooltip shows ony when the X Axis type is DateAxis
but not working when it's ValueAxis
var dateAxis = chart5.xAxes.push(new am4charts.DateAxis());
series.dataFields.dateX = "time";
带工具提示的 amChart:
amChart with tooltip:
现在,当我将这两行更改为值轴时,它不起作用
Now when I change these 2 lines to Value Axis it doesn't work
var dateAxis = chart5.xAxes.push(new am4charts.ValueAxis());
series.dataFields.valueX = "time";
没有工具提示的 amChart:
amChart without tooltip:
推荐答案
我遇到了同样的问题,并深入研究了 库代码,我意识到 ValueAxis 没有实现 getSeriesDataItem 方法(DateAxis 和 CategoryAxis 做).所以,我的解决方案是实现该方法.基于其他轴实现,我得出的代码是:
I came across the same issue, and digging into the library code, I realized ValueAxis doesn't implement getSeriesDataItem method (DateAxis and CategoryAxis do). So, the solution for me was to implement that method. Based on the other axis implementations, the code I came out with was:
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"));
}
添加此原型后,工具提示在使用 ValueAxis 时没有问题:
After adding this prototype, tooltips works with no issues when using 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>
这篇关于amCharts 工具提示仅显示在 DateAxis 而不是 ValueAxis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!