echarts 柱状图、雷达图、饼图的一些基础

本文详细介绍了如何在ECharts中创建柱状图、雷达图和饼图,包括各自的效果图展示,以及关键代码片段。柱状图展示了颜色自定义和渐变色应用,雷达图则关注了指示器配置和样式设置,饼图通过点击事件演示参数获取。


柱状图

效果图
在这里插入图片描述

代码如下:

series: {
    type: 'bar',
    barWidth: 15,
    data: yData,
    itemStyle: {
    	normal: {
            //这是重点;colors自己定义的颜色数组
       		color(params) {
          		var colorList = colors;
          		return colorList[params.dataIndex]
       		}
    	}
 	}
// 这是渐变色的
itemStyle: {
                normal: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 0.7, [{
                            offset: 0,
                            color: "#38B2E6"
                        },
                        {
                            offset: 1,
                            color: "#ddd"
                        }
                    ]),
                    opacity: .8
                },
            },
}

雷达图

效果图
在这里插入图片描述

代码如下:

radar: {
          radius: '60%',			// 这是图的大小
          center: ['50%', '45%'],	// 这是图的位置
          indicator: [				// 这是雷达图的指示器,用来指定雷达图中的多个变量
            {name: '一', max: 50},
            {name: '二', max: 50},
            {name: '三', max: 50},
            {name: '四', max: 50},
            {name: '五', max: 50},
            {name: '六', max: 50}
          ],
          shape: 'circle',		// 这是环的样式,可以是圆,可以是菱形
          splitNumber: 5,		// 这是环数,白色的圈
          name: {
            textStyle: {
              color: '#0EE3AB'
            }
          },
          nameGap: 7,
          splitLine: {
            lineStyle: {
              color: '#ffffffaa'
            }
          },
          splitArea: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: 'red'
            }
          }
        },
        series: {
          type: 'radar',
          data: yData,
          areaStyle: {
            opacity: 0.3 
          },
          lineStyle: {
            width: 0.5
          },
          symbol: 'rect', 
        }

饼图

点击之后返回的参数:
在这里插入图片描述

代码如下:


//点击动作
 myChart.setOption(option);
   myChart.on("click", eConsole);
   myChart.on("hover", eConsole);

function eConsole(param) {
      let self = this
      console.log(param)  
      // 点击返回的参数(具体在上面)
      if (typeof param.seriesIndex != 'undefined') {
        if (param.type == 'click' && Object.keys(param.data).length > 0) {
          dialog.visible = true
        }
      }
    },



### 制作柱状图 1. **引入 ECharts 文件** ```html <script src="xxx/echarts/echarts.common.min.js" charset="UTF-8"></script> ``` 2. **HTML 中创建 DOM 容器** ```html <div id="barChart" style="width: 600px;height:400px;"></div> ``` 3. **JavaScript 代码配置并渲染表** ```javascript // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('barChart')); // 指定表的配置项和数据 var option = { xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, yAxis: { type: 'value' }, series: [{ data: [120, 200, 150, 80, 70, 110, 130], type: 'bar' }] }; // 使用刚指定的配置项和数据显示表。 myChart.setOption(option); ``` 还可以对柱状图不同的柱子设置不同的渐变色,示例代码如下: ```javascript var option = { series : [ { name:'正在进行', type:'bar', stack: '状态', data: [120, 200, 150, 80, 70, 110, 130], itemStyle: { normal: { color: function (params) { var colorList = [ ['#01b0ff', '#033dff'], ['#32fe99', '#01938f'], ['#2985b1', '#6644fb'] ]; var index = params.dataIndex; if (params.dataIndex >= colorList.length) { index = params.dataIndex - colorList.length; } return new echarts.graphic.LinearGradient(0, 0, 0, 1, [ { offset: 0, color: colorList[index][0] }, { offset: 1, color: colorList[index][1] } ]); } } }, barWidth: 100 } ] }; ``` ### 制作 1. **引入 ECharts 文件**,同柱状图的引入方式。 2. **HTML 中创建 DOM 容器** ```html <div id="pieChart" style="width: 600px;height:400px;"></div> ``` 3. **JavaScript 代码配置并渲染表** ```javascript // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('pieChart')); // 指定表的配置项和数据 var option = { series: [ { name: '访问来源', type: 'pie', radius: '50%', data: [ { value: 1048, name: '搜索引擎' }, { value: 735, name: '直接访问' }, { value: 580, name: '邮件营销' }, { value: 484, name: '联盟广告' }, { value: 300, name: '视频广告' } ] } ] }; // 使用刚指定的配置项和数据显示表。 myChart.setOption(option); ``` ### 制作雷达 1. **引入 ECharts 文件** ```html <script src="xxx/echarts/echarts.common.min.js" charset="UTF-8"></script> <script src="xxx/echarts/echarts.js"></script> <script src="xxx/echarts/shine.js"></script> ``` 2. **HTML 中创建 DOM 容器** ```html <div id="radarChart" style="width: 600px;height:400px;"></div> ``` 3. **JavaScript 代码配置并渲染表** ```javascript // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('radarChart')); // 指定表的配置项和数据 var yData = [ { value: [4300, 10000, 28000, 35000, 50000, 19000], name: '预算分配(Allocated Budget)' }, { value: [5000, 14000, 28000, 31000, 42000, 21000], name: '实际开销(Actual Spending)' } ]; var option = { radar: { radius: '60%', center: ['50%', '45%'], indicator: [ { name: '一', max: 50000 }, { name: '二', max: 50000 }, { name: '三', max: 50000 }, { name: '四', max: 50000 }, { name: '五', max: 50000 }, { name: '六', max: 50000 } ], shape: 'circle', splitNumber: 5, name: { textStyle: { color: '#0EE3AB' } }, nameGap: 7, splitLine: { lineStyle: { color: '#ffffffaa' } }, splitArea: { show: false }, axisLine: { lineStyle: { color: 'red' } } }, series: [ { type: 'radar', data: yData, areaStyle: { opacity: 0.3 }, lineStyle: { width: 0.5 }, symbol: 'rect' } ] }; // 使用刚指定的配置项和数据显示表。 myChart.setOption(option); ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值