vue中使用echarts绘柱形图+折线图

一、完成后效果图

在这里插入图片描述
在这里插入图片描述

二、.vue文件完整代码

<template>
  <!-- 柱形图+折线图多个展示 -->
  <div
    id="myMaxbar"
    :style="{width: '500px', height: '300px'}"
  ></div>
</template>

<script>
export default {
  data() {
    return {};
  },
  mounted() {
    this.drawLine();
  },
  methods: {
    drawLine() {
      // 基于准备好的dom,初始化echarts实例
      let myMaxbar = this.$echarts.init(document.getElementById("myMaxbar"));
      // 绘制图表
      myMaxbar.setOption({
        color: ["#0EA9A1", "#95CE5C", "#21D86D"],
        //提示框组件配置
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器,坐标轴触发有效
            type: "shadow", // 默认为直线,可选为:'line' | 'shadow'
          },
          textStyle: {
            fontSize: 12,
            color: "#fff",
          },
          padding: 10,
        },
        // 图例组件
        legend: {
          show: true,
          top: "0",
          left:'5%',
          selectedMode: true,
          data: [
            {
              name: "销售额",
              textStyle: {
                color: "#0EA9A1",
              },
            },
            {
              name: "销售件数",
              textStyle: {
                color: "#95CE5C",
              },
            },
            {
              name: "销售品种",
              textStyle: {
                color: "#21D86D",
              },
            },
          ],
        },
        //定义折线图距离左边多少右边多少的距离
        grid: {
          left: "2%",
          right: "5%",
          bottom: "16%",
          containLabel: true, //区域是否包含坐标轴的刻度标签
        },
        xAxis: [ //定义x轴
          {
            type: "category", //设置x轴的类型
            data: [
              "茅台",
              "五粮液",
              "剑南春",
              "郎酒",
              "洋河蓝色经典",
              "水井坊",
              "泸州老窖",
              "汾酒",
              "翰格",
              "茅台",
              "洋河蓝色经典",
              "剑南春",
              "郎酒",
              "洋河蓝色经典",
              "水井坊",
              "泸州老窖",
              "汾酒",
              "翰格",
              "尊尼获加金牌",
              "剑南春",
              "郎酒",
            ],
            nameTextStyle: {
              fontSize: 14,
            },
            axisLabel: { //坐标轴刻度的相关设置
              interval: 0, //设置成 0 强制显示所有标签。
              rotate: -45, //标签旋转的角度
              margin: 15,
              textStyle:{
                color:'#fff',
              }
            },
          },
        ],
        yAxis: [ // 定义y轴
          {
            nameTextStyle: {
              color: "#666666",
              fontSize: 14,
            },
            show: true,
            type: "value",
            axisLabel: { // 坐标轴刻度标签的相关设置
              textStyle: {
                color: "#fff",
              },
            },
            axisLine: { // 坐标轴轴线相关设置
              show: false,
            },
            splitLine: { // 坐标轴在网格区域中的分隔线
              lineStyle: { //x轴网格样式设置
                color: "#12403F",
              },
            },
          },
        ],
        series: [ //系列列表。每个系列通过 type 决定自己的图表类型
          {
            name: "销售额",
            type: "bar", // 设置为柱状图
            barWidth: 14,
            //柱状图设置数值
            label: {
                normal: {
                    show: true,
                    position: 'top'
                }
            },
            data: [
              150,
              232,
              201,
              154,
              190,
              330,
              410,
              190,
              330,
              410,
              150,
              232,
              201,
              154,
              190,
              330,
              410,
              190,
              230,
              200,
              180,
            ],
          },
          {
            name: "销售件数",
            type: "line", // 设置为折线图
            symbol: "circle",
            symbolSize: [10, 10],
            data: [
              120,
              132,
              101,
              134,
              90,
              230,
              210,
              190,
              330,
              410,
              120,
              132,
              101,
              134,
              90,
              230,
              210,
              190,
              230,
              200,
              180,
            ],
          },
          {
            name: "销售品种",
            type: "line",
            symbol: "circle",
            symbolSize: [10, 10],
            data: [
              111,
              122,
              100,
              104,
              70,
              210,
              200,
              160,
              300,
              400,
              100,
              102,
              91,
              124,
              40,
              210,
              230,
              140,
              240,
              180,
              140,
            ],
          },
        ],
      });
    },
  },
};
</script>

<style>
</style>

备注

我这个有加背景图,如果是纯白,有些字体设置的#fff会看不到。

