Echarts实现一阶混淆矩阵

194 阅读1分钟

vue版本2.0,本地引用Echarts,粘贴进去即可运行。

  1. js代码如下:
<script>

const chartList = ['matrix']

export default {

  name'Matrix',

  props: {

    dataParam: {

      typeArray,

    },

  },

  data() {

    return {

      myChartnull,

      echartsEmptyfalse,

    }

  },

  mounted() {

    this.getData()

  },

  methods: {

    getData() {

      this.getMatrix()

    },

    getMatrix() {

      let xAxisArr = [],

        data_1 = [],

        data_2 = []

      this.dataParam.map((item) => {

        xAxisArr.push(item.label)

      })

      this.dataParam[0].value.map((item) => {

        let obj = {}

        obj.name = item.value

        obj.value = 1

        data_1.push(obj)

      })

      this.dataParam[1].value.map((item) => {

        let obj = {}

        obj.name = item.value

        obj.value = 1

        data_2.push(obj)

      })

      const instance = this.getInstance('matrix')

      instance.setOption({

        grid: {

          top'20%',

          left'20%',

          right'5%',

          bottom'5%',

          width'70%',

          height'70%',

        },

        xAxis: {

          data: xAxisArr,

          axisLabel: {

            margin15,

          },

          axisLine: {

            showfalse,

          },

          axisTick: {

            showfalse,

          },

          position'top',

          name'predict',

          nameLocation'center',

          nameGap35,

        },

        yAxis: {

          data: xAxisArr.slice().reverse(),

          axisLabel: {

            margin15,

          },

          axisLine: {

            showfalse,

          },

          axisTick: {

            showfalse,

          },

          name'real',

          nameLocation'center',

          nameRotate90,

          nameGap35,

        },

        series: [

          {

            type'treemap',

            width'70%',

            height'70%',

            top'20%',

            left'20%',

            right'5%',

            bottom'5%',

            roamfalse,

            nodeClickfalse,

            breadcrumb: {

              showfalse,

            },

            levels: [

              {

                itemStyle: {

                  gapWidth4,

                },

              },

              {

                itemStyle: {

                  gapWidth4,

                },

              },

            ],

            data: [

              {

                name'nodeA',

                value2,

                color: ['#5470c6', '#91cc75'],

                children: data_2.splice(0).reverse(),

              },

              {

                name'nodeB',

                color: ['#fac858', '#fc8452'],

                value2,

                children: data_1.splice(0).reverse(),

              },

            ],

          },

        ],

      })

    },

    // 获取图表实例

    getInstance(item) {

      const ele = this.$refs[item]

      let instance = this.$echarts.getInstanceByDom(ele)

      if (!instance) {

        instance = this.$echarts.init(ele)

      }

      return instance

    },

  },

}

</script>
  1. html代码如下:
<!--混淆矩阵-->

<template>

  <div class="matrix">

    <div class="grid-header">

      <span class="grid-title">混淆矩阵</span>

      <el-tooltip

        content=""

        placement="top"

        effect="light"

      >

      </el-tooltip>

    </div>

    <div ref="matrix" class="item-echart">

      <el-empty v-if="echartsEmpty"></el-empty>

    </div>

  </div>

</template>
  1. css代码如下:
<style lang="scss" scoped>

.item-echart {

  width: 300px;

  height: 300px;

  margin: 0 50px;

}

.grid-header {

  flex: none;

  padding: 30px 30px 0 30px;

}

 

.grid-title {

  font-size: 16px;

  color: #333;

  font-weight: 500;

}

 

.grid-content {

  flex: 1;

}

</style>
  1. 运行截图:

predict.png