score-chart.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <view class="score-chart">
  3. <view :class="['wrap', {'is-odd': isOdd(i)}]" v-for="(item,i) in labels">
  4. <view v-if="!isOdd(i)" class="label">{{ labels[i] }}</view>
  5. <view class="score-wrap">
  6. <view class="bg" :style="{width: scores[i] * ratio + 'rpx'}">
  7. <text class="value">{{ scores[i] }}</text>
  8. </view>
  9. </view>
  10. <view v-if="isOdd(i)" class="label">{{ labels[i] }}</view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. props: {
  17. scores: {
  18. type: Array,
  19. default: () => []
  20. }
  21. },
  22. computed: {
  23. ratio() {
  24. const totalWidth = (750 - 72 - 20 - 40) / 2;
  25. return totalWidth / (Math.max(...this.scores) + 5);
  26. }
  27. },
  28. data() {
  29. return {
  30. labels: ['E', 'I', 'S', 'N', 'T', 'F', 'J', 'P']
  31. }
  32. },
  33. methods: {
  34. isOdd(i) {
  35. return (i + 1) % 2 === 0;
  36. }
  37. }
  38. }
  39. </script>
  40. <style lang="scss" scoped>
  41. .score-chart {
  42. display: flex;
  43. align-items: center;
  44. flex-wrap: wrap;
  45. column-gap: 20rpx;
  46. row-gap: 10rpx;
  47. }
  48. .wrap {
  49. display: flex;
  50. align-items: center;
  51. justify-content: space-between;
  52. width: calc(50% - 10rpx);
  53. }
  54. .score-wrap {
  55. width: 1px;
  56. flex: 1;
  57. height: 16px;
  58. display: flex;
  59. justify-content: flex-end;
  60. }
  61. .label {
  62. width: 40rpx;
  63. font-weight: 600;
  64. }
  65. .bg {
  66. height: 100%;
  67. background-color: #F2FBD3;
  68. position: relative;
  69. display: flex;
  70. align-items: center;
  71. .value {
  72. color: #4A4A4A;
  73. font-size: 12px;
  74. padding-left: 4px;
  75. font-weight: 500;
  76. }
  77. }
  78. .is-odd {
  79. .score-wrap {
  80. justify-content: flex-start;
  81. }
  82. .bg {
  83. justify-content: flex-end;
  84. .value {
  85. padding-right: 4px;
  86. }
  87. }
  88. }
  89. </style>