8e6e8d3d9799b3852cdba45177880130de03c1da.svn-base 20 KB

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