简体   繁体   English

amCharts 工具提示仅显示在 DateAxis 而不是 ValueAxis

[英]amCharts tooltip is showing only on DateAxis but not 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我正在使用 amCharts 并且我正在制作一个具有多个系列的 XY 图表,当 X 轴类型为DateAxis时工具提示仅显示,但在ValueAxisValueAxis

var dateAxis = chart5.xAxes.push(new am4charts.DateAxis());
series.dataFields.dateX = "time";

amChart with tooltip:带工具提示的 amChart:

带工具提示的 amChart

Now when I change these 2 lines to Value Axis it doesn't work现在,当我将这两行更改为 Value Axis 时,它不起作用

var dateAxis = chart5.xAxes.push(new am4charts.ValueAxis());
    series.dataFields.valueX = "time";

amChart without tooltip:没有工具提示的 amChart:

没有工具提示的 amChart

I came across the same issue, and digging into the library code , I realized ValueAxis doesn't implement getSeriesDataItem method ( DateAxis and CategoryAxis do).我遇到了同样的问题,深入研究库代码,我意识到 ValueAxis 没有实现getSeriesDataItem方法( DateAxisCategoryAxis )。 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"));
}

After adding this prototype, tooltips works with no issues when using ValueAxis:添加此原型后,工具提示在使用 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>

Here's a reply to the request to post an example of this (it's happening to me, too) from @xorspark :这是@xorspark 对发布此示例的请求的回复(这也发生在我身上):

XY chart, Y axis is ValueAxis, X axis is CategoryAxis.... and the tooltip works: https://codepen.io/alavigne314/pen/JjozVWx XY 图表,Y 轴是 ValueAxis,X 轴是 CategoryAxis .... 工具提示有效: https ://codepen.io/alavigne314/pen/JjozVWx

Same XY chart, but X axis is changed from CategoryAxis to ValueAxis... and the tooltip is gone: https://codepen.io/alavigne314/pen/povYBdp相同的 XY 图表,但 X 轴从 CategoryAxis 更改为 ValueAxis...并且工具提示消失了: https ://codepen.io/alavigne314/pen/povYBdp

The changes between the two are only 3 lines:两者之间的变化只有3行:

var theXAxis = chart.xAxes.push(new am4charts.CategoryAxis());
theXAxis.dataFields.category = "xValue";

gets changed to更改为

var theXAxis = chart.xAxes.push(new am4charts.ValueAxis());

and

series1.dataFields.categoryX = "xValue";

gets changed to更改为

series1.dataFields.valueX = "xValue";

Maybe we're both not reading something in the documentation correctly?也许我们都没有正确阅读文档中的某些内容? If so, I can't find out what it is.如果是这样,我无法找出它是什么。 Or perhaps... are tooltips broken or not supported in a both X-and-Y-ValueAxis chart?或者……在 X 和 Y 值轴图表中工具提示是否已损坏或不受支持?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM