echarts常见图形-横向排行榜柱状图(一)

6 篇文章 0 订阅
6 篇文章 0 订阅

1.最终效果图

![效果图](https://img-blog.csdnimg.cn/20201221160344756.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMTY3NzQ4,size_16,colo

2.下载安装echarts(main.js)

import echarts from "echarts";
Vue.prototype.$echarts = echarts;

3.echarts 文件(njyyfx.js)

function compareSort(property) {
  return function(a, b) {
    let value1 = a[property];
    let value2 = b[property];
    return value2 - value1;
  };
}

export let njyyfxEcharts = dataArr => {
  // let dataArr = [
  //   {
  //     name: "南京市",
  //     value: 52
  //   },
  //   {
  //     name: "苏州市",
  //     value: 152
  //   },
  //   {
  //     name: "无锡市",
  //     value: 252
  //   },
  //   {
  //     name: "常州市",
  //     value: 352
  //   },
  //   {
  //     name: "镇江市",
  //     value: 452
  //   }
  // ];
  let dfColor = [
    {
      type: "linear",
      x: 0,
      y: 0,
      x2: 1,
      y2: 0,
      colorStops: [
        {
          offset: 1,
          color: "#0f48aa" // 0% 处的颜色
        },
        {
          offset: 0,
          color: "#09345c" // 100% 处的颜色
        }
      ],
      global: false // 缺省为 false
    },
    {
      colorEnd: "#3a7bff"
    }
  ];
  let yData = [];
  let arr = [];
  let arr2 = [];
  dataArr.map(item => {
    yData.push(item.name);
    arr.push({
      value: item.value,
      itemStyle: {
        color: dfColor
      }
    });
    arr2.push({
      value: item.value,
      itemStyle: {
        color: dfColor[1].colorEnd
      }
    });
  });
  let sData1 = arr.sort(compareSort("value"));
  let sData2 = arr2.sort(compareSort("value"));

  let option = {
    tooltip: {
      trigger: "axis",
      show: true,
      axisPointer: {
        type: "shadow"
      },
      formatter: function(objs) {
        let obj = objs[0];
        return `${obj.name}<br/>${obj.marker}${obj.seriesName} : ${obj.value} 个`;
      }
    },
    grid: {
      top: "3%",
      left: "8%",
      right: "10%",
      bottom: "1%",
      containLabel: true
    },
    xAxis: {
      type: "value",
      axisTick: {
        show: false
      },
      axisLine: {
        show: false
      },
      axisLabel: {
        show: false
      },
      splitLine: {
        show: false
      }
    },
    yAxis: {
      type: "category",
      boundaryGap: true,
      inverse: true, //反向
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        margin: 0,
        formatter: function(value, index) {
          let ind = index + 1;
          if (ind == 1) {
            return "{one|" + ind + "} {a|" + value + "}";
          } else if (ind == 2) {
            return "{two|" + ind + "} {b|" + value + "}";
          } else if (ind == 3) {
            return "{three|" + ind + "} {c|" + value + "}";
          }
          return "{four|" + ind + "} {d|" + value + "}";
        },
        rich: {
          a: {
            color: "#59c9f9"
          },
          b: {
            color: "#59c9f9"
          },
          c: {
            color: "#59c9f9"
          },
          d: {
            color: "#59c9f9"
          },
          one: {
            backgroundColor: "#66255c",
            color: "white",
            // padding: 10,
            width: 15,
            height: 15,
            lineHeight: 15,
            align: "center",
            borderRadius: 8,
            fontSize: 8
          },
          two: {
            backgroundColor: "#695a28",
            color: "white",
            width: 15,
            height: 15,
            lineHeight: 15,
            align: "center",
            borderRadius: 8,
            fontSize: 8
          },
          three: {
            backgroundColor: "#045788",
            color: "white",
            width: 15,
            height: 15,
            lineHeight: 15,
            align: "center",
            borderRadius: 8,
            fontSize: 8
          },
          four: {
            backgroundColor: "#061d36",
            color: "white",
            width: 15,
            height: 15,
            lineHeight: 15,
            align: "center",
            borderRadius: 8,
            fontSize: 8
          }
        }
      },
      data: yData
    },
    series: [
      {
        name: "销售量",
        barWidth: 8,
        type: "bar",
        label: {
          normal: {
            show: false,
            position: "insideLeft",
            formatter: "{c}",
            textStyle: {
              color: "#fff"
            }
          }
        },
        data: sData1
      },
      {
        name: "销售量",
        type: "pictorialBar",
        barGap: "100%",
        symbol: "rect",
        symbolPosition: "end",
        symbolRotate: "90",
        symbolSize: [8, 2],
        symbolOffset: [10, 0],
        data: sData2,
        color: "red"
      }
    ]
  };
  return option;
};

