首页 > 其他分享 >echarts折线组件

echarts折线组件

时间:2024-08-05 10:41:44浏览次数:10  
标签:01 item yData 折线 组件 null data echarts xData

echarts 折线组件

echarts折线组件

echarts 折线组件

<template>
  <div class="lineChartsTemplate" :id="chartsId"></div>
</template>

<script>
export default {
  name: "lineChartsTemplate",
  components: {},
  props: {
    xData: {
      type: Array,
      default: () => [],
    },
    yData: {
      type: Array,
      default: () => [],
    },
    lineColor: {
      type: String,
      default: "#F1924E",
    },
    title: {
      type: String,
      default: "",
    },
    chartsId: {
      type: String,
      default: "",
    },
  },
  watch: {
    xData(newValue, oldValue) {
      if (newValue) {
        this.initLineChartsTemplate();
      }
    },
  },
  data() {
    return {
      myChart: null,
    };
  },
  methods: {
    initLineChartsTemplate() {
      let option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "line", // 默认为直线,可选为:'line' | 'shadow'
          },
          borderColor: this.lineColor, // 边框颜色
        }, // 坐标轴指示器配置
        title: {
          text: this.title,
          left: "center",
          top: "5%",
          textStyle: {
            color: "#000",
            fontSize: 14,
            align: "center",
          },
        },
        textStyle: {
          color: "#333333", // xy轴的提示文字颜色,不包含背景刻度线
        },
        color: [this.lineColor],
        grid: {
          top: "20%",
          left: "50px",
          right: "20px",
          bottom: "35px",
        },
        xAxis: [
          {
            type: "category",
            data: this.xData,
            axisLine: {
              show: true,
              lineStyle: {
                color: "#333333",
                // width: 0,
                type: "solid",
              }, // x轴线的颜色以及宽度
            },
            // axisLabel: {
            //   show: true,
            //   textStyle: {
            //     color: "rgba(255, 255, 255, 0.3)",
            //   }
            // }, // x轴文字的配置
            splitLine: {
              show: false,
              lineStyle: {}, // 分割线配置
            },
            axisTick: {
              show: false,
            }, // x轴的刻度线
          },
        ],
        yAxis: [
          {
            type: "value",
            splitLine: {
              show: true,
              lineStyle: {
                color: "#333333",
                opacity: 0.1,
              }, // 设置横向的线的颜色
            },
            axisLabel: {
              show: true,
              margin: 20,
              // textStyle: {
              //   color: "rgba(255, 255, 255, 0.3)",
              // }, // y轴的字体配置
            },
          },
        ],
        series: [
          {
            data: this.yData,
            type: "line",
            smooth: true,
          },
        ],
      };
      this.myChart = this.$echarts.init(document.getElementById(this.chartsId));
      this.myChart.setOption(option);

      window.addEventListener("resize", () => {
        this.myChart.resize();
      });
    },
  },
  created() {},
  mounted() {
    this.initLineChartsTemplate();
  },
  beforeDestroy() {
    if (this.myChart) {
      this.myChart.dispose();
    }
  },
};
</script>

<style lang="scss" scoped>
.lineChartsTemplate {
  width: 100%;
  height: 100%;
}
</style>

父组件中使用

假数据版

