index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. //index.js
  2. //获取应用实例
  3. import * as echarts from '../../ec-canvas/echarts';
  4. var imageUtil = require('../../utils/window.js');
  5. const apiServer = require('../../api/request');
  6. var Chart = null;
  7. const app = getApp()
  8. var timeData = [
  9. '2020/6/1', '2009/6/2', '2009/6/3', '2009/6/4', '2009/6/5', '2009/6/6', '2009/6/7', '2009/6/8', '2009/6/9', '2009/6/10',
  10. ];
  11. timeData = timeData.map(function (str) {
  12. return str.replace('2009/', '');
  13. });
  14. function initChart(canvas, width, height, dpr, data) {
  15. const chart = echarts.init(canvas, null, {
  16. width: width,
  17. height: height,
  18. devicePixelRatio: dpr // new
  19. });
  20. canvas.setChart(chart);
  21. var option = {
  22. title: {//标题
  23. text: '',
  24. left: 'center'
  25. },
  26. renderAsImage: true, //支持渲染为图片模式
  27. color: ["#DF3E3E", "#80CDF8"],//图例图标颜色
  28. legend: {
  29. show: true,
  30. itemGap: 25,//每个图例间的间隔
  31. top: 30,
  32. x: 30,//水平安放位置,离容器左侧的距离 'left'
  33. z: 100,
  34. textStyle: {
  35. color: '#383838'
  36. },
  37. },
  38. grid: {//网格
  39. // left: 30,
  40. // top:100,
  41. containLabel: true,//grid 区域是否包含坐标轴的刻度标签
  42. },
  43. xAxis: {//横坐标
  44. type: 'category',
  45. // nameTextStyle: {//在name值存在下,设置name的样式
  46. // color: 'white',
  47. // fontStyle: 'normal'
  48. // },
  49. nameLocation:'end',
  50. splitLine: {//坐标轴在 grid 区域中的分隔线。
  51. show: true,
  52. lineStyle: {
  53. type: 'dashed',
  54. color: ['#2B2B2B']
  55. }
  56. },
  57. boundaryGap: false,//1.true 数据点在2个刻度直接 2.fals 数据点在分割线上,即刻度值上
  58. data: data.timeData,
  59. axisLine: {onZero: true},
  60. axisLabel: {
  61. textStyle: {
  62. fontSize: 13,
  63. color: '#5D5D5D'
  64. }
  65. }
  66. },
  67. dataZoom: [
  68. {
  69. type: 'slider',
  70. xAxisIndex: 0,
  71. filterMode: 'empty'
  72. },
  73. ],
  74. yAxis: {//纵坐标
  75. type: 'value',
  76. position:'right',
  77. // nameTextStyle:{//在name值存在下,设置name的样式
  78. // color:'red',
  79. // fontStyle:'normal'
  80. // },
  81. splitNumber: 5,//坐标轴的分割段数
  82. splitLine: {//坐标轴在 grid 区域中的分隔线。
  83. show: true,
  84. lineStyle: {
  85. type: 'dashed',
  86. color: ['#2B2B2B']
  87. }
  88. },
  89. axisLabel: {//坐标轴刻度标签
  90. // formatter: function (value) {
  91. // var xLable = [];
  92. // if (value == 20) {
  93. // xLable.push('25,883');
  94. // }
  95. // if (value == 40) {
  96. // xLable.push('26,035');
  97. // }
  98. // if (value == 60) {
  99. // xLable.push('26,187');
  100. // }
  101. // if (value == 80) {
  102. // xLable.push('26,339');
  103. // }
  104. // return xLable
  105. // },
  106. textStyle: {
  107. fontSize: 13,
  108. color: '#5D5D5D',
  109. }
  110. },
  111. // min: 0,
  112. // max: 100,
  113. },
  114. series: [{
  115. name: '实际',
  116. type: 'line',
  117. // smooth:true,
  118. data:data.actual,
  119. symbol: 'none',
  120. areaStyle: {
  121. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  122. offset: 0,
  123. color: '#DF3E3E'
  124. }, {
  125. offset: 1,
  126. color: '#1C1C1C'
  127. }])
  128. },
  129. itemStyle: {
  130. normal: {
  131. lineStyle: {
  132. color: '#DF3E3E'
  133. }
  134. }
  135. }
  136. }, {
  137. name: '预测',
  138. type: 'line',
  139. data: data.prediction,
  140. symbol: 'none',
  141. itemStyle: {
  142. normal: {
  143. lineStyle: {
  144. color: '#80CDF8'
  145. }
  146. }
  147. }
  148. }],
  149. };
  150. chart.setOption(option);
  151. return chart;
  152. }
  153. Page({
  154. data: {
  155. url:'../../images/timg.jpg',
  156. motto: '',
  157. currentTab: 0,
  158. userInfo: {},
  159. hasUserInfo: false,
  160. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  161. imagewidth: 0,//缩放后的宽
  162. imageheight: 0,//缩放后的高
  163. // chartData:{
  164. // actual:[552,586,3555,4454,552,556,4455],
  165. // prediction:[552,546,3585,4424,542,546,35,44,524,56,355,444,2224,421],
  166. // timeData : [
  167. // '2020/6/1', '2020/6/2', '2020/6/3', '2020/6/4', '2020/6/5', '2020/6/6', '2020/6/7', '2020/6/8', '2020/6/9', '2020/6/10','2020/6/11',
  168. // '2020/6/12','2020/6/13','2020/6/14',
  169. // ]
  170. // },
  171. chartDatas:{
  172. actual:[572,556,3555,4744,552,576,3555],
  173. prediction:[352,246,385,2424,542,546,255],
  174. timeData : [
  175. '2020/6/1', '2020/6/2', '2020/6/3', '2020/6/4', '2020/6/5', '2020/6/6', '2020/6/7'
  176. ]
  177. },
  178. ec: {
  179. onInit: initChart,
  180. // lazyLoad: true // 延迟加载
  181. },
  182. // ec: {
  183. // onInit: function (canvas, width, height) {
  184. // chart = echarts.init(canvas, null, {
  185. // width: width,
  186. // height: height
  187. // });
  188. // canvas.setChart(chart);
  189. // return chart;
  190. // },
  191. // lazyLoad: true // 延迟加载
  192. // },
  193. aa:[
  194. // {
  195. // id:'0',
  196. // from:"Investing.com 中文",
  197. // nickName:"买入波音(BA.US)好时机?两个问题没解决之前劝你住手",
  198. // reward:"今年迄今,美股航空板块受到公共卫生事件的冲击.....",
  199. // time:"6天前"
  200. // },
  201. // {
  202. // id:'1',
  203. // from:"chinese.aljazeera.net",
  204. // nickName: "波音裁员超1.2万人与警惕最坏时刻尚未到来",
  205. // reward: "波音公司当地时间27日宣布,将在美国裁减超1...",
  206. // time:"6天前"
  207. // },
  208. // {
  209. // id:'2',
  210. // from:"Investing.com 中文",
  211. // nickName: "美股异动|波音737MAX客机恢复生产,盘前涨逾5%",
  212. // reward: "Reuters.美股异动|波音(BA.US)737MAX客...",
  213. // time:"5天前"
  214. // },
  215. // {
  216. // id:'3',
  217. // from:"Investing.com 中文",
  218. // nickName:"买入波音(BA.US)好时机?两个问题没解决之前劝你住手",
  219. // reward:"今年迄今,美股航空板块受到公共卫生事件的冲击.....",
  220. // time:"6天前"
  221. // },
  222. // {
  223. // id:'4',
  224. // from:"chinese.aljazeera.net",
  225. // nickName: "波音裁员超1.2万人与警惕最坏时刻尚未到来",
  226. // reward: "波音公司当地时间27日宣布,将在美国裁减超1...",
  227. // time:"6天前"
  228. // },
  229. // {
  230. // id:'5',
  231. // from:"Investing.com 中文",
  232. // nickName: "美股异动|波音737MAX客机恢复生产,盘前涨逾5%",
  233. // reward: "Reuters.美股异动|波音(BA.US)737MAX客...",
  234. // time:"5天前"
  235. // },
  236. // {
  237. // id:'6',
  238. // from:"Investing.com 中文",
  239. // nickName:"买入波音(BA.US)好时机?两个问题没解决之前劝你住手",
  240. // reward:"今年迄今,美股航空板块受到公共卫生事件的冲击.....",
  241. // time:"6天前"
  242. // },
  243. // {
  244. // id:'7',
  245. // from:"chinese.aljazeera.net",
  246. // nickName: "波音裁员超1.2万人与警惕最坏时刻尚未到来",
  247. // reward: "波音公司当地时间27日宣布,将在美国裁减超1...",
  248. // time:"6天前"
  249. // },
  250. // {
  251. // id:'8',
  252. // from:"Investing.com 中文",
  253. // nickName: "美股异动|波音737MAX客机恢复生产,盘前涨逾5%",
  254. // reward: "Reuters.美股异动|波音(BA.US)737MAX客...",
  255. // time:"5天前"
  256. // },
  257. // {
  258. // id:'9',
  259. // from:"Investing.com 中文",
  260. // nickName:"买入波音(BA.US)好时机?两个问题没解决之前劝你住手",
  261. // reward:"今年迄今,美股航空板块受到公共卫生事件的冲击.....",
  262. // time:"6天前"
  263. // },
  264. // {
  265. // id:'10',
  266. // from:"chinese.aljazeera.net",
  267. // nickName: "波音裁员超1.2万人与警惕最坏时刻尚未到来",
  268. // reward: "波音公司当地时间27日宣布,将在美国裁减超1...",
  269. // time:"6天前"
  270. // },
  271. // {
  272. // id:'11',
  273. // from:"Investing.com 中文",
  274. // nickName: "美股异动|波音737MAX客机恢复生产,盘前涨逾5%",
  275. // reward: "Reuters.美股异动|波音(BA.US)737MAX客...",
  276. // time:"5天前"
  277. // },
  278. // {
  279. // id:'12',
  280. // from:"Investing.com 中文",
  281. // nickName:"买入波音(BA.US)好时机?两个问题没解决之前劝你住手",
  282. // reward:"今年迄今,美股航空板块受到公共卫生事件的冲击.....",
  283. // time:"6天前"
  284. // },
  285. // {
  286. // id:'13',
  287. // from:"chinese.aljazeera.net",
  288. // nickName: "波音裁员超1.2万人与警惕最坏时刻尚未到来",
  289. // reward: "波音公司当地时间27日宣布,将在美国裁减超1...",
  290. // time:"6天前"
  291. // },
  292. // {
  293. // id:'14',
  294. // from:"Investing.com 中文",
  295. // nickName: "美股异动|波音737MAX客机恢复生产,盘前涨逾5%",
  296. // reward: "Reuters.美股异动|波音(BA.US)737MAX客...",
  297. // time:"5天前"
  298. // },
  299. ],
  300. },
  301. //获取新闻列表
  302. getNewsList:function(){
  303. apiServer.getNewsList().then((res)=>{
  304. if(res.statusCode == 200){
  305. this.setData({
  306. aa:res.data.data
  307. })
  308. }
  309. })
  310. },
  311. //事件处理函数
  312. bindViewTap: function() {
  313. wx.navigateTo({
  314. url: '../logs/logs'
  315. })
  316. },
  317. //沪深
  318. getHushen:function(){
  319. apiServer.getHushen().then((res) =>{
  320. if(res.statusCode == 200){
  321. console.log(res);
  322. this.setData({
  323. chartDatas:this.recursion(res.data.data),
  324. })
  325. }
  326. })
  327. },
  328. recursion:function(row,a=[],b=[],c=[],d){
  329. for(let i in row.actuals){
  330. a.unshift(row.actuals[i].industryDate)
  331. b.unshift(row.actuals[i].close)
  332. c.unshift(row.actuals[i].pred)
  333. }
  334. if(row.tag){
  335. d="#D81E06"
  336. }else{
  337. d="#07c160"
  338. }
  339. return {"timeData":a,"actual":b,"prediction":c,"color":d}
  340. },
  341. onReady: function (e) {
  342. this.computeScrollViewHeight();
  343. // var query = wx.createSelectorQuery()
  344. // query.select('#test').boundingClientRect(function (res) {
  345. // console.log(res.top);
  346. // }).exec();
  347. },
  348. onLoad: function () {
  349. this.getNewsList();
  350. this.getHushen();
  351. let sysinfo = wx.getSystemInfoSync(),
  352. windowHeight = sysinfo.windowHeight,
  353. statusHeight = sysinfo.statusBarHeight,
  354. isiOS = sysinfo.system.indexOf('iOS') > -1,
  355. navHeight;
  356. if (!isiOS) {
  357. navHeight = 48;
  358. } else {
  359. navHeight = 44;
  360. }
  361. let height = windowHeight-statusHeight-navHeight;
  362. this.setData({
  363. height:height
  364. })
  365. // this.echartsComponnet = this.selectComponent('#mychart');
  366. // console.log(this.echartsComponnet);
  367. // //如果是第一次绘制
  368. // if (!Chart) {
  369. // this.init_echarts(); //初始化图表
  370. // } else {
  371. // this.setOption(Chart); //更新数据
  372. // }
  373. // this.computeScrollViewHeight();
  374. // if (app.globalData.userInfo) {
  375. // this.setData({
  376. // userInfo: app.globalData.userInfo,
  377. // hasUserInfo: true
  378. // })
  379. // } else if (this.data.canIUse){
  380. // // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  381. // // 所以此处加入 callback 以防止这种情况
  382. // app.userInfoReadyCallback = res => {
  383. // this.setData({
  384. // userInfo: res.userInfo,
  385. // hasUserInfo: true
  386. // })
  387. // }
  388. // } else {
  389. // // 在没有 open-type=getUserInfo 版本的兼容处理
  390. // wx.getUserInfo({
  391. // success: res => {
  392. // app.globalData.userInfo = res.userInfo
  393. // this.setData({
  394. // userInfo: res.userInfo,
  395. // hasUserInfo: true
  396. // })
  397. // }
  398. // })
  399. // }
  400. },
  401. computeScrollViewHeight() {
  402. var that = this
  403. let query = wx.createSelectorQuery().in(that)
  404. query.select('.swiper-tab').boundingClientRect()
  405. query.select('.chart').boundingClientRect()
  406. query.select('.news').boundingClientRect()
  407. query.exec(res => {
  408. let topHeight = res[0].height
  409. let searchHeight = res[1].height
  410. let titleHeight = res[2].height
  411. let windowHeight = wx.getSystemInfoSync().windowHeight
  412. let windowWidth = wx.getSystemInfoSync().windowWidth
  413. let height = windowHeight - topHeight - searchHeight - titleHeight
  414. let ratio = 750 / windowWidth
  415. let scrollHeight = height * ratio
  416. that.setData({ scrollHeight: scrollHeight})
  417. })
  418. },
  419. imageLoad: function (e) {
  420. var imageSize = imageUtil.imageUtil(e)
  421. this.setData({
  422. imagewidth: imageSize.imageWidth,
  423. imageheight: imageSize.imageHeight
  424. })
  425. this.computeScrollViewHeight();
  426. },
  427. onFold:function(e){
  428. wx.navigateTo({
  429. url:'../news/news?newsId='+e.currentTarget.dataset.id
  430. });
  431. },
  432. choose:function(e){
  433. wx.navigateTo({
  434. url:"../filter/filter"
  435. })
  436. },
  437. swichNav: function(e) {
  438. var that = this;
  439. if (this.data.currentTab === e.target.dataset.current) {
  440. return false;
  441. } else {
  442. that.setData({
  443. currentTab: e.target.dataset.current
  444. })
  445. }
  446. },
  447. // 滑动切换tab
  448. bindChange: function(e) {
  449. var that = this;
  450. that.setData({
  451. currentTab: e.detail.current
  452. });
  453. },
  454. catchTouchMove:function(res){
  455. return false
  456. },
  457. //初始化图表
  458. init_echarts: function () {
  459. this.echartsComponnet.init((canvas, width, height) => {
  460. // 初始化图表
  461. const Chart = echarts.init(canvas, null, {
  462. width: width,
  463. height: height
  464. });
  465. this.setOption(Chart)
  466. // 注意这里一定要返回 chart 实例,否则会影响事件处理等
  467. return Chart
  468. });
  469. },
  470. setOption: function (Chart) {
  471. Chart.clear(); // 清除
  472. Chart.setOption(this.getOption()); //获取新数据
  473. },
  474. // 图表配置项
  475. getOption() {
  476. var self = this;
  477. var option = {
  478. title: {//标题
  479. text: '',
  480. left: 'center'
  481. },
  482. renderAsImage: true, //支持渲染为图片模式
  483. color: ["#DF3E3E", "#80CDF8"],//图例图标颜色
  484. legend: {
  485. // left:'right',
  486. show: true,
  487. itemGap: 25,//每个图例间的间隔
  488. top: 20,
  489. right:20,
  490. x: 100,//水平安放位置,离容器左侧的距离 'left'
  491. z: 100,
  492. textStyle: {
  493. color: '#383838'
  494. },
  495. data: [//图例具体内容
  496. {
  497. name: '实际',
  498. textStyle: {
  499. fontSize: 13,
  500. color: 'white'
  501. },
  502. icon: 'line'
  503. },
  504. {
  505. name: '预测',
  506. textStyle: {
  507. fontSize: 13,
  508. color: 'white'
  509. },
  510. icon: 'line',
  511. }
  512. ]
  513. },
  514. grid: {//网格
  515. // left: 50,
  516. // top:100,
  517. containLabel: true,//grid 区域是否包含坐标轴的刻度标签
  518. },
  519. xAxis: {//横坐标
  520. type: 'category',
  521. // name: '区间',//横坐标名称
  522. nameTextStyle: {//在name值存在下,设置name的样式
  523. color: 'white',
  524. fontStyle: 'normal'
  525. },
  526. nameLocation:'end',
  527. splitLine: {//坐标轴在 grid 区域中的分隔线。
  528. show: true,
  529. lineStyle: {
  530. type: 'dashed',
  531. color: ['#2B2B2B']
  532. }
  533. },
  534. boundaryGap: false,//1.true 数据点在2个刻度直接 2.fals 数据点在分割线上,即刻度值上
  535. data: ['2020/6/29', '2020/6/30', '2020/7/1', '2020/7/2', '2020/7/3'],
  536. axisLine: {onZero: true},
  537. axisLabel: {
  538. textStyle: {
  539. fontSize: 13,
  540. color: '#5D5D5D'
  541. }
  542. }
  543. },
  544. yAxis: {//纵坐标
  545. type: 'value',
  546. position:'right',
  547. name: '股票值',//纵坐标名称
  548. nameTextStyle:{//在name值存在下,设置name的样式
  549. color:'red',
  550. fontStyle:'normal'
  551. },
  552. splitNumber: 5,//坐标轴的分割段数
  553. splitLine: {//坐标轴在 grid 区域中的分隔线。
  554. show: true,
  555. lineStyle: {
  556. type: 'dashed',
  557. color: ['#2B2B2B']
  558. }
  559. },
  560. axisLabel: {//坐标轴刻度标签
  561. // formatter: function (value) {
  562. // var xLable = [];
  563. // if (value == 20) {
  564. // xLable.push('1000');
  565. // }
  566. // if (value == 40) {
  567. // xLable.push('2000');
  568. // }
  569. // if (value == 60) {
  570. // xLable.push('4000');
  571. // }
  572. // if (value == 80) {
  573. // xLable.push('6000');
  574. // }
  575. // return xLable
  576. // },
  577. textStyle: {
  578. fontSize: 13,
  579. color: '#5D5D5D',
  580. }
  581. },
  582. // min: 0,
  583. // max: 100,
  584. },
  585. series: [{
  586. name: '实际',
  587. type: 'line',
  588. // smooth:true,
  589. data: [1888, 3666, 6555, 3000, 7888, 4000, 3333],
  590. symbol: 'none',
  591. areaStyle: {
  592. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  593. {
  594. offset: 0,
  595. color: '#DF3E3E'
  596. },
  597. {
  598. offset: 1,
  599. color: '#1C1C1C'
  600. }
  601. ])
  602. },
  603. itemStyle: {
  604. normal: {
  605. lineStyle: {
  606. color: '#DF3E3E'
  607. }
  608. }
  609. }
  610. }, {
  611. name: '预测',
  612. type: 'line',
  613. data: [1000, 3000, 3111, 5000, 4000, 2000, 1000],
  614. symbol: 'none',
  615. itemStyle: {
  616. normal: {
  617. lineStyle: {
  618. color: '#80CDF8'
  619. }
  620. }
  621. }
  622. }],
  623. }
  624. return option;
  625. },
  626. getUserInfo: function(e) {
  627. console.log(e)
  628. app.globalData.userInfo = e.detail.userInfo
  629. this.setData({
  630. userInfo: e.detail.userInfo,
  631. hasUserInfo: true
  632. })
  633. }
  634. })