123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- import { setPx, validatenull, addParam, loadFont } from "./util";
- import config from "./config";
- import commonOption from "./option";
- import crypto from "@/datav/utils/crypto";
- import { getFunction, funEval, uuid } from "@/datav/utils/utils";
- import COMMON from "@/datav/config";
- export default (() => {
- return {
- props: {
- stylesFormatter: Function,
- dataFormatter: Function,
- titleFormatter: Function,
- labelFormatter: Function,
- clickFormatter: Function,
- sqlFormatter: Function,
- recordFormatter: Function,
- formatter: Function,
- echartFormatter: Function,
- dataQueryType: String,
- dataQuery: String,
- dataHeader: String,
- fontFamily: String,
- width: {
- type: [Number, String],
- default: 600,
- },
- height: {
- type: [Number, String],
- default: 600,
- },
- theme: {
- type: String,
- },
- child: {
- type: Object,
- default: () => {
- return {};
- },
- },
- record: {
- type: String,
- },
- sql: {
- type: String,
- },
- time: {
- type: Number,
- default: 0,
- },
- url: {
- type: String,
- },
- wsUrl: {
- type: String,
- },
- disabled: {
- type: Boolean,
- default: true,
- },
- dataType: {
- type: Number,
- default: 0,
- },
- dataMethod: {
- type: String,
- default: "get",
- },
- id: {
- type: String,
- default: "main_" + uuid(),
- },
- data: {
- type: [Object, String, Array],
- },
- component: {
- type: Object,
- default: () => {
- return {};
- },
- },
- option: {
- type: Object,
- default: () => {
- return {};
- },
- },
- },
- mixins: [commonOption],
- data() {
- return {
- headerHeight: "",
- checkChart: "",
- myChart: "",
- dataChart: [],
- dataAxios: {},
- dataParams: {},
- wsClient: {},
- styles: {},
- appendCheck: {},
- appendObj: {},
- appendList: [],
- className: "",
- };
- },
- watch: {
- data() {
- this.updateData();
- },
- dataAppend(val) {
- this.appendObj = {};
- this.appendList = [];
- if (!val) {
- this.appendCheck = clearInterval(this.appendCheck);
- } else {
- this.dataChart = [];
- }
- this.updateData();
- },
- echartFormatter() {
- this.updateChart();
- },
- width() {
- this.$nextTick(() => {
- this.updateChart();
- });
- },
- height() {
- this.$nextTick(() => {
- this.updateChart();
- });
- this.updateChart();
- },
- theme() {
- this.myChart.dispose();
- this.init();
- },
- option: {
- handler() {
- this.updateChart();
- },
- deep: true,
- },
- },
- computed: {
- count() {
- return this.option.count;
- },
- dataAppend() {
- return this.option.dataAppend;
- },
- dataChartLen() {
- return (this.dataChart || []).length;
- },
- switchTheme() {
- return this.validData(this.option.switchTheme, false);
- },
- name() {
- let className = this.$el.className.split(" ")[0];
- const result = className.replace(config.name, "");
- return result;
- },
- minWidth() {
- const val = this.option.minWidth;
- if (val > this.width) return val;
- },
- styleChartName() {
- const obj = {
- fontFamily: loadFont(this.component.fontFamily),
- width: setPx(this.minWidth || this.width),
- height: setPx(this.height),
- opacity: this.component.opacity || 1,
- transform: `scale(${
- this.component.scale || 1
- }) perspective(${
- this.component.perspective || 500
- }px) rotateX(${this.component.rotateX || 0}deg) rotateY(${
- this.component.rotateY || 0
- }deg) rotateZ(${this.component.rotateZ || 0}deg)`,
- };
- return obj;
- },
- styleSizeName() {
- return Object.assign(
- {
- width: setPx(this.width),
- height: setPx(this.height),
- },
- (() => {
- if (this.minWidth) {
- return {
- overflowX: "auto",
- overflowY: "hidden",
- };
- }
- return {};
- })(),
- this.styles
- );
- },
- },
- mounted() {
- this.init();
- },
- methods: {
- init() {
- this.className = `animated ${this.component.animated || ""}`;
- const main = this.$refs[this.id];
- if (main) {
- // 判断是否图表去初始化
- const isChart = config.echart.includes(this.name);
- if (isChart)
- this.myChart = window.echarts.init(main, this.theme);
- }
- this.updateChart();
- this.updateData();
- },
- getItemRefs() {
- let refList = this.$parent.$parent.$refs;
- let result = {};
- Object.keys(refList).forEach((ele) => {
- if (ele.indexOf(COMMON.NAME) !== -1) {
- result[ele.replace(COMMON.NAME, "")] = refList[ele][0];
- }
- });
- return result;
- },
- updateChart() {},
- updateClick(params) {
- let refList = this.getItemRefs();
- let indexList = this.child.index;
- let indexName = this.child.paramName;
- let indexValue = this.child.paramValue || "value";
- if (validatenull(indexName) && validatenull(indexList)) return;
- let p = {};
- p[indexName] = params[indexValue];
- Object.keys(refList).forEach((ele) => {
- if (indexList.includes(ele)) refList[ele].updateData(p);
- });
- },
- updateAppend(result) {
- if (this.validatenull(this.appendObj)) {
- this.appendList = result;
- this.appendObj = result[0];
- } else {
- let appendList = [];
- for (let i = 0; i < result.length; i++) {
- const ele = result[i];
- if (ele.id === this.appendObj.id) break;
- else appendList.push(ele);
- }
- this.appendObj = result[0];
- appendList.reverse().forEach((ele) => {
- this.appendList.unshift(ele);
- });
- }
- if (this.validatenull(this.appendCheck)) {
- this.appendCheck = setInterval(() => {
- let length = this.appendList.length - 1;
- if (length >= 0) {
- let obj = this.appendList.splice(length, 1)[0];
- this.dataChart.unshift(obj);
- let len = this.dataChart.length;
- if (len > this.count) {
- this.appendList.splice(len - 1, 1);
- }
- }
- }, 2000);
- }
- },
- // 更新数据核心方法
- updateData(p = {}) {
- let record,
- key = false;
- let isRecord = this.dataType === 4;
- this.dataParams = Object.assign(this.dataParams, p);
- return new Promise((resolve, reject) => {
- this.resetData && this.resetData();
- if (key) return;
- key = true;
- let safe = this;
- const formatter = (data, params) => {
- // if (isRecord) {
- // const dataFormatter = getFunction(
- // safe.dataFormatter
- // );
- // if (typeof dataFormatter == "function") {
- // data = dataFormatter(data);
- // }
- // }
- if (typeof this.dataFormatter === "function") {
- data = this.dataFormatter(
- data,
- params,
- this.getItemRefs()
- );
- }
- return data;
- };
- const getData = () => {
- safe = record || this;
- key = false;
- let isApi = safe.dataType === 1;
- let isSql = safe.dataType === 2;
- let isWs = safe.dataType === 3;
- this.closeClient();
- const bindEvent = () => {
- this.updateChart();
- if (this.myChart) this.bindClick();
- if (typeof this.stylesFormatter === "function") {
- this.styles =
- this.stylesFormatter(
- this.dataChart,
- this.dataParams,
- this.getItemRefs()
- ) || {};
- }
- resolve(this.dataChart);
- };
- if (isApi) {
- let url = safe.url;
- if (this.validatenull(url)) return;
- let dataQuery = getFunction(safe.dataQuery);
- dataQuery =
- (typeof dataQuery === "function" &&
- dataQuery(url)) ||
- {};
- let dataHeader = getFunction(safe.dataHeader);
- dataHeader =
- (typeof dataHeader === "function" &&
- dataHeader(url)) ||
- {};
- let params = Object.assign(
- dataQuery,
- this.dataParams
- );
- let axiosParams = {};
- if (["post", "put"].includes(safe.dataMethod)) {
- axiosParams.data = params;
- if (safe.dataQueryType == "form") {
- let formData = new FormData();
- Object.keys(params).forEach((ele) => {
- formData.append(ele, params[ele]);
- });
- axiosParams.data = formData;
- }
- } else if (
- ["get", "delete"].includes(safe.dataMethod)
- ) {
- axiosParams.params = params;
- }
- this.$axios({
- ...{
- method: safe.dataMethod,
- url: url,
- headers: dataHeader,
- },
- ...axiosParams,
- }).then((res) => {
- this.dataAxios = res;
- let result = res;
- result = formatter(result, params);
- if (this.dataAppend) {
- this.updateAppend(result);
- } else {
- this.dataChart = result;
- }
- bindEvent();
- });
- } else if (isWs) {
- let url = safe.wsUrl;
- if (this.validatenull(url)) return;
- let dataQuery = getFunction(safe.dataQuery);
- dataQuery =
- (typeof dataQuery === "function" &&
- dataQuery(url)) ||
- {};
- let params = Object.assign(
- dataQuery,
- this.dataParams
- );
- url = url + addParam(params);
- this.wsClient = new WebSocket(url);
- this.wsClient.onmessage = (msgEvent = {}) => {
- let result = JSON.parse(msgEvent.data);
- this.dataChart = formatter(
- result,
- this.dataParams
- );
- bindEvent();
- };
- } else if (isSql) {
- let sql = JSON.parse(crypto.decrypt(safe.sql));
- let result;
- this.sqlFormatter({
- dataSourceId: sql.id,
- sql: sql.sql,
- }).then((res) => {
- result = res.json;
- this.dataChart = formatter(
- result,
- this.dataParams
- );
- bindEvent();
- });
- } else if (isRecord) {
- this.recordFormatter(this.record).then((res) => {
- const data = res;
- this.dataChart = data;
- bindEvent();
- });
- // bindEvent();
- } else {
- let result = safe.data;
- this.dataChart = formatter(result, this.dataParams);
- bindEvent();
- }
- };
- const sendData = () => {
- this.$nextTick(() => {
- getData();
- clearInterval(this.checkChart);
- if (this.time !== 0 && this.disabled) {
- this.checkChart = setInterval(() => {
- getData();
- }, this.time);
- }
- });
- };
- sendData();
- });
- },
- // 绑定点击事件
- bindClick() {
- this.myChart.off("click");
- this.myChart.on("click", (e) => {
- this.updateClick(this.dataChart[e.dataIndex] || e);
- this.clickFormatter &&
- this.clickFormatter(
- Object.assign(e, {
- data: this.dataChart,
- }),
- this.getItemRefs()
- );
- });
- },
- // 下面俩都是chart的公共的方法,就放这里面共用
- getColor(index, first) {
- const barColor = this.option.barColor || [];
- if (barColor[index]) {
- const color1 = barColor[index].color1;
- const color2 = barColor[index].color2;
- const postion = (barColor[index].postion || 0.9) * 0.01;
- if (first) return color1;
- if (color2) {
- return {
- type: "linear",
- x: 0,
- y: 0,
- x2: 0,
- y2: 1,
- colorStops: [
- {
- offset: 0,
- color: color1, // 0% 处的颜色
- },
- {
- offset: postion,
- color: color2, // 100% 处的颜色
- },
- ],
- global: false, // 缺省为 false
- };
- }
- return color1;
- }
- },
- getHasProp(isHas, propObj, staticObj = {}) {
- return Object.assign(
- (() => {
- return isHas ? propObj : {};
- })(),
- staticObj
- );
- },
- closeClient() {
- this.wsClient.close && this.wsClient.close();
- },
- },
- beforeDestroy() {
- clearInterval(this.checkChart);
- this.closeClient();
- },
- };
- })();
|