area-chart.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. if (this.chartData) {
  19. this.draw(this.chartData);
  20. }
  21. }
  22. },
  23. methods: {
  24. draw(data) {
  25. data.categories.push('');
  26. data.categories.unshift('');
  27. data.series[0].data.push(null);
  28. data.series[0].data.unshift(null);
  29. this.area = new uCharts({
  30. $this: this, // this
  31. type: 'area', // 类型
  32. canvasId: 'area', // id
  33. colors: [this.color ? this.color : '#FB231F'], // 颜色
  34. width: uni.upx2px(688), // 宽
  35. height: uni.upx2px(567), // 高
  36. fontSize: uni.upx2px(28), // 字体大小
  37. enableScroll: true, // 是否可以拖拽
  38. enableMarkLine: true, // 是否显示辅助线
  39. animation: true, //展示动画
  40. legend: {
  41. show: true
  42. },
  43. extra: {
  44. area: {
  45. type: 'curve',
  46. opacity: 0.3,
  47. }
  48. },
  49. dataLabel: true, //是否显示图表区域内数据点上方的数据文案
  50. dataPointShape: true, // 是否在图表中显示数据点图形标识
  51. dataPointShapeType: 'solid', // 数据点图形标识类型 可选值:实心solid、空心hollow
  52. background: this.background,
  53. categories: data.categories,
  54. series: data.series,
  55. xAxis: {
  56. type: 'grid',
  57. gridColor: '#CCCCCC',
  58. gridType: 'dash',
  59. dashLength: 8,
  60. // type: 'grid',
  61. itemCount: 5, // X轴可见区域数据数量(即X轴数据密度),配合拖拽滚动使用(即仅在启用enableScroll时有效)
  62. scrollShow: true, // 是否显示滚动条
  63. // disableGrid: true, // 不绘制 X 轴网格线
  64. scrollAlign: 'right', // 滚动条起始位置
  65. fontColor: this.color, // X 轴文字颜色
  66. scrollColor: this.color, // X轴滚动条颜色
  67. axisLineColor: '#CCCCCC', // 坐标轴轴线颜色
  68. scrollBackgroundColor: this.background, // X轴滚动条背景颜色
  69. // disabled: false,
  70. // format: v => {
  71. // var date = new Date(v);
  72. // return `${('0' + date.getHours()).slice(-2)}:${('0' + date.getMinutes()).slice(-2)}`;
  73. // }
  74. },
  75. yAxis: {
  76. gridType: 'dash',
  77. gridColor: '#CCCCCC',
  78. splitNumber: 5,
  79. min: 0,
  80. format: val => {
  81. return val;
  82. },
  83. // gridColor: '#ccc',
  84. max: Math.max.apply(Math, data.series[0].data) + 123,
  85. // gridType: 'dash'
  86. }
  87. });
  88. },
  89. touchLine(e) {
  90. this.area.scrollStart(e);
  91. },
  92. moveLine(e) {
  93. this.area.scroll(e);
  94. },
  95. touchEndLine(e) {
  96. this.area.scrollEnd(e);
  97. }
  98. }
  99. };
  100. </script>
  101. <style lang="scss" scoped>
  102. .chart-line {
  103. width: 100%;
  104. height: 100%;
  105. position: relative;
  106. z-index: 99;
  107. .chart {
  108. width: 100%;
  109. height: 100%;
  110. }
  111. }
  112. </style>