ucharts.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. :titleAs="{
  96. title: {
  97. name: '一年级'
  98. },
  99. subtitle: {
  100. name: '70%'
  101. }
  102. }"
  103. />
  104. <view style="text-align: center;line-height: 40px;">环状图 RingChart</view>
  105. <button @click="changeRingData">改变环状图数据</button>
  106. </view>
  107. </view>
  108. </template>
  109. <script>
  110. import HistogramChart from '@/components/stan-ucharts/HistogramChart.vue';
  111. import ArcbarChart from '@/components/stan-ucharts/ArcbarChart.vue';
  112. import LineChart from '@/components/stan-ucharts/LineChart.vue';
  113. import PieChart from '@/components/stan-ucharts/PieChart.vue';
  114. import RingChart from '@/components/stan-ucharts/RingChart.vue';
  115. export default {
  116. name: 'Index',
  117. components: {
  118. HistogramChart,
  119. ArcbarChart,
  120. LineChart,
  121. PieChart,
  122. RingChart
  123. },
  124. data() {
  125. return {
  126. histogramData: {
  127. //总模板
  128. categories: ['2042', '2013', '2014', '2015', '2016', '2017', '2018'],
  129. series: [
  130. {
  131. name: '成交量1', //数据名称
  132. data: [
  133. 15,
  134. {
  135. //(饼图、圆环图、玫瑰图为Number) 数据,如果传入null图表该处出现断点
  136. value: 20, // 仅针对柱状图有效,主要作用为柱状图显示数值
  137. color: '#f04864' //仅针对柱状图有效,主要作用为柱状图自定义颜色,可覆盖外框定义主题颜色
  138. },
  139. 45,
  140. 37,
  141. 43,
  142. 34,
  143. 45
  144. ],
  145. show: true, //图形显示状态,配合点击图例显示状态,也可默认指定是否显示
  146. color: '#FF7600', // 图形颜色 不传入则使用系统默认配色方案 即统一柱状图颜色
  147. textSize: 12 //图形上方标注文字的字体大小
  148. //如涉及混合图表请看 http://doc.ucharts.cn/1172126
  149. },
  150. {
  151. name: '成交量2',
  152. data: [
  153. 30,
  154. {
  155. value: 40,
  156. color: '#facc14'
  157. },
  158. 25,
  159. 14,
  160. 34,
  161. 18,
  162. 20
  163. ]
  164. }
  165. ]
  166. },
  167. histogramData2: {
  168. //柱状图Compent //label应为series value 应为
  169. label: ['2052', '2013', '2014', '2015', '2016', '2017', '2018'],
  170. value: [
  171. {
  172. name: 'labelKey改变',
  173. data: [0.3, 0.2, 0.5, 0.4, 0.3, 0.1, 0.2]
  174. }
  175. ]
  176. },
  177. histogramData3: {
  178. //柱状标准图
  179. categories: ['2013', '2014', '2015', '2016', '2017', '2018'],
  180. series: [
  181. {
  182. name: '类别一',
  183. data: [35, 36, 31, 33, 13, 34]
  184. },
  185. {
  186. name: '类别二',
  187. data: [18, 27, 21, 24, 6, 28]
  188. },
  189. {
  190. name: '类别三',
  191. data: [18, 27, 21, 24, 6, 28]
  192. }
  193. ]
  194. },
  195. arcbarList: [
  196. //圆环
  197. {
  198. series: [
  199. {
  200. name: '正确率',
  201. data: 0.2912
  202. }
  203. ]
  204. },
  205. {
  206. series: [
  207. {
  208. name: '错误率',
  209. data: 0.6921
  210. }
  211. ]
  212. },
  213. {
  214. series: [
  215. {
  216. name: '失误率',
  217. data: 0.8232
  218. }
  219. ]
  220. }
  221. ],
  222. lineData: {
  223. //带百分比的图--折线图数据
  224. categories: ['20111', '2013', '2014', '2015', '2016', '2017'],
  225. 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] }]
  226. },
  227. lineData2: {
  228. //数字的图--折线图数据
  229. categories: ['2012', '2013', '2014', '2015', '2016', '2017'],
  230. series: [
  231. { name: '成交量A', data: [35, 8, 25, 37, 4, 20] },
  232. { name: '成交量B', data: [70, 40, 65, 100, 44, 68] },
  233. { name: '成交量C', data: [100, 80, 95, 150, 112, 132] }
  234. ]
  235. },
  236. pieData: {
  237. //饼状图数据
  238. series: [
  239. {
  240. name: '一班',
  241. data: 50
  242. },
  243. {
  244. name: '二班',
  245. data: 30
  246. },
  247. {
  248. name: '三班',
  249. data: 20
  250. },
  251. {
  252. name: '四班',
  253. data: 18
  254. },
  255. {
  256. name: '五班',
  257. data: 8
  258. }
  259. ]
  260. }
  261. };
  262. },
  263. methods: {
  264. //改变柱状图数据
  265. changeHistogramData() {
  266. let categories = ['20111', '2013', '2014', '2015', '2016', '2017'];
  267. let series = [{ name: '成交量A', data: [200, 233, 25, 370, 400, 200] }];
  268. this.$refs['histogramData0'].changeData({ series, categories });
  269. },
  270. //改变折线图数据
  271. changeLineData() {
  272. let categories = ['20111', '2013', '2014', '2015', '2016', '2017'];
  273. let series = [{ name: '成交量A', data: [0.3511, 0.8233, 0.25, 0.37, 0.4, 0.2] }];
  274. this.$refs['lineData1'].changeData({ series, categories });
  275. },
  276. //改变饼状图数据
  277. changePieData() {
  278. let series = [{ name: '一班', data: 150 }, { name: '二班', data: 130 }, { name: '三班', data: 120 }, { name: '四班', data: 182 }, { name: '五班', data: 83 }];
  279. this.$refs['pieChart0'].changeData(series);
  280. },
  281. //改变环状图数据
  282. changeRingData() {
  283. let series = [{ name: '一班', data: 150 }, { name: '二班', data: 130 }, { name: '三班', data: 120 }, { name: '四班', data: 182 }, { name: '五班', data: 83 }];
  284. this.$refs['ringChart0'].changeData(series);
  285. },
  286. getServerData() {
  287. //模拟ajax调用
  288. // uni.request({
  289. // url: 'https://www.easy-mock.com/mock/5cc586b64fc5576cba3d647b/uni-wx-charts/chartsdata2',
  290. // data: {},
  291. // success: function(res) {
  292. // console.log(res.data.data);
  293. // let serverData = res.data.data;
  294. // this.$set(this.lineData, 'categories', serverData.Column.categories);
  295. // this.$set(this.lineData, 'series', serverData.Column.series);
  296. // this.$refs['lineData1'].showCharts();
  297. // },
  298. // fail: () => {
  299. // console.log('网络错误');
  300. // }
  301. // });
  302. setTimeout(() => {
  303. //延迟模拟ajax嗲用后台数据
  304. let categories = ['20111', '2013', '2014', '2015', '2016', '2017'];
  305. let series = [{ name: '成交量A', data: [0.8511, 0.233, 0.125, 0.437, 0.48, 0.1234] }];
  306. this.$set(this.lineData, 'categories', categories);
  307. this.$set(this.lineData, 'series', series);
  308. this.$refs['lineData1'].showCharts();
  309. }, 1000);
  310. }
  311. },
  312. created() {
  313. this.$nextTick(() => {
  314. //柱状图
  315. this.$refs['histogramData0'].showCharts();
  316. this.$refs['histogramData1'].showCharts();
  317. this.$refs['histogramData2'].showCharts();
  318. //圆环(注意循环可能会导致出现下面情况,请更具实际情况作出判断// console.log(this.$refs);)
  319. this.$refs['arcbar0'][0].showCharts();
  320. this.$refs['arcbar1'][0].showCharts();
  321. this.$refs['arcbar2'][0].showCharts();
  322. // 饼状图
  323. this.$refs['pieChart0'].showCharts();
  324. // 环状图
  325. this.$refs['ringChart0'].showCharts();
  326. //折线图
  327. this.$refs['lineData2'].showCharts();
  328. });
  329. //ajax调用
  330. this.getServerData();
  331. }
  332. };
  333. </script>
  334. <style scoped>
  335. .arcbar {
  336. display: flex;
  337. }
  338. </style>