gpt4 book ai didi

javascript - 如何制作 3 轴垂直的 amChart 列

转载 作者:行者123 更新时间:2023-11-29 17:39:56 25 4
gpt4 key购买 nike

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

我已经做了水平设计,如下所示的代码,我的问题是......我怎样才能使这个图表垂直?

据我所知(谷歌搜索),amChart 可以通过 rotating the labels 轻松做到这一点。我的问题是......我应该把这个“旋转”:true放在哪里?我不熟悉 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>

最佳答案

您需要将 chart.xAxes 替换为 chart.yAxes,将 chart.yAxes 替换为 chart.xAxes >.

本质上,CategoryAxis需要转到xAxesValueAxis需要转到yAxes

您还需要将 valueX 更改为 valueY,将 categoryY 更改为 categoryX:

series.dataFields.valueY = field;
series.dataFields.categoryX = "year";

下面的示例几乎没有其他变化。我建议您查看更多有关axes in the documentation的信息.

/**
* ---------------------------------------
* 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.xAxes.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.yAxes.push(new am4charts.ValueAxis());
valueAxis.renderer.opposite = true;

// Create series
function createSeries(field, name) {
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = field;
series.dataFields.categoryX = "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.verticalCenter = "bottom";
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.verticalCenter = "top";
categoryLabel.label.dx = -10;
categoryLabel.label.hideOversized = false;
categoryLabel.label.truncate = false;

categoryLabel.label.rotation = -90;
}

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>

关于javascript - 如何制作 3 轴垂直的 amChart 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53730575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com