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

时间:2019-12-24 08:42:40

标签: javascript html json charts amcharts

我正在使用amCharts,并且正在制作具有多个系列的XY图表,当X轴类型为DateAxis时,工具提示显示为ony,而当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

2 个答案:

答案 0 :(得分:1)

以下是来自@xorspark的发布此示例示例的请求的回复:

XY图表,Y轴为ValueAxis,X轴为CategoryAxis ....并且工具提示起作用: cxxomfort

相同的XY图表,但是X轴从CategoryAxis更改为ValueAxis ...,并且工具提示消失了: https://codepen.io/alavigne314/pen/JjozVWx

两者之间的更改只有3行:

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

更改为

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

series1.dataFields.categoryX = "xValue";

更改为

series1.dataFields.valueX = "xValue";

也许我们俩都没有正确阅读文档中的内容?如果是这样,我就找不到答案。也许... X和Y-ValueAxis图表中的工具提示是否损坏或不支持?

答案 1 :(得分:1)

我遇到了同样的问题,深入研究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"));
}

添加此原型后,使用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>