other.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <view class="content">
  3. <qiun-title-bar title="localdata渲染图表1+点击获取索引"/>
  4. <view class="charts-box">
  5. <!-- 只需绑定@getIndex即可获取点击索引 -->
  6. <qiun-data-charts type="column" :localdata="chartsDatalocaldataA" @getIndex="getIndex"/>
  7. </view>
  8. <qiun-title-bar title="localdata渲染图表2+禁用鼠标移动"/>
  9. <view class="charts-box">
  10. <qiun-data-charts type="pie" :localdata="chartsDatalocaldataB" :onmouse="false"/>
  11. </view>
  12. <qiun-title-bar title="渲染完成后显示自定义tooltip" />
  13. <view class="charts-box">
  14. <!-- 渲染完成后进行操作图表,需要绑定@complete事件,然后在@complete事件中进行操作 -->
  15. <qiun-data-charts
  16. type="column"
  17. :chartData="chartsDataColumn1"
  18. @complete="complete"
  19. />
  20. </view>
  21. <qiun-title-bar title="跟手tooltip示例" />
  22. <view class="charts-box" >
  23. <!-- 开启onmovetip后,建议同时开启canvas2d模式(需要传canvasId),否则在小程序端会很卡。开启onmovetip功能的前提是需要开启ontouch,并且关闭图表滚动条,即:opts="{enableScroll: false}" -->
  24. <qiun-data-charts
  25. type="column"
  26. :opts="{enableScroll: false}"
  27. canvasId="canvas2dNeedId"
  28. :chartData="chartsDataColumn1"
  29. :canvas2d="true"
  30. :ontouch="true"
  31. :onmovetip="true"
  32. @getTouchMove="getTouchMove"
  33. />
  34. </view>
  35. <qiun-title-bar title="固定位置显示tooltip+禁用图例点击" />
  36. <view class="charts-box">
  37. <!-- 这个demo演示自定义tooltip的样式及增加额外的数据,例如换行等 -->
  38. <qiun-data-charts
  39. type="line"
  40. :opts="{extra:{tooltip:{showArrow: false,borderWidth: 1,borderRadius:8,borderColor: '#FF0000',bgColor: '#FFFFFF',bgOpacity: 0.9,fontColor: '#000000',splitLine: false}}}"
  41. :chartData="chartsDataColumn1"
  42. :tapLegend="false"
  43. :tooltipCustom="{x:2,y:2}"
  44. />
  45. </view>
  46. <qiun-title-bar title="自定义tooltip(textList换行),内置方法" />
  47. <view class="charts-box">
  48. <!-- 需要关闭组件的tooltip,即:tooltipShow="false",然后在@getIndex中调用uCharts的showTooltip方法,注意,APP端不能实现,其他端需要引用config-ucharts.js作为实例承载的中间件。 -->
  49. <!-- 如果需要做跟手tooltip,需要在@getTouchMove事件中调用,注意需要添加防抖,可参考组件内防抖方法,否则会导致逻辑层与视图层频繁通信造成卡顿 -->
  50. <qiun-data-charts
  51. type="line"
  52. :tooltipShow="false"
  53. :chartData="chartsDataColumn1"
  54. @getIndex="showMyTooltip"
  55. />
  56. </view>
  57. <qiun-title-bar title="自定义tooltip(textList换行),opts参数" />
  58. <view class="charts-box">
  59. <!-- 需要关闭组件的tooltip,即:tooltipShow="false",然后在@getIndex中改变opts来实现tooltip,注意,APP端不能实现,其他端需要引用config-ucharts.js作为实例承载的中间件。 -->
  60. <qiun-data-charts
  61. type="line"
  62. :opts="optsTooltip"
  63. :animation="false"
  64. :tooltipShow="false"
  65. :chartData="chartsDataColumn1"
  66. @getIndex="showOptsTooltip"
  67. />
  68. </view>
  69. <qiun-title-bar title="强制展示错误信息"/>
  70. <button class="uni-button" type="default" @click="changeErrorMessage">点击展示错误信息</button>
  71. <view class="charts-box">
  72. <qiun-data-charts type="line" :chartData="chartsDataLine1" :errorMessage="errorMessage" :errorReload="false" @error="error"/>
  73. </view>
  74. <qiun-title-bar title="uCharts保存为图片"/>
  75. <button class="uni-button" type="default" @click="createImage('createImageUCharts')">点击保存为图片</button>
  76. <view class="charts-box">
  77. <qiun-data-charts type="area" ref="createImageUCharts" :chartData="chartsDataLine1" @getImage="getImage"/>
  78. </view>
  79. </view>
  80. </template>
  81. <script>
  82. //下面是演示数据,您的项目不需要引用,数据需要您从服务器自行获取
  83. import demodata from '@/mockdata/demodata.json';
  84. //下面是uCharts的配置文件及qiun-data-charts组件的公用中转参数,可以从本配置文件中获取uCharts实例、opts配置、format配置(APP端因采用renderjs无法从此配置文件获取uCharts实例)
  85. //***并不是所有的页面都需要引用这个文件***引入这个configjs是为了获取组件的uCharts实例,从而操作uCharts的一些方法,例如手动显示tooltip及一些其他uCharts包含的方法及事件。
  86. //再说一遍,只能在H5内使用,APP不行,APP不行,APP不行(如果需要自定义tooltip,也可以通过opts中传递tooltip参数来实现模拟显示tooltip,详见showOptsTooltip方法)
  87. import uCharts from '@/uni_modules/qiun-data-charts/js_sdk/u-charts/config-ucharts.js';
  88. export default {
  89. data() {
  90. return {
  91. //注意如果使用localdata数据格式,默认值的类型应该是数组
  92. chartsDatalocaldataA:[],
  93. chartsDatalocaldataB:[],
  94. //注意如果使用localdata数据格式,默认值的类型应该是数组
  95. chartsDataColumn1:{},
  96. chartsDataLine1:{},
  97. errorMessage:"自定义的错误信息,关闭点击重新加载",
  98. //在opts中拼接tooltip
  99. optsTooltip:{}
  100. };
  101. },
  102. onLoad() {
  103. //模拟从服务器获取数据
  104. this.getServerData()
  105. },
  106. methods: {
  107. getServerData() {
  108. setTimeout(() => {
  109. //因部分数据格式一样,这里不同图表引用同一数据源的话,需要深拷贝一下构造不同的对象
  110. //开发者需要自行处理服务器返回的数据,应与标准数据格式一致,注意series的data数值应为数字格式
  111. this.chartsDatalocaldataA=JSON.parse(JSON.stringify(demodata.localdata))
  112. this.chartsDatalocaldataB=JSON.parse(JSON.stringify(demodata.localdataB))
  113. this.chartsDataColumn1=JSON.parse(JSON.stringify(demodata.Column))
  114. this.chartsDataLine1=JSON.parse(JSON.stringify(demodata.Line))
  115. }, 1500);
  116. },
  117. complete(e) {
  118. console.log("渲染完成事件",e);
  119. //uCharts.instance[e.id]代表当前的图表实例(除APP端,APP不可在组件外调用uCharts的实例)
  120. console.log("uCharts实例",uCharts.instance[e.id]);
  121. //uCharts.option[e.id]代表当前的图表的opts(除APP端,APP不可在组件外调用uCharts的实例)
  122. console.log("uCharts的option",uCharts.option[e.id]);
  123. //下面展示渲染完成后,通过实例调用uCharts的showToolTip方法,有了uCharts实例,您也可以在其他地方调用图表的方法及数据(除APP端,APP因采用renderjs,无法获取uCharts实例)
  124. // #ifndef APP-PLUS
  125. //如果需要tooltip换行显示,也可以参照本示例,关闭组件本身的tooltip功能,即:tooltipShow="false",然后在@getIndex事件中,通过uCharts.instance[e.id].showToolTip()方法来自定义。
  126. let textList = [
  127. { text: '默认显示的tooltip', color: null },
  128. { text: '类别1:某个值xxx', color: '#2fc25b' },
  129. { text: '类别2:某个值xxx', color: '#facc14' },
  130. { text: '类别3:某个值xxx', color: '#f04864' }
  131. ];
  132. //这里指定了changedTouches的x和y坐标,当指定index索引时,x值会被自动修正到正确位置,给0即可,主要是y的坐标值
  133. uCharts.instance[e.id].showToolTip(
  134. { changedTouches: [{ x: 0, y: 100 }] },
  135. {
  136. index: 3,
  137. textList: textList
  138. }
  139. );
  140. // #endif
  141. },
  142. getIndex(e){
  143. console.log("获取点击索引事件",e);
  144. },
  145. //APP端因为拿不到uCharts的实例,只能通过opts传参的方法来实现tooltip
  146. showOptsTooltip(e){
  147. console.log("获取点击索引事件",e);
  148. //先拿到点击索引,您可以根据索引拼接一下textList
  149. let currentIndex = e.currentIndex.index;
  150. if(currentIndex > -1){
  151. let textList = [
  152. {text:"我是一个自定义标题"+this.chartsDataColumn1.categories[currentIndex],color:null},
  153. { text: '类别1:'+this.chartsDataColumn1.series[0].data[currentIndex], color: '#2fc25b' },
  154. { text: '类别2:'+this.chartsDataColumn1.series[1].data[currentIndex], color: '#facc14' },
  155. ];
  156. this.optsTooltip={
  157. tooltip:{
  158. index:currentIndex,
  159. offset:{
  160. x:e.opts.chartData.xAxisPoints[currentIndex]+e.opts.chartData.xAxisData.eachSpacing/2,//这里的x坐标为了与x轴categories中的点位对齐,我调用的opts中的参数
  161. y:e.event.y,//y坐标,用的是点击的点位的y值
  162. },
  163. textList:textList
  164. },
  165. update:true
  166. }
  167. }
  168. },
  169. //非APP端可以通过uCharts实例来调用uCharts方法来实现自定义tooltip
  170. showMyTooltip(e){
  171. console.log("获取点击索引事件",e);
  172. //拿到canvasId后即e.id,可以通过uCharts.instance[e.id]代表当前的图表实例(除APP端,APP不可在组件外调用uCharts的实例)
  173. console.log("获取uCharts实例",uCharts.instance[e.id]);
  174. //uCharts.option[e.id]代表当前的图表的opts(除APP端,APP不可在组件外调用uCharts的实例)
  175. console.log("uCharts的option",uCharts.option[e.id]);
  176. //从option(opts)中获取数据
  177. let categories = uCharts.option[e.id].categories;
  178. let series = uCharts.option[e.id].series;
  179. //e.currentIndex是点击的的点位索引值
  180. let index = e.currentIndex.index;//注意v2.1.2版本后,e.currentIndex是对象,而e.currentIndex.index是索引值,如果是时间轴图表,index在多个series的情况下会是数组
  181. //自行通过uCharts的实例调用showToolTip方法(APP端不能实现,无法通过renderjs获取到uCharts实例)
  182. // #ifndef APP-PLUS
  183. //如果需要tooltip换行显示,也可以参照本示例,关闭组件本身的tooltip功能,即:tooltipShow="false",然后在@getIndex事件中,通过uCharts.instance[e.id].showToolTip()方法来自定义。
  184. let textList = [{ text: categories[index] + "年收入情况", color: null }];
  185. for (let i = 0; i < series.length; i++) {
  186. textList.push({text: "自定义" + series[i].name + ":" + series[i].data[index] + "美元", color: series[i].color})
  187. }
  188. //changedTouches是点击的坐标值
  189. uCharts.instance[e.id].showToolTip(
  190. { changedTouches: [e.event] },
  191. {
  192. index: index,
  193. textList: textList
  194. }
  195. );
  196. // #endif
  197. },
  198. getTouchMove(e){
  199. console.log("获取TouchMove",e);
  200. },
  201. changeErrorMessage(){
  202. this.errorMessage="自定义错误信息,关闭点击重新加载"+Math.floor(Math.random() * 1000)
  203. },
  204. createImage(refid){
  205. this.$refs[refid].getImage();
  206. },
  207. getImage(e){
  208. console.log(e);
  209. },
  210. error(e){
  211. console.log("组件传递过来的错误信息",e);
  212. //TODO something 重新加载或者其他你想干的事
  213. }
  214. }
  215. };
  216. </script>
  217. <style>
  218. .content {
  219. display: flex;
  220. flex-direction: column;
  221. flex: 1;
  222. }
  223. .charts-box {
  224. width: 100%;
  225. height: 300px;
  226. }
  227. </style>