quest-chart.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <div id="quest-chart">
  3. <el-row type="flex" align="middle" justify="start" class="btn_bar">
  4. <el-col :span="10"> {{ data.topic }} [{{ data.type | topicType }}] </el-col>
  5. </el-row>
  6. <template v-if="data.type != 2">
  7. <el-row type="flex" align="middle" justify="center" v-if="btnModel.table">
  8. <el-col :span="16">
  9. <el-table :data="data.option" size="mini" border stripe>
  10. <el-table-column align="center" label="选项" prop="opname"></el-table-column>
  11. <el-table-column align="center" label="小计" prop="value"></el-table-column>
  12. <el-table-column align="center" label="比例">
  13. <template v-slot="{ row, $index }">
  14. <el-progress :text-inside="true" :stroke-width="20" :percentage="getPercent(row.value)" :color="getPS(getPercent(row.value))"></el-progress>
  15. </template>
  16. </el-table-column>
  17. </el-table>
  18. </el-col>
  19. </el-row>
  20. <el-row type="flex" align="middle" justify="center" class="btn_bar" :gutter="10" v-if="!nodata">
  21. <el-col :span="3" v-for="(i, index) in btnList" :key="index">
  22. <el-button :type="btnModel[i.model] ? 'primary' : ''" size="mini" @click="changeModel(i.model)">
  23. {{ i.text }}
  24. </el-button>
  25. </el-col>
  26. </el-row>
  27. <el-row type="flex" align="middle" justify="center">
  28. <el-col :span="16" v-if="btnModel.column">
  29. <g2column :data="data.option" :axis="axis" :gid="data._id"></g2column>
  30. </el-col>
  31. <el-col :span="16" v-if="btnModel.pie">
  32. <g2pie :data="data.option" :axis="axis" :gid="data._id"></g2pie>
  33. </el-col>
  34. <el-col :span="16" v-if="btnModel.donut">
  35. <g2donut :data="data.option" :axis="axis" :gid="data._id"></g2donut>
  36. </el-col>
  37. <el-col :span="16" v-if="btnModel.bar">
  38. <g2bar :data="data.option" :axis="axis" :gid="data._id"></g2bar>
  39. </el-col>
  40. </el-row>
  41. </template>
  42. <template v-else>
  43. <div style="margin:10px">见详情</div>
  44. </template>
  45. </div>
  46. </template>
  47. <script>
  48. import _ from 'lodash';
  49. import g2column from '@frame/parts/statistics/column.vue';
  50. import g2pie from '@frame/parts/statistics/pie.vue';
  51. import g2donut from '@frame/parts/statistics/donut.vue';
  52. import g2bar from '@frame/parts/statistics/bar.vue';
  53. import { mapState, createNamespacedHelpers } from 'vuex';
  54. export default {
  55. name: 'quest-chart',
  56. props: {
  57. data: { type: [Object, Array], default: () => [] },
  58. nodata: { type: Boolean, default: false },
  59. },
  60. components: { g2column, g2pie, g2donut, g2bar },
  61. data: function() {
  62. return {
  63. axis: {
  64. x: 'opname',
  65. xAlias: '选项',
  66. y: 'value',
  67. yAlias: '数量',
  68. },
  69. btnModel: {
  70. table: true,
  71. pie: false,
  72. dount: false,
  73. column: false,
  74. bar: false,
  75. },
  76. btnList: [
  77. { text: '表格', model: 'table' },
  78. { text: '饼状图', model: 'pie' },
  79. { text: '圆环图', model: 'donut' },
  80. { text: '柱状图', model: 'column' },
  81. { text: '条形图', model: 'bar' },
  82. ],
  83. };
  84. },
  85. created() {},
  86. methods: {
  87. changeModel(model) {
  88. if (model !== 'table') {
  89. let keys = Object.keys(this.btnModel);
  90. for (const key of keys) {
  91. if (key != 'table' && key != model) {
  92. this.btnModel[key] = false;
  93. }
  94. }
  95. }
  96. this.btnModel[model] = !this.btnModel[model];
  97. this.$forceUpdate();
  98. },
  99. getPercent(num) {
  100. let all = this.data.option.reduce((prev, next) => prev + next.value * 1 || 0, 0);
  101. if (all == 0) return 0;
  102. let percent = _.round(_.divide(num * 1, all), 4);
  103. percent = _.round(_.round(percent, 4) * 100, 2);
  104. return percent;
  105. },
  106. getPS(percent) {
  107. if (percent >= 70) return 'red';
  108. if (_.inRange(percent, 30, 70)) return 'blue';
  109. if (percent <= 30) return 'gray';
  110. },
  111. },
  112. computed: {
  113. ...mapState(['user']),
  114. pageTitle() {
  115. return `${this.$route.meta.title}`;
  116. },
  117. },
  118. filters: {
  119. topicType(val) {
  120. if (val == 0) return '单选题';
  121. if (val == 1) return '多选题';
  122. if (val == 2) return '简答题';
  123. },
  124. },
  125. metaInfo() {
  126. return { title: this.$route.meta.title };
  127. },
  128. };
  129. </script>
  130. <style lang="less" scoped></style>