charts.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <view>
  3. <canvas
  4. canvas-id="orIfOOkumJYtFcnuJkjxcySENqRtXceW"
  5. id="orIfOOkumJYtFcnuJkjxcySENqRtXceW"
  6. class="charts"
  7. @touchstart="touchstart"
  8. @touchmove="touchmove"
  9. @touchend="touchend"
  10. />
  11. </view>
  12. </template>
  13. <script>
  14. import uCharts from "../../../components/js_sdk/u-charts/u-charts/u-charts.js";
  15. var uChartsInstance = {};
  16. export default {
  17. data() {
  18. return {
  19. cWidth: 320,
  20. cHeight: 200,
  21. };
  22. },
  23. onReady() {
  24. //这里的 750 对应 css .charts 的 width
  25. // this.cWidth = uni.upx2px(340);
  26. //这里的 500 对应 css .charts 的 height
  27. // this.cHeight = uni.upx2px(200);
  28. this.getServerData();
  29. },
  30. methods: {
  31. getServerData() {
  32. //模拟从服务器获取数据时的延时
  33. setTimeout(() => {
  34. //模拟服务器返回数据,如果数据格式和标准格式不同,需自行按下面的格式拼接
  35. let res = {
  36. categories: ["2018", "2019", "2020", "2021", "2022", "2023"],
  37. series: [
  38. {
  39. name: "成交量A",
  40. linearColor: [
  41. [0, "#1890FF"],
  42. [0.25, "#00B5FF"],
  43. [0.5, "#00D1ED"],
  44. [0.75, "#00E6BB"],
  45. [1, "#90F489"],
  46. ],
  47. data: [35, 8, 25, 37, 4, 20],
  48. },
  49. ],
  50. };
  51. this.drawCharts("orIfOOkumJYtFcnuJkjxcySENqRtXceW", res);
  52. }, 500);
  53. },
  54. drawCharts(id, data) {
  55. const ctx = uni.createCanvasContext(id, this);
  56. uChartsInstance[id] = new uCharts({
  57. type: "line",
  58. context: ctx,
  59. width: this.cWidth,
  60. height: this.cHeight,
  61. categories: data.categories,
  62. series: data.series,
  63. animation: true,
  64. background: "#FFFFFF",
  65. color: [
  66. "#1890FF",
  67. "#91CB74",
  68. "#FAC858",
  69. "#EE6666",
  70. "#73C0DE",
  71. "#3CA272",
  72. "#FC8452",
  73. "#9A60B4",
  74. "#ea7ccc",
  75. ],
  76. padding: [10, 10, 10, 10],
  77. enableScroll: true,
  78. legend: {
  79. position: "top",
  80. },
  81. xAxis: {
  82. disableGrid: true,
  83. scrollShow: true,
  84. itemCount: 4,
  85. },
  86. yAxis: {
  87. gridType: "dash",
  88. dashLength: 2,
  89. },
  90. dataLabel: false,
  91. extra: {
  92. line: {
  93. type: "curve",
  94. width: 2,
  95. activeType: "hollow",
  96. },
  97. },
  98. });
  99. },
  100. touchstart(e) {
  101. uChartsInstance[e.target.id].scrollStart(e);
  102. },
  103. touchmove(e) {
  104. uChartsInstance[e.target.id].scroll(e);
  105. },
  106. touchend(e) {
  107. uChartsInstance[e.target.id].scrollEnd(e);
  108. uChartsInstance[e.target.id].touchLegend(e);
  109. uChartsInstance[e.target.id].showToolTip(e);
  110. },
  111. },
  112. };
  113. </script>
  114. <style scoped>
  115. .charts {
  116. width: 750rpx;
  117. height: 500rpx;
  118. }
  119. </style>