1fc8194f011f4bfa483111b334114923ccdb0444.svn-base 20 KB

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