)
定义计算比例函数
function fontSize(res){
let clientWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
if (!clientWidth) return;
let fontSize = 100 * (clientWidth / 1920);
return res * fontSize;
}
然后需要在图表中去使用它
//设置图表标题
title: {
text: '折线图堆叠1233标题',
textStyle:{
fontSize:fontSize(0.28)//28px就写0.28
}
},
然后当窗口大小变化是触发这个函数
mounted(){
this.init();
window.addEventListener("resize",()=>{
this.init();
})
}
全部代码
export default {
methods:{
init(){
function fontSize(res){
let clientWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
if (!clientWidth) return;
let fontSize = 100 * (clientWidth / 1920);
return res * fontSize;
};
var myChart = echarts.init(document.getElementById('main'));
const option = {
//设置线条的颜色,后面是个数组
color:['pink','red','green','blue','gray'],
//设置图表标题
title: {
text: '折线图堆叠1233标题',
textStyle:{
fontSize:fontSize(0.28)
}
},
//图表的提示框组件
tooltip: {
//触发方式 - 坐标轴
trigger: 'axis'
},
//图例组件
legend: {
//series有name了,这里的data可以删除掉
data: ['邮件营销', '联盟广告', '视频广告', '直接访问', '搜索引擎']
},
//网格配置 grid可以控制线形图 柱状图 图标大小
grid: {
left: '3%',
right: '4%',
bottom: '3%',
//是否显示刻度标签
containLabel: true
},
//工具箱组件,可以另存为图片等功能
toolbox: {
feature: {
saveAsImage: {}
}
},
//设置x轴的相关配置
xAxis: {
type: 'category',
//线条和y轴是否有缝隙
boundaryGap: false,
data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
},
//设置y轴的相关配置
yAxis: {
type: 'value'
},
//系列图表配置,决定显示那种类型的图表
series: [
{
name: '邮件营销',
type: 'line',
//总量,后面的会堆叠前面的累加起来,删除掉就会折叠了,一般不需要
//stack: '总量',
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: '联盟广告',
type: 'line',
//stack: '总量',
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: '视频广告',
type: 'line',
//stack: '总量',
data: [150, 232, 201, 154, 190, 330, 410]
},
{
name: '直接访问',
type: 'line',
//stack: '总量',
data: [320, 332, 301, 334, 390, 330, 320]
},
{
name: '搜索引擎',
type: 'line',
//stack: '总量',
data: [520, 932, 901, 934, 1290, 1330, 1320]
}
]
};
myChart.setOption(option);
// 当我们浏览器缩放的时候,图表也等比例缩放
window.addEventListener("resize", function() {
// 让我们的图表调用 resize这个方法
myChart.resize();
});
}
},
mounted(){
this.init();
window.addEventListener("resize",()=>{
this.init();
})
}
}