widgetBarlinechart.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <template>
  2. <div :style="styleObj">
  3. <v-chart :options="options" autoresize/>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: "WidgetBarlinechart",
  9. components: {},
  10. props: {
  11. value: Object,
  12. ispreview: Boolean
  13. },
  14. data() {
  15. return {
  16. options: {
  17. color: [],
  18. grid: {},
  19. title: {
  20. text: "",
  21. textStyle: {
  22. color: "#fff"
  23. }
  24. },
  25. tooltip: {
  26. trigger: "item",
  27. formatter: "{a} <br/>{b} : {c}%"
  28. },
  29. legend: {
  30. textStyle: {
  31. color: "#fff"
  32. },
  33. },
  34. xAxis: [
  35. {
  36. type: "category",
  37. data: [],
  38. axisLabel: {
  39. show: false,
  40. textStyle: {
  41. color: "#fff"
  42. }
  43. }
  44. }
  45. ],
  46. yAxis: [
  47. {
  48. type: "value",
  49. name: "",
  50. min: 0,
  51. max: 250,
  52. interval: 50,
  53. axisLabel: {
  54. show: true,
  55. textStyle: {
  56. color: "#fff"
  57. }
  58. }
  59. },
  60. {
  61. type: "value",
  62. name: "",
  63. min: 0,
  64. max: 25,
  65. interval: 5,
  66. axisLabel: {
  67. show: true,
  68. textStyle: {
  69. color: "#fff"
  70. }
  71. }
  72. }
  73. ],
  74. series: [
  75. {
  76. name: "",
  77. type: "bar",
  78. yAxisIndex: 0,
  79. data: [],
  80. itemStyle: {
  81. barBorderRadius: null
  82. }
  83. },
  84. {
  85. name: "",
  86. type: "line",
  87. yAxisIndex: 1,
  88. data: [],
  89. itemStyle: {}
  90. }
  91. ]
  92. },
  93. optionsStyle: {}, // 样式
  94. optionsData: {}, // 数据
  95. optionsCollapse: {}, // 图标属性
  96. optionsSetup: {}
  97. };
  98. },
  99. computed: {
  100. styleObj() {
  101. return {
  102. position: this.ispreview ? "absolute" : "static",
  103. width: this.optionsStyle.width + "px",
  104. height: this.optionsStyle.height + "px",
  105. left: this.optionsStyle.left + "px",
  106. top: this.optionsStyle.top + "px",
  107. background: this.optionsSetup.background
  108. };
  109. }
  110. },
  111. watch: {
  112. value: {
  113. handler(val) {
  114. this.optionsStyle = val.position;
  115. this.optionsData = val.data;
  116. this.optionsCollapse = val.collapse;
  117. this.optionsSetup = val.setup;
  118. this.editorOptions();
  119. },
  120. deep: true
  121. }
  122. },
  123. created() {
  124. this.optionsStyle = this.value.position;
  125. this.optionsData = this.value.data;
  126. this.optionsCollapse = this.value.collapse;
  127. this.optionsSetup = this.value.setup;
  128. this.editorOptions();
  129. },
  130. methods: {
  131. // 修改图标options属性
  132. editorOptions() {
  133. this.setOptionsTitle();
  134. this.setOptionsX();
  135. this.setOptionsY();
  136. this.setOptionsLine();
  137. this.setOptionsBar();
  138. this.setOptionsTooltip();
  139. this.setOptionsData();
  140. this.setOptionsMargin();
  141. this.setOptionsLegend();
  142. this.setOptionsColor();
  143. },
  144. // 标题修改
  145. setOptionsTitle() {
  146. const optionsSetup = this.optionsSetup;
  147. const title = {};
  148. title.text = optionsSetup.titleText;
  149. title.show = optionsSetup.isNoTitle;
  150. title.left = optionsSetup.textAlign;
  151. title.textStyle = {
  152. color: optionsSetup.textColor,
  153. fontSize: optionsSetup.textFontSize,
  154. fontWeight: optionsSetup.textFontWeight,
  155. fontStyle: optionsSetup.textFontStyle,
  156. };
  157. title.subtext = optionsSetup.subText;
  158. title.subtextStyle = {
  159. color: optionsSetup.subTextColor,
  160. fontWeight: optionsSetup.subTextFontWeight,
  161. fontSize: optionsSetup.subTextFontSize,
  162. fontStyle: optionsSetup.subTextFontStyle,
  163. };
  164. this.options.title = title;
  165. },
  166. // X轴设置
  167. setOptionsX() {
  168. const optionsSetup = this.optionsSetup;
  169. const xAxis = {
  170. type: "category",
  171. // 坐标轴是否显示
  172. show: optionsSetup.hideX,
  173. // 坐标轴名称
  174. name: optionsSetup.nameX,
  175. nameTextStyle: {
  176. color: optionsSetup.nameColorX,
  177. fontSize: optionsSetup.nameFontSizeX
  178. },
  179. // 轴反转
  180. inverse: optionsSetup.reversalX,
  181. axisLabel: {
  182. show: true,
  183. // 文字间隔
  184. interval: optionsSetup.textInterval,
  185. // 文字角度
  186. rotate: optionsSetup.textAngleX,
  187. textStyle: {
  188. // 坐标文字颜色
  189. color: optionsSetup.colorX,
  190. fontSize: optionsSetup.fontSizeX
  191. }
  192. },
  193. axisLine: {
  194. show: true,
  195. lineStyle: {
  196. color: optionsSetup.lineColorX,
  197. width: optionsSetup.lineWidthX,
  198. }
  199. },
  200. splitLine: {
  201. show: optionsSetup.isShowSplitLineX,
  202. lineStyle: {
  203. color: optionsSetup.splitLineColorX,
  204. width: optionsSetup.splitLineWidthX,
  205. }
  206. }
  207. };
  208. this.options.xAxis = xAxis;
  209. },
  210. // Y轴设置
  211. setOptionsY() {
  212. const optionsSetup = this.optionsSetup;
  213. const yAxis = [
  214. {
  215. type: "value",
  216. // 均分
  217. splitNumber: optionsSetup.splitNumberLeft,
  218. // 坐标轴是否显示
  219. show: optionsSetup.isShowYLeft,
  220. // 坐标轴名称
  221. name: optionsSetup.textNameYLeft,
  222. // 别名
  223. nameTextStyle: {
  224. color: optionsSetup.nameColorYLeft,
  225. fontSize: optionsSetup.nameFontSizeYLeft
  226. },
  227. axisLabel: {
  228. show: true,
  229. // 文字角度
  230. rotate: optionsSetup.textAngleYLeft,
  231. textStyle: {
  232. // 坐标文字颜色
  233. color: optionsSetup.colorYLeft,
  234. fontSize: optionsSetup.fontSizeYLeft
  235. }
  236. },
  237. axisTick: { // 刻度
  238. show: optionsSetup.tickLineYLeft,
  239. },
  240. axisLine: {
  241. show: optionsSetup.lineYLeft,
  242. lineStyle: {
  243. width: optionsSetup.lineWidthYLeft,
  244. color: optionsSetup.lineColorYLeft,
  245. }
  246. },
  247. splitLine: {
  248. show: optionsSetup.isShowSplitLineYLeft,
  249. lineStyle: {
  250. color: optionsSetup.splitLineColorYLeft,
  251. width: optionsSetup.splitLineFontWidthYLeft,
  252. }
  253. }
  254. },
  255. {
  256. type: "value",
  257. // 均分
  258. splitNumber: optionsSetup.splitNumberRight,
  259. // 坐标轴是否显示
  260. show: optionsSetup.isShowYRight,
  261. // 坐标轴名称
  262. name: optionsSetup.textNameYRight,
  263. // 别名
  264. nameTextStyle: {
  265. color: optionsSetup.nameColorYRight,
  266. fontSize: optionsSetup.nameFontSizeYRight
  267. },
  268. axisLabel: {
  269. show: true,
  270. // 文字角度
  271. rotate: optionsSetup.textAngleYRight,
  272. textStyle: {
  273. // 坐标文字颜色
  274. color: optionsSetup.colorYRight,
  275. fontSize: optionsSetup.fontSizeYRight
  276. }
  277. },
  278. axisTick: { // 刻度
  279. show: optionsSetup.tickLineYRight,
  280. },
  281. axisLine: {
  282. show: optionsSetup.lineYRight,
  283. lineStyle: {
  284. width: optionsSetup.lineWidthYRight,
  285. color: optionsSetup.lineColorYRight,
  286. }
  287. },
  288. splitLine: {
  289. show: optionsSetup.isShowSplitLineYRight,
  290. lineStyle: {
  291. color: optionsSetup.splitLineColorYRight,
  292. width: optionsSetup.splitLineFontWidthYRight,
  293. }
  294. }
  295. }
  296. ];
  297. this.options.yAxis = yAxis;
  298. },
  299. // 折线设置 数值设置
  300. setOptionsLine() {
  301. const optionsSetup = this.optionsSetup;
  302. const series = this.options.series;
  303. for (const key in series) {
  304. if (series[key].type == "line") {
  305. series[key].symbol = optionsSetup.symbol;
  306. series[key].showSymbol = optionsSetup.markPoint;
  307. series[key].symbolSize = optionsSetup.pointSize;
  308. series[key].smooth = optionsSetup.smoothCurve;
  309. if (optionsSetup.area) {
  310. series[key].areaStyle = {
  311. opacity: optionsSetup.areaThickness / 100
  312. };
  313. } else {
  314. series[key].areaStyle = {
  315. opacity: 0
  316. };
  317. }
  318. series[key].lineStyle = {
  319. width: optionsSetup.lineWidth
  320. };
  321. series[key].itemStyle.borderRadius = optionsSetup.radius;
  322. series[key].label = {
  323. show: optionsSetup.isShowLine,
  324. position: "top",
  325. distance: optionsSetup.distanceLine,
  326. fontSize: optionsSetup.fontSizeLine,
  327. color: optionsSetup.subTextColorLine,
  328. fontWeight: optionsSetup.fontWeightLine
  329. };
  330. }
  331. }
  332. this.options.series = series;
  333. },
  334. // 柱体设置 数值设置
  335. setOptionsBar() {
  336. const optionsSetup = this.optionsSetup;
  337. const series = this.options.series;
  338. for (const key in series) {
  339. if (series[key].type == "bar") {
  340. series[key].label = {
  341. show: optionsSetup.isShowBar,
  342. position: "top",
  343. distance: optionsSetup.distanceBar,
  344. fontSize: optionsSetup.fontSizeBar,
  345. color: optionsSetup.subTextColorBar,
  346. fontWeight: optionsSetup.fontWeightBar
  347. };
  348. series[key].barWidth = optionsSetup.maxWidth;
  349. series[key].barMinHeight = optionsSetup.minHeight;
  350. series[key].itemStyle.barBorderRadius = optionsSetup.radius;
  351. }
  352. }
  353. this.options.series = series;
  354. },
  355. // tooltip 设置
  356. setOptionsTooltip() {
  357. const optionsSetup = this.optionsSetup;
  358. const tooltip = {
  359. trigger: "item",
  360. show: true,
  361. textStyle: {
  362. color: optionsSetup.tipsColor,
  363. fontSize: optionsSetup.tipsFontSize
  364. }
  365. };
  366. this.options.tooltip = tooltip;
  367. },
  368. // 边距设置
  369. setOptionsMargin() {
  370. const optionsSetup = this.optionsSetup;
  371. const grid = {
  372. left: optionsSetup.marginLeft,
  373. right: optionsSetup.marginRight,
  374. bottom: optionsSetup.marginBottom,
  375. top: optionsSetup.marginTop,
  376. containLabel: true
  377. };
  378. this.options.grid = grid;
  379. },
  380. setOptionsLegend() {
  381. const optionsSetup = this.optionsSetup;
  382. const legend = this.options.legend;
  383. legend.show = optionsSetup.isShowLegend;
  384. legend.left = optionsSetup.lateralPosition;
  385. legend.top = optionsSetup.longitudinalPosition;
  386. legend.bottom =
  387. optionsSetup.longitudinalPosition;
  388. legend.orient = optionsSetup.layoutFront;
  389. legend.textStyle = {
  390. color: optionsSetup.legendColor,
  391. fontSize: optionsSetup.legendFontSize
  392. };
  393. legend.itemWidth = optionsSetup.legendWidth;
  394. },
  395. // 图例名称设置
  396. setOptionsLegendName(name){
  397. const optionsSetup = this.optionsSetup;
  398. const series = this.options.series;
  399. const legendName = optionsSetup.legendName;
  400. // 图例没有手动写则显示原值,写了则显示新值
  401. if (null == legendName || legendName == '') {
  402. for (let i = 0; i < name.length; i++) {
  403. series[i].name = name[i];
  404. }
  405. this.options.legend['data'] = name;
  406. }else {
  407. const arr = legendName.split('|');
  408. for (let i = 0; i < arr.length; i++) {
  409. series[i].name = arr[i];
  410. }
  411. this.options.legend['data'] = arr
  412. }
  413. },
  414. // 图例颜色修改
  415. setOptionsColor() {
  416. const optionsSetup = this.optionsSetup;
  417. const customColor = optionsSetup.customColor;
  418. if (!customColor) return;
  419. const arrColor = [];
  420. for (let i = 0; i < customColor.length; i++) {
  421. arrColor.push(customColor[i].color);
  422. }
  423. this.options.color = arrColor;
  424. this.options = Object.assign({}, this.options);
  425. },
  426. // 数据处理
  427. setOptionsData() {
  428. const optionsData = this.optionsData; // 数据类型 静态 or 动态
  429. optionsData.dataType == "staticData"
  430. ? this.staticDataFn(optionsData.staticData)
  431. : this.dynamicDataFn(optionsData.dynamicData, optionsData.refreshTime);
  432. },
  433. staticDataFn(val) {
  434. const series = this.options.series;
  435. let axis = [];
  436. let bar = [];
  437. let line = [];
  438. for (const i in val) {
  439. axis[i] = val[i].axis;
  440. bar[i] = val[i].bar;
  441. line[i] = val[i].line;
  442. }
  443. // x轴
  444. this.options.xAxis.data = axis;
  445. // series
  446. for (const i in series) {
  447. if (series[i].type == "bar") {
  448. series[i].data = bar;
  449. } else {
  450. series[i].data = line;
  451. }
  452. }
  453. const legendName = [];
  454. legendName.push('bar');
  455. legendName.push('line');
  456. this.options.legend['data'] = legendName;
  457. this.setOptionsLegendName(legendName);
  458. },
  459. dynamicDataFn(val, refreshTime) {
  460. if (!val) return;
  461. if (this.ispreview) {
  462. this.getEchartData(val);
  463. this.flagInter = setInterval(() => {
  464. this.getEchartData(val);
  465. }, refreshTime);
  466. } else {
  467. this.getEchartData(val);
  468. }
  469. },
  470. getEchartData(val) {
  471. const data = this.queryEchartsData(val);
  472. data.then(res => {
  473. this.renderingFn(res);
  474. });
  475. },
  476. renderingFn(val) {
  477. this.options.xAxis.data = val.xAxis;
  478. // series
  479. const series = this.options.series;
  480. const legendName = [];
  481. for (const i in series) {
  482. for (const j in val.series) {
  483. if (series[i].type == val.series[j].type) {
  484. series[i].data = val.series[j].data;
  485. }
  486. }
  487. legendName.push(val.series[i].name);
  488. }
  489. this.options.legend['data'] = legendName;
  490. this.setOptionsLegendName(legendName);
  491. }
  492. }
  493. };
  494. </script>
  495. <style scoped lang="scss">
  496. .echarts {
  497. width: 100%;
  498. height: 100%;
  499. overflow: hidden;
  500. }
  501. </style>