kline.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. var myChart;
  2. var app = new Vue({
  3. el: '#app',
  4. data: {
  5. timer: '',
  6. MA5: '',
  7. MA10: '',
  8. MA30: '',
  9. volMA5: '',
  10. volMA10: '',
  11. current: "1m",
  12. tabs: [{
  13. 'label': '1分鐘',
  14. 'value': "1m"
  15. },
  16. {
  17. 'label': '15分鐘',
  18. 'value': "15m"
  19. },
  20. {
  21. 'label': '30分鐘',
  22. 'value': "30m"
  23. },
  24. {
  25. 'label': '1小時',
  26. 'value': "1H"
  27. },
  28. {
  29. 'label': '4小時',
  30. 'value': "4H"
  31. },
  32. {
  33. 'label': '1日',
  34. 'value': "1D"
  35. },
  36. ],
  37. category: 2,
  38. categoryList: [{
  39. 'label': '深度',
  40. 'value': 1
  41. },
  42. {
  43. 'label': '成交',
  44. 'value': 2
  45. },
  46. {
  47. 'label': '简介',
  48. 'value': 3
  49. },
  50. ],
  51. txData: {
  52. lastPrice: '',
  53. high: '',
  54. volume: '',
  55. low: '',
  56. upRate: '',
  57. }, //交易数据统计
  58. buyList: [],
  59. sellList: [],
  60. dealHis: [],
  61. tokenInfo: {},
  62. page: 1,
  63. // 保存商品id
  64. typeId: '',
  65. // 保存socket对象
  66. webSocket: '',
  67. // 保存当前k线数据
  68. dataKLine: {
  69. data: [],
  70. dates: [],
  71. volumes: [],
  72. },
  73. // 保存初始未处理的时间数据
  74. oldDataKLine: ''
  75. },
  76. created() {
  77. // this.getTxData()
  78. // this.getDepth()
  79. // 保存商品id
  80. this.typeId = getQueryString('type');
  81. // 简历长连接
  82. // if (this.typeId == 'GCB-USDT') {
  83. // } else {
  84. // }
  85. this.scoketInit()
  86. if(this.typeId == 'GCB-USDT') {
  87. this.gcb()
  88. }
  89. },
  90. mounted() {
  91. myChart = echarts.init(document.getElementById('main'));
  92. this.draw()
  93. this.getKline((new Date()).getTime());
  94. // 获取成交记录
  95. this.getDealHis();
  96. },
  97. methods: {
  98. sgToFixed(num,digit=2){
  99. let nums=(num||0).toString().split('.');
  100. return nums[0]+'.'+(nums[1]||'0').padEnd(digit, "0");
  101. },
  102. //
  103. gcb() {
  104. let that = this
  105. axiosGet('/Levertade/gcb', {}).then(res => {
  106. console.log(JSON.stringify(res), 'gcb++++')
  107. console.log(res.name,'res.name+++++++++++++++++++++++++++')
  108. if(that.typeId == 'GCB-USDT') {
  109. that.txData.name = res.name
  110. that.txData.lastPrice = +res.last
  111. that.txData.high = that.sgToFixed(+res.max_price)
  112. that.txData.volume = that.sgToFixed(+res.hou_price)
  113. that.txData.low = that.sgToFixed(+res.min_price)
  114. console.log(that.txData.lastPrice,that.txData.volume,'lastPricelastPricelastPricelastPricelastPricelastPrice')
  115. that.txData.upRate = that.sgToFixed(((that.txData.lastPrice - that.txData.volume) / that.txData
  116. .volume * 100).toFixed(2))
  117. txData.upFlag = +that.txData.upRate > 0 ? 1 : 2;
  118. if (that.timer) {
  119. clearTimeout(that.timer)
  120. that.timer = null
  121. }
  122. that.timer = setTimeout(() => {
  123. that.gcb()
  124. },5000)
  125. }else {
  126. if (that.timer) {
  127. clearTimeout(that.timer)
  128. that.timer = null
  129. }
  130. }
  131. })
  132. },
  133. // 返回上一页
  134. back(url) {
  135. console.log('cf', url);
  136. uni.navigateTo({
  137. url
  138. })
  139. uni.postMessage({
  140. data: {
  141. action: 'message'
  142. }
  143. });
  144. },
  145. scoketInit() {
  146. const that = this;
  147. // 初始化websocket
  148. that.webSocket = new WebSocket("wss://wsaws.okx.com:8443/ws/v5/public");
  149. that.webSocket.onopen = function(event) {
  150. console.log('打开链接成功');
  151. const requestKData = JSON.stringify({
  152. "op": "subscribe",
  153. "args": [{
  154. "channel": "candle" + that.current,
  155. "instId": that.typeId
  156. }]
  157. })
  158. const requestNewData = JSON.stringify({
  159. "op": "subscribe",
  160. "args": [{
  161. "channel": "tickers",
  162. "instId": that.typeId
  163. }]
  164. })
  165. // 获取k线数据
  166. that.webSocket.send(requestKData)
  167. // 获取当前行情数据
  168. that.webSocket.send(requestNewData)
  169. }
  170. // 监听socket回复事件
  171. that.webSocket.addEventListener('message', function(event) {
  172. const item = JSON.parse(event.data);
  173. try {
  174. // 判断是否为
  175. if (item.arg.channel == ("candle" + that.current) && item.data) {
  176. const daytime = new Date(+item.data[0][0]);
  177. item.data[0][0] = that.initDay(daytime, "YYYY-mm-dd HH:MM:SS")
  178. if (item.data[0][0] != that.dataKLine.dates[that.dataKLine.dates.length - 1]) {
  179. // console.log(item.data[0][0], 'jiange', that.dataKLine.dates[that.dataKLine
  180. // .dates.length - 1]);
  181. const itemi = item.data[0]
  182. that.dataKLine.dates.push(itemi[0])
  183. that.dataKLine.data.push([+itemi[1], +itemi[2], +itemi[3], +itemi[4], +
  184. itemi[5]
  185. ])
  186. that.dataKLine.volumes.push([that.dataKLine.volumes.length, +itemi[5], +
  187. itemi[1] > +itemi[2] ? 1 : -1
  188. ])
  189. that.setKline()
  190. }
  191. // that.txData.lastPrice = data.data[0][]
  192. }
  193. //
  194. if (item.arg.channel == ("tickers") && item.data[0]) {
  195. // console.log('tickers');
  196. const data = item.data[0]
  197. that.txData.name = data.instId
  198. that.txData.lastPrice = +data.last
  199. that.txData.high = +data.high24h
  200. that.txData.volume = +data.open24h
  201. that.txData.low = +data.low24h
  202. that.txData.upRate = ((that.txData.lastPrice - that.txData.volume) / that.txData
  203. .volume * 100).toFixed(2)
  204. txData.upFlag = +that.txData.upRate > 0 ? 1 : 2;
  205. }
  206. } catch (e) {
  207. console.log("item: " + JSON.stringify(item));
  208. }
  209. });
  210. that.webSocket.onclose = function(event) {
  211. that.$nextTick(() => {
  212. that.scoketInit()
  213. that.dataKLine = {
  214. data: [],
  215. dates: [],
  216. volumes: [],
  217. };
  218. that.getKline((new Date()).getTime())
  219. })
  220. };
  221. },
  222. // 获取24小时交易数据统计
  223. getTxData() {
  224. this.txData = txData;
  225. },
  226. // 初始化数据结构
  227. dataInit(data, type) {
  228. let items;
  229. if (type == 1) {
  230. items = data.map(function(item) {
  231. return item[0];
  232. });
  233. }
  234. if (type == 2) {
  235. items = data.map(function(item) {
  236. // console.log(
  237. // `[${+item[1]},${+item[2]},${+item[3]},${+item[4]},${+item[5]}, ${+item[1] > +item[2] ? 1 : -1}]`,
  238. // '数据结果');
  239. return [+item[1], +item[4], +item[3], +item[2], +item[5]];
  240. });
  241. }
  242. if (type == 3) {
  243. items = data.map(function(item, index) {
  244. // console.log(`[${index},${+item[1]},${+item[2]},${+item[3]},${+item[4]},${+item[5]}, ${+item[1] > +item[2] ? 1 : -1}]`,'数据结果');
  245. return [index, +item[5], +item[1] > +item[4] ? 1 : -1];
  246. });
  247. }
  248. return items
  249. },
  250. // 初始化时间
  251. initDay(time, fmt) {
  252. let ret;
  253. const opt = {
  254. "Y+": time.getFullYear().toString(), //年
  255. "m+": (time.getMonth() + 1).toString(), //月
  256. "d+": time.getDate().toString(), //日
  257. "H+": time.getHours().toString(), //小时
  258. "M+": time.getMinutes().toString(), //分
  259. "S+": time.getSeconds().toString() //秒
  260. };
  261. for (let k in opt) {
  262. ret = new RegExp("(" + k + ")").exec(fmt)
  263. if (ret) {
  264. fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length,
  265. "0")));
  266. }
  267. }
  268. return fmt;
  269. },
  270. // 获取k线数据,生成k线
  271. getKline(time) {
  272. console.log('qq');
  273. const that = this;
  274. // const tiemt = (new Date()).getTime() - (1000*60*60*24*6);
  275. // console.log(tiemt,'k闲时间')
  276. if (that.typeId != 'GCB-USDT') {
  277. axiosGet('/index/history', {
  278. // axiosGet('/api/v5/market/history-candles', {
  279. after: time,
  280. instId: that.typeId,
  281. bar: that.current,
  282. }).then((res) => {
  283. // 保存初始返回res对象
  284. that.oldDataKLine = res[res.length - 1][0];
  285. // console.log(that.initDay(new Date(+that.oldDataKLine), "YYYY-mm-dd HH:MM:SS"))
  286. // console.log(that.initDay(new Date(+res[0][0]), "YYYY-mm-dd HH:MM:SS"))
  287. const ar = res.map((e) => {
  288. let dateTime = new Date(+e[0]);
  289. e[0] = that.initDay(dateTime, "YYYY-mm-dd HH:MM:SS");
  290. return e
  291. }).reverse()
  292. that.dataKLine.dates.unshift(...that.dataInit(ar, 1))
  293. that.dataKLine.data.unshift(...that.dataInit(ar, 2))
  294. that.dataKLine.volumes.unshift(...that.dataInit(ar, 3))
  295. // that.dataKLine = {
  296. // dates: that.dataInit(ar, 1),
  297. // data: that.dataInit(ar, 2),
  298. // volumes: that.dataInit(ar, 3),
  299. // }
  300. that.setKline()
  301. }).catch((e) => {
  302. console.log(e, '2222');
  303. })
  304. } else if (that.typeId == 'GCB-USDT') {
  305. axiosGet('/Levertade/gcbk', {
  306. // axiosGet('/api/v5/market/history-candles', {
  307. // after: time,
  308. instId: that.typeId,
  309. bar: that.current,
  310. }).then((res) => {
  311. console.log(res,'K线6++++++++++++++++')
  312. // 保存初始返回res对象
  313. that.oldDataKLine = res[res.length - 1][0];
  314. // console.log(that.initDay(new Date(+that.oldDataKLine), "YYYY-mm-dd HH:MM:SS"))
  315. // console.log(that.initDay(new Date(+res[0][0]), "YYYY-mm-dd HH:MM:SS"))
  316. const ar = res.map((e) => {
  317. let dateTime = new Date(+e[0]);
  318. e[0] = that.initDay(dateTime, "YYYY-mm-dd HH:MM:SS");
  319. return e
  320. }).reverse()
  321. that.dataKLine.dates.unshift(...that.dataInit(ar, 1))
  322. that.dataKLine.data.unshift(...that.dataInit(ar, 2))
  323. that.dataKLine.volumes.unshift(...that.dataInit(ar, 3))
  324. // that.dataKLine = {
  325. // dates: that.dataInit(ar, 1),
  326. // data: that.dataInit(ar, 2),
  327. // volumes: that.dataInit(ar, 3),
  328. // }
  329. that.setKline()
  330. }).catch((e) => {
  331. console.log(e, '2222');
  332. })
  333. }
  334. },
  335. // 设置线条数据
  336. setKline() {
  337. const that = this;
  338. var dataMA5 = that.calculateMA(5, that.dataKLine.data);
  339. var dataMA10 = that.calculateMA(10, that.dataKLine.data);
  340. var dataMA30 = that.calculateMA(30, that.dataKLine.data);
  341. var volumeMA5 = that.calculateMA(5, that.dataKLine.volumes);
  342. var volumeMA10 = that.calculateMA(10, that.dataKLine.volumes);
  343. myChart.setOption({
  344. xAxis: [{
  345. data: that.dataKLine.dates
  346. },
  347. {
  348. data: that.dataKLine.dates
  349. },
  350. ],
  351. series: [{
  352. name: '日K',
  353. data: that.dataKLine.data
  354. },
  355. {
  356. name: 'MA5',
  357. data: dataMA5
  358. },
  359. {
  360. name: 'MA10',
  361. data: dataMA10
  362. },
  363. {
  364. name: 'MA30',
  365. data: dataMA30
  366. },
  367. {
  368. name: 'Volume',
  369. data: that.dataKLine.volumes
  370. },
  371. {
  372. name: 'VolumeMA5',
  373. data: volumeMA5
  374. },
  375. {
  376. name: 'VolumeMA10',
  377. data: volumeMA10
  378. },
  379. ]
  380. })
  381. },
  382. // 列表条数不足补全
  383. addItem(list, type) {
  384. // type: 1开头加,2末尾加
  385. list = list || [];
  386. let len = 20 - list.length;
  387. if (len > 0) {
  388. for (let i = 0; i < len; i++) {
  389. if (type == 1) {
  390. list.unshift({})
  391. } else {
  392. list.push({})
  393. }
  394. }
  395. }
  396. return list;
  397. },
  398. // 获取深度数据
  399. // getDepth() {
  400. // this.buyList = this.addItem(depthList().buyList || []);
  401. // this.sellList = this.addItem(depthList().sellList || []);
  402. // },
  403. // 获取成交记录
  404. getDealHis() {
  405. const that = this;
  406. if (this.typeId == 'GCB-USDT') {
  407. axiosGet('/Levertade/gcbm', {
  408. }).then((res) => {
  409. // 处理返回数据
  410. this.dealHis = res.map((e) => {
  411. const dateTime = new Date(+e.addtime*1000)
  412. console.log(e.addtime)
  413. return {
  414. "date": that.initDay(dateTime, "mm-dd HH:MM:SS"),
  415. // "data": e.addtime,
  416. // 1买入 2卖出
  417. "takerFlag": e.type,
  418. "price": e.price,
  419. "amount": e.num
  420. }
  421. })
  422. }).catch((e) => {
  423. console.log("e: " + JSON.stringify(e));
  424. console.log(e);
  425. })
  426. }else {
  427. axiosGet('/index/deal', {
  428. // axiosGet('/api/v5/market/trades', {
  429. instId: that.typeId,
  430. limit: 150
  431. }).then((res) => {
  432. // 处理返回数据
  433. this.dealHis = res.map((e) => {
  434. const dateTime = new Date(+e.ts)
  435. return {
  436. "date": that.initDay(dateTime, "mm-dd HH:MM:SS"),
  437. // 1买入 2卖出
  438. "takerFlag": e.side == 'buy' ? "1" : '2',
  439. "price": e.px,
  440. "amount": e.sz
  441. }
  442. })
  443. }).catch((e) => {
  444. console.log("e: " + JSON.stringify(e));
  445. console.log(e);
  446. })
  447. }
  448. },
  449. // 获取项目简介信息
  450. getTokenInfo() {
  451. this.tokenInfo = tokenInfo;
  452. },
  453. // 切换tab
  454. switchTab(val) {
  455. const that = this;
  456. if (that.current == val) {
  457. return
  458. };
  459. that.current = val;
  460. that.webSocket.close()
  461. if (that.timer) {
  462. clearTimeout(that.timer)
  463. that.timer = null
  464. }
  465. },
  466. // 切换类目
  467. switchCategory(val) {
  468. if (this.category == val) return;
  469. this.category = val;
  470. if (this.category == 1) {
  471. this.getDepth()
  472. } else if (this.category == 2) {
  473. this.getDealHis()
  474. } else {
  475. this.getTokenInfo()
  476. }
  477. },
  478. // 截取数字字符串 保留precision小数
  479. formatterNum(value, precision) {
  480. // console.log(value)
  481. let reg = new RegExp('^\\d+(?:\\.\\d{0,' + precision + '})?')
  482. // return value.toString().match(reg)
  483. return value.toString()
  484. },
  485. // 计算MA
  486. calculateMA(dayCount, data) {
  487. var result = [];
  488. for (var i = 0, len = data.length; i < len; i++) {
  489. if (i < dayCount) {
  490. result.push('-');
  491. continue;
  492. }
  493. var sum = 0;
  494. for (var j = 0; j < dayCount; j++) {
  495. sum += data[i - j][1];
  496. }
  497. // console.log(sum, dayCount)
  498. result.push((sum / dayCount));
  499. // result.push((sum / dayCount).toFixed(2));
  500. }
  501. return result;
  502. },
  503. // 绘制(配置项)
  504. draw() {
  505. let that = this;
  506. var upColor = '#03ad91';
  507. var downColor = '#dd345b';
  508. var colorList = ['#c23531', '#2f4554', '#61a0a8', '#d48265', '#91c7ae', '#749f83', '#ca8622',
  509. '#bda29a', '#6e7074',
  510. '#546570', '#c4ccd3'
  511. ];
  512. var labelFont = 'bold 12px Sans-serif';
  513. var option = {
  514. backgroundColor: '#0d1723',
  515. title: {
  516. show: false
  517. },
  518. legend: {
  519. show: false
  520. },
  521. visualMap: {
  522. show: false,
  523. seriesIndex: 4,
  524. dimension: 2,
  525. pieces: [{
  526. value: 1,
  527. color: downColor
  528. }, {
  529. value: -1,
  530. color: upColor
  531. }]
  532. },
  533. grid: [{
  534. top: '5%',
  535. left: 20,
  536. right: 20,
  537. height: '70%'
  538. },
  539. {
  540. top: '80%',
  541. left: 20,
  542. right: 20,
  543. height: '16%'
  544. },
  545. ],
  546. axisPointer: { //坐标轴指示器配置项
  547. link: {
  548. xAxisIndex: 'all'
  549. },
  550. label: {
  551. backgroundColor: '#0d1723',
  552. color: '#fff',
  553. borderColor: 'rgb(99, 117, 139)',
  554. borderWidth: 1,
  555. borderRadius: 2,
  556. fontSize: 10
  557. }
  558. },
  559. xAxis: [{
  560. type: 'category', //坐标轴类型。(value:数值轴,适用于连续数据。,category:类目轴,适用于离散的类目数据,time: 时间轴,适用于连续的时序数据,log:对数轴。适用于对数数据)
  561. data: [], //类目数据,在类目轴(type: 'category')中有效。
  562. scale: true,
  563. boundaryGap: false, //坐标轴两边留白策略,类目轴和非类目轴的设置和表现不一样。
  564. axisLine: {
  565. show: false
  566. }, //坐标轴轴线相关设置
  567. axisTick: {
  568. show: false
  569. }, //坐标轴刻度相关设置。
  570. axisLabel: {
  571. show: false,
  572. }, //坐标轴刻度标签的相关设置。
  573. splitLine: {
  574. show: false,
  575. lineStyle: {
  576. color: 'rgba(255,255,255, 0.1)'
  577. }
  578. }, //坐标轴在 grid 区域中的分隔线。
  579. min: 'dataMin', //坐标轴刻度最小值。可以设置成特殊值 'dataMin',此时取数据在该轴上的最小值作为最小刻度。
  580. max: 'dataMax', //坐标轴刻度最大值。可以设置成特殊值 'dataMax',此时取数据在该轴上的最大值作为最大刻度。
  581. axisPointer: {
  582. label: {
  583. margin: 200
  584. }
  585. },
  586. }, {
  587. type: 'category',
  588. gridIndex: 1, //x 轴所在的 grid 的索引,默认位于第一个 grid。
  589. data: [], //类目数据,在类目轴(type: 'category')中有效。
  590. scale: true,
  591. boundaryGap: false, //坐标轴两边留白策略,类目轴和非类目轴的设置和表现不一样。
  592. axisLine: {
  593. show: false,
  594. lineStyle: {
  595. color: 'rgba(255,255,255,1)',
  596. width: 1
  597. }
  598. }, //坐标轴轴线相关设置
  599. axisTick: {
  600. show: false
  601. }, //坐标轴刻度相关设置。
  602. axisLabel: { //坐标轴刻度标签的相关设置。
  603. show: true,
  604. margin: 6,
  605. fontSize: 10,
  606. color: 'rgba(99, 117, 139, 1.0)',
  607. formatter: function(value) {
  608. // console.log(value,'mmdd');
  609. return echarts.format.formatTime('MM-dd', value);
  610. }
  611. },
  612. splitNumber: 20,
  613. splitLine: {
  614. show: false,
  615. lineStyle: {
  616. color: 'rgba(255,255,255, 0.1)'
  617. }
  618. }, //坐标轴在 grid 区域中的分隔线。
  619. min: 'dataMin', //坐标轴刻度最小值。可以设置成特殊值 'dataMin',此时取数据在该轴上的最小值作为最小刻度。
  620. max: 'dataMax', //坐标轴刻度最大值。可以设置成特殊值 'dataMax',此时取数据在该轴上的最大值作为最大刻度。
  621. // axisPointer: { show: true, type: 'none', label: { show: false }},
  622. }],
  623. yAxis: [{
  624. type: 'value', //坐标轴类型。(value:数值轴,适用于连续数据。,category:类目轴,适用于离散的类目数据,time: 时间轴,适用于连续的时序数据,log:对数轴。适用于对数数据)
  625. position: 'right', //y 轴的位置。'left','right'
  626. scale: true, //是否是脱离 0 值比例。设置成 true 后坐标刻度不会强制包含零刻度。在双数值轴的散点图中比较有用。(在设置 min 和 max 之后该配置项无效。)
  627. axisLine: {
  628. show: true
  629. }, //坐标轴轴线相关设置。
  630. axisTick: {
  631. show: true,
  632. inside: true
  633. }, //坐标轴刻度相关设置。
  634. axisLabel: { //坐标轴刻度标签的相关设置。
  635. show: true,
  636. color: 'rgba(99, 117, 139, 1.0)',
  637. inside: true,
  638. fontSize: 10,
  639. formatter: function(value) {
  640. return Number(value)
  641. // return Number(value).toFixed(2)
  642. }
  643. },
  644. splitLine: {
  645. show: false,
  646. lineStyle: {
  647. color: 'rgba(255,255,255, 0.1)'
  648. }
  649. }, //坐标轴在 grid 区域中的分隔线。
  650. }, {
  651. type: 'value',
  652. position: 'right',
  653. scale: true,
  654. gridIndex: 1,
  655. axisLine: {
  656. show: false
  657. },
  658. axisTick: {
  659. show: false
  660. },
  661. axisLabel: {
  662. show: false
  663. },
  664. splitLine: {
  665. show: false
  666. }
  667. }],
  668. animation: false, //是否开启动画。
  669. color: colorList,
  670. tooltip: {
  671. show: true, //是否显示提示框组件,包括提示框浮层和 axisPointer。
  672. trigger: 'axis', //触发类型。item,axis,none
  673. formatter(params) {
  674. console.log("params: " + JSON.stringify(params));
  675. let tooltip = '';
  676. let time = '',
  677. open = 0,
  678. high = 0,
  679. low = 0,
  680. close = 0,
  681. amount = 0;
  682. for (var i = 0; i < params.length; i++) {
  683. if (params[i].seriesName === '日K') {
  684. time = params[i].name;
  685. // open = params[i].data.length > 1 ? Number(that.formatterNum(params[i].data[
  686. // 1], 2)) : 0;
  687. // close = params[i].data.length > 1 ? Number(that.formatterNum(params[i].data[
  688. // 2], 2)) : 0;
  689. // low = params[i].data.length > 1 ? Number(that.formatterNum(params[i].data[
  690. // 3], 2)) : 0;
  691. // high = params[i].data.length > 1 ? Number(that.formatterNum(params[i].data[
  692. // 4], 2)) : 0;
  693. open = params[i].data.length > 1 ? that.sgToFixed(that.formatterNum(params[i].data[
  694. 1], 2)*1) : 0;
  695. close = params[i].data.length > 1 ?that.sgToFixed(that.formatterNum(params[i].data[
  696. 2], 2)*1) : 0;
  697. low = params[i].data.length > 1 ? that.sgToFixed(that.formatterNum(params[i].data[
  698. 3], 2)*1) : 0;
  699. high = params[i].data.length > 1 ? that.sgToFixed(that.formatterNum(params[i].data[
  700. 4], 2)*1) : 0;
  701. amount = params[i].data.length > 1 ? that.sgToFixed(that.formatterNum(params[i]
  702. .data[5], 2)) : 0;
  703. // console.log(time,open,close,low,high,amount)
  704. tooltip = '<div class="charts-tooltip">' +
  705. '<div class="charts-tooltip-row"><div class="ctr-label">' + '时间' +
  706. '</div><div class="ctr-value">' + time + '</div></div>' +
  707. '<div class="charts-tooltip-row"><div class="ctr-label">' + '开' +
  708. '</div><div class="ctr-value">' + open + '</div></div>' +
  709. '<div class="charts-tooltip-row"><div class="ctr-label">' + '高' +
  710. '</div><div class="ctr-value">' + high + '</div></div>' +
  711. '<div class="charts-tooltip-row"><div class="ctr-label">' + '低' +
  712. '</div><div class="ctr-value">' + low + '</div></div>' +
  713. '<div class="charts-tooltip-row"><div class="ctr-label">' + '收' +
  714. '</div><div class="ctr-value">' + close + '</div></div>' +
  715. '<div class="charts-tooltip-row"><div class="ctr-label">' + '数量' +
  716. '</div><div class="ctr-value">' + amount + '</div></div></div>';
  717. }
  718. if (params[i].seriesName === 'MA5') {
  719. that.MA5 = params[i].data !== 'NAN' ? Number(that.formatterNum(params[i]
  720. .data, 2)) : 0
  721. }
  722. if (params[i].seriesName === 'MA10') {
  723. that.MA10 = params[i].data !== 'NAN' ? Number(that.formatterNum(params[i]
  724. .data, 2)) : 0
  725. }
  726. if (params[i].seriesName === 'MA30') {
  727. that.MA30 = params[i].data !== 'NAN' ? Number(that.formatterNum(params[i]
  728. .data, 2)) : 0
  729. }
  730. if (params[i].seriesName === 'VolumeMA5') {
  731. that.volMA5 = params[i].data !== 'NAN' ? Number(that.formatterNum(params[i]
  732. .data, 2)) : 0
  733. }
  734. if (params[i].seriesName === 'VolumeMA10') {
  735. that.volMA10 = params[i].data !== 'NAN' ? Number(that.formatterNum(params[i]
  736. .data, 2)) : 0
  737. }
  738. }
  739. return tooltip;
  740. },
  741. triggerOn: 'click', //提示框触发的条件 'mousemove','click','mousemove|click','none'
  742. textStyle: {
  743. fontSize: 10
  744. }, //提示框浮层的文本样式
  745. backgroundColor: 'rgba(30,42,66,0.8);', //提示框浮层的背景颜色。
  746. borderColor: '#2f3a56', //提示框浮层的边框颜色。
  747. borderWidth: 2,
  748. position: function(pos, params, el, elRect, size) { //提示框浮层的位置,默认不设置时位置会跟随鼠标的位置。
  749. var obj = {
  750. top: 20
  751. };
  752. obj[['left', 'right'][+(pos[0] < size.viewSize[0] / 2)]] = 10;
  753. return obj;
  754. },
  755. axisPointer: { //坐标轴指示器配置项。
  756. label: {
  757. color: 'rgba(255,255,255,.87)',
  758. fontSize: 9,
  759. backgroundColor: '#020204',
  760. borderColor: "#9c9fa4",
  761. shadowBlur: 0,
  762. borderWidth: 0.5,
  763. padding: [4, 2, 3, 2],
  764. },
  765. animation: false,
  766. type: 'cross',
  767. lineStyle: {
  768. color: {
  769. type: 'linear',
  770. x: 0,
  771. y: 0,
  772. x2: 0,
  773. y2: 1,
  774. colorStops: [{
  775. offset: 0,
  776. color: 'rgba(30, 42, 66, 0.1)' // 0% 处的颜色
  777. }, {
  778. offset: 0.7,
  779. color: 'rgba(30, 42, 66,0.9)' // 100% 处的颜色
  780. }, {
  781. offset: 1,
  782. color: 'rgba(30, 42, 66,0.2)' // 100% 处的颜色
  783. }]
  784. },
  785. width: 10,
  786. shadowColor: 'rgba(30, 42, 66,0.7)',
  787. shadowBlur: 0,
  788. shadowOffsetY: 68,
  789. }
  790. }
  791. },
  792. dataZoom: [{ //用于区域缩放
  793. type: 'inside',
  794. xAxisIndex: [0, 1],
  795. realtime: false,
  796. start: 50,
  797. end: 100,
  798. }],
  799. series: [{
  800. type: 'candlestick',
  801. name: '日K',
  802. data: [],
  803. itemStyle: {
  804. color: upColor,
  805. color0: downColor,
  806. borderColor: upColor,
  807. borderColor0: downColor
  808. },
  809. markPoint: {
  810. symbol: 'rect',
  811. symbolSize: [-10, 0.5],
  812. symbolOffset: [-5, 0],
  813. itemStyle: {
  814. color: 'rgba(255,255,255,.87)'
  815. },
  816. label: {
  817. color: 'rgba(255,255,255,.87)',
  818. offset: [3, 0],
  819. fontSize: 10,
  820. position: "left",
  821. align: 'right',
  822. formatter: function(params) {
  823. // console.log(params.value,'传入的处理数值');
  824. // return Number(params.value).toFixed(2)
  825. return Number(params.value)
  826. }
  827. },
  828. data: [{
  829. name: 'max',
  830. type: 'max',
  831. valueDim: 'highest'
  832. },
  833. {
  834. name: 'min',
  835. type: 'min',
  836. valueDim: 'lowest'
  837. }
  838. ]
  839. },
  840. },
  841. {
  842. name: 'MA5',
  843. type: 'line',
  844. data: [],
  845. symbol: 'none', //去除圆点
  846. smooth: true,
  847. lineStyle: {
  848. normal: {
  849. opacity: 1,
  850. width: 1,
  851. color: "#eef4ba"
  852. }
  853. },
  854. z: 5
  855. },
  856. {
  857. name: 'MA10',
  858. type: 'line',
  859. data: [],
  860. symbol: 'none', //去除圆点
  861. smooth: true,
  862. lineStyle: {
  863. normal: {
  864. opacity: 1,
  865. width: 1,
  866. color: '#83c1c5'
  867. }
  868. },
  869. z: 4
  870. },
  871. {
  872. name: 'MA30',
  873. type: 'line',
  874. data: [],
  875. symbol: 'none', //去除圆点
  876. smooth: true,
  877. lineStyle: {
  878. normal: {
  879. opacity: 1,
  880. width: 1,
  881. color: '#b39cd8'
  882. }
  883. },
  884. z: 3
  885. },
  886. {
  887. name: 'Volume',
  888. type: 'bar',
  889. xAxisIndex: 1,
  890. yAxisIndex: 1,
  891. data: []
  892. },
  893. {
  894. name: 'VolumeMA5',
  895. type: 'line',
  896. xAxisIndex: 1,
  897. yAxisIndex: 1,
  898. data: [],
  899. symbol: 'none', //去除圆点
  900. smooth: true,
  901. lineStyle: {
  902. normal: {
  903. opacity: 1,
  904. width: 1,
  905. color: "#eef4ba"
  906. }
  907. },
  908. z: 5
  909. },
  910. {
  911. name: 'VolumeMA10',
  912. type: 'line',
  913. xAxisIndex: 1,
  914. yAxisIndex: 1,
  915. data: [],
  916. symbol: 'none', //去除圆点
  917. smooth: true,
  918. lineStyle: {
  919. normal: {
  920. opacity: 1,
  921. width: 1,
  922. color: '#83c1c5'
  923. }
  924. },
  925. z: 4
  926. },
  927. ]
  928. };
  929. myChart.setOption(option);
  930. // 加载上一页数据
  931. myChart.on('datazoom', function(params) {
  932. let num = params.batch[0]['start'];
  933. if (num == 0) {
  934. console.log('到最左边了')
  935. // console.log(that.initDay(new Date(+that.oldDataKLine), "YYYY-mm-dd HH:MM:SS"));
  936. // that.getKline(that.oldDataKLine)
  937. }
  938. })
  939. window.addEventListener('resize', () => {
  940. myChart.resize()
  941. })
  942. }
  943. }
  944. })