65d63db2202ce46edab1304e5c54581ba46d4270.svn-base 21 KB

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