gpt4 book ai didi

javascript - 切换 amchart 图形类型,不带调用功能

转载 作者:行者123 更新时间:2023-12-01 01:17:07 25 4
gpt4 key购买 nike

我的函数中有多个图表,它们依赖于前一个图表。工作正常,但当我在其中添加切换按钮并尝试切换时,就会出现问题。函数又需要数据。因此,在不重新初始化图形的情况下,我想切换图形类型,从列到行。有什么办法可以这样做吗?帮助将不胜感激

var dataset = [{
"category": "category 1",
"column-1": 8,
"column-2": 5
},
{
"category": "category 2",
"column-1": 6,
"column-2": 7
},
{
"category": "category 3",
"column-1": 2,
"column-2": 3
}
]
AmCharts.makeChart("chartdiv", {
"type": "serial",
"categoryField": "category",
"startDuration": 1,
"categoryAxis": {
"gridPosition": "start"
},
"trendLines": [],
"graphs": [{
"balloonText": "[[title]] of [[category]]:[[value]]",
"fillAlphas": 1,
"id": "AmGraph-1",
"title": "graph 1",
"type": "column",
"valueField": "column-1"
},
{
"balloonText": "[[title]] of [[category]]:[[value]]",
"fillAlphas": 1,
"id": "AmGraph-2",
"title": "graph 2",
"type": "column",
"valueField": "column-2"
}
],
"guides": [],
"valueAxes": [{
"id": "ValueAxis-1",
"title": "Axis title"
}],
"allLabels": [],
"balloon": {},
"legend": {
"enabled": true,
"useGraphSettings": true
},
"titles": [{
"id": "Title-1",
"size": 15,
"text": "Chart Title"
}],
"dataProvider": dataset
});
<!DOCTYPE html>
<html>

<head>
<title>chart created with amCharts | amCharts</title>
<meta name="description" content="chart created using amCharts live editor" />

<!-- amCharts javascript sources -->
<script type="text/javascript" src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script type="text/javascript" src="https://www.amcharts.com/lib/3/serial.js"></script>


<!-- amCharts javascript code -->
<script type="text/javascript">
</script>
</head>

<body>
<div id="chartdiv" style="width: 100%; height: 400px; background-color: #FFFFFF;"></div>
</body>

</html>

最佳答案

您可以设置图中的任何公共(public)属性并调用 validateData()重新绘制图表,而无需再次提供整个数据集/makeChart 配置。假设您将图表实例存储到变量中:

chart.graphs.forEach(function(graph) {
graph.type = /* set new type */;
});
chart.validateData();

这是一个在单击外部按钮时切换图形类型和其他视觉方面的演示:

var dataset = [{
"category": "category 1",
"column-1": 8,
"column-2": 5
},
{
"category": "category 2",
"column-1": 6,
"column-2": 7
},
{
"category": "category 3",
"column-1": 2,
"column-2": 3
}
]
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"categoryField": "category",
"startDuration": 1,
"categoryAxis": {
"gridPosition": "start"
},
"trendLines": [],
"graphs": [{
"balloonText": "[[title]] of [[category]]:[[value]]",
"fillAlphas": 1,
"id": "AmGraph-1",
"title": "graph 1",
"type": "column",
"valueField": "column-1"
},
{
"balloonText": "[[title]] of [[category]]:[[value]]",
"fillAlphas": 1,
"id": "AmGraph-2",
"title": "graph 2",
"type": "column",
"valueField": "column-2"
}
],
"guides": [],
"valueAxes": [{
"id": "ValueAxis-1",
"title": "Axis title"
}],
"allLabels": [],
"balloon": {},
"legend": {
"enabled": true,
"useGraphSettings": true
},
"titles": [{
"id": "Title-1",
"size": 15,
"text": "Chart Title"
}],
"dataProvider": dataset
});

document.getElementById('toggle-type').addEventListener('click', function() {
chart.graphs.forEach(function(graph) {
if (graph.type == "column") {
graph.type = "line";
graph.fillAlphas = 0;
graph.bullet = "round";
}
else {
graph.type = "column";
graph.fillAlphas = 1;
graph.bullet = undefined;
}
});
chart.validateData();
});
<!DOCTYPE html>
<html>

<head>
<title>chart created with amCharts | amCharts</title>
<meta name="description" content="chart created using amCharts live editor" />

<!-- amCharts javascript sources -->
<script type="text/javascript" src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script type="text/javascript" src="https://www.amcharts.com/lib/3/serial.js"></script>


<!-- amCharts javascript code -->
<script type="text/javascript">
</script>
</head>

<body>
<button id="toggle-type">Toggle line/column</button>
<div id="chartdiv" style="width: 100%; height: 400px; background-color: #FFFFFF;"></div>
</body>

</html>

关于javascript - 切换 amchart 图形类型,不带调用功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54646515/

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