【问题标题】:AmCharts4 Responsive Label SizingAmCharts4 响应式标签尺寸
【发布时间】:2020-11-20 22:46:31
【问题描述】:
【问题讨论】:
标签:
label
responsive
amcharts
font-size
amcharts4
【解决方案1】:
我找到了解决方案。当图表改变大小时,关键是缩放标签。文档https://www.amcharts.com/docs/v4/tutorials/automatically-resize-label-to-fit-donut-inner-radius/ 中提供了一个演示,但这使得字体适合宽度而不是保持一致的大小,因此需要将比例链接到高度而不是宽度。
const chart = am4core.create('chart', am4charts.GaugeChart);
...
/*
* Create Container to hold responsive label
* Height dictates the font size
*/
const container = chart.createChild(am4core.Container);
container.horizontalCenter = 'middle';
container.verticalCenter = 'bottom';
container.height = am4core.percent(20);
container.x = am4core.percent(50);
container.y = am4core.percent(100);
container.isMeasured = false;
/*
* Create the label as a child of container
*/
const label = container.createChild(am4core.Label);
label.horizontalCenter = 'middle';
label.verticalCenter = 'bottom';
label.x = am4core.percent(50);
label.y = am4core.percent(100);
label.fontSize = 30; // irrelevant, can be omitted
label.text = 'Auto sizing text';
/*
* Update the scale of the label on the
* sizechanged event
*/
chart.events.on('sizechanged', () => {
label.scale = container.bbox.height / this.label.bbox.height;
});