IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> 周末在家学习echarts -> 正文阅读

[JavaScript知识库]周末在家学习echarts

周末在家没事,做了两个数据可视化的echarts图表,学到了很多有关echarts的知识点。现在总结一下,供各位同学一起学习,成长。

需求

图表的练习
在这里插入图片描述

图1-1

以上为土1-2
以上为图1-2

效果图

图2-1
以上为 图2-1

图2-2
图2-2

思路

分析需求图1-1

首先是需求图1-1. 是一个典型的双y轴,柱形图加折线图。可以使用这个案例来改造。
折柱混合
双y轴的话 主要是使用下面代码配置

yAxis: [
    {
        type: 'value',
        name: '水量',
        min: 0,
        max: 250,
        interval: 50,
        axisLabel: {
            formatter: '{value} ml'
        }
    },
    {
        type: 'value',
        name: '温度',
        min: 0,
        max: 25,
        interval: 5,
        axisLabel: {
            formatter: '{value} °C'
        }
    }
],

对于图表中一些自定义的元素,如,右上角的二个元素,如下图3-1
在这里插入图片描述
这两个元素可以使用 echarts中的 graphicelements 属性。
如下图 3-2时对 graphic 属性的简短解释。可以使用这个属性将一些图片,文本,圆,线,多面图,矩形,线,元素群组,添加到图表的指定位置。
在这里插入图片描述
此外由于该图表有二个产品,一个全部。所以使用的是dataset的数据源来存储图表的数据。

如下是全部产品的数据

const dataseSource = [
      ['product', '已交付', '累计百分比'],
      ['批次1', 850, 21],
      ['批次2', 1500, 36],
      ['批次3', 1800, 43],
      ['批次4', 2700, 65],
      ['批次5', 2600, 72],
      ['批次6', 2700, 85],
      ['下一批次', 3220, 80],
    ]

点击产品D,或产品E ,只要修改数据源,重新设置图表options就可以了。

此外还有一点,就是折线图的label属性是个动态值,大于50,字体变红,并增加一个警告图标。
此方案是这样实现的,主要是用 label中的rich 属性与 formatter属性.
rich 用于设置样式,就像为css中的class增加样式。
而 formatter 则可为不同的折线点,设置不用的样式。使用这种写法 {b| value} b为样式的名称,value为要显示的文本,或值。

{
  name: '累计百分比',
  type: 'line',
  label: {
      show: true,
      distance: 5,
      rich:{
          red:{color:'red',fontSize:14},
          b: {
              backgroundColor: {
                image: 'https://img-blog.csdnimg.cn/2021070312161116.png'
              },
              height: 30,
            },
      },
      formatter: function(params) {
          const { data } = params;
          const value = data[2]
        if (value > 50) {
          return "{red|" + value + "%}{b| }"
        }
        return value+"%"
      }
  },
  yAxisIndex: 1,
}

分析需求图1-2

需求图1-2 难点是在贴图的处理上。
由于是一个类目累加的柱状图,又称堆叠柱状图。因此可以基于该图例改造实现。
堆叠柱状图
堆叠柱状体 主要是在 series 中设置该属stack: 'total', 及可将同样设置stack: 'total' 的series 。堆积起来。
此外,该图例需要开始 贴图。不开启,设置了贴图是不会显示的。

aria: {
    enabled: true,
    decal: {
        show: true,
    }
}

设置蓝色的 已用产能 贴图。

itemStyle:{
    decal:{
        symbol: 'rect',
        symbolSize: 1,
        maxTileWidth:512,
        maxTileHeight:512,
        symbolKeepAspect: true,
        dashArrayX: [1,0],
        dashArrayY: [2,2],
        color: '#5470c6',
        // rotation: 0.5235987755982988
    }
}

有关 属性decal 的更多解释 请看。
decal详细解释
在这里插入图片描述

代码

图1-1的核心代码

const allData = []
//[21,36,43,65,72,85,92]
// [850,1500,1800,2700,2600,2700,3220]
const proD = []
const proE = []
const dataseSource = [
      ['product', '已交付', '累计百分比'],
      ['批次1', 850, 21],
      ['批次2', 1500, 36],
      ['批次3', 1800, 43],
      ['批次4', 2700, 65],
      ['批次5', 2600, 72],
      ['批次6', 2700, 85],
      ['下一批次', 3220, 80],
    ]

