【echarts图例模糊】解决

背景

目前的需求中遇到饼图,需要自适应 1920*1080 屏幕的 100%、125%、150%的笔记本屏幕。过程中发现图例比较模糊(公司电脑配置较低)。 追求完美的领导和测试要求不要有毛边和模糊,要高清。
以上图一

以上图二

图一和图二对比看,就发现确认是有模糊。

解决方案

方案一,写媒体查询设置不同高度的容器下,重新设置字体

源码如下:

vue 复制代码
     let myChart = echarts.init(chartDom)
    const option = {
      legend: {
        show: true,
        bottom: -5,
        left: 'center',
        padding: [5, 15],
        itemWidth: 12,
        itemHeight: 12,
        selectedMode: false,
        textStyle: {
          fontFamily: 'SourceHanSansCN-Regular',
          fontWeight: 400,
          fontSize: '0.845rem',
          color: '#092649'
        }
      },
      color: pieChartsInfo.value.legendColor,
      series: [
        {
          type: 'pie',
          radius: ['50%', '58%'],
          center: ['49%', '40%'],
          startAngle: 90,
          clockwise: true,
          label: {
            show: false
          },
          labelLine: {
            show: false
          },
          data: pieChartsInfo.value.data,
          emphasis: {
            disabled: true // 关闭鼠标 hover 效果
          },
          animation: true
        }
      ],
      graphic: [
        {
          type: 'text',
          left: 'center',
          top: '35%',
          style: {
            text: pieCleanValue.value,
            fill: '#092649',
            fontSize: calcFontSize(32),
            fontWeight: 'bold',
            fontFamily: 'D-DIN-PRO-Bold'
          }
        }
      ],
      media: [
        {
          query: {
            minHeight: 200 // 笔记本100%
          },
          option: {
            legend: {
              textStyle: {
                fontSize: 14
              }
            }
          }
        },
        {
          query: {
            minHeight: 300 // 笔记本125%
          },
          option: {
            legend: {
              textStyle: {
                fontSize: 16
              }
            }
          }
        }
      ]
    }

方案二, 设置echarts初始化参数 render 或 devicePixelRatio

vue 复制代码
let myChart = echarts.init(chartDom, null, { render: 'svg' })
// let myChart = echarts.init(chartDom, null, { devicePixelRatio: '2.5' })

方案三 直接设置fontSize为rem参数

同时也需要考虑不同缩放比,去适配图例对齐。

vue 复制代码
 legend: {
        show: true,
        bottom: -5,
        left: 'center',
        padding: [5, 15],
        itemWidth: 12,
        itemHeight: 12,
        selectedMode: false,
        textStyle: {
          fontFamily: 'SourceHanSansCN-Regular',
          fontWeight: 400,
          fontSize: '0.845rem',
          color: '#092649'
        }
      },

完整源码

vue 复制代码
function getSinglePieChart() {
   let chartDom = document.getElementById('pieSingleCharts')
   if (!chartDom) {
     return
   }

   let myChart = echarts.init(chartDom, null, { render: 'svg' })
   // let myChart = echarts.init(chartDom, null, { devicePixelRatio: '2.5' })
   const option = {
     legend: {
       show: true,
       bottom: -5,
       left: 'left',
       padding: [5, 15],
       itemWidth: 12,
       itemHeight: 12,
       data: [
         '洁净(≥90)                     ',
         '轻度污染(80≤x<90)',
         '中度污染(60≤x<80)',
         '重度污染(<60)'
       ],
       selectedMode: false,
       textStyle: {
         fontFamily: 'SourceHanSansCN-Regular',
         fontWeight: 400,
         fontSize: '0.845rem',
         color: '#092649'
       }
     },
     color: pieChartsInfo.value.legendColor,
     series: [
       {
         type: 'pie',
         radius: ['50%', '58%'],
         center: ['49%', '40%'],
         startAngle: 90,
         clockwise: true,
         label: {
           show: false
         },
         labelLine: {
           show: false
         },
         data: pieChartsInfo.value.data,
         emphasis: {
           disabled: true // 关闭鼠标 hover 效果
         },
         animation: true
       }
     ],
     graphic: [
       {
         type: 'text',
         left: 'center',
         top: '35%',
         style: {
           text: pieCleanValue.value,
           fill: '#092649',
           fontSize: calcFontSize(32),
           fontWeight: 'bold',
           fontFamily: 'D-DIN-PRO-Bold'
         }
       }
     ],
     media: [
       // {
       //   query: {
       //     minHeight: 254 // 笔记本100%
       //   },
       //   option: {
       //     legend: {
       //       padding: [5, 10],
       //       textStyle: {
       //         fontSize: '0.875rem'
       //       }
       //     }
       //   }
       // }
       // {
       //   query: {
       //     minHeight: 204 // 笔记本125%
       //   },
       //   option: {
       //     legend: {
       //       padding: [5, 20],
       //       textStyle: {
       //         fontSize: '0.875rem'
       //       }
       //     }
       //   }
       // }
       // {
       //   query: {
       //     minHeight: 170 // 笔记本150%
       //   },
       //   option: {
       //     legend: {
       //       bottom: 4,
       //       padding: [0, -5, 0, 0],
       //       textStyle: {
       //         fontSize: '0.875rem'
       //       },
       //       data: [
       //         '洁净(≥90)                     ',
       //         '轻度污染(80≤x<90)',
       //         '中度污染(60≤x<80)',
       //         '重度污染(<60)'
       //       ]
       //     }
       //   }
       // }
     ]
   }

   myChart.clear()
   option && myChart.setOption(option)

   window.addEventListener('resize', () => {
     myChart.resize()
   })
 }

经验分享,感谢查阅,望指正。

相关推荐
前端大卫30 分钟前
Vue3 + Element-Plus 自定义虚拟表格滚动实现方案【附源码】
前端
却尘1 小时前
Next.js 请求最佳实践 - vercel 2026一月发布指南
前端·react.js·next.js
ccnocare1 小时前
浅浅看一下设计模式
前端
Lee川1 小时前
🎬 从标签到屏幕:揭秘现代网页构建与适配之道
前端·面试
Ticnix1 小时前
ECharts初始化、销毁、resize 适配组件封装(含完整封装代码)
前端·echarts
纯爱掌门人1 小时前
终焉轮回里,藏着 AI 与人类的答案
前端·人工智能·aigc
twl1 小时前
OpenClaw 深度技术解析
前端
崔庆才丨静觅1 小时前
比官方便宜一半以上!Grok API 申请及使用
前端
星光不问赶路人2 小时前
vue3使用jsx语法详解
前端·vue.js
天蓝色的鱼鱼2 小时前
shadcn/ui,给你一个真正可控的UI组件库
前端