<div class="trendAnalysis-charts" v-if="!isShowTable">
      <div id="myChartBox1" class="charts">
        <lineChartsTemplate ref="lineChartsTemplate"
                            :chartsId="'charts1'"
                            :title="'平均数据'"
                            :lineColor="'#1492FF'"
                            :xData="[1, 2, 3, 4, 5 ,6 , 6,2]"
                            :yData="[12, 3, 23, 2, 1, 35, 21 ,6]"
        />
      </div>

      <div id="myChartBox2" class="charts">
        <lineChartsTemplate ref="lineChartsTemplate"
                            :chartsId="'charts2'"
                            :title="'行程时间'"
                            :xData="[1, 2, 3, 4, 5 ,6 , 6,2]"
                            :yData="[12, 3, 23, 14, 66, 35, 1 ,6]"
        />
      </div>

      <div id="myChartBox3" class="charts">
        <lineChartsTemplate ref="lineChartsTemplate"
                            :chartsId="'charts3'"
                            :title="'TTI(行程时间比)'"
                            :lineColor="'#1492FF'"
                            :xData="[1, 2, 3, 4, 5 ,6 , 6,2]"
                            :yData="[12, 3, 23, 125, 5, 35, 4 ,6]"
        />
      </div>

      <div id="myChartBox4" class="charts">
        <lineChartsTemplate ref="lineChartsTemplate"
                            :chartsId="'charts4'"
                            :title="'拥堵指数'"
                            :xData="[1, 2, 3, 4, 5 ,6 , 6,2]"
                            :yData="[12, 3, 23, 125, 66, 35, 123 ,6]"
        />
      </div>
    </div>


import lineChartsTemplate from './lineChartsTemplate'

  components: {
    lineChartsTemplate
  },
      
      
      // 直接替换 xData yData title chartsId lineColor 即可

动态获取数据版

在这里插入图片描述

 <div class="chartsPage-charts">
      <div class="charts" v-for="(item, index) in chartsData" :key="index">
        <lineChartsTemplate
          ref="lineChartsTemplate"
          :charts-id="'charts' + index"
          :title="item.title"
          :line-color="item.lineColor"
          :x-data="item.xData"
          :y-data="item.yData"
        ></lineChartsTemplate>
      </div>
    </div>

import lineChartsTemplate from "@/views/statisticAnalysis/trendAnalysis/lineChartsTemplate.vue";
import { getRoadTrendChart } from "@/api/xxx"; // 接口

  components: {
    lineChartsTemplate,
  },
      
  data() {
    return {
      chartsData: [
        {
          title: "平均速度",
          lineColor: "#1492FF",
          xData: [],
          yData: [],
        },
        {
          title: "行驶时间",
          lineColor: "#1492FF",
          xData: [],
          yData: [],
        },
        {
          title: "延时指数",
          lineColor: "#1492FF",
          xData: [],
          yData: [],
        },
        {
          title: "拥堵指数",
          xData: [],
          yData: [],
        },
        {
          title: "流量",
          xData: [],
          yData: [],
        },
        {
          title: "饱和度",
          xData: [],
          yData: [],
        },
      ],
    };
  },
      
        methods: {
    getChartsData() {
      let { queryAllTime, ...obj } = this.paginationConfig;
      getRoadTrendChart(obj).then((res) => {
        this.chartsData.forEach((item, index) => {
          switch (item.title) {
            case "平均速度":
              item.xData = res.data.avgSpeedChart[1];
              item.yData = res.data.avgSpeedChart[0];
              break;
            case "行驶时间":
              item.xData = res.data.driveTimeChart[1];
              item.yData = res.data.driveTimeChart[0];
              break;
            case "延时指数":
              item.xData = res.data.driveTimeRatioChart[1];
              item.yData = res.data.driveTimeRatioChart[0];
              break;
            case "拥堵指数":
              item.xData = res.data.normChart[1];
              item.yData = res.data.normChart[0];
              break;
            case "流量":
              item.xData = res.data.driveFlowChart[1];
              item.yData = res.data.driveFlowChart[0];
              break;
            case "饱和度":
              item.xData = res.data.saturationChart[1];
              item.yData = res.data.saturationChart[0];
              break;
          }
        });
      });
    },
  },
  created() {
    this.getChartsData();
  },
      
      
<style lang="scss" scoped>
.chartsPage {
  &-charts {
    height: calc(100vh - 236px);
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;

    .charts {
      width: calc(33% - 8px);
      height: calc(50% - 10px);
    }
  }
}
</style>


