18c06a5ec56e1b0cf0e097c2f0410bf3198ad8c9.svn-base 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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.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. }
  377. })
  378. },
  379. // linkChart(chart) {
  380. // const data = {
  381. // chart_id: chart.chart_id,
  382. // dashboard_id: this.dashboard.dashboard_id
  383. // }
  384. // addChartToDB(data).then(resp => {
  385. // this.showChartList = false
  386. // this.getList()
  387. // this.$message({
  388. // type: 'success',
  389. // message: this.$t('common.saveSuccess')
  390. // })
  391. // })
  392. // },
  393. linkChart(chart) {
  394. const data = {
  395. name: 'insertdashboard_chart',
  396. parammaps: {
  397. cid: chart.chart_id,
  398. did: this.dashboard.dashboard_id,
  399. pastureid: Cookies.get('pastureid')
  400. }
  401. }
  402. PostDataByName(data).then(response => {
  403. console.log('新增保存发送参数', data)
  404. this.showChartList = false
  405. this.getList()
  406. if (data.msg == 'fail') {
  407. this.$message({
  408. type: 'fail',
  409. message: '添加失败!'
  410. })
  411. } else {
  412. this.$message({
  413. type: 'success',
  414. message: this.$t('common.saveSuccess')
  415. })
  416. }
  417. this.$router.push({ name: 'Addboard', params: { id: '~' + this.dashboard.dashboard_id } })
  418. console.log('Addboard~' + this.dashboard.dashboard_id)
  419. this.$emit('fatherMethod')
  420. })
  421. },
  422. isExisted(chart) {
  423. return this.charts.findIndex(item => item.chart_id === chart.chart_id) >= 0
  424. },
  425. // handleEdit(chart) {
  426. // this.$router.push(`/chartpanel/${chart.chart_id}`)
  427. // },
  428. handleEdit(chart) {
  429. this.$router.push({ name: 'ChartPanel', params: { id: chart.chart_id, isEdit: 'edit' } })
  430. localStorage.setItem("ChartPanelIsEditId", chart.chart_id)
  431. },
  432. // handleDelete(chart) {
  433. // this.$confirm(this.$t('dashboard.removeChartConfirm'), this.$t('common.confirm'), {
  434. // type: 'warning'
  435. // }).then(() => {
  436. // // deleteChart(index)
  437. // const deleteChartIndex = this.layout.findIndex(item => item.id === chart.chart_id)
  438. // const layout = JSON.parse(JSON.stringify(this.layout))
  439. // layout.splice(deleteChartIndex, 1)
  440. // this.dashboard.content.layout = layout
  441. // const data = {
  442. // chart_id: chart.chart_id,
  443. // dashboard_id: this.dashboard.dashboard_id
  444. // }
  445. // Promise.all([updateDashboard(this.dashboard), unMapChartDb(data)]).then(resp => {
  446. // this.getList()
  447. // this.$message({
  448. // type: 'success',
  449. // message: this.$t('common.deleteSuccess')
  450. // })
  451. // })
  452. // })
  453. // },
  454. handleDelete(chart) {
  455. this.$confirm(this.$t('dashboard.removeChartConfirm'), this.$t('common.confirm'), {
  456. type: 'warning'
  457. }).then(() => {
  458. // deleteChart(index)
  459. const deleteChartIndex = this.layout.findIndex(item => item.id === chart.chart_id)
  460. const layout = JSON.parse(JSON.stringify(this.layout))
  461. layout.splice(deleteChartIndex, 1)
  462. this.dashboard.content.layout = layout
  463. const data = {
  464. name: 'deletedashboard_chart',
  465. parammaps: {
  466. cid: chart.chart_id,
  467. did: this.dashboard.dashboard_id,
  468. pastureid: Cookies.get('pastureid')
  469. }
  470. }
  471. PostDataByName(data).then(response => {
  472. console.log('删除发送参数', data)
  473. this.$message({
  474. type: 'success',
  475. message: this.$t('common.deleteSuccess')
  476. })
  477. this.getList()
  478. })
  479. })
  480. },
  481. // 拖拽
  482. // handleLayoutChange() {
  483. // if (this.mode === 'view') return
  484. // this.dashboard.content = this.dashboard.content || {}
  485. // this.dashboard.content.layout = this.layout
  486. // // updateDashboard(this.dashboard)
  487. // },
  488. handleLayoutChange() {
  489. console.log('this.mode:', this.mode)
  490. if (this.mode === 'view') return
  491. console.log('this.dashboard1', this.dashboard)
  492. this.dashboard.content = this.dashboard.content || {}
  493. this.dashboard.content.layout = this.layout
  494. console.log('this.dashboard2', this.dashboard)
  495. var send_data = { dashboard_id: this.dashboard.dashboard_id, content: this.dashboard.content, pastureid: Cookies.get('pastureid') }
  496. updateDashboardJiade(send_data).then(response => {
  497. console.log('拖拽位置的数据', response.data.list)
  498. // if (response.data !== null) {
  499. // this.loading = false
  500. // this.result = response.data
  501. // } else {
  502. // this.loading = false
  503. // this.result = []
  504. // }
  505. })
  506. },
  507. handleResize(i, newH, newW, newHPx, newWPx) {
  508. this.$refs[`chartInstance${i}`][0].$children[0].$emit('resized')
  509. },
  510. exeSql(sqlSentence, item, index) {
  511. this.$set(this.chartLoading, item.chart_id, true)
  512. if (!sqlSentence) {
  513. this.$message.warning(this.$t('dashboard.chartQueryException', item.chart_name))
  514. this.$set(this.chartLoading, item.chart_id, false)
  515. return
  516. }
  517. exeSql().fetch({ source_id: item.source_id, sql: sqlSentence }).then(resp => {
  518. this.$set(this.chartLoading, item.chart_id, false)
  519. this.$set(this.results, item.chart_id, resp.data)
  520. }).catch(() => {
  521. this.$set(this.chartLoading, item.chart_id, false)
  522. })
  523. }
  524. }
  525. }
  526. </script>
  527. <style lang="scss" scoped>
  528. .back-button {
  529. display: inline-block;
  530. padding-right: 10px;
  531. margin-right: 10px;
  532. border-right: 1px solid #909090;
  533. cursor: pointer;
  534. span {
  535. padding: 5px;
  536. font-size: 14px;
  537. }
  538. }
  539. .tool-bar {
  540. display: flex;
  541. justify-content: space-between;
  542. border-top: none;
  543. height: 45px;
  544. line-height: 45px;
  545. color: #303133;
  546. padding: 0 10px;
  547. position: relative;
  548. .db-name {
  549. font-size: 1.2em;
  550. font-weight: 600;
  551. color: #909399;
  552. margin-left: 0;
  553. }
  554. span {
  555. color: #c0c4cc;
  556. font-size: 0.8em;
  557. margin-left: 10px;
  558. }
  559. }
  560. .visualize-card {
  561. /deep/ .el-card__header {
  562. padding: 0;
  563. .operation-bar {
  564. font-size: 14px;
  565. display: flex;
  566. justify-content: space-between;
  567. height: 35px;
  568. padding: 0 10px;
  569. line-height: 35px;
  570. z-index: 9;
  571. i {
  572. margin-right: 10px;
  573. color: #409eff;
  574. cursor: pointer;
  575. }
  576. }
  577. }
  578. }
  579. .welcome-container {
  580. text-align: center;
  581. height: 500px;
  582. color: #c0c4cc;
  583. /deep/ .el-button {
  584. margin-top: 200px;
  585. margin-bottom: 25px;
  586. }
  587. }
  588. </style>