<template>
  <div class="gy-chart chart" data-title="本场考试成绩统计">
  </div>
</template>

<script>
import { Chart } from '@antv/g2'

import { findStatisticDataApi } from '@/api/statistic'

export default {
  props: {
    exam: {}
  },
  watch: {
    exam: {
      deep: true,
      immediate: true,
      handler (nv) {
        nv && this.$nextTick(() => { this.findData(nv.examId) })
      }
    }
  },
  methods: {
    async findData (id) {
      if (this.chart) {
        this.chart.destroy()
      }
      this.chart = new Chart({
        container: this.$el,
        autoFit: true,
        height: 500,
        padding: [50, 20, 50, 20]
      })
      this.chart.scale('value', {
        alias: '分数'
      })
      const data = await findStatisticDataApi('exam-details-score-order-tower', { noCatch: true, examId: id })
      // 添加文本标注
      data.forEach((item) => {
        this.chart
          .annotation()
          .text({
            position: [item.type, item.value],
            content: item.value,
            style: {
              textAlign: 'center'
            },
            offsetY: -5
          })
      })
      this.chart.data(data)
      this.chart.axis('type', {
        tickLine: {
          alignTick: false
        }
      })
      this.chart.axis('value', false)

      this.chart.tooltip({
        showMarkers: false
      })
      this.chart.interval().position('type*value')
      this.chart.interaction('element-active')
      this.chart.render()
    }
  }

}
</script>
