typescript - AmCharts 4 中的可滚动外部图表图例
问题描述
如何在 AmCharts 4 中实现与此 AmCharts 3 教程相同的效果,即有一个可滚动的图例以避免图例占用所有可用空间?
在 AmCharts 4.legend.divId
中不再存在,到目前为止我只能用 来控制图例大小chart.legend.height = am4core.percent(50);
,控制是有限的,因为整个东西被渲染为单个 SVG 元素......
我想需要创建一个单独的容器legend.parent = container;
,然后通过设置“附加”图例,但还不能使其工作。
当前尝试:codepen
解决方案
var legendContainer = am4core.create("legenddiv", am4core.Container);
legendContainer.width = am4core.percent(100);
legendContainer.height = am4core.percent(100);
chart.legend.parent = legendContainer;
请在此处查看以下示例和更多信息。
/**
* --------------------------------------------------------
* This demo was created using amCharts V4 preview release.
*
* V4 is the latest installement in amCharts data viz
* library family, to be released in the first half of
* 2018.
*
* For more information and documentation visit:
* https://www.amcharts.com/docs/v4/
* --------------------------------------------------------
*/
/* Set theme(s) */
am4core.useTheme(am4themes_animated);
/* Create chart */
var chart = am4core.create("chartdiv", am4charts.PieChart);
/* Add data */
chart.data = [{
"country": "Lithuania",
"litres": 501.9
}, {
"country": "Czech Republic",
"litres": 301.9
}, {
"country": "Ireland",
"litres": 201.1
}, {
"country": "Germany",
"litres": 165.8
}, {
"country": "Australia",
"litres": 139.9
}, {
"country": "Austria",
"litres": 128.3
}, {
"country": "UK",
"litres": 99
}, {
"country": "Belgium",
"litres": 60
}, {
"country": "The Netherlands",
"litres": 50
}, {
"country": "Hungary",
"litres": 50
}, {
"country": "Poland",
"litres": 50
}, {
"country": "Greece",
"litres": 50
}, {
"country": "Italy",
"litres": 50
}, {
"country": "France",
"litres": 50
}, {
"country": "Spain",
"litres": 50
}];
/* Create series */
var series = chart.series.push(new am4charts.PieSeries());
series.dataFields.value = "litres";
series.dataFields.category = "country";
/* Disable labels */
series.labels.template.disabled = true;
series.ticks.template.disabled = true;
/* Create legend */
chart.legend = new am4charts.Legend();
/* Create a separate container to put legend in */
var legendContainer = am4core.create("legenddiv", am4core.Container);
legendContainer.width = am4core.percent(100);
legendContainer.height = am4core.percent(100);
chart.legend.parent = legendContainer;
chart.events.on("datavalidated", resizeLegend);
chart.events.on("maxsizechanged", resizeLegend);
function resizeLegend(ev) {
document.getElementById("legenddiv").style.height = chart.legend.contentHeight + "px";
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
#chartdiv, #legendwrapper {
width: 48%;
height: 200px;
border: 1px dotted #c99;
margin: 1em 0;
display: inline-block;
float: left;
}
#legenddiv {
height: 100%;
}
#legendwrapper {
overflow-x: none;
overflow-y: auto;
}
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<script src="//www.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>
<div id="legendwrapper">
<div id="legenddiv"></div>
<div>
推荐阅读
- pointers - 如何取消引用 Inno Setup Pascal 脚本中的指针?
- vue.js - Vue JS Jest:mount() 无法正确渲染
- reactjs - 反应:如何观察我的 UseRef 变量的属性变化(UseEffect 不起作用)
- javascript - 我无法理解为什么我无法对我在 React 程序中设置的状态返回的数据进行 .map 映射
- excel - 子或函数未定义excel vba错误
- python - 是否可以将选择字段选择映射到 django 中的外键
- r - 在同一图中绘制正态分布和二项式分布
- c - Win api上ListView中的列过滤器
- reactjs - 如果服务器返回错误,如何取消选中开关?
- mysql - 如何删除名称为`的表