uniapp app中使用柱状图 折线图 圆环图和饼图

实现思路

借助echarts.min.js 搭配l-echart进行配置

废话不多说上代码后自己百度了解配置项的意思就好

下面代码是折线图的 ,柱状图和它一摸一样,只需要把line换成bar就好

javascript 复制代码
<template>
  <l-echart ref="chart"></l-echart>
</template>
<script>
import * as echarts from '@/uni_modules/lime-echart/static/echarts.min.js'
export default {
  props: {
		// 以下注释部分是从父组件传递过来的options 这仅仅是一个示例 可以很好的展示ui效果
    options: {
      type: Object,
      default: () => {
        return null
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  watch: {
    options: {
      handler(n) {
        if (n) {
          this.$nextTick(() => {
            this.initChart()
          })
        }
      },
      deep: true
    }
  },
  methods: {
    initChart() {
      const {
        labelX,
        data1,
        data2,
        legends,
        tipLegends,
        legendIcon,
        legendWidth,
        legendColor,
        gridLeft,
        gridRight,
        axisColor,
        splitColor,
        lineColor1,
        lineColor2,
        formatterAfterTextArr
        // labels
      } = this.options

      const series = [
        {
          type: 'line',
          name: (legends && legends[0].name) || tipLegends,
          data: data1,
          symbolSize: 6,
          smooth: true,
          areaStyle: {
            color: 'rgba(' + (lineColor1 || '60, 127, 252') + ', 0.2)'
          },
          lineStyle: {
            color: 'rgba(' + (lineColor1 || '60, 127, 252') + ', 1)'
          },
          itemStyle: {
            color: 'rgba(' + (lineColor1 || '60, 127, 252') + ', 1)'
          }
        }
      ]
      if (data2) {
        series.push({
          type: 'line',
          name: legends && legends[1].name,
          data: data2,
          symbolSize: 6,
          smooth: true,
          areaStyle: {
            color: 'rgba(' + (lineColor2 || '254, 189, 145') + ', 0.2)'
          },
          lineStyle: {
            color: 'rgba(' + (lineColor2 || '254, 189, 145') + ', 1)'
          },
          itemStyle: {
            color: 'rgba(' + (lineColor2 || '254, 189, 145') + ', 1)'
          }
        })
      }
      this.$refs.chart.init(echarts, (chart) => {
        this.chart = chart
        this.chart.setOption({
          animation: false,
          tooltip: {
            trigger: 'axis',
            // 使用mousemove时,左右滑动会导致图表消失
            triggerOn: 'click',
            formatter:
              formatterAfterTextArr && formatterAfterTextArr.length > 0
                ? function (params) {
                    // x轴文字
                    var result = params[0].axisValue + '\n'
                    params.forEach(function (item) {
                      /**
                       * marker:图例样式;
                       * seriesName:series中每一项的name;
                       * value:data数据中value字段
                       * formatterAfterTextArr: value之后需要补充的文字,数组格式
                       * seriesIndex: series数据索引,当有多条series数据时,会自动获取当前选中索引
                       */
                      result += `${item.marker}${item.seriesName}:${item.value}${formatterAfterTextArr[item.seriesIndex]}`
                    })
                    return result
                  }
                : null
          },
          legend: legends && {
						top: '5%', // 控制 板块控制器的位置
						right: 'center',
            data: legends,
            icon: legendIcon || 'roundRect',
            itemWidth: legendWidth || 14,
            itemHeight: 8,
            itemGap: 24, //设置两个legend之间的间距
            textStyle: {
              fontSize: 12,
              color: legendColor || '#69748A',
              padding: [3, 0, 0, 0],
              rich: {}
            }
          },
          grid: {
            left: gridLeft || '5%',
            top: legends ? '20%' : '10%',
            right: gridRight || '8%',
            bottom: '5%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              boundaryGap: false, // 两端空白
              axisTick: {
                show: false
              },
              axisLine: {
                show: false
              },
              axisLabel: {
                color: axisColor || '#333333',
                fontSize: 12,
								margin: 10
              },
              data: labelX || ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
            }
          ],
          yAxis: [
            {
              type: 'value',
              minInterval: 1,
              axisTick: {
                show: false
              },
              axisLabel: {
                color: axisColor || '#333333',
                fontSize: 12,
								margin: 10
              },
              axisLine: {
                show: false
              },
              splitLine: {
                lineStyle: {
                  type: 'dashed',
									dashOffset: 20, // 设置虚线的起始偏移量
									gap: 20, // 设置虚线间的距离
                  color: [splitColor || '#ccc']
                },
              },
              splitArea: { show: false }
            }
          ],
          series: series
        })
      })
    }
  }
}
</script>

饼图和柱状图也基本一样 稍微改改就好,很简单。大家可以自己搜索,我这里放的是四个之中相对复杂的,也是摸索了一整子,下面附上效果图,希望能帮到大家。

相关推荐
不爱说话郭德纲10 小时前
告别漫长的HbuilderX云打包排队!uni-app x 安卓本地打包保姆级教程(附白屏、包体积过大排坑指南)
android·前端·uni-app
ANQH13 小时前
Manim三大核心类详解
数据可视化
HashTang2 天前
【AI 编程实战】第 12 篇:从 0 到 1 的回顾 - 项目总结与 AI 协作心得
前端·uni-app·ai编程
JunjunZ2 天前
uniapp 文件预览:从文件流到多格式预览的完整实现
前端·uni-app
郑州光合科技余经理2 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
B站_计算机毕业设计之家2 天前
电影知识图谱推荐问答系统 | Python Django系统 Neo4j MySQL Echarts 协同过滤 大数据 人工智能 毕业设计源码(建议收藏)✅
人工智能·python·机器学习·django·毕业设计·echarts·知识图谱
TT_Close2 天前
“啪啪啪”三下键盘,极速拉起你的 uni-app 项目!
vue.js·uni-app·前端工程化
特立独行的猫a3 天前
uni-app x跨平台开发实战:开发鸿蒙HarmonyOS影视票房榜组件完整实现过程
华为·uni-app·harmonyos·轮播图·uniapp-x
00后整顿职场3 天前
Hbuilderx APP真机无法识别iqoo Z9+手机设备解决方案
uni-app·uniapp真机调试·真机运行
前端小雪的博客.3 天前
【保姆级教程】uniAI 插件高效开发 uni-app 微信小程序(附实战案例)
微信小程序·uni-app·ai编程·uniai