// 数据格式如下:
{
    "code": 1,
    "message": "success",
    "data": {
        "avgSpeedChart": [
            [
                null,
                null,
                null,
                null,
                22.8,
                24.98,
                26.79,
                27.6,
                26.39,
                27.71,
                29.66,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null
            ],
            [
                "01/01",
                "01/02",
                "01/03",
                "01/04",
                "01/05",
                "01/06",
                "01/07",
                "01/08",
                "01/09",
                "01/10",
                "01/11",
                "01/12",
                "01/13",
                "01/14",
                "01/15",
                "01/16",
                "01/17",
                "01/18",
                "01/19",
                "01/20",
                "01/21",
                "01/22",
                "01/23",
                "01/24",
                "01/25",
                "01/26",
                "01/27",
                "01/28",
                "01/29",
                "01/30",
                "01/31"
            ]
        ],
        "driveTimeChart": [
            [
                null,
                null,
                null,
                null,
                243.96,
                546.12,
                489.45,
                491.25,
                539.62,
                411.25,
                16.28,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null
            ],
            [
                "01/01",
                "01/02",
                "01/03",
                "01/04",
                "01/05",
                "01/06",
                "01/07",
                "01/08",
                "01/09",
                "01/10",
                "01/11",
                "01/12",
                "01/13",
                "01/14",
                "01/15",
                "01/16",
                "01/17",
                "01/18",
                "01/19",
                "01/20",
                "01/21",
                "01/22",
                "01/23",
                "01/24",
                "01/25",
                "01/26",
                "01/27",
                "01/28",
                "01/29",
                "01/30",
                "01/31"
            ]
        ],
        "driveTimeRatioChart": [
            [
                null,
                null,
                null,
                null,
                0.74,
                1.27,
                1.49,
                1.47,
                1.53,
                1.46,
                1.37,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null
            ],
            [
                "01/01",
                "01/02",
                "01/03",
                "01/04",
                "01/05",
                "01/06",
                "01/07",
                "01/08",
                "01/09",
                "01/10",
                "01/11",
                "01/12",
                "01/13",
                "01/14",
                "01/15",
                "01/16",
                "01/17",
                "01/18",
                "01/19",
                "01/20",
                "01/21",
                "01/22",
                "01/23",
                "01/24",
                "01/25",
                "01/26",
                "01/27",
                "01/28",
                "01/29",
                "01/30",
                "01/31"
            ]
        ],
        "normChart": [
            [
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null
            ],
            [
                "01/01",
                "01/02",
                "01/03",
                "01/04",
                "01/05",
                "01/06",
                "01/07",
                "01/08",
                "01/09",
                "01/10",
                "01/11",
                "01/12",
                "01/13",
                "01/14",
                "01/15",
                "01/16",
                "01/17",
                "01/18",
                "01/19",
                "01/20",
                "01/21",
                "01/22",
                "01/23",
                "01/24",
                "01/25",
                "01/26",
                "01/27",
                "01/28",
                "01/29",
                "01/30",
                "01/31"
            ]
        ],
        "driveFlowChart": [
            [
                null,
                null,
                null,
                null,
                13017,
                0,
                0,
                0,
                1857,
                12917,
                59425,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null
            ],
            [
                "01/01",
                "01/02",
                "01/03",
                "01/04",
                "01/05",
                "01/06",
                "01/07",
                "01/08",
                "01/09",
                "01/10",
                "01/11",
                "01/12",
                "01/13",
                "01/14",
                "01/15",
                "01/16",
                "01/17",
                "01/18",
                "01/19",
                "01/20",
                "01/21",
                "01/22",
                "01/23",
                "01/24",
                "01/25",
                "01/26",
                "01/27",
                "01/28",
                "01/29",
                "01/30",
                "01/31"
            ]
        ],
        "saturationChart": [
            [
                null,
                null,
                null,
                null,
                0.09,
                0.0,
                0.0,
                0.0,
                0.01,
                0.06,
                0.23,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null
            ],
            [
                "01/01",
                "01/02",
                "01/03",
                "01/04",
                "01/05",
                "01/06",
                "01/07",
                "01/08",
                "01/09",
                "01/10",
                "01/11",
                "01/12",
                "01/13",
                "01/14",
                "01/15",
                "01/16",
                "01/17",
                "01/18",
                "01/19",
                "01/20",
                "01/21",
                "01/22",
                "01/23",
                "01/24",
                "01/25",
                "01/26",
                "01/27",
                "01/28",
                "01/29",
                "01/30",
                "01/31"
            ]
        ]
    }
}

