c190ecce78f89d7987d35d7d9b45b9cb222bc257.svn-base 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. <template>
  2. <div class="dashboard-wrapper">
  3. <el-card body-style="padding:0;" style="margin-bottom: 20px;" class="panel-header" v-if="title !=='3'">
  4. <div slot="header" style="display: flex; justify-content:space-between;">
  5. <span>
  6. <span class="back-button" @click="$router.push({ name: 'BoardEchartManagement', params: { tab: 'first' } })">
  7. <i class="el-icon-back" />
  8. <span>返回</span>
  9. </span>
  10. <span v-if="mode == 'view'">查看仪表盘</span>
  11. <span v-if="mode == 'edit'">编辑仪表盘</span>
  12. <span v-if="mode == 'create'">新增仪表盘</span>
  13. </span>
  14. </div>
  15. </el-card>
  16. <div class="tool-bar">
  17. <div>
  18. <span class="db-name">{{ dashboard.name }}</span>
  19. <span>{{ dashboard.desc }}</span>
  20. </div>
  21. <div v-show="mode === 'edit' || mode === 'create' ">
  22. <!-- <el-button type="primary" size="mini" @click="handleShare">
  23. {{ $t('common.share') }}
  24. </el-button> -->
  25. <el-button type="primary" size="mini" @click="handleLinkChart">
  26. 添加图表
  27. </el-button>
  28. </div>
  29. </div>
  30. <grid-layout v-if="charts.length!==0" v-loading="loading" :layout="layout || []" :col-num="24" :row-height="30" :is-draggable="mode === 'edit' || mode === 'create' " :is-resizable="mode === 'edit' || mode === 'create' " :is-mirrored="false" :vertical-compact="true" :pane-container="false" :margin="[10, 10]" :use-css-transforms="true" style="min-height: 500px;" @layout-updated="handleLayoutChange">
  31. <grid-item v-for="item in layout || []" :key="item.index" :x="item.x" :y="item.y" :w="item.w" :h="item.h" :i="item.i" @resized="handleResize">
  32. <el-card v-loading="chartLoading[item.i]" class="visualize-card" body-style="padding: 10px;">
  33. <div slot="header" class="operation-bar">
  34. <div>
  35. <span>{{ getChartItem(item.i).chart_name }}</span>
  36. </div>
  37. <div>
  38. <i v-show="mode === 'edit' || mode === 'create' " class="el-icon-edit" @click="handleEdit(getChartItem(item.i))" />
  39. <i v-show="mode === 'edit' || mode === 'create' " class="el-icon-delete" @click="handleDelete(getChartItem(item.i))" />
  40. <el-tooltip :content="getChartItem(item.i).desc" class="item" effect="dark" placement="top-end">
  41. <i class="el-icon-info" style="color:#409eff;cursor:pointer;" />
  42. </el-tooltip>
  43. </div>
  44. </div>
  45. <visualize-panel :key="item.index" :ref="`chartInstance${item.i}`" :data="results[item.i]" :schema="getChartItem(item.i).content.allSelected" :chart-type.sync="getChartItem(item.i).content.chartType" :is-edit-mode="false" :chart-style="{height: `${item.h*30 + 10 * (item.h-1) - 60}px`}" />
  46. </el-card>
  47. </grid-item>
  48. </grid-layout>
  49. <div v-if="charts.length === 0 && mode === 'edit' || mode === 'create' && charts.length === 0" v-loading="loading" class="welcome-container">
  50. <el-button type="primary" size="mini" @click="handleLinkChart">
  51. {{ $t('dashboard.addChart') }}
  52. </el-button>
  53. <div>
  54. <el-link type="info" :underline="false">
  55. <router-link to="/chartpanel/create">
  56. {{ $t('dashboard.emptyDashboardTip') }}
  57. </router-link>
  58. </el-link>
  59. </div>
  60. </div>
  61. <el-dialog :title="$t('chart.myChart')" :visible.sync="showChartList">
  62. <!-- <el-button type="primary" size="mini" @click="$router.push({ name: 'ChartPanel', params: { id: 'create' , isEdit: 'edit'}})">
  63. 创建新的图表
  64. </el-button> -->
  65. <el-button type="primary" size="mini" @click="jumpCreate()">
  66. 创建新的图表
  67. </el-button>
  68. <el-table :data="myChartList">
  69. <el-table-column :label="$t('common.name')" width="200" prop="chart_name" />
  70. <el-table-column :label="$t('common.desc')" prop="desc" />
  71. <el-table-column :label="$t('common.operation')" align="center">
  72. <template slot-scope="scope">
  73. <el-button size="mini" type="primary" :disabled="isExisted(scope.row)" @click="linkChart(scope.row)">添加</el-button>
  74. <el-button size="mini" type="warning" @click="jumpEdit2(scope.row)">
  75. 编辑
  76. </el-button>
  77. </template>
  78. </el-table-column>
  79. </el-table>
  80. <span slot="footer" class="dialog-footer">
  81. <el-button type="primary" size="small" @click="showChartList = false">关闭</el-button>
  82. </span>
  83. </el-dialog>
  84. </div>
  85. </template>
  86. <script>
  87. import { GridLayout } from 'vue-grid-layout'
  88. import { GridItem } from 'vue-grid-layout'
  89. import visualizePanel from '../ChartPanel/components/visualizePanel'
  90. import exeSql from '@/api/exeSql'
  91. import { chartByDashboard, updateDashboard, addChartToDB, unMapChartDb } from '@/api/dashboard'
  92. import { GetDataByName, ExeSqlJiade, PostDataByName, dashboardListJiade, updateDashboardJiade } from '@/api/common'
  93. import { chartList } from '@/api/chart'
  94. import Cookies from 'js-cookie'
  95. import { buildSqlSentence, buildFilterSentence } from '@/utils/buildSentence'
  96. function isLineOverLap(line1, line2) {
  97. const start1 = {
  98. x: line1[0][0],
  99. y: line1[0][1]
  100. }
  101. const end1 = {
  102. x: line1[1][0],
  103. y: line1[1][1]
  104. }
  105. const start2 = {
  106. x: line2[0][0],
  107. y: line2[0][1]
  108. }
  109. const end2 = {
  110. x: line2[1][0],
  111. y: line2[1][1]
  112. }
  113. if (start1.y === start2.y && end1.y === end2.y) {
  114. if (start1.x >= start2.x && start1.x <= end2.x) {
  115. return true
  116. } else {
  117. return false
  118. }
  119. } else {
  120. return false
  121. }
  122. }
  123. export default {
  124. inject: ['reload'],
  125. components: { GridLayout, GridItem, visualizePanel },
  126. props: {
  127. dashboard: {
  128. required: false,
  129. type: Object,
  130. default: () => {
  131. return {}
  132. }
  133. },
  134. mode: {
  135. required: false,
  136. type: String,
  137. default: 'edit'
  138. },
  139. title: {
  140. required: false,
  141. type: String,
  142. default: '2'
  143. }
  144. },
  145. data() {
  146. return {
  147. charts: [],
  148. results: {},
  149. loading: false,
  150. layout: [],
  151. myChartList: [],
  152. showChartList: false,
  153. chartLoading: {}
  154. }
  155. },
  156. watch: {
  157. 'dashboard.dashboard_id': {
  158. immediate: true,
  159. handler(value) {
  160. console.log('watch的:', value)
  161. if (!value) return
  162. this.getList(value)
  163. }
  164. }
  165. },
  166. methods: {
  167. // getList() {
  168. // this.charts = []
  169. // this.layout = []
  170. // this.loading = true
  171. // chartByDashboard(this.dashboard.dashboard_id).then(resp => {
  172. // this.loading = false
  173. // this.charts = resp.data || []
  174. // let filterStrs = []
  175. // console.log(this.dashboard.content)
  176. // const layout = (this.dashboard.content && this.dashboard.content.layout) || []
  177. // this.charts.forEach((chart, index) => {
  178. // this.$set(this.results, chart.chart_id, [])
  179. // this.$set(this.chartLoading, chart.chart_id, false)
  180. // chart.content = JSON.parse(chart.content)
  181. // chart.content.allSelected = []
  182. // chart.content.allSelected = chart.content.allSelected.concat(chart.content.selectedCalcul).concat(chart.content.selectedDimension)
  183. // if (chart.content.filters) {
  184. // filterStrs = chart.content.filters.map(buildFilterSentence)
  185. // }
  186. // const sqlSentence = buildSqlSentence({
  187. // dataSrc: chart.content.dataSrc,
  188. // selectedCalcul: chart.content.selectedCalcul,
  189. // selectedDimension: chart.content.selectedDimension,
  190. // orderByStrs: chart.content.orderByStrs,
  191. // filterStr: filterStrs.join(' and '),
  192. // limit: chart.content.limit
  193. // })
  194. // this.exeSql(sqlSentence, chart, index)
  195. // console.log(layout.find(layoutItem => layoutItem.id === chart.chart_id))
  196. // if (!layout.find(layoutItem => layoutItem.id === chart.chart_id)) {
  197. // this.generatePosition(chart, layout, index)
  198. // }
  199. // })
  200. // this.layout = layout.filter(item => {
  201. // return this.charts.find(chart => chart.chart_id === item.id)
  202. // })
  203. // this.handleLayoutChange()
  204. // })
  205. // },
  206. getList() {
  207. console.log('进入页面组件=================')
  208. this.charts = []
  209. this.layout = []
  210. this.loading = true
  211. var send_data = {
  212. name: 'getdashboard_chart',
  213. parammaps: {
  214. pastureid: Cookies.get('pastureid'),
  215. did: this.dashboard.dashboard_id,
  216. empid: Cookies.get('employeid')
  217. }
  218. }
  219. GetDataByName(send_data).then(resp => {
  220. console.log('图表数据', resp)
  221. this.loading = false
  222. this.charts = resp.data.list || []
  223. let filterStrs = []
  224. console.log('this.dashboard', this.dashboard)
  225. const layout = (this.dashboard.content && this.dashboard.content.layout) || []
  226. console.log('layout', layout)
  227. console.log('this.charts', this.charts)
  228. this.charts.forEach((chart, index) => {
  229. this.$set(this.results, chart.chart_id, [])
  230. this.$set(this.chartLoading, chart.chart_id, false)
  231. chart.content = JSON.parse(chart.content)
  232. chart.content.allSelected = []
  233. chart.content.allSelected = chart.content.allSelected.concat(chart.content.selectedCalcul).concat(chart.content.selectedCalcul2).concat(chart.content.selectedDimension)
  234. console.log('chart.content.filters', chart.content.filters)
  235. if (chart.content.filters) {
  236. filterStrs = chart.content.filters.map(buildFilterSentence)
  237. }
  238. // const sqlSentence = buildSqlSentence({
  239. // dataSrc: chart.content.dataSrc,
  240. // selectedCalcul: chart.content.selectedCalcul,
  241. // selectedDimension: chart.content.selectedDimension,
  242. // orderByStrs: chart.content.orderByStrs,
  243. // filterStr: filterStrs.join(' and '),
  244. // limit: chart.content.limit
  245. // })
  246. this.exeSqlJia2(chart.content, chart, index)
  247. console.log(layout.find(layoutItem => layoutItem.id === chart.chart_id))
  248. })
  249. this.layout = layout.filter(item => {
  250. return this.charts.find(chart => chart.chart_id === item.id)
  251. })
  252. console.log('layout:', this.layout)
  253. this.handleLayoutChange()
  254. })
  255. },
  256. jumpCreate() {
  257. this.$router.push({ name: 'ChartPanel', params: { id: 'create', isEdit: 'edit' } })
  258. localStorage.setItem("ChartPanelIsEditId", 'create')
  259. },
  260. jumpEdit2(row) {
  261. // $router.push({ name: 'ChartPanel', params: { id: scope.row.id , isEdit: 'edit'}})
  262. console.log('点击了编辑', row)
  263. this.$router.push({ name: 'ChartPanel', params: { id: row.id, isEdit: 'edit' } })
  264. Cookies.set('ChartPanelIsEdit', 'edit')
  265. Cookies.set('ChartPanelIsEditId', row.id)
  266. localStorage.setItem("ChartPanelIsEditId", row.id)
  267. },
  268. exeSqlJia2(content, item, index) {
  269. this.$set(this.chartLoading, item.chart_id, true)
  270. if (!content) {
  271. this.$message.warning(this.$t('dashboard.chartQueryException', item.chart_name))
  272. this.$set(this.chartLoading, item.chart_id, false)
  273. return
  274. }
  275. // exeSql().fetch({ source_id: item.source_id, sql: sqlSentence }).then(resp => {
  276. // this.$set(this.chartLoading, item.chart_id, false)
  277. // this.$set(this.results, item.chart_id, resp.data)
  278. // }).catch(() => {
  279. // this.$set(this.chartLoading, item.chart_id, false)
  280. // })
  281. var send_data = { source_id: item.source_id, content: content, pastureid: Cookies.get('pastureid') }
  282. ExeSqlJiade(send_data).then(resp => {
  283. console.log('图的数据', resp.data.list)
  284. this.$set(this.chartLoading, item.chart_id, false)
  285. this.$set(this.results, item.chart_id, resp.data)
  286. }).catch(() => {
  287. this.$set(this.chartLoading, item.chart_id, false)
  288. })
  289. },
  290. getChartItem(id) {
  291. return this.charts.find(chart => chart.chart_id === id)
  292. },
  293. handleCaculPos(layout) {
  294. // const layout = JSON.parse(JSON.stringify(layout))
  295. const bottomItems = []
  296. layout.forEach(i => {
  297. i.yOffSet = i.y + i.h
  298. i.xOffSet = i.x + i.w
  299. i.bottomLine = [[i.x, i.yOffSet], [i.xOffSet, i.yOffSet]]
  300. i.topLine = [[i.x, i.y], [i.xOffSet, i.y]]
  301. })
  302. layout.forEach(i => {
  303. const flag = layout.every(j => {
  304. return !isLineOverLap(i.bottomLine, j.topLine)
  305. })
  306. if (flag) {
  307. bottomItems.push(i)
  308. }
  309. })
  310. return bottomItems
  311. },
  312. generatePosition(chart, layout, index) {
  313. let posObj
  314. if (layout.length === 0) {
  315. posObj = {
  316. id: chart.chart_id,
  317. x: 0,
  318. y: 0,
  319. w: 12,
  320. h: 9,
  321. i: chart.chart_id
  322. }
  323. } else {
  324. const bottomItems = this.handleCaculPos(layout)
  325. const highestItem = bottomItems.reduce((result, item) => {
  326. if (result.bottomLine[0][1] > item.bottomLine[0][1]) {
  327. result = item
  328. }
  329. return result
  330. }, bottomItems[0])
  331. posObj = {
  332. id: chart.chart_id,
  333. x: highestItem.x,
  334. y: highestItem.yOffSet,
  335. w: highestItem.w,
  336. h: 9,
  337. i: chart.chart_id
  338. }
  339. }
  340. layout.push(posObj)
  341. },
  342. handleShare() {
  343. const h = this.$createElement
  344. const link = `http://${location.host}/#/fullscreendb/${this.dashboard.dashboard_id}`
  345. this.$msgbox({
  346. title: this.$t('dashboard.shareLink'),
  347. message: h('p', null, [
  348. h('a', { style: 'color: #205cd8', attrs: { href: link, target: '_blank' } }, link)
  349. ])
  350. })
  351. },
  352. // handleLinkChart() {
  353. // chartList().then(resp => {
  354. // this.myChartList = resp.data
  355. // this.showChartList = true
  356. // })
  357. // },
  358. handleLinkChart() {
  359. var send_data = {
  360. name: 'getChartListV2',
  361. page: 1,
  362. offset: 1,
  363. pagecount: 10,
  364. returntype: 'Map',
  365. parammaps: {
  366. pastureid: Cookies.get('pastureid'),
  367. empid: Cookies.get('employeid')
  368. }
  369. }
  370. GetDataByName(send_data).then(response => {
  371. console.log('table数据', response.data.list)
  372. if (response.data.list !== null) {
  373. this.myChartList = response.data.list
  374. this.showChartList = true
  375. } else {
  376. this.myChartList = []
  377. this.showChartList = true
  378. }
  379. })
  380. },
  381. // linkChart(chart) {
  382. // const data = {
  383. // chart_id: chart.chart_id,
  384. // dashboard_id: this.dashboard.dashboard_id
  385. // }
  386. // addChartToDB(data).then(resp => {
  387. // this.showChartList = false
  388. // this.getList()
  389. // this.$message({
  390. // type: 'success',
  391. // message: this.$t('common.saveSuccess')
  392. // })
  393. // })
  394. // },
  395. linkChart(chart) {
  396. const data = {
  397. name: 'insertdashboard_chart',
  398. parammaps: {
  399. cid: chart.chart_id,
  400. did: this.dashboard.dashboard_id,
  401. pastureid: Cookies.get('pastureid')
  402. }
  403. }
  404. PostDataByName(data).then(response => {
  405. console.log('新增保存发送参数', data)
  406. this.showChartList = false
  407. if (data.msg == 'fail') {
  408. this.$message({
  409. type: 'fail',
  410. message: '添加失败!'
  411. })
  412. } else {
  413. this.$message({
  414. type: 'success',
  415. message: this.$t('common.saveSuccess')
  416. })
  417. }
  418. this.$router.push({ name: 'Addboard', params: { id: '~' + this.dashboard.dashboard_id } })
  419. console.log('Addboard~' + this.dashboard.dashboard_id)
  420. this.getList()
  421. this.$emit('fatherMethod')
  422. //重新刷新页面
  423. this.reload()
  424. })
  425. },
  426. isExisted(chart) {
  427. return this.charts.findIndex(item => item.chart_id === chart.chart_id) >= 0
  428. },
  429. // handleEdit(chart) {
  430. // this.$router.push(`/chartpanel/${chart.chart_id}`)
  431. // },
  432. handleEdit(chart) {
  433. this.$router.push({ name: 'ChartPanel', params: { id: chart.chart_id, isEdit: 'edit' } })
  434. localStorage.setItem("ChartPanelIsEditId", chart.chart_id)
  435. },
  436. // handleDelete(chart) {
  437. // this.$confirm(this.$t('dashboard.removeChartConfirm'), this.$t('common.confirm'), {
  438. // type: 'warning'
  439. // }).then(() => {
  440. // // deleteChart(index)
  441. // const deleteChartIndex = this.layout.findIndex(item => item.id === chart.chart_id)
  442. // const layout = JSON.parse(JSON.stringify(this.layout))
  443. // layout.splice(deleteChartIndex, 1)
  444. // this.dashboard.content.layout = layout
  445. // const data = {
  446. // chart_id: chart.chart_id,
  447. // dashboard_id: this.dashboard.dashboard_id
  448. // }
  449. // Promise.all([updateDashboard(this.dashboard), unMapChartDb(data)]).then(resp => {
  450. // this.getList()
  451. // this.$message({
  452. // type: 'success',
  453. // message: this.$t('common.deleteSuccess')
  454. // })
  455. // })
  456. // })
  457. // },
  458. handleDelete(chart) {
  459. this.$confirm(this.$t('dashboard.removeChartConfirm'), this.$t('common.confirm'), {
  460. type: 'warning'
  461. }).then(() => {
  462. // deleteChart(index)
  463. const deleteChartIndex = this.layout.findIndex(item => item.id === chart.chart_id)
  464. const layout = JSON.parse(JSON.stringify(this.layout))
  465. layout.splice(deleteChartIndex, 1)
  466. this.dashboard.content.layout = layout
  467. const data = {
  468. name: 'deletedashboard_chart',
  469. parammaps: {
  470. cid: chart.chart_id,
  471. did: this.dashboard.dashboard_id,
  472. pastureid: Cookies.get('pastureid')
  473. }
  474. }
  475. PostDataByName(data).then(response => {
  476. console.log('删除发送参数', data)
  477. this.$message({
  478. type: 'success',
  479. message: this.$t('common.deleteSuccess')
  480. })
  481. this.getList()
  482. })
  483. })
  484. },
  485. // 拖拽
  486. // handleLayoutChange() {
  487. // if (this.mode === 'view') return
  488. // this.dashboard.content = this.dashboard.content || {}
  489. // this.dashboard.content.layout = this.layout
  490. // // updateDashboard(this.dashboard)
  491. // },
  492. handleLayoutChange() {
  493. console.log('this.mode:', this.mode)
  494. if (this.mode === 'view') return
  495. console.log('this.dashboard1', this.dashboard)
  496. this.dashboard.content = this.dashboard.content || {}
  497. this.dashboard.content.layout = this.layout
  498. console.log('this.dashboard2', this.dashboard)
  499. var send_data = { dashboard_id: this.dashboard.dashboard_id, content: this.dashboard.content, pastureid: Cookies.get('pastureid') }
  500. updateDashboardJiade(send_data).then(response => {
  501. console.log('拖拽位置的数据', response.data.list)
  502. // if (response.data !== null) {
  503. // this.loading = false
  504. // this.result = response.data
  505. // } else {
  506. // this.loading = false
  507. // this.result = []
  508. // }
  509. })
  510. },
  511. handleResize(i, newH, newW, newHPx, newWPx) {
  512. this.$refs[`chartInstance${i}`][0].$children[0].$emit('resized')
  513. },
  514. exeSql(sqlSentence, item, index) {
  515. this.$set(this.chartLoading, item.chart_id, true)
  516. if (!sqlSentence) {
  517. this.$message.warning(this.$t('dashboard.chartQueryException', item.chart_name))
  518. this.$set(this.chartLoading, item.chart_id, false)
  519. return
  520. }
  521. exeSql().fetch({ source_id: item.source_id, sql: sqlSentence }).then(resp => {
  522. this.$set(this.chartLoading, item.chart_id, false)
  523. this.$set(this.results, item.chart_id, resp.data)
  524. }).catch(() => {
  525. this.$set(this.chartLoading, item.chart_id, false)
  526. })
  527. }
  528. }
  529. }
  530. </script>
  531. <style lang="scss" scoped>
  532. .back-button {
  533. display: inline-block;
  534. padding-right: 10px;
  535. margin-right: 10px;
  536. border-right: 1px solid #909090;
  537. cursor: pointer;
  538. span {
  539. padding: 5px;
  540. font-size: 14px;
  541. }
  542. }
  543. .tool-bar {
  544. display: flex;
  545. justify-content: space-between;
  546. border-top: none;
  547. height: 45px;
  548. line-height: 45px;
  549. color: #303133;
  550. padding: 0 10px;
  551. position: relative;
  552. .db-name {
  553. font-size: 1.2em;
  554. font-weight: 600;
  555. color: #909399;
  556. margin-left: 0;
  557. }
  558. span {
  559. color: #c0c4cc;
  560. font-size: 0.8em;
  561. margin-left: 10px;
  562. }
  563. }
  564. .visualize-card {
  565. /deep/ .el-card__header {
  566. padding: 0;
  567. .operation-bar {
  568. font-size: 14px;
  569. display: flex;
  570. justify-content: space-between;
  571. height: 35px;
  572. padding: 0 10px;
  573. line-height: 35px;
  574. z-index: 9;
  575. i {
  576. margin-right: 10px;
  577. color: #409eff;
  578. cursor: pointer;
  579. }
  580. }
  581. }
  582. }
  583. .welcome-container {
  584. text-align: center;
  585. height: 500px;
  586. color: #c0c4cc;
  587. /deep/ .el-button {
  588. margin-top: 200px;
  589. margin-bottom: 25px;
  590. }
  591. }
  592. </style>