HistogramChart.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <template>
  2. <view class="histogram_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 histogramuCharts = {},
  18. optionAs = {};
  19. export default {
  20. name: 'HistogramChart',
  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: `histogram_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. mounted() {},
  87. methods: {
  88. showCharts() {
  89. let defaultOption = {
  90. //通用基础项设置 basicAs
  91. $this: this, //this实例组件内使用图表,必须传入this实例
  92. canvasId: this.canvasId, //页面组件canvas-id,支付宝中为id
  93. type: 'column', //图表类型,可选值为pie、line、column、area、ring、radar、arcbar、gauge、candle、bar、mix、rose、word
  94. padding: [15, 15, 0, 15], //画布填充边距,顺序为上右下左,同css,但必须4位
  95. colors: ['#1890ff', '#2fc25b', '#facc14', '#f04864', '#8543e0', '#90ed7d'], //图表配色方案,不传则使用系统默认配置
  96. rotate: false, //是否横屏展示
  97. rotateLock: true, // 锁定横屏模式,如果在支付宝和百度小程序中使用横屏模式,请赋值true,否则每次都会旋转90度。跨端使用通过uni-app的条件编译来赋值
  98. enableScroll: true, //是否开启图表可拖拽滚动 默认false 支持line, area, column, candle图表类型(需配合绑定@touchstart, @touchmove, @touchend方法)
  99. enableMarkLine: false, //是否显示辅助线 默认false 支持line, area, column, candle, mix图表类型
  100. animation: true, //是否动画展示
  101. dataLabel: true, //是否在图表中显示数据标签内容值
  102. duration: 1000, //动画展示时长单位毫秒
  103. fontSize: 12, //全局默认字体大小(可选,单位为px,默认13px)高分屏不必乘像素比,自动根据pixelRatio计算
  104. background: '#fff', //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. fontColor: '#666666', //X轴数据点颜色
  125. boundaryGap: 'center', //折线图、区域图起画点结束点方法:center为单元格中间起画,justify为0点起画即两端对齐
  126. axisLine: false, //坐标轴轴线是否显示
  127. axisLineColor: '#cccccc' //坐标轴轴线颜色
  128. },
  129. yAxis: {
  130. //如果是多坐标系则传数组型对象[{disabled:true},{disabled:false}]
  131. disabled: false, //不绘制Y轴
  132. position: 'left', //Y轴位置,可选值左侧left右侧right(未起效果)
  133. format: val => {
  134. let defaultSetting = { type: 'number', fixed: 0, name: '' };
  135. let { type, fixed, name } = this.yAxisAs && this.yAxisAs.formatter ? Object.assign(defaultSetting, this.yAxisAs.formatter) : defaultSetting;
  136. if (type == 'number') {
  137. return `${val.toFixed(fixed)}${name}`;
  138. } else if (type == 'percent') {
  139. let newName = name || '%';
  140. return `${(val * 100).toFixed(fixed)}${name}${newName}`;
  141. } else {
  142. return val.toFixed(0);
  143. }
  144. },
  145. title: '' //Y轴标题
  146. },
  147. //图列配置 legendAs
  148. legend: {
  149. show: true, //是否显示各类别的图例标识
  150. position: 'top',
  151. float: 'left',
  152. padding: 10,
  153. margin: 0
  154. // itemGap:10,//各个item之间的间隔,单位px,默认为10,横向布局时为水平间隔,纵向布局时为纵向间隔
  155. // fontColor:'666666',
  156. // lineHeight:17,//默认opts.fontSize+5
  157. // fontSize:12,//默认opts.fontSize
  158. // backgroundColor:rgba(0,0,0,0),//图例背景颜色
  159. // borderColor:rgba(0,0,0,0),//图例边框颜色
  160. // format:()=>{}//未来预留,暂未生效】自定义显示数据内容
  161. },
  162. //扩展配置 extraAs 详情请看 http://doc.ucharts.cn/1172130
  163. extra: {
  164. column: {
  165. //参考官网配置
  166. type: 'group',
  167. width: (this.cWidth * 0.45) / this.dataAs[this.labelKey].length
  168. }
  169. }
  170. };
  171. optionAs[this.canvasId] = Object.assign(defaultOption, this.basicAs, this.xAxisAS, this.yAxisAS, this.legendAs, this.extraAs);
  172. histogramuCharts[this.canvasId] = new uCharts(optionAs[this.canvasId]);
  173. },
  174. touchstart(e) {
  175. let that = this;
  176. histogramuCharts[this.canvasId].touchLegend(e, { animation: false });
  177. histogramuCharts[this.canvasId].scrollStart(e);
  178. histogramuCharts[this.canvasId].showToolTip(e, {
  179. //修改点击事弹出文字
  180. format: function(item, category) {
  181. let defaultSetting = { type: 'number', fixed: 0, name: '' };
  182. let newName;
  183. let { type, fixed, name } = that.yAxisAs && that.yAxisAs.formatter ? Object.assign(defaultSetting, that.yAxisAs.formatter) : defaultSetting;
  184. if (typeof item.data === 'object') {
  185. if (type == 'number') {
  186. return `${category} ${item.name}:${item.data.value.toFixed(fixed)}${name}`;
  187. } else if (type == 'percent') {
  188. newName = name || '%';
  189. return `${category} ${item.name}:${(item.data.value * 100).toFixed(fixed)}${newName}`;
  190. } else {
  191. return `${category} ${item.name}:${item.data.value}`;
  192. }
  193. } else {
  194. if (type == 'number') {
  195. return `${category} ${item.name}:${item.data.toFixed(fixed)}${name}`;
  196. } else if (type == 'percent') {
  197. newName = name || '%';
  198. return `${category} ${item.name}:${(item.data * 100).toFixed(fixed)}${newName}`;
  199. } else {
  200. return `${category} ${item.name}:${item.data}`;
  201. }
  202. }
  203. }
  204. });
  205. },
  206. touchmove(e) {
  207. histogramuCharts[this.canvasId].scroll(e);
  208. },
  209. touchend(e) {
  210. histogramuCharts[this.canvasId].scrollEnd(e);
  211. },
  212. changeData({ series, categories }) {
  213. histogramuCharts[this.canvasId].updateData({
  214. series,
  215. categories
  216. });
  217. }
  218. }
  219. };
  220. </script>
  221. <style scoped></style>