option = {
    color: ['#53f3f3', '#fff85f', '#EE6666'],
    textStyle: {color: '#fff'},
    backgroundColor: '#020403',//背景色
    dataset: {
      source: dataseSource,
    },
    graphic: {
      elements: [
        {
          type: 'rect',
          x: 120,
          y: 10,
          shape: {
            width: 150,
            height: 50
          },
          style: {
            fill: 'rgba(255,255,255,.6)'
          }
        },
        {
          type: 'rect',
          x: 320,
          y: 10,
          shape: {
            width: 150,
            height: 50
          },
          style: {
            fill: 'rgba(255,255,255,.5)'
          }
        },
        {
          type: 'image',
          style: {
            image: 'data:i yf8A/9k=',
            x: 325,
            y: 15,
            width: 40,
            height: 40
          },
          onclick:function(data){
            console.log(data)
          }
        },
        {
          type: 'text',
          x: 180,
          y: 20,
          style: {
            fill: '#000',
            text: '9999',
            font: 'bold 34px Microsoft YaHei',
          },
          onclick:function(data){
            console.log(data)
            console.log( arguments)
            const { target} = data;
            target.style.text = Math.floor(Math.random()*1000)+"";
          }
        },
        {
          type: 'text',
          x: 380,
          y: 20,
          style: {
            fill: '#000',
            text: '12H',
            font: 'bold 34px Microsoft YaHei',
          }
        },
        {
          type: 'image',
          style: {
            image: 'data:image/png; /9k=',
            x: 125,
            y: 15,
            width: 40,
            height: 40
          }
        },
        
        {
            type: 'group',
            bottom: 20,
            children: [
                {type: 'image',style:{image:'dat A/9k'} },
                {type: 'text', left:10, style: {
                    fill: '#fff85f',
                text: '全部',
                font: 'bold 34px Microsoft YaHei',}}
            ],
            onclick:function(data){
            console.log(data)
          }
         
        },
        {
            type: 'group',
            bottom: 20,
            left: 120,
            children: [
                {type: 'image',style:{image:'d  9k'} },
                {type: 'text', style: {
                    fill: '#fff',
            text: '产品D',
            font: 'bold 34px Microsoft YaHei',}}
            ],
            onclick:function(data){
            console.log(data)
          }
         
        },
        {
            type: 'group',
            bottom: 20,
            left: 240,
            children: [
                {type: 'image',style:{image:' 9k'} },
                {type: 'text', style: {
                    fill: '#fff',
            text: '产品E',
            font: 'bold 34px Microsoft YaHei',}}
            ],
            onclick:function(data){
            console.log(data)
          }
         
        },
        
      ]
    },
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'cross',
            crossStyle: {
                color: '#999'
            }
        }
    },
    legend: {
        bottom: 20,
        right:20,
        textStyle:{color:'#fff'},
        data: [
            {name: '已交付',icon: 'circle'},  
            {name:'累计百分比',icon:'circle'}]
    },
    xAxis: [
        {
            type: 'category',
            axisPointer: {
                type: 'shadow'
            }
        }
    ],
    yAxis: [
        {
            type: 'value',
            show:true,
            name: '件',
            min: 0,
            max: 5000,
            axisLine:{
                show: true,
                lineStyle:{
                    color: '#02180b'
                }
            },
            splitLine: {
                lineStyle: {
                    type: 'dashed',
                    color: '#02180b',
                }
            }
        },
        {
            type: 'value',
            name: '',
            min: 0,
            max: 100,
            axisLabel: {
                formatter: '{value}.00 %'
            },
            axisLine:{
                show: true,
                lineStyle:{
                    color: '#02180b'
                }
            },
            splitLine: {
                lineStyle: {
                    type: 'dashed',
                    color: '#02180b',
                }
            }
        }
    ],
    series: [
        {
            name: '已交付',
            type: 'bar',
            barWidth: 20,
            label: {
                show: true,
                position: 'top',
                valueAnimation: true,
            }
        },
        {
            name: '累计百分比',
            type: 'line',
            label: {
                show: true,
                distance: 5,
                rich:{
                    red:{color:'red',fontSize:14},
                    b: {
                        backgroundColor: {
                          image: 'https://img-blog.csdnimg.cn/2021070312161116.png'
                        },
                        height: 30,
                      },
                },
                formatter: function(params) {
                    const { data } = params;
                    const value = data[2]
                  if (value > 50) {
                    return "{red|" + value + "%}{b| }"
                  }
                  return value+"%"
                }
            },
            yAxisIndex: 1,
        }
    ]
};

图1-2的核心代码

option = {
    // color: ['#5470c6', '#fac858'],
    color: ['#fff', '#fff'],
    legend: {
        data: ['已用产能', '剩余产能']
    },
    xAxis: {
        type: 'category',
        data: ["制芯","组芯","熔炼","浇注","冷却","落砂","切割","抛丸","打磨","检测"]
    },
    yAxis: {
        type: 'value',
        axisLabel:{
          formatter: '{value}%'
        }
    },
    series: [
        {
            name: '已用产能',
            type: 'bar',
            stack: 'total',
            label: {
                show: true,
                formatter:'{c}%'
            },
            emphasis: {
                focus: 'series'
            },
            data: [76,54,62,59,67,81,84,92,96,83],
            itemStyle:{
                ....
            }
        },
        {
            name: '剩余产能',
            type: 'bar',
            stack: 'total',
            label: {
                show: true,
                formatter:'{c}%'
            },
            emphasis: {
                focus: 'series'
            },
            data: [24,46,38,41,33,19,16,8,4,17],
            itemStyle:{
                decal:{
                   ....
                }
            }
        },
    ],
    aria: {
        enabled: true,
        decal: {
            show: true,
        }
    }
};

将代码复制到 echarts的编辑器里就可以实时预览效果了。
https://echarts.apache.org/examples/zh/editor.html?c=line-simple

后续的我会给大家详细讲解echarts中的贴花。 这是一个非常具有表现力,并且可以玩出花的特性。
大家敬请期待。

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2021-07-09 17:30:43  更:2021-07-09 17:31:03 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/8 22:51:29-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码