echarts 自定义图表

首先附上实现效果,被分了段的柱状图

幸亏了公司的设计师出的难题,不然我也不会去捣鼓这个东西的 

这个图表的类型是 pictoriaBar ,在echarts 3.4版本(版本记得不太明确)之后有了这个类型的图

介绍下这个属性的具体用处:

barWidth: 设置柱子的宽度

symbol:设置小块的样式

symbolRepeat: 设置柱子上的小块是否重复,我尝试过发现,默认为false,只显示一个小块

symbolOffset: 设置每个柱子的位置(每个柱子的位置需要进行计算得出)

symbolSize: 设置每个小块的大小

const chartInit = {
  color: [
    "#7AC9D2",
    "#00C1DE",
    "#00749F",
    "#77BEE8",
    "#1890FF",
    "#3436C7",
    "#0103A0",
    "#000272"
  ],
  backgroundColor: "transparent",
  grid: {
    left: "10px",
    top: "50px",
    right: "50px",
    bottom: "16px",
    containLabel: true
  },
  xAxis: {
    type: "category",
    data: [
      "2018/08/15",
      "2018/10/02",
      "2018/01/19",
      "2018/05/23",
      "2018/09/28",
      "2018/02/21",
      "2018/11/11",
      "2018/06/14"
    ],
    axisLabel: {
      color: "#AFBCC4"
    },
    axisTick: {
      show: false
    },
    axisLine: {
      show: false
    }
  },
  yAxis: {
    name: "单位",
    axisLine: {
      show: false
    },
    type: "value",
    nameGap: 10,
    axisTick: {
      show: false
    },
    axisLabel: {
      color: "#747677"
    },
    splitLine: {
      lineStyle: {
        color: "rgba(216,216,216,0.10)",
        type: "solid"
      }
    }
  },
  legend: {
    top: 20,
    data: [
      { name: "东向西直行", icon: "square" },
      { name: "东向西右转", icon: "square" },
      { name: "西向东直行", icon: "square" },
      { name: "西向东左转", icon: "square" },
      { name: "南向北直行", icon: "square" },
      { name: "南向北左转", icon: "square" },
      { name: "北向南直行", icon: "square" },
      { name: "北向南左转", icon: "square" }
    ]
  },
  series: [
    {
      name: "东向西直行",
      type: "pictorialBar",
      symbol: "roundRect",
      symbolRepeat: true, // 分节
      barWidth: 10,
      symbolSize: [10, 5], //size,单个symbol的大小
      data: [120, 132, 101, 134, 90, 230, 210, 180]
    },
    {
      name: "东向西右转",
      type: "pictorialBar",
      barWidth: 10,
      symbolRepeat: true,
      symbol: "roundRect",
      symbolOffset: [13, 0], // 柱子的位置
      symbolSize: [10, 5], //size,单个symbol的大小
      data: [220, 182, 191, 234, 290, 330, 310, 290]
    },
    {
      name: "西向东直行",
      type: "pictorialBar",
      symbolRepeat: true,
      symbol: "roundRect",
      symbolOffset: [26, 0], // 柱子的位置
      symbolSize: [10, 5], //size,单个symbol的大小
      barWidth: 10,
      data: [150, 232, 201, 154, 190, 330, 410, 430]
    },
    {
      name: "西向东左转",
      type: "pictorialBar",
      symbolRepeat: true,
      symbol: "roundRect",
      symbolOffset: [39, 0], // 柱子的位置
      symbolSize: [10, 5], //size,单个symbol的大小
      barWidth: 10,
      data: [320, 332, 301, 334, 390, 330, 320, 503]
    },
    {
      name: "南向北直行",
      type: "pictorialBar",
      barWidth: 10,
      symbol: "roundRect",
      symbolRepeat: true,
      symbolOffset: [52, 0], // 柱子的位置
      symbolSize: [10, 5], //size,单个symbol的大小
      data: [820, 932, 901, 934, 1290, 1330, 1320, 1222]
    },
    {
      name: "南向北左转",
      type: "pictorialBar",
      barWidth: 10,
      symbol: "roundRect",
      symbolRepeat: true,
      symbolOffset: [65, 0], // 柱子的位置
      symbolSize: [10, 5], //size,单个symbol的大小
      data: [820, 932, 901, 934, 1290, 1330, 1320, 1290]
    },
    {
      name: "北向南直行",
      type: "pictorialBar",
      barWidth: 10,
      symbol: "roundRect",
      symbolRepeat: true,
      symbolOffset: [78, 0], // 柱子的位置
      symbolSize: [10, 5], //size,单个symbol的大小
      data: [120, 132, 101, 134, 90, 230, 210, 180]
    },
    {
      name: "北向南左转",
      type: "pictorialBar",
      barWidth: 10,
      symbol: "roundRect",
      symbolRepeat: true,
      symbolOffset: [91, 0], // 柱子的位置
      symbolSize: [10, 5], //size,单个symbol的大小
      data: [120, 132, 101, 134, 90, 230, 210, 180]
    }
  ]
};

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue 中使用 Echarts 绘制自定义图表组件可以按照以下步骤进行: 1. 首先,确保已经安装了 echarts 库。可以使用 npm 或 yarn 进行安装: ``` npm install echarts --save ``` 2. 创建一个 Vue 组件,用于包含 Echarts 图表。可以命名为 `EchartsChart.vue`。 ```html <template> <div :ref="chartRef" class="echarts-chart"></div> </template> <script> import echarts from 'echarts'; export default { props: ['chartData'], data() { return { chartRef: 'echartsChart', chartInstance: null, }; }, mounted() { this.chartInstance = echarts.init(this.$refs[this.chartRef]); this.renderChart(); }, methods: { renderChart() { // 使用 chartData 配置图表数据和样式 this.chartInstance.setOption(this.chartData); }, }, beforeDestroy() { if (this.chartInstance) { this.chartInstance.dispose(); // 销毁图表实例,释放资源 } }, }; </script> <style scoped> .echarts-chart { width: 100%; height: 400px; /* 根据需求设置图表高度 */ } </style> ``` 在上面的示例中,我们创建了一个 `EchartsChart` 组件,接受一个 `chartData` 属性,用于配置图表的数据和样式。在 `mounted` 钩子中,我们初始化了 Echarts 实例,并在 `renderChart` 方法中使用 `setOption` 方法渲染图表。在组件销毁前,我们通过调用 `dispose` 方法销毁图表实例,释放资源。 3. 在使用自定义图表组件的父组件中,引入并使用 `EchartsChart` 组件。可以通过传递 `chartData` 属性来配置图表。 ```html <template> <div> <echarts-chart :chartData="chartData"></echarts-chart> </div> </template> <script> import EchartsChart from '@/components/EchartsChart.vue'; export default { components: { EchartsChart, }, data() { return { chartData: { // 配置图表的数据和样式 }, }; }, }; </script> ``` 在上面的示例中,我们在父组件中引入了 `EchartsChart` 组件,并通过 `chartData` 属性配置图表的数据和样式。 你可以根据自己的需求在 `chartData` 中配置图表的相关数据,例如 `title`、`tooltip`、`xAxis`、`yAxis`、`series` 等。具体的配置可以参考 Echarts 的文档。 希望这个示例能够帮助你创建自定义Echarts 图表组件!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值