TableEcharts.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <template>
  2. <div>
  3. <el-dialog v-model="visible" :before-close="handleClose" center style="width: 700px;">
  4. <el-card>
  5. <template #header>
  6. <div v-if="isShowAll === false" style="text-align: center;">
  7. <el-button type="text" @click="currentDayChart">今日</el-button>
  8. <el-button type="text" @click="currentMonthChart">本月</el-button>
  9. <el-button type="text" @click="allDatasChart">累计</el-button>
  10. </div>
  11. <div v-else>
  12. <el-form inline ref="ruleFormRef" :model="form" style="width:600px">
  13. <el-form-item style="margin-top: 20px;">
  14. <el-select v-model="form.workspace" placeholder="请选择车间"
  15. style="width: 140px ;text-align: left;">
  16. <el-option v-for="item in BoardDeptList" :key="item.value" :value="item.value"
  17. :label="item.label"></el-option>
  18. </el-select>
  19. </el-form-item>
  20. <el-button style="text-align: left;width: 100px;" type="primary"
  21. @click="getDepartmentData">生成柱状图</el-button>
  22. <el-button style="text-align: left; margin-left: 180px;" type="text"
  23. @click="currentDayChart">今日</el-button>
  24. <el-button style="text-align: left;" type="text" @click="currentMonthChart">本月</el-button>
  25. <el-button style="text-align: left;" type="text" @click="allDatasChart">累计</el-button>
  26. </el-form>
  27. </div>
  28. </template>
  29. <div id="container" style="width: 600px; height: 470px;margin-left: 20px;"></div>
  30. </el-card>
  31. </el-dialog>
  32. </div>
  33. </template>
  34. <script setup lang="ts">
  35. import * as echarts from 'echarts';
  36. import { ref, markRaw } from 'vue';
  37. import { ElMessageBox } from 'element-plus';
  38. import { BoardDeptList } from './config'
  39. import { watch } from 'vue';
  40. import { onMounted } from 'vue';
  41. import { VisitsModel } from '@/api/datamanagement/dataplatform';
  42. interface DataOption {
  43. title: {},//标题
  44. tooltip: {},//虚线
  45. calculate: Boolean,
  46. xAxis: {},
  47. yAxis: {},
  48. series: SerialModel[]
  49. }
  50. const visible = ref(false);
  51. const userId = ref(1);
  52. const isShowAll = ref(false);//控制标题是否居中
  53. const chart = ref<any>("");
  54. watch(
  55. () => props.dialogVisible,
  56. (newvisible) => {
  57. visible.value = newvisible
  58. console.log('tablechart', visible.value)
  59. },
  60. { immediate: true },
  61. )
  62. watch(
  63. () => props.userId,
  64. (newuserId) => {
  65. userId.value = newuserId
  66. console.log('tablechart-data:', userId.value)
  67. currentDayChart()
  68. },
  69. { immediate: true },
  70. )
  71. const form = ref({
  72. workspace: '',
  73. });
  74. const props = defineProps<{
  75. dialogVisible: boolean,
  76. userId: number,
  77. currentDayChart: (d: number) => Promise<VisitsModel[]>,
  78. currentMonthChart: (d: number) => Promise<VisitsModel[]>,
  79. allDatasChart: (d: number) => Promise<VisitsModel[]>,
  80. closeDialog(),
  81. // getDepartmentData: () => Promise<unknown>,
  82. }>();
  83. // 关闭对话框
  84. function closeDialog() {
  85. visible.value = false;
  86. props.closeDialog();
  87. // emit('closeDialog');
  88. }
  89. // 关闭对话框
  90. function handleClose(done: () => void) {
  91. ElMessageBox.confirm('确认关闭?')
  92. .then(() => {
  93. done()
  94. closeDialog();//关闭对话框
  95. })
  96. .catch(() => {
  97. // catch error
  98. })
  99. }
  100. // 查看今日数据
  101. function currentDayChart() {
  102. isShowAll.value = false;
  103. console.log('table-chart-currentday');
  104. const daytitle = ref();
  105. const seriesdata = ref()
  106. props.currentDayChart(userId.value).then(res => {
  107. daytitle.value = getHorizontalTitle(res);
  108. seriesdata.value = getVorizontalData(res);
  109. console.log('查看今日数据', res);
  110. console.log('查看今日数据', daytitle);
  111. console.log('查看今日数据', seriesdata);
  112. initChart(optionday.value, daytitle.value, seriesdata.value)
  113. })
  114. };
  115. // 查看本月数据
  116. function currentMonthChart() {
  117. isShowAll.value = false;
  118. console.log('currentmonth:', userId.value);
  119. const monthtitle = ref();
  120. const monthseriesdata = ref()
  121. props.currentMonthChart(userId.value).then(res => {
  122. console.log('table-chart-currentmonth:', res);
  123. monthtitle.value = getHorizontalTitle(res);
  124. monthseriesdata.value = getVorizontalData(res);
  125. console.log('查看今日数据', res);
  126. console.log('查看今日数据', monthtitle);
  127. console.log('查看今日数据', monthseriesdata);
  128. initChart(optionmonth.value, monthtitle.value, monthseriesdata.value)
  129. })
  130. };
  131. // 查看累计数据
  132. function allDatasChart() {
  133. isShowAll.value = true;
  134. console.log('alldata');
  135. const alltitle = ref();
  136. const allseriesdata = ref()
  137. props.allDatasChart(userId.value).then(res => {
  138. console.log(res);
  139. alltitle.value = getHorizontalTitle(res);
  140. allseriesdata.value = getVorizontalData(res);
  141. console.log('查看今日数据', res);
  142. console.log('查看今日数据', alltitle);
  143. console.log('查看今日数据', allseriesdata);
  144. initChart(optionall.value, alltitle.value, allseriesdata.value)
  145. })
  146. }
  147. interface SerialModel {
  148. data: [],
  149. type: string,
  150. }
  151. function initChart(data: DataOption, title: [], series: []) {
  152. data.xAxis = {};
  153. const dataseries = ref<SerialModel[]>([]);
  154. const type = ref('category');
  155. const horidata = ref([]) //todo,获取横坐标
  156. const nameLocation = ref('center');
  157. const axisLabel = { interval: 0, rotate: 30 };
  158. const nameTextStyle = {
  159. color: 'red',
  160. fontSize: 6,
  161. };
  162. horidata.value = title;
  163. data.xAxis = { type: type.value, data: horidata.value, nameLocation: nameLocation.value, axisLabel: axisLabel, nameTextStyle: nameTextStyle }
  164. const seriesdata = ref<SerialModel>({
  165. data: [],//todo,获取数值,
  166. type: 'bar'
  167. })
  168. seriesdata.value.data = series;
  169. dataseries.value.push(seriesdata.value)
  170. data.series = dataseries.value;
  171. console.log('data.xAxis.data', data.xAxis);
  172. console.log('data.yAxis.data', data.series)
  173. console.log(data)
  174. createChart(data);
  175. }
  176. // 创建图表
  177. function createChart(option: DataOption) {
  178. chart.value = markRaw(echarts.init(document.getElementById('container') as HTMLDivElement))
  179. chart.value.setOption(option);
  180. // 大小自适应
  181. window.addEventListener('resize', () => {
  182. chart.value.resize();
  183. })
  184. }
  185. // 创建柱状图
  186. function getDepartmentData() {
  187. // todo,原型未定义
  188. }
  189. function getHorizontalTitle(data: VisitsModel[]) {
  190. console.log('getHorizontalTitle:', data)
  191. const title = ref<string[]>([]);
  192. for (var i = 0; i < data.length; i++) {
  193. title.value.push(data[i].workshopName)
  194. console.log('getHorizontalTitle-data-i', data[i].workshopName)
  195. }
  196. console.log('title:', title);
  197. return title;
  198. }
  199. function getVorizontalData(data: VisitsModel[]) {
  200. console.log('getvorizontaldata:', data)
  201. const seriesData = ref<number[]>([]);
  202. for (var i = 0; i < data.length; i++) {
  203. seriesData.value.push(data[i].visits)
  204. console.log('getHorizontalTitle-data-i', data[i].visits)
  205. }
  206. console.log('seriesData:', seriesData);
  207. return seriesData;
  208. }
  209. const optionday = ref();
  210. optionday.value = {
  211. title: {
  212. text: '各车间地点访问次数柱状图(天)',
  213. x: "center", //设置标题位置居中
  214. textStyle: {//设置主标题的文字风格
  215. fontSize: 10 //文字大小
  216. },
  217. },//标题
  218. tooltip: {
  219. trigger: 'axis'
  220. },//虚线
  221. calculate: true,//显示数据
  222. xAxis: {
  223. type: 'category',
  224. data: [],//todo,获取横坐标
  225. nameLocation: 'center',
  226. axisLabel: { interval: 0, rotate: 30 },
  227. nameTextStyle: {
  228. color: 'red',
  229. fontSize: 6,
  230. }
  231. },
  232. yAxis: {
  233. type: 'value',
  234. name: '访问次数',
  235. nameTextStyle: {
  236. color: 'black',
  237. fontSize: 8,
  238. padding: 0,
  239. }
  240. },
  241. series: [
  242. {
  243. data: [],//todo,获取数值,
  244. type: 'bar'
  245. }
  246. ]
  247. };
  248. const optionmonth = ref();
  249. optionmonth.value = {
  250. title: {
  251. text: '各车间地点访问次数柱状图(月)',
  252. x: "center", //设置标题位置居中
  253. textStyle: {//设置主标题的文字风格
  254. fontSize: 10 //文字大小
  255. },
  256. },//标题
  257. tooltip: {
  258. trigger: 'axis'
  259. },//虚线
  260. calculate: true,//显示数据
  261. xAxis: {
  262. type: 'category',
  263. data: [],//todo,获取横坐标
  264. nameLocation: 'center',
  265. axisLabel: { interval: 0, rotate: 30 },
  266. nameTextStyle: {
  267. color: 'red',
  268. fontSize: 6,
  269. }
  270. },
  271. yAxis: {
  272. type: 'value',
  273. name: '访问次数',
  274. nameTextStyle: {
  275. color: 'black',
  276. fontSize: 8,
  277. padding: 0,
  278. }
  279. },
  280. series: [
  281. {
  282. data: [],//todo,获取数值,
  283. type: 'bar'
  284. }
  285. ]
  286. };
  287. const optionall = ref();
  288. optionall.value = {
  289. title: {
  290. text: '各车间地点访问次数柱状图(汇总)',
  291. x: "center", //设置标题位置居中
  292. textStyle: {//设置主标题的文字风格
  293. fontSize: 10 //文字大小
  294. },
  295. },//标题
  296. tooltip: {
  297. trigger: 'axis'
  298. },//虚线
  299. calculate: true,//显示数据
  300. xAxis: {
  301. type: 'category',
  302. data: [],//todo,获取横坐标
  303. nameLocation: 'center',
  304. axisLabel: { interval: 0, rotate: 30 },
  305. nameTextStyle: {
  306. color: 'red',
  307. fontSize: 6,
  308. }
  309. },
  310. yAxis: {
  311. type: 'value',
  312. name: '访问次数',
  313. nameTextStyle: {
  314. color: 'black',
  315. fontSize: 8,
  316. padding: 0,
  317. }
  318. },
  319. series: [
  320. {
  321. data: [],//todo,获取数值,
  322. type: 'bar'
  323. }
  324. ]
  325. };
  326. onMounted(() => {
  327. isShowAll.value = false;
  328. })
  329. </script>
  330. <style scoped></style>