common.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. import { setPx, validatenull, addParam, loadFont } from "./util";
  2. import config from "./config";
  3. import commonOption from "./option";
  4. import crypto from "@/datav/utils/crypto";
  5. import { getFunction, funEval, uuid } from "@/datav/utils/utils";
  6. import COMMON from "@/datav/config";
  7. export default (() => {
  8. return {
  9. props: {
  10. stylesFormatter: Function,
  11. dataFormatter: Function,
  12. titleFormatter: Function,
  13. labelFormatter: Function,
  14. clickFormatter: Function,
  15. sqlFormatter: Function,
  16. recordFormatter: Function,
  17. formatter: Function,
  18. echartFormatter: Function,
  19. dataQueryType: String,
  20. dataQuery: String,
  21. dataHeader: String,
  22. fontFamily: String,
  23. width: {
  24. type: [Number, String],
  25. default: 600,
  26. },
  27. height: {
  28. type: [Number, String],
  29. default: 600,
  30. },
  31. theme: {
  32. type: String,
  33. },
  34. child: {
  35. type: Object,
  36. default: () => {
  37. return {};
  38. },
  39. },
  40. record: {
  41. type: String,
  42. },
  43. sql: {
  44. type: String,
  45. },
  46. time: {
  47. type: Number,
  48. default: 0,
  49. },
  50. url: {
  51. type: String,
  52. },
  53. wsUrl: {
  54. type: String,
  55. },
  56. disabled: {
  57. type: Boolean,
  58. default: true,
  59. },
  60. dataType: {
  61. type: Number,
  62. default: 0,
  63. },
  64. dataMethod: {
  65. type: String,
  66. default: "get",
  67. },
  68. id: {
  69. type: String,
  70. default: "main_" + uuid(),
  71. },
  72. data: {
  73. type: [Object, String, Array],
  74. },
  75. component: {
  76. type: Object,
  77. default: () => {
  78. return {};
  79. },
  80. },
  81. option: {
  82. type: Object,
  83. default: () => {
  84. return {};
  85. },
  86. },
  87. },
  88. mixins: [commonOption],
  89. data() {
  90. return {
  91. headerHeight: "",
  92. checkChart: "",
  93. myChart: "",
  94. dataChart: [],
  95. dataAxios: {},
  96. dataParams: {},
  97. wsClient: {},
  98. styles: {},
  99. appendCheck: {},
  100. appendObj: {},
  101. appendList: [],
  102. className: "",
  103. };
  104. },
  105. watch: {
  106. data() {
  107. this.updateData();
  108. },
  109. dataAppend(val) {
  110. this.appendObj = {};
  111. this.appendList = [];
  112. if (!val) {
  113. this.appendCheck = clearInterval(this.appendCheck);
  114. } else {
  115. this.dataChart = [];
  116. }
  117. this.updateData();
  118. },
  119. echartFormatter() {
  120. this.updateChart();
  121. },
  122. width() {
  123. this.$nextTick(() => {
  124. this.updateChart();
  125. });
  126. },
  127. height() {
  128. this.$nextTick(() => {
  129. this.updateChart();
  130. });
  131. this.updateChart();
  132. },
  133. theme() {
  134. this.myChart.dispose();
  135. this.init();
  136. },
  137. option: {
  138. handler() {
  139. this.updateChart();
  140. },
  141. deep: true,
  142. },
  143. },
  144. computed: {
  145. count() {
  146. return this.option.count;
  147. },
  148. dataAppend() {
  149. return this.option.dataAppend;
  150. },
  151. dataChartLen() {
  152. return (this.dataChart || []).length;
  153. },
  154. switchTheme() {
  155. return this.validData(this.option.switchTheme, false);
  156. },
  157. name() {
  158. let className = this.$el.className.split(" ")[0];
  159. const result = className.replace(config.name, "");
  160. return result;
  161. },
  162. minWidth() {
  163. const val = this.option.minWidth;
  164. if (val > this.width) return val;
  165. },
  166. styleChartName() {
  167. const obj = {
  168. fontFamily: loadFont(this.component.fontFamily),
  169. width: setPx(this.minWidth || this.width),
  170. height: setPx(this.height),
  171. opacity: this.component.opacity || 1,
  172. transform: `scale(${
  173. this.component.scale || 1
  174. }) perspective(${
  175. this.component.perspective || 500
  176. }px) rotateX(${this.component.rotateX || 0}deg) rotateY(${
  177. this.component.rotateY || 0
  178. }deg) rotateZ(${this.component.rotateZ || 0}deg)`,
  179. };
  180. return obj;
  181. },
  182. styleSizeName() {
  183. return Object.assign(
  184. {
  185. width: setPx(this.width),
  186. height: setPx(this.height),
  187. },
  188. (() => {
  189. if (this.minWidth) {
  190. return {
  191. overflowX: "auto",
  192. overflowY: "hidden",
  193. };
  194. }
  195. return {};
  196. })(),
  197. this.styles
  198. );
  199. },
  200. },
  201. mounted() {
  202. this.init();
  203. },
  204. methods: {
  205. init() {
  206. this.className = `animated ${this.component.animated || ""}`;
  207. const main = this.$refs[this.id];
  208. if (main) {
  209. // 判断是否图表去初始化
  210. const isChart = config.echart.includes(this.name);
  211. if (isChart)
  212. this.myChart = window.echarts.init(main, this.theme);
  213. }
  214. this.updateChart();
  215. this.updateData();
  216. },
  217. getItemRefs() {
  218. let refList = this.$parent.$parent.$refs;
  219. let result = {};
  220. Object.keys(refList).forEach((ele) => {
  221. if (ele.indexOf(COMMON.NAME) !== -1) {
  222. result[ele.replace(COMMON.NAME, "")] = refList[ele][0];
  223. }
  224. });
  225. return result;
  226. },
  227. updateChart() {},
  228. updateClick(params) {
  229. let refList = this.getItemRefs();
  230. let indexList = this.child.index;
  231. let indexName = this.child.paramName;
  232. let indexValue = this.child.paramValue || "value";
  233. if (validatenull(indexName) && validatenull(indexList)) return;
  234. let p = {};
  235. p[indexName] = params[indexValue];
  236. Object.keys(refList).forEach((ele) => {
  237. if (indexList.includes(ele)) refList[ele].updateData(p);
  238. });
  239. },
  240. updateAppend(result) {
  241. if (this.validatenull(this.appendObj)) {
  242. this.appendList = result;
  243. this.appendObj = result[0];
  244. } else {
  245. let appendList = [];
  246. for (let i = 0; i < result.length; i++) {
  247. const ele = result[i];
  248. if (ele.id === this.appendObj.id) break;
  249. else appendList.push(ele);
  250. }
  251. this.appendObj = result[0];
  252. appendList.reverse().forEach((ele) => {
  253. this.appendList.unshift(ele);
  254. });
  255. }
  256. if (this.validatenull(this.appendCheck)) {
  257. this.appendCheck = setInterval(() => {
  258. let length = this.appendList.length - 1;
  259. if (length >= 0) {
  260. let obj = this.appendList.splice(length, 1)[0];
  261. this.dataChart.unshift(obj);
  262. let len = this.dataChart.length;
  263. if (len > this.count) {
  264. this.appendList.splice(len - 1, 1);
  265. }
  266. }
  267. }, 2000);
  268. }
  269. },
  270. // 更新数据核心方法
  271. updateData(p = {}) {
  272. let record,
  273. key = false;
  274. let isRecord = this.dataType === 4;
  275. this.dataParams = Object.assign(this.dataParams, p);
  276. return new Promise((resolve, reject) => {
  277. this.resetData && this.resetData();
  278. if (key) return;
  279. key = true;
  280. let safe = this;
  281. const formatter = (data, params) => {
  282. // if (isRecord) {
  283. // const dataFormatter = getFunction(
  284. // safe.dataFormatter
  285. // );
  286. // if (typeof dataFormatter == "function") {
  287. // data = dataFormatter(data);
  288. // }
  289. // }
  290. if (typeof this.dataFormatter === "function") {
  291. data = this.dataFormatter(
  292. data,
  293. params,
  294. this.getItemRefs()
  295. );
  296. }
  297. return data;
  298. };
  299. const getData = () => {
  300. safe = record || this;
  301. key = false;
  302. let isApi = safe.dataType === 1;
  303. let isSql = safe.dataType === 2;
  304. let isWs = safe.dataType === 3;
  305. this.closeClient();
  306. const bindEvent = () => {
  307. this.updateChart();
  308. if (this.myChart) this.bindClick();
  309. if (typeof this.stylesFormatter === "function") {
  310. this.styles =
  311. this.stylesFormatter(
  312. this.dataChart,
  313. this.dataParams,
  314. this.getItemRefs()
  315. ) || {};
  316. }
  317. resolve(this.dataChart);
  318. };
  319. if (isApi) {
  320. let url = safe.url;
  321. if (this.validatenull(url)) return;
  322. let dataQuery = getFunction(safe.dataQuery);
  323. dataQuery =
  324. (typeof dataQuery === "function" &&
  325. dataQuery(url)) ||
  326. {};
  327. let dataHeader = getFunction(safe.dataHeader);
  328. dataHeader =
  329. (typeof dataHeader === "function" &&
  330. dataHeader(url)) ||
  331. {};
  332. let params = Object.assign(
  333. dataQuery,
  334. this.dataParams
  335. );
  336. let axiosParams = {};
  337. if (["post", "put"].includes(safe.dataMethod)) {
  338. axiosParams.data = params;
  339. if (safe.dataQueryType == "form") {
  340. let formData = new FormData();
  341. Object.keys(params).forEach((ele) => {
  342. formData.append(ele, params[ele]);
  343. });
  344. axiosParams.data = formData;
  345. }
  346. } else if (
  347. ["get", "delete"].includes(safe.dataMethod)
  348. ) {
  349. axiosParams.params = params;
  350. }
  351. this.$axios({
  352. ...{
  353. method: safe.dataMethod,
  354. url: url,
  355. headers: dataHeader,
  356. },
  357. ...axiosParams,
  358. }).then((res) => {
  359. this.dataAxios = res;
  360. let result = res;
  361. result = formatter(result, params);
  362. if (this.dataAppend) {
  363. this.updateAppend(result);
  364. } else {
  365. this.dataChart = result;
  366. }
  367. bindEvent();
  368. });
  369. } else if (isWs) {
  370. let url = safe.wsUrl;
  371. if (this.validatenull(url)) return;
  372. let dataQuery = getFunction(safe.dataQuery);
  373. dataQuery =
  374. (typeof dataQuery === "function" &&
  375. dataQuery(url)) ||
  376. {};
  377. let params = Object.assign(
  378. dataQuery,
  379. this.dataParams
  380. );
  381. url = url + addParam(params);
  382. this.wsClient = new WebSocket(url);
  383. this.wsClient.onmessage = (msgEvent = {}) => {
  384. let result = JSON.parse(msgEvent.data);
  385. this.dataChart = formatter(
  386. result,
  387. this.dataParams
  388. );
  389. bindEvent();
  390. };
  391. } else if (isSql) {
  392. let sql = JSON.parse(crypto.decrypt(safe.sql));
  393. let result;
  394. this.sqlFormatter({
  395. dataSourceId: sql.id,
  396. sql: sql.sql,
  397. }).then((res) => {
  398. result = res.json;
  399. this.dataChart = formatter(
  400. result,
  401. this.dataParams
  402. );
  403. bindEvent();
  404. });
  405. } else if (isRecord) {
  406. this.recordFormatter(this.record).then((res) => {
  407. const data = res;
  408. this.dataChart = data;
  409. bindEvent();
  410. });
  411. // bindEvent();
  412. } else {
  413. let result = safe.data;
  414. this.dataChart = formatter(result, this.dataParams);
  415. bindEvent();
  416. }
  417. };
  418. const sendData = () => {
  419. this.$nextTick(() => {
  420. getData();
  421. clearInterval(this.checkChart);
  422. if (this.time !== 0 && this.disabled) {
  423. this.checkChart = setInterval(() => {
  424. getData();
  425. }, this.time);
  426. }
  427. });
  428. };
  429. sendData();
  430. });
  431. },
  432. // 绑定点击事件
  433. bindClick() {
  434. this.myChart.off("click");
  435. this.myChart.on("click", (e) => {
  436. this.updateClick(this.dataChart[e.dataIndex] || e);
  437. this.clickFormatter &&
  438. this.clickFormatter(
  439. Object.assign(e, {
  440. data: this.dataChart,
  441. }),
  442. this.getItemRefs()
  443. );
  444. });
  445. },
  446. // 下面俩都是chart的公共的方法,就放这里面共用
  447. getColor(index, first) {
  448. const barColor = this.option.barColor || [];
  449. if (barColor[index]) {
  450. const color1 = barColor[index].color1;
  451. const color2 = barColor[index].color2;
  452. const postion = (barColor[index].postion || 0.9) * 0.01;
  453. if (first) return color1;
  454. if (color2) {
  455. return {
  456. type: "linear",
  457. x: 0,
  458. y: 0,
  459. x2: 0,
  460. y2: 1,
  461. colorStops: [
  462. {
  463. offset: 0,
  464. color: color1, // 0% 处的颜色
  465. },
  466. {
  467. offset: postion,
  468. color: color2, // 100% 处的颜色
  469. },
  470. ],
  471. global: false, // 缺省为 false
  472. };
  473. }
  474. return color1;
  475. }
  476. },
  477. getHasProp(isHas, propObj, staticObj = {}) {
  478. return Object.assign(
  479. (() => {
  480. return isHas ? propObj : {};
  481. })(),
  482. staticObj
  483. );
  484. },
  485. closeClient() {
  486. this.wsClient.close && this.wsClient.close();
  487. },
  488. },
  489. beforeDestroy() {
  490. clearInterval(this.checkChart);
  491. this.closeClient();
  492. },
  493. };
  494. })();