fluency.vue 6.03 KB
<!-- 流利度 -->
<template>
  <div>
    <div class="box">
      <div class="box-item">
        <div class="left">
          <div class="title">语速</div>
          <div class="value">{{ speedData.starText }}</div>
        </div>
        <div class="line"></div>
        <div class="content-text">{{ speedData.starDescribe }}</div>
      </div>
      <div class="box-item">
        <div class="left">
          <div class="title">冗余词</div>
          <div class="value">{{ wordsData.wordNum }}个</div>
        </div>
        <div class="line"></div>
        <div class="content-text">{{ wordsData.resultWords }}</div>
      </div>
    </div>
    <div class="chart-box">
      <div class="wave-chart" id="wave-chart"></div>
      <div class="pie-chart">
        <div v-if="wordsData.wordNum > 0" class="pie-chart" id="pie-chart"></div>
        <div class="empty-box" v-else>
          <div class="icon">
            <img src="@/assets/empty-3.png" />
          </div>
          <div class="text">
            无冗余词
          </div>
        </div>
      </div>
      
    </div>
  </div>
</template>
<script>
import chartOption from './chartOption'
import * as echarts from 'echarts'
import contantData from './contantData'
export default {
  name: 'fluency',
  props: {
    itemData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      wordsData: {},
      speedData: {}
    }
  },
  mounted() {
    this.initData()
  },
  methods: {
    initData() {
      this.filterWordsData()
      this.filterSpeedData()
      this.$nextTick(() => {
        window.addEventListener("resize", () => this.resize())
      })
    },
    resize() {
      this.waveChart && this.waveChart.resize()
      this.pieChart && this.pieChart.resize()
    },
    // 处理冗余词数据
    filterWordsData() {
      const toneWords = contantData.toneWords
      const toneList = contantData.toneList
      const {redundantWords} = this.itemData.fluency
      let words = ''
      try {
        words = JSON.parse(redundantWords).content
      } catch (error) {
      }
      const wordCountMap = new Map();
      toneWords.forEach(word => {
        const count = (words.match(new RegExp(word, 'g')) || []).length;
        if(count>0) {
          wordCountMap.set(word, count);
        }
        
      });
      const positionX = [...wordCountMap.keys()]
      const positionY = [...wordCountMap.values()];
      const wordNum = positionY.reduce((cur, prev) => {
        return cur + prev
      }, 0)
      let star = 5 - Math.ceil(wordNum / 2)
      if (star < 1) {
        star = 1
      }
      this.wordsData = {
        wordNum,
        resultWords: toneList[star - 1],
        positionX,
        positionY
      }
      if (wordNum > 0) this.initPieChart()
    },
    // 处理语速数据
    filterSpeedData() {
      const { speechSpeed } = this.itemData.fluency
      let data = {}
      try {
        data = JSON.parse(speechSpeed)
      } catch (error) {
      }
      this.speedData = {
        starText: contantData.speedText[data?.star - 1],
        starDescribe: contantData.speedDescribe[data?.star - 1],
        x: data?.x,
        y: data?.y,
        min: data?.min,
        max: data?.max
      }
      if (data?.x && data?.y) this.initWaveChart()
    },
    initWaveChart() {
      this.$nextTick(() => {
        this.waveChart = echarts.init(document.getElementById('wave-chart'))
        chartOption.waveChartOption.xAxis.data = this.speedData.x
        chartOption.waveChartOption.yAxis.max = this.speedData.max > Math.max(...this.speedData.y) ? this.speedData.max : Math.max(...this.speedData.y)
        chartOption.waveChartOption.series[0].data = this.speedData.y
        chartOption.waveChartOption.series[0].markArea.data = [
          [{ xAxis: this.speedData.x[0], yAxis: this.speedData.min }, { xAxis: this.speedData.x[this.speedData.x.length - 1], yAxis: this.speedData.max }]
        ]
        chartOption.waveChartOption.xAxis.axisLabel.formatter = (value, index) => {
          if (index == 0 || index == this.speedData.x.length - 1) {
            return value
          } else {
            return ''
          }
        }
        this.waveChart.setOption(chartOption.waveChartOption)
      })
    },
    initPieChart() {
      this.$nextTick(() => {
        let result = this.wordsData?.positionX.map((name, index) => {
          let color = contantData.colorContentData.find((item) => item.name == name)?.color
          return { name: name, value: this.wordsData?.positionY[index], itemStyle: {color: color} };
        })
        this.pieChart = echarts.init(document.getElementById('pie-chart'))
        chartOption.pieChartOption.series[0].data = result
        this.pieChart.setOption(chartOption.pieChartOption)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.box{
  display: flex;
  .box-item{
    flex: 1;
    box-sizing: border-box;
    height: 92px;
    padding: 16px 20px;
    margin-right: 16px;
    display: flex;
    align-items: center;
    color: #333333;
    background: #FBFBFB;
    border-radius: 4px;
    &:last-child{
      margin: 0;
    }
    .left{
      flex-shrink: 0;
      width: 100px;
      .title{
        font-weight: 14px;
        margin-bottom: 8px;
      }
      .value{
        font-weight: 700;
        font-size: 22px;
      }
    }
    .line{
      flex-shrink: 0;
      width: 1px;
      height: 44px;
      background: #E5E5E5;
      margin: 0 32px;
    }
    .content-text{
      color: #999EA8;
      font-size: 12px;
    }
  }
}
.chart-box{
  height: 216px;
  display: flex;
  margin-top: 12px;
  .wave-chart{
    box-sizing: border-box;
    height: 100%;
    flex: 1;
    margin-right: 16px;
    padding: 8px 16px;
  }
  .pie-chart{
    height: 100%;
    flex: 1;
  }
  .empty-box {
    display: flex;
    flex-direction: column;
    font-size: 12px;
    color: #999999;
    text-align: center;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    border-radius: 0 4px 4px 4px;
    .icon {
      width: 270px;
      height: 120px;
      margin: 0 auto 8px;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}
</style>