area-chart.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <view class="chart-line">
  3. <canvas canvas-id="area" id="area" 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, 'data')
  24. this.area = new uCharts({
  25. $this: this, // this
  26. type: 'area', // 类型
  27. canvasId: 'area', // id
  28. colors: [this.color ? this.color : '#FB231F'], // 颜色
  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. area: {
  40. type: 'curve',
  41. opacity: 0.3,
  42. }
  43. },
  44. dataLabel: true, //是否显示图表区域内数据点上方的数据文案
  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. gridColor: '#CCCCCC',
  53. gridType: 'dash',
  54. dashLength: 8,
  55. // type: 'grid',
  56. itemCount: 5, // X轴可见区域数据数量(即X轴数据密度),配合拖拽滚动使用(即仅在启用enableScroll时有效)
  57. scrollShow: true, // 是否显示滚动条
  58. // disableGrid: true, // 不绘制 X 轴网格线
  59. scrollAlign: 'right', // 滚动条起始位置
  60. fontColor: this.color, // X 轴文字颜色
  61. scrollColor: this.color, // X轴滚动条颜色
  62. axisLineColor: '#CCCCCC', // 坐标轴轴线颜色
  63. scrollBackgroundColor: this.background, // X轴滚动条背景颜色
  64. // disabled: false,
  65. // format: v => {
  66. // var date = new Date(v);
  67. // return `${('0' + date.getHours()).slice(-2)}:${('0' + date.getMinutes()).slice(-2)}`;
  68. // }
  69. },
  70. yAxis: {
  71. gridType: 'dash',
  72. gridColor: '#CCCCCC',
  73. splitNumber: 5,
  74. min: 0,
  75. format: val => {
  76. return val;
  77. },
  78. // gridColor: '#ccc',
  79. max: Math.max.apply(Math, data.series[0].data) + 123,
  80. // gridType: 'dash'
  81. }
  82. });
  83. },
  84. touchLine(e) {
  85. this.area.scrollStart(e);
  86. },
  87. moveLine(e) {
  88. this.area.scroll(e);
  89. },
  90. touchEndLine(e) {
  91. this.area.scrollEnd(e);
  92. }
  93. }
  94. };
  95. </script>
  96. <style lang="scss" scoped>
  97. .chart-line {
  98. width: 100%;
  99. height: 100%;
  100. position: relative;
  101. z-index: 99;
  102. .chart {
  103. width: 100%;
  104. height: 100%;
  105. }
  106. }
  107. </style>