weixin_58412143 2023-02-13 14:30 采纳率: 91.6%
浏览 215
已结题

echarts 柱状图如何排序

1、echarts 柱状图如何排序?
下面这个index设置不管用

datasetIndex: 1

2、排序之后如何根据高低给每个柱条设置不同的颜色?visualMap这个吗?
有这个错加上之后,Error in nextTick: "SyntaxError: Failed to execute 'addColorStop' on 'CanvasGradient': The value provided ('undefined') could not be parsed as a color."

完整代码:

<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
  import echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  // import resize from './mixins/resize'

  export default {
    // mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '450px'
      },
      autoResize: {
        type: Boolean,
        default: true
      },
      chartData: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        chart: null
      }
    },
    watch: {
      chartData: {
        deep: true,
        handler(val) {
          this.setOptions(val)
        }
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')
        this.setOptions(this.chartData)
      
        console.log('销售数量',this.chartData.Brand.amountData)
      },
      setOptions({
        brandNameData,
        amountData,
        timeAxisFieldData
      } = {}) {
        this.chart.setOption({
          dataset: [{
              dimensions: ['品牌名称', 'date', '销售数量'],
              source: this.chartData.Brand.amountData,
              // source: [
              //   ['MilkTea', '2011', 58],
              //   ['Tea', '2011', 12],
              //   ['Miea', '2011', 212]
              // ]
            },
            {
              transform: {
                type: 'sort',
                config: {
                  dimension: '销售数量',
                  order: 'asc'
                  // order: 'desc'
                }
              }
            },
          ],
          xAxis: {
            name: '销售数量',
            axisLabel: {
              interval: 0,
              rotate: 30
            },
          },
          yAxis: {
            name: '品牌名称',
            type: 'category',
          },
          // visualMap: {
          //   orient: 'horizontal',
          //   left: 'center',
          //   min: 0,
          //   max: this.chartData.Brand.salesVolumesMax,
          //   // max: this.chartData.Brand.salesVolumesMax,
          //   text: ['高', '低'],
          //   dimension: 0,
          //   inRange: {
          //     color: ['#65B581', '#FFCE34', '#FD665F']
          //   }
          // },
          series: {
            type: 'bar',
            encode: {
              x: '销售数量',
              y: '品牌名称'
            },
            datasetIndex: 1
            // itemStyle: {
            //       normal: {
            //         color: function (params) {
            //           // 给出颜色组
            //           var colorList = ['#cca272', '#74608f'];
            //           //循环调用
            //           return colorList[params.dataIndex % colorList.length];
            //         }
            //       }
            //     }
          }
        })
      }
    }
  }
</script>


  • 写回答

3条回答 默认 最新

  • Yang2023. 2023-02-13 14:33
    关注

    1、echarts柱状图可以通过设置datasetIndex属性来排序,比如datasetIndex: 1,表示优先使用第1个数据集进行排序。
    2、可以使用visualMap来设置柱条的颜色,比如:

    visualMap: {
      orient: 'horizontal',
      left: 'center',
      min: 0,
      max: this.chartData.Brand.salesVolumesMax,
      // max: this.chartData.Brand.salesVolumesMax,
      text: ['高', '低'],
      dimension: 0,
      inRange: {
        color: ['#65B581', '#FFCE34', '#FD665F']
      }
    },
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月15日
  • 已采纳回答 4月7日
  • 创建了问题 2月13日

悬赏问题

  • ¥15 已知平面坐标系(非直角坐标系)内三个点的坐标,反求两坐标轴的夹角
  • ¥15 webots有问题,无响应
  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上