ucharts.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <template>
  2. <view>
  3. <cu-custom bgColor="bg-gradual-blue" :isBack="true">
  4. <block slot="backText">返回</block>
  5. <block slot="content">图表示例</block>
  6. </cu-custom>
  7. <!-- Number柱状图Compent1 -->
  8. <view class="cu-bar bg-white solid-bottom margin-top">
  9. <histogram-chart ref="histogramData0" :dataAs="histogramData" canvasId="ht0" />
  10. <view style="text-align: center;line-height: 40px;">柱状图histogram Number</view>
  11. <button @click="changeHistogramData">改变柱状图数据</button>
  12. </view>
  13. <!-- 百分比柱状图Compent2 -->
  14. <view class="cu-bar bg-white solid-bottom margin-top">
  15. <histogram-chart
  16. :dataAs="histogramData2"
  17. ref="histogramData1"
  18. canvasId="ht1"
  19. labelKey="label"
  20. valueKey="value"
  21. :yAxisAs="{
  22. formatter: {
  23. type: 'percent' //type:number percent String,额外参数:fixed:NUmber,name:String
  24. }
  25. }"
  26. />
  27. <view style="text-align: center;line-height: 40px;">柱状图histogram percent</view>
  28. </view>
  29. <!-- 柱状图Compent extra拓展 -->
  30. <view class="cu-bar bg-white solid-bottom margin-top">
  31. <histogram-chart
  32. :dataAs="histogramData3"
  33. ref="histogramData2"
  34. canvasId="ht3"
  35. :extraAs="{
  36. extra: {
  37. column: {
  38. type: 'stack',
  39. width: 20
  40. }
  41. }
  42. }"
  43. />
  44. <view style="text-align: center;line-height: 40px;">柱状图histogram extra拓展</view>
  45. </view>
  46. <!-- 圆环arcbar -->
  47. <view class="cu-bar bg-white solid-bottom margin-top">
  48. <view class="arcbar">
  49. <block v-for="(item, index) in arcbarList" :key="index">
  50. <arcbar-chart
  51. :canvasId="`index_arcbar_${index}`"
  52. :ref="`arcbar${index}`"
  53. :dataAs="item"
  54. :basicAs="{
  55. colors: ['#ff7600']
  56. }"
  57. />
  58. </block>
  59. </view>
  60. <view style="text-align: center;line-height: 40px;">圆环arcbar</view>
  61. </view>
  62. <!-- 折线Line带百分比 -->
  63. <view class="cu-bar bg-white solid-bottom margin-top line">
  64. <line-chart
  65. canvasId="index_line_1"
  66. ref="lineData1"
  67. :dataAs="lineData"
  68. :yAxisAs="{
  69. formatter: {
  70. type: 'percent', //type:number percent String,额外参数:fixed:NUmber,name:String
  71. fixed: 2
  72. }
  73. }"
  74. />
  75. <view style="text-align: center;line-height: 40px;">折线LineChart percent</view>
  76. <button @click="changeLineData">改变折线图数据</button>
  77. </view>
  78. <!-- 折线Line纯数字-->
  79. <view class="cu-bar bg-white solid-bottom margin-top line">
  80. <line-chart ref="lineData2" canvasId="index_line_2" :dataAs="lineData2" />
  81. <view style="text-align: center;line-height: 40px;">折线LineChart Number</view>
  82. </view>
  83. <!-- 饼状图 -->
  84. <view <view class="cu-bar bg-white solid-bottom margin-top pie_chart">
  85. <pie-chart ref="pieChart0" :dataAs="pieData" canvasId="index_pie_1" />
  86. <view style="text-align: center;line-height: 40px;">饼状图PieChart</view>
  87. <button @click="changePieData">改变饼状图数据</button>
  88. </view>
  89. <!-- 环状图 -->
  90. <view class="cu-bar bg-white solid-bottom margin-top ring_chart">
  91. <ring-chart
  92. :dataAs="pieData"
  93. ref="ringChart0"
  94. canvasId="index_ring_1"
  95. />
  96. <view style="text-align: center;line-height: 40px;">环状图 RingChart</view>
  97. <button @click="changeRingData">改变环状图数据</button>
  98. </view>
  99. </view>
  100. </template>
  101. <script>
  102. import HistogramChart from '@/components/stan-ucharts/HistogramChart.vue';
  103. import ArcbarChart from '@/components/stan-ucharts/ArcbarChart.vue';
  104. import LineChart from '@/components/stan-ucharts/LineChart.vue';
  105. import PieChart from '@/components/stan-ucharts/PieChart.vue';
  106. import RingChart from '@/components/stan-ucharts/RingChart.vue';
  107. export default {
  108. name: 'Index',
  109. components: {
  110. HistogramChart,
  111. ArcbarChart,
  112. LineChart,
  113. PieChart,
  114. RingChart
  115. },
  116. data() {
  117. return {
  118. histogramData: {
  119. //总模板
  120. categories: ['2042', '2013', '2014', '2015', '2016', '2017', '2018'],
  121. series: [
  122. {
  123. name: '成交量1', //数据名称
  124. data: [
  125. 15,
  126. {
  127. //(饼图、圆环图、玫瑰图为Number) 数据,如果传入null图表该处出现断点
  128. value: 20, // 仅针对柱状图有效,主要作用为柱状图显示数值
  129. color: '#f04864' //仅针对柱状图有效,主要作用为柱状图自定义颜色,可覆盖外框定义主题颜色
  130. },
  131. 45,
  132. 37,
  133. 43,
  134. 34,
  135. 45
  136. ],
  137. show: true, //图形显示状态,配合点击图例显示状态,也可默认指定是否显示
  138. color: '#FF7600', // 图形颜色 不传入则使用系统默认配色方案 即统一柱状图颜色
  139. textSize: 12 //图形上方标注文字的字体大小
  140. //如涉及混合图表请看 http://doc.ucharts.cn/1172126
  141. },
  142. {
  143. name: '成交量2',
  144. data: [
  145. 30,
  146. {
  147. value: 40,
  148. color: '#facc14'
  149. },
  150. 25,
  151. 14,
  152. 34,
  153. 18,
  154. 20
  155. ]
  156. }
  157. ]
  158. },
  159. histogramData2: {
  160. //柱状图Compent //label应为series value 应为
  161. label: ['2052', '2013', '2014', '2015', '2016', '2017', '2018'],
  162. value: [
  163. {
  164. name: 'labelKey改变',
  165. data: [0.3, 0.2, 0.5, 0.4, 0.3, 0.1, 0.2]
  166. }
  167. ]
  168. },
  169. histogramData3: {
  170. //柱状标准图
  171. categories: ['2013', '2014', '2015', '2016', '2017', '2018'],
  172. series: [
  173. {
  174. name: '类别一',
  175. data: [35, 36, 31, 33, 13, 34]
  176. },
  177. {
  178. name: '类别二',
  179. data: [18, 27, 21, 24, 6, 28]
  180. },
  181. {
  182. name: '类别三',
  183. data: [18, 27, 21, 24, 6, 28]
  184. }
  185. ]
  186. },
  187. arcbarList: [
  188. //圆环
  189. {
  190. series: [
  191. {
  192. name: '正确率',
  193. data: 0.2912
  194. }
  195. ]
  196. },
  197. {
  198. series: [
  199. {
  200. name: '错误率',
  201. data: 0.6921
  202. }
  203. ]
  204. },
  205. {
  206. series: [
  207. {
  208. name: '失误率',
  209. data: 0.8232
  210. }
  211. ]
  212. }
  213. ],
  214. lineData: {
  215. //带百分比的图--折线图数据
  216. categories: ['20111', '2013', '2014', '2015', '2016', '2017'],
  217. series: [{ name: '成交量B', data: [0.7, 0.4, 0.65, 0.1, 0.44, 0.68] }, { name: '成交量C', data: [0.1, 0.8, 0.95, 0.15, 0.112, 0.132] }]
  218. },
  219. lineData2: {
  220. //数字的图--折线图数据
  221. categories: ['2012', '2013', '2014', '2015', '2016', '2017'],
  222. series: [
  223. { name: '成交量A', data: [35, 8, 25, 37, 4, 20] },
  224. { name: '成交量B', data: [70, 40, 65, 100, 44, 68] },
  225. { name: '成交量C', data: [100, 80, 95, 150, 112, 132] }
  226. ]
  227. },
  228. pieData: {
  229. //饼状图数据
  230. series: [
  231. {
  232. name: '一班',
  233. data: 50
  234. },
  235. {
  236. name: '二班',
  237. data: 30
  238. },
  239. {
  240. name: '三班',
  241. data: 20
  242. },
  243. {
  244. name: '四班',
  245. data: 18
  246. },
  247. {
  248. name: '五班',
  249. data: 8
  250. }
  251. ]
  252. }
  253. };
  254. },
  255. methods: {
  256. //改变柱状图数据
  257. changeHistogramData() {
  258. let categories = ['20111', '2013', '2014', '2015', '2016', '2017'];
  259. let series = [{ name: '成交量A', data: [200, 233, 25, 370, 400, 200] }];
  260. this.$refs['histogramData0'].changeData({ series, categories });
  261. },
  262. //改变折线图数据
  263. changeLineData() {
  264. let categories = ['20111', '2013', '2014', '2015', '2016', '2017'];
  265. let series = [{ name: '成交量A', data: [0.3511, 0.8233, 0.25, 0.37, 0.4, 0.2] }];
  266. this.$refs['lineData1'].changeData({ series, categories });
  267. },
  268. //改变饼状图数据
  269. changePieData() {
  270. let series = [{ name: '一班', data: 150 }, { name: '二班', data: 130 }, { name: '三班', data: 120 }, { name: '四班', data: 182 }, { name: '五班', data: 83 }];
  271. this.$refs['pieChart0'].changeData(series);
  272. },
  273. //改变环状图数据
  274. changeRingData() {
  275. let series = [{ name: '一班', data: 150 }, { name: '二班', data: 130 }, { name: '三班', data: 120 }, { name: '四班', data: 182 }, { name: '五班', data: 83 }];
  276. this.$refs['ringChart0'].changeData(series);
  277. },
  278. getServerData() {
  279. //模拟ajax调用
  280. // uni.request({
  281. // url: 'https://www.easy-mock.com/mock/5cc586b64fc5576cba3d647b/uni-wx-charts/chartsdata2',
  282. // data: {},
  283. // success: function(res) {
  284. // console.log(res.data.data);
  285. // let serverData = res.data.data;
  286. // this.$set(this.lineData, 'categories', serverData.Column.categories);
  287. // this.$set(this.lineData, 'series', serverData.Column.series);
  288. // this.$refs['lineData1'].showCharts();
  289. // },
  290. // fail: () => {
  291. // console.log('网络错误');
  292. // }
  293. // });
  294. setTimeout(() => {
  295. //延迟模拟ajax嗲用后台数据
  296. let categories = ['20111', '2013', '2014', '2015', '2016', '2017'];
  297. let series = [{ name: '成交量A', data: [0.8511, 0.233, 0.125, 0.437, 0.48, 0.1234] }];
  298. this.$set(this.lineData, 'categories', categories);
  299. this.$set(this.lineData, 'series', series);
  300. this.$refs['lineData1'].showCharts();
  301. }, 1000);
  302. }
  303. },
  304. created() {
  305. this.$nextTick(() => {
  306. //柱状图
  307. this.$refs['histogramData0'].showCharts();
  308. this.$refs['histogramData1'].showCharts();
  309. this.$refs['histogramData2'].showCharts();
  310. //圆环(注意循环可能会导致出现下面情况,请更具实际情况作出判断// console.log(this.$refs);)
  311. this.$refs['arcbar0'][0].showCharts();
  312. this.$refs['arcbar1'][0].showCharts();
  313. this.$refs['arcbar2'][0].showCharts();
  314. // 饼状图
  315. this.$refs['pieChart0'].showCharts();
  316. // 环状图
  317. this.$refs['ringChart0'].showCharts();
  318. //折线图
  319. this.$refs['lineData2'].showCharts();
  320. });
  321. //ajax调用
  322. this.getServerData();
  323. }
  324. };
  325. </script>
  326. <style scoped>
  327. .arcbar {
  328. display: flex;
  329. }
  330. </style>