vue中Echarts柱状图渐变

本文介绍了如何在Vue项目中使用Echarts创建柱状图并实现渐变效果。首先提供了Echarts的官方教程链接,然后指导如何通过npm安装Echarts,并在main.js中挂载到Vue原型上。接着,文章会展示具体实现渐变柱状图的代码片段。

echarts的中文文档地址:https://echarts.baidu.com/tutorial.html#5%20%E5%88%86%E9%92%9F%E4%B8%8A%E6%89%8B%20ECharts

先来张效果图:

安装echarts

你可以使用如下命令通过 npm 安装 ECharts

npm install echarts --save

在main.js中加入:

       import echarts from 'echarts'

      Vue.prototype.$echarts = echarts

具体代码

<template>
     <div>
        <div id="myChart" ref="myChart" :style="{width: '800px', height: '300px'}"></div>
        
      </div>
</template>
<script>
import echarts from 'echarts';
  export default {
 
  data () {
          return {
                    
           }
   },
 mounted(){
    this.drawLine();
    
 },
 methods: {
  drawLine(){
    // 初始化echarts实例
    let myChart = this.$echarts.init(this.$refs.myChart)
    // 绘制图表
    myChart.setOption({
      title: { text: 'echarts表格' },
      tooltip: {},
      xAxis: {
        data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
      },
      yAxis: {},
      series: [
        {
            name:'echarts表格',//系列名称,用于tooltip的显示,legend 的图例筛选,在 setOption 更新数据和配置项时用于指定对应的系列。
            type:'bar',
            itemStyle:{//
              normal: {
                    color: new echarts.graphic.LinearGradient(
                        0, 0, 0, 1,
                        [
                            {offset: 0, color: 'rgba(255,255,255,0)'},
                            {offset: 0.5, color: '#44C0C1'},
                            {offset: 1, color: '#188df0'}
                        ]
                    )
                },
            },
            label:{//label要加入normal才可生效,label即为x轴对应Y轴的值
                normal:{
                  show:true,  
                  color:'red',//设置渐变时候控制不到颜色,只能通过全局textStyle来控制
                  position:'top'
                }
            },
            barWidth: '40%',
            data:[10, 52, 200, 334, 390, 330]
        }
      ]
    });
  },
 }
}
</script>

<style>
 

</style>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值