### 回答1: 在Vue使用echarts柱形图预警变色,可以通过以下步骤实现: 1. 安装echarts ``` npm install echarts --save ``` 2. 在Vue组件引入echarts ``` import echarts from 'echarts' ``` 3. 在Vue组件定义echarts图表的配置项和数据 ``` data() { return { chartData: { xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, yAxis: { type: 'value' }, series: [{ data: [120, 200, 150, 80, 70, 110, 130], type: 'bar' }] } } } ``` 4. 在Vue组件的mounted钩子函数初始化echarts图表,并设置预警变色 ``` mounted() { const chart = echarts.init(this.$refs.chart) chart.setOption(this.chartData) chart.setOption({ series: [{ itemStyle: { normal: { color: function(params) { if (params.data >= 150) { return '#FF5722' } else { return '#2196F3' } } } } }] }) } ``` 以上代码,我们通过设置itemStyle.normal.color属性来设置每个数据项的颜色。在这里,我们设置当数据项的值大于等于150时,颜色为橙色;否则,为蓝色。 5. 在Vue组件的template添加echarts图表 ``` <template> <div ref="chart" style="height: 400px;"></div> </template> ``` 通过以上步骤,我们就可以在Vue使用echarts柱形图预警变色了。 ### 回答2: 在Vue使用Echarts柱形图预警变色的方法如下: 1. 首先,在Vue项目安装echarts插件。可以使用npm或者yarn命令安装,例如:npm install echarts --save 2. 在Vue组件导入echarts并创建一个echarts实例。 3. 在data定义柱形图的数据和预警的阈值。 4. 在mounted或者created生命周期钩子函数使用echarts的setOption方法设置柱形图的配置。 5. 在柱形图的配置,设置柱子的颜色。 6. 在柱形图的配置,根据预警标准,判断柱子是否需要变色,并设置相应的颜色。 下面是一个简单的示例代码,在Vue组件实现柱形图的预警变色: <template> <div id="chart"></div> </template> <script> import echarts from 'echarts' export default { data() { return { chartData: [120, 200, 150, 80, 70, 110, 130], // 柱形图的数据 warningThreshold: 100 // 预警阈值 } }, mounted() { this.renderChart() }, methods: { renderChart() { let chart = echarts.init(document.getElementById('chart')) chart.setOption({ xAxis: { type: 'category', data: ['A', 'B', 'C', 'D', 'E', 'F', 'G'] }, yAxis: { type: 'value' }, series: [ { type: 'bar', data: this.chartData, itemStyle: { normal: { color: function(params) { // 根据预警阈值判断柱子是否需要变色 if (params.value >= this.warningThreshold) { return 'red' } else { return 'blue' } }.bind(this) } } } ] }) } } } </script> 以上代码使用echarts.init方法创建了一个echarts实例,并将其渲染到指定的DOM元素。在配置项的series属性,定义了一系列柱形图的配置,其的itemStyle属性用于设置柱子的颜色。通过判断柱子的值是否大于等于预警阈值,并返回相应的颜色来实现预警变色的效果。需要注意的是,在itemStyle的color配置项使用了bind方法绑定了this指向,以便在其访问this.warningThreshold。 通过上述步骤,就可以在Vue实现echarts柱形图的预警变色功能。 ### 回答3: 在Vue使用echarts柱形图预警变色可以通过以下步骤实现: 1. 首先,引入echarts并在Vue组件进行相关配置。可以使用npm安装echarts,并在Vue组件引入echarts: ``` import echarts from 'echarts' ``` 2. 在Vue组件,创建一个DOM元素作为echarts图表的容器。可以使用Vue的ref属性来获取该元素的引用,并定义一个变量来保存echarts实例: ``` <template> <div ref="chartContainer"></div> </template> <script> export default { data() { return { chart: null } }, mounted() { this.initChart() }, methods: { initChart() { this.chart = echarts.init(this.$refs.chartContainer) // 省略其他配置 } } } </script> ``` 3. 在echarts的配置选项,通过设置itemStyle来实现预警柱形的颜色变化。可以根据需求设置不同的阈值和预警颜色: ``` initChart() { this.chart = echarts.init(this.$refs.chartContainer) const option = { // 省略其他配置 series: [{ type: 'bar', data: [10, 20, 30, 40, 50], itemStyle: { normal: { color: function(params) { // 根据条件判断是否触发预警变色 if (params.value > 30) { return 'red'; // 设置预警颜色为红色 } else { return 'blue'; // 设置其他柱形的颜色 } } } } }] } this.chart.setOption(option) } ``` 以上就是在Vue使用echarts柱形图预警变色的简单实现方式。根据具体需求,可以根据实际情况调整代码和配置,实现更灵活的柱形图预警变色效果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值