bilateralBarChart.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <div>
  3. <el-button-group>
  4. <el-button v-if="tab === '0'" size="small" type="primary">
  5. 收视率
  6. </el-button>
  7. <el-button v-else size="small" @click="()=>change('0')">
  8. 收视率
  9. </el-button>
  10. <el-button v-if="tab === '1'" size="small" type="primary">
  11. 市占率
  12. </el-button>
  13. <el-button v-else size="small" @click="()=>change('1')">
  14. 市占率
  15. </el-button>
  16. </el-button-group>
  17. <div class="bilateralBarChart" ref="bilateralBarChart"></div>
  18. </div>
  19. </template>
  20. <script>
  21. import * as echarts from "echarts";
  22. export default {
  23. name: "bilateralBarChart",
  24. props: ["list", "keyName"],
  25. watch: {
  26. list() {
  27. if (this.chart) this.chart.dispose();
  28. this.createChart();
  29. },
  30. },
  31. data: function() {
  32. return {
  33. leftList: [],
  34. rightList: [],
  35. chart: undefined,
  36. tab: "0"
  37. };
  38. },
  39. filters: {},
  40. methods: {
  41. change(type){
  42. this.tab = type;
  43. if(!this.chart) return;
  44. let source = this.formatte();
  45. this.chart.setOption({
  46. title: [
  47. {
  48. text: this.tab === '0' ? "收视率" : "市占率",
  49. left: "center",
  50. textAlign: "center",
  51. },
  52. ],
  53. yAxis: [
  54. {
  55. type: "category",
  56. gridIndex: 0,
  57. data: source[this.tab].texts.reverse(),
  58. }
  59. ],
  60. series: [
  61. {
  62. data: source[this.tab].value.reverse(),
  63. type: "bar",
  64. label: {
  65. show: true,
  66. position: "right",
  67. formatter: arr => {
  68. return this.read(arr.value).toFixed(4) + "%";
  69. },
  70. },
  71. backgroundStyle: {
  72. color: "rgba(180, 180, 180, 0.2)",
  73. },
  74. },
  75. ],
  76. })
  77. },
  78. read(v) {
  79. if (isNaN(v)) return 0;
  80. let o = v;
  81. if (v > 10000) {
  82. o = (v / 10000).toFixed(4) + "万";
  83. }
  84. return o;
  85. },
  86. formatte() {
  87. let source = [
  88. { texts: [], value: [] },
  89. { texts: [], value: [] },
  90. ];
  91. let list = JSON.parse(JSON.stringify(this.list));
  92. this.leftList = JSON.parse(
  93. JSON.stringify(list.sort((a, b) => b.tv_ratings - a.tv_ratings))
  94. ).splice(0, 30);
  95. this.rightList = JSON.parse(
  96. JSON.stringify(list.sort((a, b) => b.market_ratings - a.market_ratings))
  97. ).splice(0, 30);
  98. this.leftList.map(v => {
  99. source[0].texts.push(v[this.keyName]);
  100. source[0].value.push(v.tv_ratings * 100);
  101. });
  102. this.rightList.map(v => {
  103. source[1].texts.push(v[this.keyName]);
  104. source[1].value.push(v.market_ratings * 100);
  105. });
  106. return source;
  107. },
  108. createChart() {
  109. if (!this.$refs.bilateralBarChart) return;
  110. let source = this.formatte();
  111. this.chart = echarts.init(this.$refs.bilateralBarChart);
  112. this.chart.resize({
  113. height: (this.$refs.bilateralBarChart.offsetWidth * 9) / 16,
  114. });
  115. var option = ({
  116. title: [
  117. {
  118. text: this.tab === '0' ? "收视率" : "市占率",
  119. left: "center",
  120. textAlign: "center",
  121. },
  122. ],
  123. tooltip: {
  124. formatter: arr => {
  125. return arr.name + ":" + this.read(arr.value).toFixed(4) + "%";
  126. },
  127. },
  128. yAxis: [
  129. {
  130. type: "category",
  131. gridIndex: 0,
  132. data: source[this.tab].texts.reverse(),
  133. interval:0
  134. }
  135. ],
  136. xAxis: [
  137. { gridIndex: 0, position: "top" },
  138. ],
  139. grid: { left: 150, top: "15%", buttom: 0, right: "10%" },
  140. series: [
  141. {
  142. data: source[this.tab].value.reverse(),
  143. type: "bar",
  144. label: {
  145. show: true,
  146. position: "right",
  147. formatter: arr => {
  148. return this.read(arr.value).toFixed(4) + "%";
  149. },
  150. },
  151. backgroundStyle: {
  152. color: "rgba(180, 180, 180, 0.2)",
  153. },
  154. },
  155. ],
  156. });
  157. option && this.chart.setOption(option);
  158. },
  159. },
  160. mounted() {
  161. this.createChart();
  162. },
  163. beforeUnmount: function() {
  164. this.chart.dispose();
  165. },
  166. components: {},
  167. };
  168. </script>
  169. <style></style>