LineChart.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <template>
  2. <view class="line_chart">
  3. <canvas
  4. :canvasId="canvasId"
  5. id="canvasId"
  6. disable-scroll="true"
  7. :style="{ width: cWidth + 'px', height: cHeight + 'px' }"
  8. @touchstart="touchstart"
  9. @touchmove="touchmove"
  10. @touchend="touchend"
  11. />
  12. <slot />
  13. </view>
  14. </template>
  15. <script>
  16. import uCharts from '@/plugins/stan-ucharts/u-charts/u-charts.js'; //可以优化放全局 uCharts ==>使用全局
  17. const lineCharts = {},
  18. optionAs = {};
  19. export default {
  20. name: 'LineChart',
  21. props: {
  22. dataAs: {
  23. //数据
  24. type: Object,
  25. default: () => ({})
  26. },
  27. basicAs: {
  28. ////通用基础项设置
  29. type: Object,
  30. default: () => ({})
  31. },
  32. xAxisAs: {
  33. //xAxis YAxis等轴线基础设置(圆环饼图无轴线无需设置)
  34. type: Object,
  35. default: () => ({})
  36. },
  37. yAxisAs: {
  38. //xAxis YAxis等轴线基础设置(圆环饼图无轴线无需设置)
  39. type: Object,
  40. default: () => ({})
  41. },
  42. legendAs: {
  43. //图例设置
  44. type: Object,
  45. default: () => ({})
  46. },
  47. extraAs: {
  48. //详情请看 http://doc.ucharts.cn/1172130
  49. type: Object,
  50. default: () => ({})
  51. },
  52. width: {
  53. //图标宽度
  54. type: Number,
  55. default: 750
  56. },
  57. height: {
  58. //图标高度
  59. type: Number,
  60. default: 500
  61. },
  62. labelKey: {
  63. type: String,
  64. default: 'categories'
  65. },
  66. valueKey: {
  67. type: String,
  68. default: 'series'
  69. },
  70. canvasId: {
  71. type: String,
  72. default: `line_canvas_${Math.ceil(Math.random(5) * 10000)}`
  73. }
  74. },
  75. data() {
  76. return {};
  77. },
  78. computed: {
  79. cWidth() {
  80. return uni.upx2px(this.width);
  81. },
  82. cHeight() {
  83. return uni.upx2px(this.height);
  84. }
  85. },
  86. methods: {
  87. showCharts() {
  88. let defaultOption = {
  89. //通用基础项设置 basicAs
  90. $this: this, //this实例组件内使用图表,必须传入this实例
  91. canvasId: this.canvasId, //页面组件canvas-id,支付宝中为id
  92. type: 'line', //图表类型,可选值为pie、line、column、area、ring、radar、arcbar、gauge、candle、bar、mix、rose、word
  93. padding: [15, 15, 0, 15], //画布填充边距,顺序为上右下左,同css,但必须4位
  94. colors: ['#1890ff', '#2fc25b', '#facc14', '#f04864', '#8543e0', '#90ed7d'], //图表配色方案,不传则使用系统默认配置
  95. rotate: false, //是否横屏展示
  96. rotateLock: true, // 锁定横屏模式,如果在支付宝和百度小程序中使用横屏模式,请赋值true,否则每次都会旋转90度。跨端使用通过uni-app的条件编译来赋值
  97. enableScroll: true, //是否开启图表可拖拽滚动 默认false 支持line, area, column, candle图表类型(需配合绑定@touchstart, @touchmove, @touchend方法)
  98. enableMarkLine: false, //是否显示辅助线 默认false 支持line, area, column, candle, mix图表类型
  99. animation: true, //是否动画展示
  100. dataLabel: true, //是否在图表中显示数据标签内容值
  101. dataPointShape: true,
  102. duration: 1000, //动画展示时长单位毫秒
  103. fontSize: 12, //全局默认字体大小(可选,单位为px,默认13px)高分屏不必乘像素比,自动根据pixelRatio计算
  104. background: '#ffffff', //canvas背景颜色(如果页面背景颜色不是白色请设置为页面的背景颜色,默认#ffffff)无作用
  105. pixelRatio: 1, //像素比,默认为1,仅支付宝小程序需要大于1,其他平台必须为1
  106. width: this.cWidth, //canvas宽度,单位为px,支付宝高分屏需要乘像素比(pixelRatio)
  107. height: this.cHeight, //canvas高度,单位为px,支付宝高分屏需要乘像素比
  108. //数据列表配置项 dataAS
  109. categories: this.dataAs[this.labelKey], //数据类别(饼图、圆环图不需要)
  110. series: this.dataAs[this.valueKey], //数据列表
  111. //坐标轴配置项 axisAs
  112. xAxis: {
  113. // X轴配置
  114. type: 'grid',
  115. rotateLabel: true, //X轴刻度(数值)标签是否旋转(仅在文案超过单屏宽度时有效)
  116. itemCount: 5, //X轴可见区域数据数量(即X轴数据密度),配合拖拽滚动使用(即仅在启用enableScroll时有效)
  117. // labelCount:Number,//X轴可见区域标签数量(即X轴数刻度标签单屏幕限制显示的数量)
  118. scrollShow: true, //是否显示滚动条,配合拖拽滚动使用(即仅在启用enableScroll时有效)
  119. scrollAlign: 'left', //滚动条初始位置,left为数据整体左对齐,right为右对齐
  120. scrollBackgroundColor: '#EFEBEF', // X轴滚动条背景颜色,配合拖拽滚动使用(即仅在启用enableScroll时有效)
  121. scrollColor: '#A6A6A6', //X轴滚动条颜色,配合拖拽滚动使用(即仅在启用enableScroll时有效)
  122. disabled: false, //不绘制X轴
  123. disableGrid: true, //不绘制X轴网格(即默认绘制网格)
  124. calibration: true, //坐标轴刻度线是否显示 注v1.9.0有效
  125. gridColor: '#cccccc', //X轴网格颜色 例如#7cb5ec
  126. gridType: 'dash', // X轴网格线型 'solid'为实线、'dash'为虚线`
  127. gridEval: 1, //X轴网格线显示间隔,即假设为2时,是隔一个刻度显示间隔
  128. dashLength: 4, //X轴网格为虚线时,单段虚线长度
  129. fontColor: '#666666', //X轴数据点颜色
  130. boundaryGap: 'center', //折线图、区域图起画点结束点方法:center为单元格中间起画,justify为0点起画即两端对齐
  131. axisLine: false, //坐标轴轴线是否显示
  132. axisLineColor: '#cccccc' //坐标轴轴线颜色
  133. // splitNumber:Number
  134. },
  135. yAxis: {
  136. //如果是多坐标系则传数组型对象[{disabled:true},{disabled:false}]
  137. disabled: false, //不绘制Y轴
  138. position: 'left', //Y轴位置,可选值左侧left右侧right(未起效果)
  139. format: val => {
  140. let defaultSetting = { type: 'number', fixed: 0, name: '' };
  141. let { type, fixed, name } = this.yAxisAs && this.yAxisAs.formatter ? Object.assign(defaultSetting, this.yAxisAs.formatter) : defaultSetting;
  142. if (type == 'number') {
  143. return val.toFixed(fixed) + name;
  144. } else if (type == 'percent') {
  145. let newName = name || '%';
  146. return (val * 100).toFixed(fixed) + newName;
  147. } else {
  148. return val.toFixed(0);
  149. }
  150. }
  151. // title:'Y轴标题',//Y轴标题
  152. // titleFontSize:basic.fontSize titleFontColor==>#666666 fontColor==>666666 Y轴数据点颜色 fontSizeY轴数据标签字体大小 等详见http://doc.ucharts.cn/1172128
  153. },
  154. //图列配置 legendAs
  155. legend: {
  156. show: true, //是否显示各类别的图例标识
  157. position: 'top',
  158. float: 'left',
  159. padding: 10,
  160. margin: 0
  161. // itemGap:10,//各个item之间的间隔,单位px,默认为10,横向布局时为水平间隔,纵向布局时为纵向间隔
  162. // fontColor:'666666',
  163. // lineHeight:17,//默认opts.fontSize+5
  164. // fontSize:12,//默认opts.fontSize
  165. // backgroundColor:rgba(0,0,0,0),//图例背景颜色
  166. // borderColor:rgba(0,0,0,0),//图例边框颜色
  167. // format:()=>{}//未来预留,暂未生效】自定义显示数据内容
  168. },
  169. //扩展配置 extraAs 详情请看 http://doc.ucharts.cn/1172130
  170. extra: {
  171. line: {
  172. type: 'straight'
  173. }
  174. }
  175. };
  176. optionAs[this.canvasId] = Object.assign(defaultOption, this.basicAs, this.xAxisAs, this.yAxisAs, this.legendAs, this.extraAs);
  177. lineCharts[this.canvasId] = new uCharts(optionAs[this.canvasId]);
  178. },
  179. touchstart(e) {
  180. let that = this;
  181. lineCharts[this.canvasId].touchLegend(e, { animation: false });
  182. lineCharts[this.canvasId].scrollStart(e);
  183. lineCharts[this.canvasId].showToolTip(e, {
  184. //修改点击事弹出文字
  185. format: function(item, category) {
  186. let defaultSetting = { type: 'number', fixed: 0, name: '' };
  187. let newName;
  188. let { type, fixed, name } = that.yAxisAs && that.yAxisAs.formatter ? Object.assign(defaultSetting, that.yAxisAs.formatter) : defaultSetting;
  189. if (typeof item.data === 'object') {
  190. if (type == 'number') {
  191. return `${category} ${item.name}:${item.data.value.toFixed(fixed)}${name}`;
  192. } else if (type == 'percent') {
  193. newName = name || '%';
  194. return `${category} ${item.name}:${(item.data.value * 100).toFixed(fixed)}${newName}`;
  195. } else {
  196. return `${category} ${item.name}:${item.data.value}`;
  197. }
  198. } else {
  199. if (type == 'number') {
  200. return `${category} ${item.name}:${item.data.toFixed(fixed)}${name}`;
  201. } else if (type == 'percent') {
  202. newName = name || '%';
  203. return `${category} ${item.name}:${(item.data * 100).toFixed(fixed)}${newName}`;
  204. } else {
  205. return `${category} ${item.name}:${item.data}`;
  206. }
  207. }
  208. }
  209. });
  210. },
  211. touchmove(e) {
  212. lineCharts[this.canvasId].scroll(e);
  213. },
  214. touchend(e) {
  215. lineCharts[this.canvasId].scrollEnd(e);
  216. },
  217. changeData({ series, categories }) {
  218. lineCharts[this.canvasId].updateData({
  219. series,
  220. categories
  221. });
  222. }
  223. }
  224. };
  225. </script>
  226. <style></style>