vue echart 立体柱状图 带阴影

根据一个博主代码改编而来

<template>
  <div class="indexBox">
    <div id="chart"></div>
  </div>
</template>
    
<script  setup>
import * as echarts from "echarts";
import { onMounted } from "vue";
import imgsss from "@/assets/imgs/111.png";

onMounted(() => {
  create();
});

const colorArr = (arrList, type) => {
  let returnList = [];
  if (type == 1) {
    arrList.forEach((item) => {
      let info = {
        value: item.val,
        itemStyle: {
          normal: {
            borderColor: "#89e3ec",
            borderWidth: 2,
            color: item.starColor,
          },
        },
      };
      returnList.push(info);
    });
  } else {
    arrList.forEach((item) => {
      let infor = {
        value: item.val,
        itemStyle: {
          color: {
            // https://echarts.apache.org/zh/option.html#color
            //疑似根据整个demo尺寸,定位图片未知
            //   image: imgsss, // 支持为 HTMLImageElement, HTMLCanvasElement,不支持路径字符串
            //   repeat: 'repeat' // 是否平铺,可以是 'repeat-x', 'repeat-y', 'no-repeat'
            type: "linear",
            x: 0,
            x2: 1,
            y: 0,
            y2: 0,
            //给菱形上色
            colorStops: [
              {
                offset: 0,
                color: item.starColor,
              },
              {
                offset: 0.5,
                color: item.starColor,
              },
              {
                offset: 0.5,
                color: item.endColor,
              },
              {
                offset: 1,
                color: item.endColor,
              },
            ],
          },
        },
      };

      returnList.push(infor);
    });
  }

  return returnList;
};

function create() {
  var chartDom = document.getElementById("chart");
  var myChart = echarts.init(chartDom);
  var xData = ["周一", "周二", "周三", "周四", "周五"];
  // 完成数据数组
  var completeData = [320, 132, 101, 11, 90];
  var barWidth = 50;
  var showData = [];
  var otherData = [];

  let completeDataList = [
    { val: 320, starColor: "#3dc6cc", endColor: "#267A7B" },
    { val: 132, starColor: "#003EFF", endColor: "#262F7B" },
    { val: 131, starColor: "#6C29C1", endColor: "#4B267B" },
    { val: 111, starColor: "#DB00FF", endColor: "#74267B" },
    { val: 90, starColor: "#FF8A00", endColor: "#56401F" },
  ];
  completeData = colorArr(completeDataList);
  let otherDataList = [
    {
      val: 680,
      starColor: "rgba(53, 140, 144,0.5)",
      endColor: "rgba(31, 82, 86,0.5)",
    },
    {
      val: 868,
      starColor: "rgba(0, 62, 255,0.5)",
      endColor: "rgba(38, 47, 123,0.5)",
    },
    {
      val: 869,
      starColor: "rgba(108, 41, 193,0.5)",
      endColor: "rgba(75, 38, 123,0.5)",
    },
    {
      val: 889,
      starColor: "rgba(219, 0, 255,0.5)",
      endColor: "rgba(116, 38, 123,0.5)",
    },
    {
      val: 910,
      starColor: "rgba(255, 138, 0,0.5)",
      endColor: "rgba(86, 64, 31,0.5)",
    },
  ];
  otherData = colorArr(otherDataList);

  //  中间菱形
  let constDatasList = [
    { val: 320, starColor: "#56fdfd", endColor: "#3fc3c3" },
    { val: 132, starColor: "#5699fd", endColor: "#3f64c3" },
    { val: 131, starColor: "#8156fd", endColor: "#613fc3" },
    { val: 111, starColor: "#c24fe5", endColor: "#9c3bb2" },
    { val: 90, starColor: "#Fdce56", endColor: "#c3b63f" },
  ];
  let constDatas = colorArr(constDatasList);

  let showDataList = [
    {
      val: 1000,
      starColor: "rgba(53, 140, 144,0.7)",
      endColor: "rgba(31, 82, 86,0.5)",
    },
    {
      val: 1000,
      starColor: "rgba(0, 62, 255,0.7)",
      endColor: "rgba(38, 47, 123,0.5)",
    },
    {
      val: 1000,
      starColor: "rgba(108, 41, 193,0.7)",
      endColor: "rgba(75, 38, 123,0.5)",
    },
    {
      val: 1000,
      starColor: "rgba(219, 0, 255,0.7)",
      endColor: "rgba(116, 38, 123,0.5)",
    },
    {
      val: 1000,
      starColor: "rgba(255, 138, 0,0.7)",
      endColor: "rgba(86, 64, 31,0.5)",
    },
  ];
  showData = colorArr(showDataList, 1);

  var option = {
    //调整光标移入,展示数据的样式
    tooltip: {
      trigger: "axis",
      // formatter: function (params) {
      //   return (
      //     params[0].axisValue +
      //     ":" +
      //     "<br/>计划:" +
      //     planData[params[0].dataIndex] +
      //     "<br/>完成:" +
      //     completeData[params[0].dataIndex]
      //   );
      // },
    },
    title: {
      text: "单位(数量)",
      left: "3%",
      textStyle: {
        color: "#07D1FA",
        fontWeight: "normal",
        fontSize: 14,
      },
    },
    //调整图例
    legend: {
      data: ["计划", "完成"],
      show: false,
    },
    //调整图表距边框的距离
    grid: {
      left: "3%",
      right: "3%",
      top: "15%",
      bottom: "5%",
      containLabel: true,
    },
    // 调整图像x轴
    xAxis: {
      data: xData,
      axisTick: {
        show: false,
      },

      axisLabel: {
        textStyle: {
          fontFamily: "Microsoft YaHei",
          color: "#07D1FA",
          fontWeight: "400",
          fontSize: "14",
        },
      },
    },
    //调整图像y轴
    yAxis: {
      axisTick: {
        show: false,
      },
      axisLine: {
        show: false,
      },
      axisLabel: {
        color: "#07D1FA",
        // show: false,
      },
      splitLine: {
        show: true,
        lineStyle: {
          type: [5, 10],
          dashOffset: 5,
          color: "#07D1FA",
          width: 1,
          opacity: 0.21,
        },
      },
    },
    //图像的关键显示部分,此处5个拼接一个堆叠柱子,注意此处的调整
    series: [
      {
        z: 1,
        name: "计划",
        type: "bar",
        stack: "1",
        barWidth: barWidth,
        // stack: "总量",
        // color: color[0],
        data: completeData,
      },
      {
        z: 2,
        name: "完成",
        type: "bar",
        stack: "1",
        barWidth: barWidth,
        // stack: "总量",
        // color: color[1],
        data: otherData,
      },
      //柱形底部
      {
        z: 3,
        name: "项目",
        type: "pictorialBar",
        data: completeData,
        symbol: "diamond",
        symbolOffset: ["0%", "60%"],
        symbolSize: [barWidth, 10],

        // itemStyle: {
        //   normal: {
        //     color: function (params) {
        //       let arr = ["#3dc6cc", "#003EFF", "#6C29C1", "#DB00FF", "#FF8A00"];
        //       console.log(arr[params.dataIndex]);
        //       let ass = arr[params.dataIndex];
        //       if (params.dataIndex == 0) {
        //         return "#DB00FF";
        //       }
        //       if (params.dataIndex == 1) {
        //         return "#003EFF";
        //       }
        //     },
        //   },
        // },
        tooltip: {
          show: false,
        },
      },
      {
        z: 3,
        name: "1",
        type: "pictorialBar",
        data: constDatas,
        symbol: "diamond",
        symbolPosition: "end",
        symbolOffset: ["0%", "-50%"],
        symbolSize: [barWidth, 10],
      },
      {
        z: 5,
        name: "项目",
        type: "pictorialBar",
        symbolPosition: "end",
        data: showData,
        symbol: "diamond",
        symbolOffset: ["0%", "-50%"],
        symbolSize: [barWidth, (10 * barWidth) / barWidth],
        tooltip: {
          show: false,
        },
        // itemStyle: {
        //   normal: {
        //     label: {
        //       formatter: "{c}",
        //       show: true,
        //       position: "top",
        //       textStyle: {
        //         fontWeight: 400,
        //         fontSize: "13",
        //         color: "#F2F7FA",
        //         lineHeight: 20,
        //       },
        //     },
        //   },
        // },
      },
    ],
  };
  option && myChart.setOption(option);
}
</script>

    
    