4.vue文件中

4.1 html

<div  ref="njyyfxChart" style="width:30vh,height:30vh"></div>

4.2 引入njyyfx.js

<script>
	import { njyyfxEcharts } from "@/assets/js/nchx/njyyfx"; // 农机拥有分析
	export default {
		data(){},
		methods: {
			// 农机拥有分析
		    njyyfx() {
				    let njyyfxChartEchart = this.$echarts.init(this.$refs.njyyfxChart);
				   	let dataArr = [
								    {
								      name: "南京市",
								      value: 52
								    },
								    {
								      name: "苏州市",
								      value: 152
								    },
								    {
								      name: "无锡市",
								      value: 252
								    },
								    {
								      name: "常州市",
								      value: 352
								    },
								    {
								      name: "镇江市",
								      value: 452
								    }
								  ];
				    njyyfxChartEchart.setOption(njyyfxEcharts(dataArr));
			},
			// 初始化图形
    		initEcharts() {
				let njyyfxChartEchart = this.$echarts.init(this.$refs.njyyfxChart); //农机拥有分析
				window.addEventListener("resize", function() {
			        njyyfxChartEchart.resize(); //农机拥有分析
			    });
			}
		},
		mounted() {
			this.njyyfx(); //农机拥有分析
			this.initEcharts();
		}
	}
</script>
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
对于vue3-seamless-scroll无缝滚动echarts横向,你可以按照以下步骤进行操作: 1. 首先,安装vue3-seamless-scroll和echarts依赖: ```shell npm install vue3-seamless-scroll echarts ``` 2. 在Vue组件中引入vue3-seamless-scroll和echarts: ```javascript import { defineComponent } from 'vue'; import VueSeamlessScroll from 'vue3-seamless-scroll'; import * as echarts from 'echarts'; ``` 3. 在Vue组件的template中添加一个容器元素,用于渲染echarts表: ```html <template> <div class="chart-container"> <vue-seamless-scroll> <div class="chart-item" v-for="dataItem in data" :key="dataItem.id"> <div class="chart" ref="chart"></div> </div> </vue-seamless-scroll> </div> </template> ``` 4. 在Vue组件的script中定义data和mounted钩子函数: ```javascript export default defineComponent({ data() { return { data: [ { id: 1, value: 100 }, { id: 2, value: 200 }, { id: 3, value: 300 }, // 添加更多数据项... ] }; }, mounted() { this.initCharts(); }, methods: { initCharts() { this.data.forEach((dataItem) => { const chartElement = this.$refs.chart[dataItem.id - 1]; const chart = echarts.init(chartElement); // 使用echarts配置生成横向 const option = { xAxis: { type: 'value' }, yAxis: { type: 'category', data: ['A', 'B', 'C', 'D', 'E', 'F'] }, series: [{ type: 'bar', data: [dataItem.value, dataItem.value, dataItem.value, dataItem.value, dataItem.value, dataItem.value] }] }; chart.setOption(option); }); } } }); ``` 5. 在Vue组件的style中定义样式: ```css <style> .chart-container { width: 100%; height: 200px; overflow: hidden; } .chart-item { width: 100%; height: 100%; } .chart { width: 100%; height: 100%; } </style> ``` 这样,你就可以实现vue3-seamless-scroll无缝滚动echarts横向了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值