index.vue 11 KB

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