f1834d554285dc99a7b41d8a2c490e22c0b78c4d.svn-base 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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="jumpEdit2(scope.row)">
  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. jumpEdit2(row) {
  253. // $router.push({ name: 'ChartPanel', params: { id: scope.row.id , isEdit: 'edit'}})
  254. console.log('点击了编辑', row)
  255. this.$router.push({ name: 'ChartPanel', params: { id: row.id, isEdit: 'edit' } })
  256. Cookies.set('ChartPanelIsEdit', 'edit')
  257. Cookies.set('ChartPanelIsEditId', row.id)
  258. localStorage.setItem("ChartPanelIsEditId", row.id)
  259. },
  260. exeSqlJia2(content, item, index) {
  261. this.$set(this.chartLoading, item.chart_id, true)
  262. if (!content) {
  263. this.$message.warning(this.$t('dashboard.chartQueryException', item.chart_name))
  264. this.$set(this.chartLoading, item.chart_id, false)
  265. return
  266. }
  267. // exeSql().fetch({ source_id: item.source_id, sql: sqlSentence }).then(resp => {
  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. var send_data = { source_id: item.source_id, content: content, pastureid: Cookies.get('pastureid') }
  274. ExeSqlJiade(send_data).then(resp => {
  275. console.log('图的数据', resp.data.list)
  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. },
  282. getChartItem(id) {
  283. return this.charts.find(chart => chart.chart_id === id)
  284. },
  285. handleCaculPos(layout) {
  286. // const layout = JSON.parse(JSON.stringify(layout))
  287. const bottomItems = []
  288. layout.forEach(i => {
  289. i.yOffSet = i.y + i.h
  290. i.xOffSet = i.x + i.w
  291. i.bottomLine = [[i.x, i.yOffSet], [i.xOffSet, i.yOffSet]]
  292. i.topLine = [[i.x, i.y], [i.xOffSet, i.y]]
  293. })
  294. layout.forEach(i => {
  295. const flag = layout.every(j => {
  296. return !isLineOverLap(i.bottomLine, j.topLine)
  297. })
  298. if (flag) {
  299. bottomItems.push(i)
  300. }
  301. })
  302. return bottomItems
  303. },
  304. generatePosition(chart, layout, index) {
  305. let posObj
  306. if (layout.length === 0) {
  307. posObj = {
  308. id: chart.chart_id,
  309. x: 0,
  310. y: 0,
  311. w: 12,
  312. h: 9,
  313. i: chart.chart_id
  314. }
  315. } else {
  316. const bottomItems = this.handleCaculPos(layout)
  317. const highestItem = bottomItems.reduce((result, item) => {
  318. if (result.bottomLine[0][1] > item.bottomLine[0][1]) {
  319. result = item
  320. }
  321. return result
  322. }, bottomItems[0])
  323. posObj = {
  324. id: chart.chart_id,
  325. x: highestItem.x,
  326. y: highestItem.yOffSet,
  327. w: highestItem.w,
  328. h: 9,
  329. i: chart.chart_id
  330. }
  331. }
  332. layout.push(posObj)
  333. },
  334. handleShare() {
  335. const h = this.$createElement
  336. const link = `http://${location.host}/#/fullscreendb/${this.dashboard.dashboard_id}`
  337. this.$msgbox({
  338. title: this.$t('dashboard.shareLink'),
  339. message: h('p', null, [
  340. h('a', { style: 'color: #205cd8', attrs: { href: link, target: '_blank' } }, link)
  341. ])
  342. })
  343. },
  344. // handleLinkChart() {
  345. // chartList().then(resp => {
  346. // this.myChartList = resp.data
  347. // this.showChartList = true
  348. // })
  349. // },
  350. handleLinkChart() {
  351. var send_data = {
  352. name: 'getChartListV2',
  353. page: 1,
  354. offset: 1,
  355. pagecount: 10,
  356. returntype: 'Map',
  357. parammaps: {
  358. pastureid: Cookies.get('pastureid'),
  359. empid: Cookies.get('employeid')
  360. }
  361. }
  362. GetDataByName(send_data).then(response => {
  363. console.log('table数据', response.data.list)
  364. if (response.data.list !== null) {
  365. this.myChartList = response.data.list
  366. this.showChartList = true
  367. } else {
  368. this.myChartList = []
  369. }
  370. })
  371. },
  372. // linkChart(chart) {
  373. // const data = {
  374. // chart_id: chart.chart_id,
  375. // dashboard_id: this.dashboard.dashboard_id
  376. // }
  377. // addChartToDB(data).then(resp => {
  378. // this.showChartList = false
  379. // this.getList()
  380. // this.$message({
  381. // type: 'success',
  382. // message: this.$t('common.saveSuccess')
  383. // })
  384. // })
  385. // },
  386. linkChart(chart) {
  387. const data = {
  388. name: 'insertdashboard_chart',
  389. parammaps: {
  390. cid: chart.chart_id,
  391. did: this.dashboard.dashboard_id,
  392. pastureid: Cookies.get('pastureid')
  393. }
  394. }
  395. PostDataByName(data).then(response => {
  396. console.log('新增保存发送参数', data)
  397. this.showChartList = false
  398. this.getList()
  399. if (data.msg == 'fail') {
  400. this.$message({
  401. type: 'fail',
  402. message: '添加失败!'
  403. })
  404. } else {
  405. this.$message({
  406. type: 'success',
  407. message: this.$t('common.saveSuccess')
  408. })
  409. }
  410. this.$router.push({ name: 'Addboard', params: { id: '~' + this.dashboard.dashboard_id } })
  411. console.log('Addboard~' + this.dashboard.dashboard_id)
  412. this.$emit('fatherMethod')
  413. })
  414. },
  415. isExisted(chart) {
  416. return this.charts.findIndex(item => item.chart_id === chart.chart_id) >= 0
  417. },
  418. // handleEdit(chart) {
  419. // this.$router.push(`/chartpanel/${chart.chart_id}`)
  420. // },
  421. handleEdit(chart) {
  422. this.$router.push({ name: 'ChartPanel', params: { id: chart.chart_id, isEdit: 'edit' } })
  423. },
  424. // handleDelete(chart) {
  425. // this.$confirm(this.$t('dashboard.removeChartConfirm'), this.$t('common.confirm'), {
  426. // type: 'warning'
  427. // }).then(() => {
  428. // // deleteChart(index)
  429. // const deleteChartIndex = this.layout.findIndex(item => item.id === chart.chart_id)
  430. // const layout = JSON.parse(JSON.stringify(this.layout))
  431. // layout.splice(deleteChartIndex, 1)
  432. // this.dashboard.content.layout = layout
  433. // const data = {
  434. // chart_id: chart.chart_id,
  435. // dashboard_id: this.dashboard.dashboard_id
  436. // }
  437. // Promise.all([updateDashboard(this.dashboard), unMapChartDb(data)]).then(resp => {
  438. // this.getList()
  439. // this.$message({
  440. // type: 'success',
  441. // message: this.$t('common.deleteSuccess')
  442. // })
  443. // })
  444. // })
  445. // },
  446. handleDelete(chart) {
  447. this.$confirm(this.$t('dashboard.removeChartConfirm'), this.$t('common.confirm'), {
  448. type: 'warning'
  449. }).then(() => {
  450. // deleteChart(index)
  451. const deleteChartIndex = this.layout.findIndex(item => item.id === chart.chart_id)
  452. const layout = JSON.parse(JSON.stringify(this.layout))
  453. layout.splice(deleteChartIndex, 1)
  454. this.dashboard.content.layout = layout
  455. const data = {
  456. name: 'deletedashboard_chart',
  457. parammaps: {
  458. cid: chart.chart_id,
  459. did: this.dashboard.dashboard_id,
  460. pastureid: Cookies.get('pastureid')
  461. }
  462. }
  463. PostDataByName(data).then(response => {
  464. console.log('删除发送参数', data)
  465. this.$message({
  466. type: 'success',
  467. message: this.$t('common.deleteSuccess')
  468. })
  469. this.getList()
  470. })
  471. })
  472. },
  473. // 拖拽
  474. // handleLayoutChange() {
  475. // if (this.mode === 'view') return
  476. // this.dashboard.content = this.dashboard.content || {}
  477. // this.dashboard.content.layout = this.layout
  478. // // updateDashboard(this.dashboard)
  479. // },
  480. handleLayoutChange() {
  481. console.log('this.mode:', this.mode)
  482. if (this.mode === 'view') return
  483. console.log('this.dashboard1', this.dashboard)
  484. this.dashboard.content = this.dashboard.content || {}
  485. this.dashboard.content.layout = this.layout
  486. console.log('this.dashboard2', this.dashboard)
  487. var send_data = { dashboard_id: this.dashboard.dashboard_id, content: this.dashboard.content, pastureid: Cookies.get('pastureid') }
  488. updateDashboardJiade(send_data).then(response => {
  489. console.log('拖拽位置的数据', response.data.list)
  490. // if (response.data !== null) {
  491. // this.loading = false
  492. // this.result = response.data
  493. // } else {
  494. // this.loading = false
  495. // this.result = []
  496. // }
  497. })
  498. },
  499. handleResize(i, newH, newW, newHPx, newWPx) {
  500. this.$refs[`chartInstance${i}`][0].$children[0].$emit('resized')
  501. },
  502. exeSql(sqlSentence, item, index) {
  503. this.$set(this.chartLoading, item.chart_id, true)
  504. if (!sqlSentence) {
  505. this.$message.warning(this.$t('dashboard.chartQueryException', item.chart_name))
  506. this.$set(this.chartLoading, item.chart_id, false)
  507. return
  508. }
  509. exeSql().fetch({ source_id: item.source_id, sql: sqlSentence }).then(resp => {
  510. this.$set(this.chartLoading, item.chart_id, false)
  511. this.$set(this.results, item.chart_id, resp.data)
  512. }).catch(() => {
  513. this.$set(this.chartLoading, item.chart_id, false)
  514. })
  515. }
  516. }
  517. }
  518. </script>
  519. <style lang="scss" scoped>
  520. .back-button {
  521. display: inline-block;
  522. padding-right: 10px;
  523. margin-right: 10px;
  524. border-right: 1px solid #909090;
  525. cursor: pointer;
  526. span {
  527. padding: 5px;
  528. font-size: 14px;
  529. }
  530. }
  531. .tool-bar {
  532. display: flex;
  533. justify-content: space-between;
  534. border-top: none;
  535. height: 45px;
  536. line-height: 45px;
  537. color: #303133;
  538. padding: 0 10px;
  539. position: relative;
  540. .db-name {
  541. font-size: 1.2em;
  542. font-weight: 600;
  543. color: #909399;
  544. margin-left: 0;
  545. }
  546. span {
  547. color: #c0c4cc;
  548. font-size: 0.8em;
  549. margin-left: 10px;
  550. }
  551. }
  552. .visualize-card {
  553. /deep/ .el-card__header {
  554. padding: 0;
  555. .operation-bar {
  556. font-size: 14px;
  557. display: flex;
  558. justify-content: space-between;
  559. height: 35px;
  560. padding: 0 10px;
  561. line-height: 35px;
  562. z-index: 9;
  563. i {
  564. margin-right: 10px;
  565. color: #409eff;
  566. cursor: pointer;
  567. }
  568. }
  569. }
  570. }
  571. .welcome-container {
  572. text-align: center;
  573. height: 500px;
  574. color: #c0c4cc;
  575. /deep/ .el-button {
  576. margin-top: 200px;
  577. margin-bottom: 25px;
  578. }
  579. }
  580. </style>