chart-line.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. data.categories.push('');
  23. data.categories.unshift('');
  24. data.series[0].data.push(null);
  25. data.series[0].data.unshift(null);
  26. this.line = new uCharts({
  27. $this: this, // this
  28. type: 'line', // 类型
  29. canvasId: 'line', // id
  30. colors: [this.color ? this.color : '#F76454'], // 颜色
  31. width: uni.upx2px(688), // 宽
  32. height: uni.upx2px(567), // 高
  33. fontSize: uni.upx2px(28), // 字体大小
  34. enableScroll: true, // 是否可以拖拽
  35. enableMarkLine: true, // 是否显示辅助线
  36. animation: true, //展示动画
  37. legend: {
  38. show: false
  39. },
  40. extra: {
  41. line: {
  42. type: 'curve',
  43. width: uni.upx2px(8)
  44. }
  45. },
  46. dataPointShape: true, // 是否在图表中显示数据点图形标识
  47. dataPointShapeType: 'hollow', // 数据点图形标识类型 可选值:实心solid、空心hollow
  48. background: this.background,
  49. categories: data.categories,
  50. series: data.series,
  51. xAxis: {
  52. itemCount: 7, // 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. },
  62. yAxis: {
  63. min: 0,
  64. disabled: false,
  65. format: val => '',
  66. gridColor: '#FFFFFF',
  67. max: Math.max.apply(Math, data.series[0].data) + 123
  68. }
  69. });
  70. },
  71. touchLine(e) {
  72. this.line.scrollStart(e);
  73. },
  74. moveLine(e) {
  75. this.line.scroll(e);
  76. },
  77. touchEndLine(e) {
  78. this.line.scrollEnd(e);
  79. }
  80. }
  81. };
  82. </script>
  83. <style lang="scss" scoped>
  84. .chart-line {
  85. width: 100%;
  86. height: 100%;
  87. position: relative;
  88. .chart {
  89. width: 100%;
  90. height: 100%;
  91. }
  92. }
  93. </style>