cap-line.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <view class="chart-line">
  3. <canvas canvas-id="line" id="line" class="chart" disable-scroll="true" @touchstart="touchLine"
  4. @touchmove="moveLine" @touchend="touchEndLine"></canvas>
  5. </view>
  6. </template>
  7. <script>
  8. import uCharts from './js/u-charts.js';
  9. export default {
  10. props: {
  11. unit: String,
  12. color: String,
  13. chartData: Object,
  14. background: String
  15. },
  16. watch: {
  17. chartData() {
  18. this.draw(this.chartData);
  19. }
  20. },
  21. methods: {
  22. draw(data) {
  23. // console.log(data.series, 1111);
  24. this.line = new uCharts({
  25. $this: this, // this
  26. type: 'line', // 类型
  27. canvasId: 'line', // id
  28. colors: [this.color ? this.color : '#F76454'], // 颜色
  29. width: uni.upx2px(688), // 宽
  30. height: uni.upx2px(567), // 高
  31. fontSize: uni.upx2px(28), // 字体大小
  32. enableScroll: true, // 是否可以拖拽
  33. enableMarkLine: true, // 是否显示辅助线
  34. animation: true, //展示动画
  35. legend: {
  36. show: true
  37. },
  38. extra: {
  39. line: {
  40. type: 'curve',
  41. width: uni.upx2px(5)
  42. }
  43. },
  44. dataLabel: false,
  45. dataPointShape: true, // 是否在图表中显示数据点图形标识
  46. dataPointShapeType: 'solid', // 数据点图形标识类型 可选值:实心solid、空心hollow
  47. background: this.background,
  48. categories: data.categories,
  49. series: data.series,
  50. xAxis: {
  51. type: 'grid',
  52. itemCount: 5, // X轴可见区域数据数量(即X轴数据密度),配合拖拽滚动使用(即仅在启用enableScroll时有效)
  53. scrollShow: true, // 是否显示滚动条
  54. disableGrid: true, // 不绘制 X 轴网格线
  55. scrollAlign: 'right', // 滚动条起始位置
  56. fontColor: this.color, // X 轴文字颜色
  57. // boundaryGap: 'justify', // 折线图、区域图起画点结束点方法:center为单元格中间起画,justify为0点起画即两端对齐
  58. scrollColor: this.color, // X轴滚动条颜色
  59. axisLineColor: '#CCCCCC', // 坐标轴轴线颜色
  60. scrollBackgroundColor: this.background, // X轴滚动条背景颜色
  61. disabled: false,
  62. // rotateLabel:true,
  63. format: v => {
  64. var date = new Date(v);
  65. return `${('0' + date.getHours()).slice(-2)}:${('0' + date.getMinutes()).slice(-2)}`;
  66. }
  67. },
  68. yAxis: {
  69. min: 0,
  70. disabled: false,
  71. format: val => {
  72. return val;
  73. },
  74. gridColor: '#ccc',
  75. max: Math.max.apply(Math, data.series[0].data) + 123,
  76. gridType: 'dash'
  77. }
  78. });
  79. },
  80. touchLine(e) {
  81. this.line.scrollStart(e);
  82. },
  83. moveLine(e) {
  84. this.line.scroll(e);
  85. },
  86. touchEndLine(e) {
  87. this.line.scrollEnd(e);
  88. }
  89. }
  90. };
  91. </script>
  92. <style lang="scss" scoped>
  93. .chart-line {
  94. width: 100%;
  95. height: 100%;
  96. position: relative;
  97. z-index: 99;
  98. .chart {
  99. width: 100%;
  100. height: 100%;
  101. }
  102. }
  103. </style>