HorizontalChart.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <div ref="Horizontal" class="mid"></div>
  3. </template>
  4. <script>
  5. import * as echarts from "echarts";
  6. export default {
  7. props: ["data"],
  8. methods: {
  9. setHorizontalData() {
  10. const Horizontal = echarts.init(this.$refs["Horizontal"]);
  11. let option = {
  12. grid: {
  13. top: 80, // 设置网格顶部的边距为 50px
  14. left: 10,
  15. bottom: 0,
  16. right: "100px",
  17. // left:'-10px'
  18. },
  19. title: {
  20. text: "创客活动资金",
  21. },
  22. tooltip: {
  23. trigger: "item",
  24. },
  25. dataset: {
  26. source: [["product", "amount"], ...this.data],
  27. },
  28. xAxis: {
  29. position: "top",
  30. name: "单位:元",
  31. nameTextStyle: {
  32. fontSize: 14,
  33. align: "left",
  34. verticalAlign: "bottom",
  35. padding: [0, 0, 7, 10],
  36. },
  37. },
  38. yAxis: {
  39. type: "category",
  40. zlevel: 1,
  41. axisLabel: {
  42. inside: true,
  43. margin: 4,
  44. // formatter: function (value) {
  45. // const vl = Math.floor(value.length/2);
  46. // let newText = value.slice(0,vl)+'\n'+value.slice(vl)
  47. // return newText;
  48. // },
  49. color: function (value, index) {
  50. const textWidth =
  51. option["dataset"]["source"][
  52. option["dataset"]["source"].length - 1
  53. ][1] /
  54. 6 /
  55. 2;
  56. const vl = option["dataset"]["source"][index + 1][1];
  57. return vl >= textWidth ? "white" : "#3c4654";
  58. },
  59. },
  60. },
  61. series: [
  62. {
  63. type: "bar",
  64. barWidth: 55,
  65. label: {
  66. show: true,
  67. position: "right",
  68. },
  69. encode: {
  70. x: "amount",
  71. y: "product",
  72. },
  73. itemStyle: {
  74. color: "#3eb370",
  75. },
  76. },
  77. ],
  78. };
  79. Horizontal.setOption(option);
  80. },
  81. },
  82. mounted() {
  83. this.setHorizontalData();
  84. console.log(this.HorizontalData);
  85. },
  86. };
  87. </script>
  88. <style lang="less" scoped>
  89. </style>