echarts实现温度曲线折线图

如图所示:
在这里插入图片描述
首先我想到了使用echarts,用属性控制x轴y轴的隐藏,然后修改拐点以及折线上的文字
代码:
图表组件

<template>
  <div
    id="tem-chart"
    style=" height: 120px" />
</template>
<script>
const echarts = require('echarts');
export default {
  name: 'TemChart',
  props: {
    temList: {
      type: Array,
      default: () => []
    },
    temXAxis: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      temChart: null
    };
  },
  mounted() {
    this.showChart();
  },
  watch: {
    temList: {
      handler(newVal) {
        this.showChart();
      }
    }
  },
  methods: {
    showChart() {
      if (this.temChart) this.temChart.dispose();   
      this.temChart = echarts.init(document.getElementById('tem-chart'));
      let option = {
        tooltip: {
          show: false
        },
        grid: {
          x: -20, // 距离左边
          x2: -20, // 距离右边
          y: 20, // 距离上边
          y2: 0 // 距离下边
        },
        xAxis: {
          show: false, // 不展示x轴
          data: this.temXAxis
        },
        yAxis: {
          show: false // 不展示y轴
        },
        series: [
          {
            name: '温度',
            type: 'line',
            smooth: true,
            symbolSize: 8, // 拐点圆的大小
            animation: false, // hover圆点不缩放
            // label 图形上的文本标签,可用于说明图形的一些数据信息,比如值,名称等
            label: {
              normal: {
                show: true, // 折线上的文字是否显示
                formatter: '{c}' + '℃', // 格式化显示文字
                position: 'top',
                textStyle: {
                  color: '#001832',
                  fontSize: '13'
                }
              }
            },
            data: this.temList,
            itemStyle: {
              normal: {
                color: '#1E4CFE',
                borderWidth: 2 // 拐点边框大小
              }
            }
          }
        ]
      };
      this.temChart.setOption(option);
      const self = this;
      setTimeout(function() {
        self.temChart.resize();
      });
    }
  }
};
</script>

使用:

<tem-chart
  :tem-list="temList"
  :tem-x-axis="temXAxis"
  class="weather-tem" />

import temChart from './temChart.vue';
export default {
  name: 'Message',
  components: { temChart },

  data() {
    return {
      temList: [26, 24, 23, 15, 26, 29, 30, 31],
      temXAxis: [20, 23, 02, 05, 08, 11, 14, 17]
    };
  }
};


  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
好的,以下是一个基于 Vue3 和 Echarts 的双 y 轴折线图实现: 1. 首先安装 Echarts: ``` npm install echarts --save ``` 2. 在 Vue3 组件中引入 Echarts: ```javascript import * as echarts from 'echarts' ``` 3. 在 Vue3 组件中定义数据和配置项并初始化 Echarts: ```javascript <template> <div class="chart-container"> <div ref="chart" class="chart"></div> </div> </template> <script> export default { name: 'DoubleLineChart', data() { return { chartData: { xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, yAxis: [ { type: 'value', name: 'y1' }, { type: 'value', name: 'y2' } ], series: [ { name: 'y1', type: 'line', data: [820, 932, 901, 934, 1290, 1330, 1320], yAxisIndex: 0 }, { name: 'y2', type: 'line', data: [10, 20, 30, 40, 50, 60, 70], yAxisIndex: 1 } ] } } }, mounted() { this.chart = echarts.init(this.$refs.chart) this.chart.setOption(this.chartData) } } </script> <style> .chart-container { width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; } .chart { width: 600px; height: 400px; } </style> ``` 4. 在父组件中使用 DoubleLineChart 组件: ```javascript <template> <div class="app"> <DoubleLineChart /> </div> </template> <script> import DoubleLineChart from './components/DoubleLineChart.vue' export default { name: 'App', components: { DoubleLineChart } } </script> ``` 5. 运行项目,即可看到双 y 轴折线图的效果。 注意:这里的代码只是一个简单的示例,实际使用时需要根据自己的需求进行适当的修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值