cap-line.vue 2.7 KB

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