如何制作3轴垂直的amChart列

时间:2018-12-11 18:50:30

标签: javascript html css charts amcharts

我想使用带有3轴的amChart创建垂直聚集的柱形图。

我已经像下面的代码一样进行了水平设计,我的问题是...如何使此图表垂直?

据我所知(谷歌搜索),amChart可以通过rotating the labels轻松地做到这一点。我的问题是...我应该在哪里放置这个“旋转”:是的? 我对javascript不熟悉。有人可以帮忙吗?

任何帮助将不胜感激。

/**
 * ---------------------------------------
 * This demo was created using amCharts 4.
 * 
 * For more information visit:
 * https://www.amcharts.com/
 * 
 * Documentation is available at:
 * https://www.amcharts.com/docs/v4/
 * ---------------------------------------
 */

// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end

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

// Add data
chart.data = [{
  "year": 2005,
  "income": 23.5,
  "expenses": 18.1,
  "cumi" : 13
},{
  "year": 2006,
  "income": 26.2,
  "expenses": 22.8,
  "cumi" : 13
},{
  "year": 2007,
  "income": 30.1,
  "expenses": 23.9,
  "cumi" : 13
},{
  "year": 2008,
  "income": 29.5,
  "expenses": 25.1,
  "cumi" : 13
},{
  "year": 2009,
  "income": 24.6,
  "expenses": 25,
  "cumi" : 13
}];

// Create axes
var categoryAxis = chart.yAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "year";
categoryAxis.numberFormatter.numberFormat = "#";
categoryAxis.renderer.inversed = true;
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.cellStartLocation = 0.1;
categoryAxis.renderer.cellEndLocation = 0.9;

var  valueAxis = chart.xAxes.push(new am4charts.ValueAxis()); 
valueAxis.renderer.opposite = true;

// Create series
function createSeries(field, name) {
  var series = chart.series.push(new am4charts.ColumnSeries());
  series.dataFields.valueX = field;
  series.dataFields.categoryY = "year";
  series.name = name;
  series.columns.template.tooltipText = "{name}: [bold]{valueX}[/]";
  series.columns.template.height = am4core.percent(100);
  series.sequencedInterpolation = true;

  var valueLabel = series.bullets.push(new am4charts.LabelBullet());
  valueLabel.label.text = "{valueX}";
  valueLabel.label.horizontalCenter = "left";
  valueLabel.label.dx = 10;
  valueLabel.label.hideOversized = false;
  valueLabel.label.truncate = false;

  var categoryLabel = series.bullets.push(new am4charts.LabelBullet());
  categoryLabel.label.text = "{name}";
  categoryLabel.label.horizontalCenter = "right";
  categoryLabel.label.dx = -10;
  categoryLabel.label.fill = am4core.color("#fff");
  categoryLabel.label.hideOversized = false;
  categoryLabel.label.truncate = false;
}

createSeries("income", "Income");
createSeries("expenses", "Expenses");
createSeries("cumi", "Cumi");
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: 500px;
}
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>

1 个答案:

答案 0 :(得分:4)

您需要将chart.yAxes的{​​{1}}替换为chart.xAxes的{​​{1}}。

基本上chart.xAxes需要转到chart.yAxesCategoryAxis需要转到xAxes

您还需要将ValueAxis的{​​{1}}和yAxes的{​​{1}}更改:

valueX

以下示例中的其他更改很少。我建议您查看有关axes in the documentation的更多信息。

valueY
categoryY
categoryX