<style scoped lang="scss">
.indexLeft {
  width: 360px;
  height: 1016px;
  margin-top: 64px;
  background: linear-gradient(90deg,
      #031425 0%,
      rgba(3, 20, 37, 0.5) 50%,
      rgba(3, 20, 37, 0) 100%);
}

#chart {
  width: 700px;
  height: 300px;
}
</style>
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于使用 Vue 3 和 ECharts 创建柱状图,您可以按照以下步骤进行操作: 1. 首先,确保您已经安装了 Vue 3 和 ECharts。可以使用以下命令进行安装: ```bash npm install vue@next echarts ``` 2. 在 Vue 组件中引入 ECharts 和其样式文件。可以在 `main.js` 或者需要使用柱状图的组件中进行引入: ```javascript import { createApp } from 'vue'; import * as echarts from 'echarts'; import 'echarts/dist/echarts.min.css'; const app = createApp(App); app.config.globalProperties.$echarts = echarts; app.mount('#app'); ``` 3. 在需要显示柱状图的组件中,创建一个容器来容纳图表,并引入 ECharts 的实例: ```html <template> <div class="chart-container"> <div ref="chart" style="width: 100%; height: 400px;"></div> </div> </template> <script> export default { mounted() { this.renderChart(); }, methods: { renderChart() { const chartDom = this.$refs.chart; const myChart = this.$echarts.init(chartDom); // 在这里使用 ECharts 的 API 绘制柱状图 // 例如: myChart.setOption({ title: { text: '柱状图示例' }, xAxis: { type: 'category', data: ['A', 'B', 'C', 'D', 'E'] }, yAxis: { type: 'value' }, series: [{ data: [10, 20, 30, 40, 50], type: 'bar' }] }); } } }; </script> <style scoped> .chart-container { width: 100%; height: 400px; } </style> ``` 以上是一个简单的示例,您可以根据您的需求进行进一步的配置和样式调整。希望对您有所帮助!如果您有任何问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值