标签:01,item,yData,折线,组件,null,data,echarts,xData
From: https://www.cnblogs.com/ProgrammerMao-001/p/18342759

相关文章

  • echarts设置tooltip遇到值为0不展示的问题(已解决)
    echarts设置tooltip遇到值为0不展示的问题(已解决)遇到值为0时tooltip:{trigger:"axis",extraCssText:"z-index:3",axisPointer:{type:"shadow",//默认为直线,可选为:'line'|'shadow'......
  • echarts设置tooltip的层级
    echarts设置tooltip的层级tooltip:{trigger:"axis",extraCssText:'z-index:3',//修改层级borderColor:"rgba(0,170,255)",},完整的option示例如下:option={tooltip:{trig......
  • 打造自己的前端组件库
    打造自己的前端组件库新建vue项目(sass+js+vue2)vuecreateyt-ui 修改文件目录(如下)修改:1.src更名examples;2.src/components移动到项目最外层;3.vue.config.js更改入口文件//vue.config.jsconst{defineConfig}=require('@vue/cli-service')module.export......
  • vue项目如何在框架最外层包裹一层class 包裹弹框组件
    在Vue项目中,如果你想要在框架的最外层包裹一层类,并且这个类能够包裹所有的弹框组件(如ElementUI的<el-dialog>),你可以通过几种方式实现:1.使用Vue的根实例在Vue应用的入口文件(通常是main.js或app.js)中,你可以在创建Vue根实例时使用:class绑定:importVuefr......
  • searchBar组件的用法
    文章目录1.概念介绍2.使用方法3.代码与效果3.1示例代码3.2运行效果4.内容总结我们在上一章回中介绍了"Material3中的IconButton"相关的内容,本章回中将介绍SearchBar组件.闲话休提,让我们一起TalkFlutter吧。1.概念介绍我们在本章回中介绍的SearchBar是指......
  • ECharts与数据可视化:如何高效使用JavaScript实现复杂图表
    ECharts与数据可视化:如何高效使用JavaScript实现复杂图表大家好,我是微赚淘客系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!在现代网页开发中,数据可视化已成为展示复杂数据的关键手段。ECharts是一个强大的JavaScript图表库,可以帮助开发者轻松实现各种复杂的图表。本文......
  • 从数据爬取到可视化展示:Flask框架与ECharts深度解析
    目录......
  • UE5学习笔记3-关于charactor的相机和弹簧臂组件
    一、环境说明,UE5.4+ vs2022 +win11二、相机和弹簧臂的作用    个人理解上,相机的作用相当于一个视角,我将其理解成是一个人在哪个地方朝向哪个方向看,弹簧臂的用用我将其理解成为一个将人的视角和人物模型或其他模型连接的桥梁三、相机和弹簧臂的代码    ......
  • NGUI Tweens组件:实现流畅动画的魔法
    引言在Unity游戏开发中,UI动画是提升用户界面交互性和吸引力的重要手段。NGUI(Next-GenUI)框架中的Tweens组件,为UI元素提供了平滑且高效的动画效果。本文将详细介绍NGUITweens的使用方法和技巧,帮助你轻松实现UI动画效果。NGUITweens简介NGUITweens是NGUI框架中的一组用......
  • vue 组件透传所有属性—useAttrs
    vue组件透传所有属性——useAttrs引言在对某些基础组件进行封装的时候,可能无法通过props将所有可能的属性都声明了,这时候就可以使用useAttrs,拿到不通过props声明的属性<scriptlang="ts">import{defineComponent,computed}from'vue'import{useAttrs}from'v......