echarts折线图点的形状和颜色自定义

<script>
import * as echarts from "echarts";
export default {
  name: "EChartsExample",
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose();
    }
  },
  methods: {
    initChart() {
      // 基于准备好的dom,初始化echarts实例
      this.chart = echarts.init(this.$refs.chart);
      // 原始数据
      var rawData = [820, 932, 901, 934, 1290, 1331, 1320];

      // 处理数据,为每个数据点设置形状
      // 折线图设置点的形状

      var processedData = rawData.map(function (value) {
        return {
          value: value,
          symbol: value % 2 === 0 ? "rect" : "circle", // 根据奇偶性设置形状
          symbolSize: 7, // 形状大小(可选)
          itemStyle: {
            color: value % 2 === 0 ? "red" : "blue",
          },
        };
      });

     

      // 指定图表的配置项和数据
      const option = {
        title: {
          text: "一周数据变化",
        },
        tooltip: {
          trigger: "axis",
        },
        xAxis: {
          type: "category",
          data: ["周一", "周二", "周三", "周四", "周五", "周六", "周日"],
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            data: processedData,
            type: "line",

            smooth: true, // 如果需要平滑曲线,可以设置为true
          },
        ],
      };

      // 使用刚指定的配置项和数据显示图表
      this.chart.setOption(option);
    },
  },
};
</script>
<template>
  <div ref="chart" style="width: 600px; height: 400px; margin: 50px"></div>
</template>
<style  scoped>
</style>

效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值