k-line.nvue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <template>
  2. <view class="">
  3. <view class="aa">
  4. </view>
  5. <fy-kline ref="fyKLine" :lang="lang" :max-count="maxCount" :is-line="isLine" :main-type="mainType"
  6. :child-type="childType" :is-right-padding="isRightPadding" :show-dash-line="showDashLine"
  7. :is-can-roll="isCanRoll" :theme="theme2" :size="size" :split-count="splitCount"
  8. @onLoadHistory="onLoadHistory" @onLoad="kLineLoad" :style="'width:750rpx;height:' + height"></fy-kline>
  9. </view>
  10. </template>
  11. <script>
  12. // import {
  13. // startSocket,
  14. // closeSocket
  15. // } from "@/utils/websocket.js"
  16. let mapDataCollection = require('./data.json')
  17. // 由于这个data.json里面的数据,是按照时间倒序排列的,所以这里对数组进行了翻转
  18. mapDataCollection.reverse()
  19. // Socket.startSocket()
  20. var _self
  21. // import {
  22. // mapGetters
  23. // } from 'vuex'
  24. export default {
  25. name: "k-line",
  26. props: {
  27. height: {
  28. type: String,
  29. default: '600rpx'
  30. }
  31. },
  32. data() {
  33. return {
  34. lang: 'zh', // 默认中文,非‘zh’字符串一律为英文
  35. maxCount: 1000, // 默认最多显示1000条数据
  36. isLine: false, // 默认为K线
  37. mainType: 0, // 主指标默认MA
  38. childType: 0, // 副指标默认MACD
  39. klineHeight: 0,
  40. isRightPadding: true, // 右边刻度在K线图之外
  41. showDashLine: true, // 是否显示虚线
  42. isCanRoll: false, // 上下滑动K线图区域时,页面是否可上下滚动,一般设置为false,默认也为false
  43. splitCount: {
  44. main: 3,
  45. vol: 1,
  46. child: 1
  47. },
  48. theme1: {
  49. chart_background: '#181B2C',
  50. chart_up: '#d72c4c', // 上涨K柱颜色
  51. chart_down: '#02ac8f', // 下跌K柱颜色
  52. chart_line: '#c9933e', // 分时线颜色
  53. chart_line_background: '#1ac9933e', // 分时线背景色,前两项'1a'表示透明度
  54. chart_point_background: '#202326', // 长按弹出的光标末端点数和日期游标背景色
  55. chart_point_text: '#818596', // 长按弹出的光标末端点数和日期游标文本颜色
  56. chart_selected_yline: '#8040424d', // 十字光标选择器竖线颜色
  57. chart_selected_xline: '#ffffff', // 十字光标选择器横线颜色
  58. chart_grid_line: '#1affffff', // 网格线颜色
  59. chart_dotted_line: '#d72c4c', // 收盘虚线颜色
  60. chart_dotted_line_text: '#398fff', // 收盘虚线末端动态游标文本颜色
  61. chart_selector_background: '#cc202326', // 长按弹出的高低开收详情弹框(以下简称选择器)背景颜色
  62. chart_selector_lable: '#ffffff', // 选择器左侧标签文本颜色
  63. chart_selector_text: '#818596', // 选择器右侧数值文本颜色
  64. chart_selector_text_up: '#d72c4c', // 选择器文本中“涨跌额”、“涨跌幅”的值上涨时的颜色
  65. chart_selector_text_down: '#02ac8f', // 选择器文本中“涨跌额”、“涨跌幅”的值下跌时的颜色
  66. chart_selector_border: '#808080', // 选择器圆角边框线颜色
  67. chart_ma: { // 主图,vol图的ma线和文本的颜色
  68. ma5: '#da8ae5',
  69. ma10: '#39b0e8',
  70. ma30: '#ffc76d',
  71. ma60: '#39b0e8', // 分时模式时会显示ma60指标,K线图模式显示 ma5、ma10、ma30三个指标
  72. },
  73. chart_boll: { // 主图BOLL指标下的三轨线文本和线的颜色
  74. boll: '#39b0e8', // boll,中轨线
  75. ub: '#da8ae5', // ub,上轨线
  76. lb: '#ffc76d' // lb,下轨线
  77. },
  78. chart_macd: { // 副图MACD指标下的MACD,DIF,DEA三个指标线和文本的颜色
  79. macd: '#39b0e8',
  80. dif: '#da8ae5',
  81. dea: '#ffc76d'
  82. },
  83. chart_kdj: { // 副图KDJ指标下的K,D,J三个指标线和文本的颜色
  84. k: '#39b0e8',
  85. d: '#da8ae5',
  86. j: '#ffc76d'
  87. },
  88. chart_rsi: '#da8ae5', // 副图RSI指标线和文本的颜色
  89. chart_wr: '#da8ae5', // 副图WR指标线和文本的颜色
  90. chart_m_text: '#ffffff', // 最大/最小 值文本颜色
  91. chart_scale_text: '#818596', // 刻度值文本颜色
  92. chart_vol_text: '#818596', // vol图的 "VOL:XXXXXX" 文本的颜色
  93. chart_child_text: '#818596', // 副图的指标名字文本颜色,例如副图MACD时,"MACD(12,26,9)" 文本的颜色
  94. },
  95. theme2: {
  96. chart_background: '#ffffff',
  97. chart_up: '#02ac8f', // 上涨K柱颜色
  98. chart_down: '#d72c4c', // 下跌K柱颜色
  99. chart_line: '#c9933e', // 分时线颜色
  100. chart_line_background: '#1ac9933e', // 分时线背景色,前两项'1a'表示透明度
  101. chart_point_background: '#202326', // 长按弹出的光标末端点数和日期游标背景色
  102. chart_point_text: '#818596', // 长按弹出的光标末端点数和日期游标文本颜色
  103. chart_selected_yline: '#80a01aff', // 十字光标选择器竖线颜色
  104. chart_selected_xline: '#a01aff', // 十字光标选择器横线颜色
  105. chart_grid_line: '#1a202326', // 网格线颜色
  106. chart_dotted_line: '#02ac8f', // 收盘虚线颜色
  107. chart_dotted_line_text: '#ffff1e', // 收盘虚线末端动态游标文本颜色
  108. chart_selector_background: '#ccd8eeff', // 选择器背景颜色
  109. chart_selector_lable: '#545862', // 选择器左侧标签文本颜色
  110. chart_selector_text: '#717583', // 选择器右侧数值文本颜色
  111. chart_selector_text_up: '#02ac8f', // 选择器文本中“涨跌额”、“涨跌幅”的值上涨时的颜色
  112. chart_selector_text_down: '#d72c4c', // 选择器文本中“涨跌额”、“涨跌幅”的值下跌时的颜色
  113. chart_selector_border: '#323232', // 选择器圆角边框线颜色
  114. chart_ma: { // 主图,vol图的ma线和文本的颜色
  115. ma5: '#ffc76d',
  116. ma10: '#02ac8f',
  117. ma30: '#da8ae5',
  118. ma60: '#02ac8f', // 分时模式时会显示ma60指标,K线图模式显示 ma5、ma10、ma30三个指标
  119. },
  120. chart_boll: { // 主图BOLL指标下的三轨线文本和线的颜色
  121. boll: '#02ac8f', // boll,中轨线
  122. ub: '#ffc76d', // ub,上轨线
  123. lb: '#da8ae5' // lb,下轨线
  124. },
  125. chart_macd: { // 副图MACD指标下的MACD,DIF,DEA三个指标线和文本的颜色
  126. macd: '#ffc76d',
  127. dif: '#39b0e8',
  128. dea: '#da8ae5'
  129. },
  130. chart_kdj: { // 副图KDJ指标下的K,D,J三个指标线和文本的颜色
  131. k: '#da8ae5',
  132. d: '#39b0e8',
  133. j: '#ffc76d'
  134. },
  135. chart_rsi: '#da8ae5', // 副图RSI指标线和文本的颜色
  136. chart_wr: '#da8ae5', // 副图WR指标线和文本的颜色
  137. chart_m_text: '#2f4eff', // 最大/最小 值文本颜色
  138. chart_scale_text: '#179600', // 刻度值文本颜色
  139. chart_vol_text: '#3e1aaa', // vol图的 "VOL:XXXXXX" 文本的颜色
  140. chart_child_text: '#3e1aaa', // 副图的指标名字文本颜色,例如副图MACD时,"MACD(12,26,9)" 文本的颜色
  141. },
  142. size: {
  143. chart_scale_x: 10, // x轴(时间轴)刻度字体大小, 默认值为10,这里也推荐你设置成10
  144. chart_scale_y: 10, // y轴(数据轴)刻度字体大小, 默认值为10,这里也推荐你设置成10
  145. },
  146. logo: {
  147. src: 'static/huobi.png', // 注意!重点!src路径,必须是static目录
  148. left: 3, // 左边距,默认为0,在android里单位为dp
  149. bottom: 5, // 下边距,默认为0,在android里单位为dp
  150. width: 146, // 绘制区域宽度,默认为图片宽度,在android里单位为dp
  151. height: 22, // 绘制区域高度,默认为图片高度,在android里单位为dp
  152. trans: 0.3, // 透明度,默认为1不透明,0为全透明
  153. },
  154. dataIndex: 0,
  155. latestData: null,
  156. updateIndex: 0,
  157. historyNumber: 0, // 调用history的次数
  158. };
  159. },
  160. // computed: {
  161. // ...mapGetters([
  162. // 'currencyVal'
  163. // ]),
  164. // },
  165. onLoad() {
  166. // startSocket()
  167. },
  168. onReady() {
  169. },
  170. created() {
  171. _self = this
  172. this.dataIndex = parseInt(mapDataCollection.length / 3)
  173. },
  174. mounted() {
  175. this.klineHeight = uni.getSystemInfoSync().windowHeight - uni.upx2px(200) + 'px';
  176. setTimeout(function() {
  177. if (_self.updateIndex < mapDataCollection.length) {
  178. _self.latestData = mapDataCollection[_self.updateIndex++]
  179. }
  180. }, 100)
  181. },
  182. methods: {
  183. History(e) {
  184. console.log('最左边第一条数据的日期时间戳', e.detail.id)
  185. },
  186. // K线图组件加载完毕
  187. kLineLoad() {
  188. console.log("K线图组件已初始化完成,可以加载数据了1")
  189. this.initData();
  190. },
  191. initData() {
  192. console.log(this.$refs.fyKLine)
  193. if (this.$refs.fyKLine) {
  194. this.$refs.fyKLine.loading();
  195. }
  196. // 请求数据,加载数据。这里测试,获取的是本地json数据
  197. if (this.$refs.fyKLine) {
  198. // mapDataCollection 数据太大了,这里为了便于测试,只加载了100条数据
  199. this.$refs.fyKLine.initData(mapDataCollection.slice(this.dataIndex, this.dataIndex * 2));
  200. // 隐藏加载动画
  201. this.$refs.fyKLine.hideLoading();
  202. this.updateIndex = this.dataIndex * 2
  203. }
  204. },
  205. onLoadHistory(e) {
  206. // 这里只是示例,加载了本地历史数据,实际上你需要根据 e.detail.id 从服务器获取正确的数据
  207. if (this.$refs.fyKLine) {
  208. this.historyNumber++
  209. if (this.historyNumber == 1) {
  210. // 第一次加载历史数据
  211. let history = mapDataCollection.slice(parseInt(this.dataIndex / 2), this.dataIndex)
  212. this.$refs.fyKLine.loadHistory(history);
  213. uni.showModal({
  214. title: "第一次触发onLoadHistory回调",
  215. content: "成功加载历史数据" + history.length + "条",
  216. showCancel: false
  217. })
  218. } else if (this.historyNumber == 2) {
  219. // 第二次加载历史数据
  220. let history = mapDataCollection.slice(0, parseInt(this.dataIndex / 2))
  221. this.$refs.fyKLine.loadHistory(history);
  222. uni.showModal({
  223. title: "第二次触发onLoadHistory回调",
  224. content: "成功加载历史数据" + history.length + "条",
  225. showCancel: false
  226. })
  227. } else if (this.historyNumber == 3) {
  228. uni.showModal({
  229. title: "第三次触发onLoadHistory回调",
  230. content: "但是此时已经没有历史数据可以加载了,你先往回滑动,然后再次滑到最后,也不会再触发onLoadHistory回调了",
  231. showCancel: false
  232. })
  233. } else {
  234. uni.showModal({
  235. title: "???",
  236. content: "你还是触发了onLoadHistory回调\n我很失望=͟͟͞͞(꒪⌓꒪*)",
  237. showCancel: false
  238. })
  239. }
  240. }
  241. },
  242. changeMainType(type) {
  243. this.mainType = type
  244. if (this.$refs.fyKLine) {
  245. this.$refs.fyKLine.changeMainType(type)
  246. }
  247. },
  248. changeChildType(type) {
  249. this.childType = type
  250. if (this.$refs.fyKLine) {
  251. this.$refs.fyKLine.changeChildType(type)
  252. }
  253. },
  254. changeTheme(e) {
  255. if (this.$refs.fyKLine) {
  256. if (e) {
  257. this.$refs.fyKLine.changeTheme(this.theme2)
  258. } else {
  259. this.$refs.fyKLine.changeTheme(this.theme1)
  260. }
  261. }
  262. }
  263. },
  264. watch: {
  265. // 加载新数据,一般是 socket更新一个变量,监听器深度监听该变量,然后把数据加载进K线图
  266. // 此demo没有实现 socket连接
  267. latestData: {
  268. handler(newData, oldData) {
  269. this.$refs.fyKLine.updateData(newData);
  270. },
  271. deep: true
  272. },
  273. lang() {
  274. if (this.$refs.fyKLine) {
  275. this.$refs.fyKLine.changeLang(this.lang)
  276. }
  277. },
  278. isLine() {
  279. if (this.$refs.fyKLine) {
  280. this.$refs.fyKLine.changeMainLine(this.isLine)
  281. }
  282. },
  283. isRightPadding() {
  284. if (this.$refs.fyKLine) {
  285. this.$refs.fyKLine.changeIsRightPadding(this.isRightPadding)
  286. }
  287. },
  288. showDashLine() {
  289. if (this.$refs.fyKLine) {
  290. this.$refs.fyKLine.changeShowDashLine(this.showDashLine)
  291. }
  292. },
  293. isCanRoll() {
  294. if (this.$refs.fyKLine) {
  295. this.$refs.fyKLine.changeIsCanRoll(this.isCanRoll)
  296. }
  297. },
  298. },
  299. }
  300. </script>
  301. <style lang="scss" scoped>
  302. .aa {
  303. width: 750rpx;
  304. height: 100rpx;
  305. background-color: red;
  306. }
  307. </style>