highchart.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <div
  3. id="hightchart"
  4. :prop="config"
  5. :change:prop="highchart.changeConifg"
  6. class="chart-box"
  7. ></div>
  8. </template>
  9. <script>
  10. import Option from "@/api/option";
  11. import { mapState } from "vuex";
  12. export default {
  13. props: ["symbolName"],
  14. computed: {
  15. linkSymbol() {
  16. return this.symbolName.split("/").join("").toLowerCase();
  17. },
  18. ...mapState({
  19. ws: "ws",
  20. servetheme: "theme",
  21. }),
  22. msg() {
  23. return `newPrice_${this.linkSymbol}`;
  24. },
  25. },
  26. watch: {
  27. msg(n, o) {
  28. if (o) {
  29. this.unSub(o);
  30. this.getList();
  31. }
  32. },
  33. },
  34. data() {
  35. return {
  36. list: [],
  37. // 任务对象 自动派发(set即可)
  38. config: {},
  39. };
  40. },
  41. methods: {
  42. getList() {
  43. let data = {
  44. symbol: this.symbolName,
  45. name: this.symbolName,
  46. };
  47. Option.getNewPriceBook(data).then((res) => {
  48. let arr = res.data.map((item) => {
  49. return [item.ts, item.price];
  50. });
  51. // 变更任务
  52. this.config = {
  53. type: "resetList",
  54. list: arr,
  55. symbol: data.symbol,
  56. theme: this.servetheme,
  57. };
  58. this.sub();
  59. });
  60. },
  61. // 订阅消息
  62. sub() {
  63. this.ws.send({
  64. cmd: "sub",
  65. msg: this.msg,
  66. });
  67. },
  68. // 取消订阅
  69. unSub(name) {
  70. this.ws.send({
  71. cmd: "unsub",
  72. msg: name,
  73. });
  74. },
  75. // socket接受数据
  76. socketGetData() {
  77. this.ws.on("message", (res) => {
  78. let { data, sub } = res;
  79. if (sub == this.msg) {
  80. this.$emit("newData", data);
  81. this.config = {
  82. type: "addPoint",
  83. data,
  84. };
  85. }
  86. });
  87. },
  88. },
  89. created() {
  90. this.config = {
  91. type: "init",
  92. theme: this.servetheme,
  93. };
  94. },
  95. mounted() {
  96. this.getList();
  97. this.socketGetData();
  98. },
  99. destroyed() {
  100. this.unSub(this.msg);
  101. },
  102. };
  103. </script>
  104. <script module="highchart" lang="renderjs">
  105. import highstock from "highcharts/highstock";
  106. export default {
  107. data(){
  108. return {
  109. chart: undefined,
  110. theme: "light",
  111. }
  112. },
  113. methods: {
  114. intiChart($box) {
  115. highstock.setOptions({
  116. global: {
  117. timezoneOffset: -8 * 60,
  118. },
  119. });
  120. let colorMap={
  121. light:{
  122. line:'#dcdee0',
  123. color:'#333'
  124. },
  125. dark:{
  126. line:'#49495F',
  127. color:'#dcdee0'
  128. }
  129. }
  130. this.chart = highstock.stockChart($box, {
  131. chart: {
  132. marginRight: 80,
  133. marginTop: 0,
  134. marginLeft: 0,
  135. padding: 0,
  136. events: {
  137. click: function () {},
  138. load: function () {},
  139. redraw: function () {},
  140. },
  141. panning: !0,
  142. backgroundColor: "transparent",
  143. borderColor: "#ffffff00",
  144. borderWidth: "1",
  145. plotBackgroundColor: "transparent",
  146. Point: {
  147. visible: !1,
  148. },
  149. },
  150. rangeSelector: {
  151. enabled: false,
  152. },
  153. navigator: {
  154. enabled: false,
  155. },
  156. scrollbar: {
  157. enabled: false,
  158. },
  159. exporting: {
  160. enabled: !0,
  161. },
  162. credits: {
  163. enabled: !1,
  164. },
  165. mapNavigation: {
  166. enabled: !1,
  167. enableButtons: !1,
  168. },
  169. navigation: {
  170. enabled: !1,
  171. buttonOptions: {},
  172. },
  173. title: {
  174. text: null,
  175. },
  176. yAxis: {
  177. gridLineWidth: 0.5,
  178. tickWidth: 0.5,
  179. tickColor: colorMap[this.theme].line,
  180. tickAmount: 7,
  181. // lineWidth: 1,
  182. lineColor: colorMap[this.theme].line,
  183. labels: {
  184. align: "center",
  185. x: 20,
  186. style: {
  187. color:colorMap[this.theme].color,
  188. },
  189. useHTML: !0,
  190. },
  191. opposite: !0,
  192. gridLineColor: colorMap[this.theme].line,
  193. },
  194. xAxis: {
  195. type: "datetime",
  196. dateTimeLabelFormats: {
  197. millisecond: "%H:%M:%S.%L",
  198. second: "%H:%M:%S",
  199. minute: "%H:%M",
  200. hour: "%H:%M",
  201. day: "%m-%d",
  202. week: "%m-%d",
  203. month: "%Y-%m",
  204. year: "%Y",
  205. },
  206. tickWidth: 1,
  207. tickColor: colorMap[this.theme].line,
  208. lineColor: colorMap[this.theme].line,
  209. labels: {
  210. style: {
  211. color: colorMap[this.theme].color,
  212. },
  213. },
  214. plotLines: [
  215. {
  216. color: "red",
  217. dashStyle: "solid",
  218. value: 0,
  219. width: 2,
  220. },
  221. ],
  222. plotBands: [
  223. {
  224. color: colorMap[this.theme].line,
  225. from: 0,
  226. to: 0,
  227. zIndex: 1,
  228. },
  229. ],
  230. // gridLineWidth: 1,
  231. // gridLineColor: "#1E2E3F",
  232. },
  233. tooltip: {
  234. dateTimeLabelFormats: {
  235. millisecond: "%H:%M:%S.%L",
  236. second: "%H:%M:%S",
  237. minute: "%H:%M",
  238. hour: "%H:%M",
  239. day: "%Y-%m-%d",
  240. week: "%m-%d",
  241. month: "%Y-%m",
  242. year: "%Y",
  243. },
  244. },
  245. legend: {
  246. enabled: !1,
  247. },
  248. plotOptions: {
  249. areaspline: {
  250. fillColor: {
  251. linearGradient: {
  252. x1: 0,
  253. y1: 0,
  254. x2: 0,
  255. y2: 1,
  256. },
  257. stops: [
  258. [0, "rgba(192,96,194,.58)"],
  259. [1, "rgba(90,99,251,.13)"],
  260. ],
  261. },
  262. marker: {
  263. radius: 2,
  264. },
  265. lineWidth: 2,
  266. states: {
  267. hover: {
  268. lineWidth: 2,
  269. },
  270. },
  271. threshold: null,
  272. },
  273. series: {
  274. point: {
  275. events: {
  276. mouseOver: function () {},
  277. },
  278. },
  279. events: {
  280. mouseOut: function () {},
  281. },
  282. },
  283. },
  284. series: [
  285. {
  286. marker: {
  287. radius: 0,
  288. lineWidth: 0,
  289. lineColor: "#fff",
  290. fillColor: "#fff",
  291. states: {},
  292. },
  293. type: "areaspline",
  294. name:'',
  295. data: [],
  296. lineWidth: 3,
  297. color: {
  298. linearGradient:{
  299. x1:'0%',
  300. y1:'0%',
  301. x2:'100%',
  302. y2:'100%'
  303. },
  304. stops:[
  305. [0, '#C060C2'],
  306. [1, '#5A63FB'],
  307. ]
  308. },
  309. },
  310. ],
  311. });
  312. },
  313. // 收到通知
  314. changeConifg(n,o){
  315. this.task(n)
  316. },
  317. // 任务派发
  318. task(task){
  319. switch (task.type) {
  320. // 重置数据
  321. case 'resetList':
  322. this.chart.series[0].setData(task.list.map(item=>{
  323. return [item[0],item[1]*1]
  324. }));
  325. this.chart.series[0].name = task.symbol;
  326. break;
  327. // 追加点
  328. case 'addPoint':
  329. this.chart.series[0].addPoint([task.data.ts, task.data.price], true, true);
  330. break;
  331. case 'init':
  332. this.theme = task.theme
  333. break;
  334. // 默认事件
  335. default:
  336. break;
  337. }
  338. }
  339. },
  340. mounted() {
  341. this.task(this.config)
  342. this.intiChart(this.$el);
  343. },
  344. }
  345. </script>
  346. <style lang="scss" scoped>
  347. .chart-box {
  348. height: 100%;
  349. background-color: getColor("panel-3");
  350. }
  351. </style>