Amcharts V4 使用 valueYShow 以百分比而不是绝对值绘制图表

Amcharts V4 plating a chart in percent rather than absolute using valueYShow

我正在尝试使用 AMCharts V4 创建图表。这些值以绝对值给出,我希望图表以百分比显示:值、y 轴等。

文档在这里:https://www.amcharts.com/docs/v4/tutorials/plotting-series-from-calculated-values/

但是,我遇到了一些麻烦。请考虑从官方 AMCharts 文档中分叉的以下代码笔:

https://codepen.io/otmezger/pen/RwVzmjv

正如文档所建议的,我启用了 valueAxis.calculateTotals = true;

series.dataFields.valueYShow = "totalPercent";series.dataFields.valueYShow = "percent"; 使图表无用。

只有当我禁用它们时,图表才会以绝对数字显示。

我错过了什么?我怎样才能让 series.dataFields.valueYShow = "percent"; 工作?

如果您想在系列中使用百分比值,您需要将系列中的 calculatePercent 设置为 true,如 here

所述
series.calculatePercent = true;
series.dataFields.valueYShow = "percent";
series.tooltipText = "{dateX}: [b]{valueY.percent}[/]%";

演示:

// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);

// Add data
chart.data = [{
  "date": new Date(2018, 0, 1),
  "value": 10
}, {
  "date": new Date(2018, 0, 2),
  "value": 15
}, {
  "date": new Date(2018, 0, 3),
  "value": 2
}];

// Create axes
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.grid.template.location = 0;
dateAxis.renderer.minGridDistance = 30;

var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.calculateTotals = true;
valueAxis.renderer.labels.template.adapter.add("text", function(text) {
  return text + "%";
});


// Create series
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.valueY = "value";
series.dataFields.dateX = "date";
series.calculatePercent = true;
series.dataFields.valueYShow = "percent";

series.tooltipText = "{dateX}: [b]{valueY.percent}[/]%";
series.strokeWidth = 2;

var bullet = series.bullets.push(new am4charts.CircleBullet());
bullet.circle.stroke = am4core.color("#fff");
bullet.circle.strokeWidth = 2;

// Finish up setting chart up
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: 250px;
}
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<div id="chartdiv"></div>