首页 > 解决方案 > 如何在 v4 中旋转 Am 图表?

问题描述

如何在 v4 中旋转 Am 图表?

我想像 x 轴数据一样将图表轴旋转到 y1 和 y1 轴到图表顶部,例如:

https://c3js.org/samples/axes_rotated.html

标签: amcharts

解决方案


旋转 v4 的图表只需将类别/值轴分配给所需的 xAxes/yAxes 数组,并将轴对象中的oppsiteandinversed属性设置为 true renderer,具体取决于轴。例如:

// place category axis on the y axis 
// use inversed to reverse the order so 
// the first category is on top
var categoryAxis = chart.yAxes.push(new am4charts.CategoryAxis());
// ...
categoryAxis.renderer.inversed = true;

// place value axis on the x axis and use the opposite property to move it up top
var valueAxis = chart.xAxes.push(new am4charts.ValueAxis());
// ...
valueAxis.renderer.opposite = true;

演示:

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

// Add data
chart.data = [{
  "country": "USA",
  "visits": 3025
}, {
  "country": "China",
  "visits": 1882
}, {
  "country": "Japan",
  "visits": 1809
}, {
  "country": "Germany",
  "visits": 1322
}, {
  "country": "UK",
  "visits": 1122
}, {
  "country": "France",
  "visits": 1114
}, {
  "country": "India",
  "visits": 984
}, {
  "country": "Spain",
  "visits": 711
}, {
  "country": "Netherlands",
  "visits": 665
}, {
  "country": "Russia",
  "visits": 580
}, {
  "country": "South Korea",
  "visits": 443
}, {
  "country": "Canada",
  "visits": 441
}];

// place category axis on the y axis 
// use inversed to reverse the order so 
// the first category is on top
var categoryAxis = chart.yAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "country";
categoryAxis.renderer.inversed = true;

// place value axis on the x axis and use the opposite property to move it up top
var valueAxis = chart.xAxes.push(new am4charts.ValueAxis());
valueAxis.renderer.opposite = true;

// Create series
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueX = "visits";
series.dataFields.categoryY = "country";
series.tooltipText = "[{categoryX}: bold]{valueY}[/]";
#chartdiv {
  width: 95%;
  height: 300px;
}
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<div id="chartdiv"></div>


推荐阅读