javascript - amCharts 工具提示仅在 DateAxis 上显示,但在 ValueAxis 上不显示

标签 javascript html json charts amcharts

我正在使用 amCharts,并且正在制作包含多个系列的 XY 图表,工具提示仅在 X 轴类型为 DateAxis 时显示,但在 X 轴类型为 ValueAxis 时则不起作用>

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

带有工具提示的 amChart:

amChart with tooltip

现在,当我将这两行更改为值轴时,它不起作用

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

不带工具提示的 amChart:

amChart without tooltip

最佳答案

我遇到了同样的问题,并深入研究了 library code ,我意识到 ValueAxis 没有实现 getSeriesDataItem方法( DateAxisCategoryAxis 执行)。所以,我的解决方案是实现该方法。基于其他轴实现,我得出的代码是:

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/

相关文章:

javascript - 在嵌套 json 对象中按键查找并按值替换

json - 使用 fs.writeFileSync 将 JSON 对象写入 JSON 文件

javascript - 如果我传递 "this"中的函数,监听器将不起作用

javascript - RequireJS 的异步特性

javascript - 如何在代码级别 (HTML/JavaScript) 中识别 Amazon Fire TV 和 Fire Stick ..?

html - 裁剪图像以填充具有相对于宽度的高度的父级

javascript - 为什么登录时 html event.currentTarget 为 null

python - 将字符串输出转换为 JSON

java - 如何在 Web 服务响应中将 json 响应作为 block 返回?

javascript - Node.JS 中的同步 "GET"