【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()
   })
 }

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

相关推荐
冬夜戏雪2 分钟前
实习面经记录(十)
java·前端·javascript
爱学习的程序媛1 小时前
【Web前端】JavaScript设计模式全解析
前端·javascript·设计模式·web
小码哥_常2 小时前
从SharedPreferences到DataStore:Android存储进化之路
前端
老黑2 小时前
开源工具 AIDA:给 AI 辅助开发加一个数据采集层,让 AI 从错误中自动学习(Glama 3A 认证)
前端·react.js·ai·nodejs·cursor·vibe coding·claude code
jessecyj2 小时前
Spring boot整合quartz方法
java·前端·spring boot
苦瓜小生2 小时前
【前端】|【js手撕】经典高频面试题:手写实现function.call、apply、bind
java·前端·javascript
天若有情6732 小时前
前端HTML精讲03:页面性能优化+懒加载,搞定首屏加速
前端·性能优化·html
踩着两条虫2 小时前
AI驱动的Vue3应用开发平台深入探究(十):物料系统之内置组件库
android·前端·vue.js·人工智能·低代码·系统架构·rxjava
swipe3 小时前
AI 应用里的 Memory,不是“保存聊天记录”,而是管理上下文预算
前端·llm·agent
慧一居士3 小时前
nuxt3 项目和nuxt4 项目区别和对比
前端·vue.js