简体   繁体   English

如何在echarts中自动缩放字体大小?

[英]How to auto scale font size in echarts?

I don't really have a question but just to share my knowledge here.我真的没有问题,只是在这里分享我的知识。

This is a knowledge sharing on how to auto scale font size in echarts.js based on the div size.这是一个关于如何在echarts.js中根据div大小自动缩放字体大小的知识分享。

 let myChart = echarts.init(document.getElementById('myChart')); function autoFontSize() { let width = document.getElementById('myChart').offsetWidth; let newFontSize = Math.round(width / 11); console.log(`Current width : ${width}, Updating Fontsize to ${newFontSize}`); return newFontSize; }; const fruits = [{ value: 100, name: "Apple(s)" }, { value: 200, name: "Banana(s)" } ]; const sum = fruits.reduce(function(prev, current) { return prev + current.value }, 0); let myChartOption = { grid: { left: 0, top: 0, right: 0, bottom: 0 }, tooltip: { trigger: 'item', textStyle: { fontSize: '15', }, formatter: 'You have {c} {b}' }, legend: { orient: 'vertical', x: 'right', y: 'bottom', padding: 0, itemGap: 0, textStyle: { fontSize: '15', }, formatter: function(name) { var targetValue = 0; fruits.forEach(function(c) { if (c.name == name) { targetValue = c.value; } }); return targetValue + ' ' + name; } }, series: [{ name: 'fruits', type: 'pie', center: ['50%', '40%'], radius: ['50%', '60%'], avoidLabelOverlap: false, label: { position: 'center', textStyle: { fontSize: autoFontSize(), fontWeight: 'bold' }, color: 'black', formatter: '' + sum, }, labelLine: { normal: { show: false } }, data: fruits, itemStyle: { emphasis: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' } } }] }; myChart.setOption(myChartOption); $(window).on('resize', function() { if (myChart != null && myChart != undefined) { myChart.resize({ width: 'auto', height: 'auto' }); myChart.setOption({ series: { label: { textStyle: { fontSize: autoFontSize() } } } }) } });
 #myChart { width: 100%; min-height: 330px; margin: 0 auto; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.6.0/echarts.min.js"></script> <div id="myChart"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM