vue-PC数据大屏可视化自适应效果(postcss-pxtorem、lib-flexible、eCharts、dataV)


大屏适配效果预览

1.第三方内容插件

  1. 左侧图表效果 — echarts
  2. 数字跳动效果 – data-V
  3. 中间数字翻滚效果 – 借鉴博客
  4. 中间-底部水波纹图表 – echarts-liquidfill

2.第三方适配插件

1.postcss-pxtorem (自动把项目中的px转为rem)

npm install postcss-pxtorem --save-dev

在vue.config.js配置

module.exports = {
   	...
    css: {//这个是真正解决大屏适配有关的代码
        sourceMap: false,
        loaderOptions: {
            css: {
                // options here will be passed to css-loader
            },
            postcss: {
                // options here will be passed to postcss-loader
                plugins: [require('postcss-pxtorem')({
                    "rootValue": 192, // 设计稿宽度的1/10,(JSON文件中不加注释,此行注释及下行注释均删除)
                    "propList": ["*"] // 需要做转化处理的属性,如`hight`、`width`、`margin`等,`*`表示全部, 如果某个样式不需要转换把px改为PX,Px即可
                })]
            }
        }
    },

}

2.lib-flexible

npm install lib-flexible --save-dev

这个插件只能适配移动端,所以需要修改源码,但每次npm install都会被覆盖,所以安装后找到源码 node_modules/lib-flexible/flexible.js 下载到本地放到资源目录。找到 refreshRem 方法

function refreshRem(){
        var width = docEl.getBoundingClientRect().width;
        // 注释掉也可以,修改适配效果也可以
        // if (width / dpr > 540) {
		//     width = 540 * dpr;
		// }
        var rem = width / 10;
        docEl.style.fontSize = rem + 'px';
        flexible.rem = win.rem = rem;
    }

3.项目中图表适配

完成前两步已经可以自动把项目中的px转为rem,但是代码中的图表是使用js创建,所以需要另行适配,把所有涉及到宽高,字体的地方都乘以缩放比例。 例:

mounted() {
        // this.scale = (document.body.clientWidth<1920?1920:document.body.clientWidth) / 1920;
        this.scale = document.body.clientWidth / 1920;
    },
initServer(sources) {
            const myChart = echarts.init(document.getElementById('server'));
            myChart.clear();
            var option = {
                title: {
                    top: 'center',
                    left: 'center',
                    text: parseInt(sources.applyRate) + "%",
                    textStyle: {
                        color: '#fff',
                        fontStyle: 'normal',
                        fontWeight: 'bold',
                        fontSize: 22 * this.scale,
                    },
                },
                legend: {
                    orient: 'vertical',
                    selectedMode: false,
                    // icon: 'circle', //图例形状
                    data: ["参与率"],
                    top: 8,
                    left: 8,
                    itemWidth: 8, // 图例标记的图形宽度。[ default: 25 ]
                    itemHeight: 8, // 图例标记的图形高度。[ default: 14 ]
                    itemGap: 22, // 图例每项之间的间隔。[ default: 10 ]横向布局时为水平间隔,纵向布局时为纵向间隔。
                    textStyle: {
                        fontSize: 14 * this.scale,
                        // fontWeight: 'bold',
                        color: '#FFFFFF',
                    },
                },
                series: [{
                    type: 'liquidFill',
                    color: [new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                        offset: 0,
                        color: "#148FD9"
                    },
                    {
                        offset: 1,
                        color: '#195682'
                    }
                    ])],
                    name: "参与率",
                    data: [{
                        value: parseInt(sources.applyRate) / 100,
                    }],
                    radius: '80%',
                    center: ['50%', '50%'],
                    backgroundStyle: {
                        color: "rgba(0,0,0,0)",
                    },
                    label: {
                        normal: {
                            formatter: '',
                            textStyle: {
                                fontSize: 12 * this.scale
                            }
                        }
                    },
                    outline: {
                        itemStyle: {
                            borderColor: 'rgba(20, 143, 217, 0.6)',
                            borderWidth: 1
                        },
                        borderDistance: 8
                    }
                },

                ]
            }
            myChart.setOption(option, true);
        },
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
postcss-px-to-viewport是一个优秀的插件,它可以将px单位转换为口单位vw。在项目中使用postcss-px-to-viewport插件的步骤如下: 1. 首先,需要在项目根目录下创建一个.postcssrc.js文件,并添加以下配置: ```javascript module.exports = { plugins: { 'autoprefixer': {}, // 用来给不同的浏览器自动添加相应前缀 'postcss-px-to-viewport': { unitToConvert: "px", // 要转的单位 viewportWidth: 750, // UI设计稿的宽度 unitPrecision: 6, // 转换后的精度,即小数点位数 propList: ["*"], // 指定转换的css属性的单位,*代表全部css属性的单位都进行转换 viewportUnit: "vw", // 指定需要转换成的窗单位,默认vw fontViewportUnit: "vw", // 指定字体需要转换成的窗单位,默认vw selectorBlackList: ["wrap"], // 指定不转换为窗单位的类名 minPixelValue: 1, // 默认值1,小于或等于1px则不进行转换 mediaQuery: true, // 是否在媒体查询的css代码中也进行转换,默认false replace: true, // 是否转换后直接更换属性值 exclude: [/node_modules/], // 设置忽略文件,用正则做目录名匹配 landscape: false // 是否处理横情况 } } }; ``` 2. 然后,需要在项目的package.json文件中添加postcss-px-to-viewport插件的依赖: ```json "devDependencies": { "postcss-px-to-viewport": "^0.1.0" } ``` 3. 最后,重新运行项目,使配置文件生效。在需要转换的样式中,使用px单位进行编写,并在打开控制台后,可以看到已经进行了转换。 这样,postcss-px-to-viewport插件就会自动将样式中的px单位转换为口单位vw,以适配不同尺寸的幕。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [postcss-pxtorem移动端适配的实现](https://download.csdn.net/download/weixin_38743391/14802061)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [移动端布局之postcss-px-to-viewport](https://blog.csdn.net/sinat_17775997/article/details/127101451)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值