index.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <template>
  2. <view class="page">
  3. <view class="chart" v-if="testData.length">
  4. <cusEcharts :option="testOption"></cusEcharts>
  5. </view>
  6. <view class="chart" v-if="kxData.length">
  7. <cusEcharts :option="kxOption"></cusEcharts>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import * as echarts from "echarts";
  13. export default {
  14. data(){
  15. return {
  16. testData:[],
  17. testOption:null,
  18. kxData:[],
  19. kxOption:null
  20. }
  21. },
  22. mounted() {
  23. this.init();
  24. },
  25. methods:{
  26. init(){
  27. this.initTestChart();
  28. this.initKxChart();
  29. },
  30. initTestChart(){
  31. this.testData = [150, 230, 224, 218, 135, 147, 260];
  32. this.testOption = {
  33. xAxis: {
  34. type: 'category',
  35. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  36. },
  37. yAxis: {
  38. type: 'value'
  39. },
  40. series: [
  41. {
  42. data: this.testData,
  43. type: 'line'
  44. }
  45. ]
  46. };
  47. },
  48. initKxChart(){
  49. this.kxData = [270,220,350,210,280,400];
  50. const offsetX = 4;
  51. const offsetY = 2;
  52. // 绘制左侧面
  53. const CubeLeft = echarts.graphic.extendShape({
  54. shape: {
  55. x: 0,
  56. y: 0,
  57. },
  58. buildPath: function (ctx, shape) {
  59. // 会canvas的应该都能看得懂,shape是从custom传入的
  60. const xAxisPoint = shape.xAxisPoint;
  61. const c0 = [shape.x, shape.y];
  62. const c1 = [shape.x - offsetX, shape.y - offsetY];
  63. const c2 = [xAxisPoint[0] - offsetX, xAxisPoint[1] - offsetY];
  64. const c3 = [xAxisPoint[0], xAxisPoint[1]];
  65. ctx
  66. .moveTo(c0[0], c0[1])
  67. .lineTo(c1[0], c1[1])
  68. .lineTo(c2[0], c2[1])
  69. .lineTo(c3[0], c3[1])
  70. .closePath();
  71. },
  72. });
  73. // 绘制右侧面
  74. const CubeRight = echarts.graphic.extendShape({
  75. shape: {
  76. x: 0,
  77. y: 0,
  78. },
  79. buildPath: function (ctx, shape) {
  80. const xAxisPoint = shape.xAxisPoint;
  81. const c1 = [shape.x, shape.y];
  82. const c2 = [xAxisPoint[0], xAxisPoint[1]];
  83. const c3 = [xAxisPoint[0] + offsetX, xAxisPoint[1] - offsetY];
  84. const c4 = [shape.x + offsetX, shape.y - offsetY];
  85. ctx
  86. .moveTo(c1[0], c1[1])
  87. .lineTo(c2[0], c2[1])
  88. .lineTo(c3[0], c3[1])
  89. .lineTo(c4[0], c4[1])
  90. .closePath();
  91. },
  92. });
  93. // 绘制顶面
  94. const CubeTop = echarts.graphic.extendShape({
  95. shape: {
  96. x: 0,
  97. y: 0,
  98. },
  99. buildPath: function (ctx, shape) {
  100. const c1 = [shape.x, shape.y];
  101. const c2 = [shape.x + offsetX, shape.y - offsetY]; //右点
  102. const c3 = [shape.x, shape.y - offsetX];
  103. const c4 = [shape.x - offsetX, shape.y - offsetY];
  104. ctx
  105. .moveTo(c1[0], c1[1])
  106. .lineTo(c2[0], c2[1])
  107. .lineTo(c3[0], c3[1])
  108. .lineTo(c4[0], c4[1])
  109. .closePath();
  110. },
  111. });
  112. // 注册三个面图形
  113. echarts.graphic.registerShape("CubeLeft", CubeLeft);
  114. echarts.graphic.registerShape("CubeRight", CubeRight);
  115. echarts.graphic.registerShape("CubeTop", CubeTop);
  116. this.kxOption = {
  117. tooltip: {
  118. trigger: 'axis',
  119. backgroundColor: 'rgba(13,5,30,.6)',
  120. borderWidth: 1,
  121. borderColor: '#4ddd8f',
  122. padding: 5,
  123. textStyle: {
  124. color: '#fff'
  125. }
  126. },
  127. legend: {
  128. right: 170,
  129. top: 15,
  130. textStyle: {
  131. fontSize: 12,
  132. color: '#A7BFDE'
  133. },
  134. itemWidth: 6,
  135. itemHeight: 8
  136. },
  137. grid: {
  138. left: '4%',
  139. right: '3%',
  140. top: '25%',
  141. bottom: '8%',
  142. containLabel: true,
  143. },
  144. xAxis: {
  145. type: 'category',
  146. data: ['3月', '4月', '5月', '6月', '7月', '8月'],
  147. axisPointer: {
  148. type: 'shadow'
  149. },
  150. axisTick: {
  151. show: true,
  152. length: 4,
  153. lineStyle: {
  154. color: 'rgba(199,211,229,0.5)'
  155. }
  156. },
  157. axisLine: {
  158. show: true,
  159. lineStyle: {
  160. color: 'rgba(199,211,229,0.5)'
  161. }
  162. },
  163. axisLabel: {
  164. color: '#c7d3e5'
  165. }
  166. },
  167. yAxis: {
  168. name:'(单位)',
  169. type: 'value',
  170. nameTextStyle: {
  171. fontSize: 12,
  172. color: '#C6D7F1',
  173. fontWeight: 400,
  174. },
  175. nameGap: 25,
  176. axisLine: {
  177. show: false,
  178. lineStyle: {
  179. width: 1,
  180. color: '#545454'
  181. }
  182. },
  183. splitLine: {
  184. show: true,
  185. lineStyle: {
  186. color: 'rgba(199,211,229,0.3)',
  187. width: 1,
  188. type: 'dashed'
  189. }
  190. },
  191. axisTick: {
  192. show: false
  193. },
  194. axisLabel: {
  195. fontSize: 12,
  196. color: '#c7d3e5'
  197. }
  198. },
  199. series: [
  200. {
  201. name: "国内",
  202. type: "custom",
  203. renderItem: (params, api) => {
  204. const location = api.coord([api.value(0), api.value(1)]);
  205. const xAxisPoint = api.coord([api.value(0), 0]);
  206. const distance = 7;
  207. return {
  208. type: "group",
  209. children: [{
  210. type: "CubeLeft",
  211. shape: {
  212. api,
  213. xValue: api.value(0),
  214. yValue: api.value(1),
  215. x: location[0] + distance,
  216. y: location[1],
  217. xAxisPoint: [xAxisPoint[0] + distance, xAxisPoint[1]],
  218. },
  219. style: {
  220. fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  221. offset: 0,
  222. color: '#0B5FA9',
  223. },
  224. {
  225. offset: 1,
  226. color: 'rgba(11,95,169,0)'
  227. }
  228. ]),
  229. },
  230. },
  231. {
  232. type: "CubeRight",
  233. shape: {
  234. api,
  235. xValue: api.value(0),
  236. yValue: api.value(1),
  237. x: location[0] + distance,
  238. y: location[1],
  239. xAxisPoint: [xAxisPoint[0] + distance, xAxisPoint[1]],
  240. },
  241. style: {
  242. fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  243. offset: 0,
  244. color: '#088BFF',
  245. },
  246. {
  247. offset: 1,
  248. color: 'rgba(8,139,255,0)'
  249. }
  250. ]),
  251. },
  252. },
  253. {
  254. type: "CubeTop",
  255. shape: {
  256. api,
  257. xValue: api.value(0),
  258. yValue: api.value(1),
  259. x: location[0] + distance,
  260. y: location[1],
  261. xAxisPoint: [xAxisPoint[0] + distance, xAxisPoint[1]],
  262. },
  263. style: {
  264. fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  265. offset: 0,
  266. color: '#61B5FF',
  267. },
  268. {
  269. offset: 1,
  270. color: '#61B5FF'
  271. }
  272. ]),
  273. },
  274. },
  275. ],
  276. };
  277. },
  278. itemStyle: {
  279. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  280. offset: 0,
  281. color: "#00BFF4",
  282. },
  283. {
  284. offset: 1,
  285. color: "#A5E2FF",
  286. },
  287. ]),
  288. },
  289. data: [410,330,210,210,480,190],
  290. },
  291. {
  292. name: "出口",
  293. type: "custom",
  294. renderItem: (params, api) => {
  295. const location = api.coord([api.value(0), api.value(1)]);
  296. const xAxisPoint = api.coord([api.value(0), 0]);
  297. const distance = 7;
  298. return {
  299. type: "group",
  300. children: [{
  301. type: "CubeLeft",
  302. shape: {
  303. api,
  304. xValue: api.value(0),
  305. yValue: api.value(1),
  306. x: location[0] - distance,
  307. y: location[1],
  308. xAxisPoint: [xAxisPoint[0] - distance, xAxisPoint[1]],
  309. },
  310. style: {
  311. fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  312. offset: 0,
  313. color: '#28A9A2',
  314. },
  315. {
  316. offset: 1,
  317. color: 'rgba(40,169,162,0)'
  318. }
  319. ]),
  320. },
  321. },
  322. {
  323. type: "CubeRight",
  324. shape: {
  325. api,
  326. xValue: api.value(0),
  327. yValue: api.value(1),
  328. x: location[0] - distance,
  329. y: location[1],
  330. xAxisPoint: [xAxisPoint[0] - distance, xAxisPoint[1]],
  331. },
  332. style: {
  333. fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  334. offset: 0,
  335. color: '#35FFF4',
  336. },
  337. {
  338. offset: 1,
  339. color: 'rgba(53,255,244,0)'
  340. }
  341. ]),
  342. },
  343. },
  344. {
  345. type: "CubeTop",
  346. shape: {
  347. api,
  348. xValue: api.value(0),
  349. yValue: api.value(1),
  350. x: location[0] - distance,
  351. y: location[1],
  352. xAxisPoint: [xAxisPoint[0] - distance, xAxisPoint[1]],
  353. },
  354. style: {
  355. fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  356. offset: 0,
  357. color: '#7EFFF8',
  358. },
  359. {
  360. offset: 1,
  361. color: '#7EFFF8'
  362. }
  363. ]),
  364. },
  365. },
  366. ],
  367. };
  368. },
  369. itemStyle: {
  370. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  371. offset: 0,
  372. color: "#6AAAFF",
  373. },
  374. {
  375. offset: 1,
  376. color: "#046EFE",
  377. },
  378. ]),
  379. },
  380. data: this.kxData,
  381. }
  382. ],
  383. };
  384. },
  385. }
  386. }
  387. </script>
  388. <style scoped lang="less">
  389. .chart{
  390. width: 100%;
  391. height: 300px;
  392. margin-top: 50px;
  393. &:first-child{
  394. margin-top: 0;
  395. }
  396. }
  397. </style>