90dcf64289f6cc730e38c1ef797d5f843386d5e8.svn-base 19 KB

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