widgetBarchart.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <template>
  2. <div :style="styleObj">
  3. <v-chart :options="options" autoresize/>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: "WidgetBarchart",
  9. components: {},
  10. props: {
  11. value: Object,
  12. ispreview: Boolean
  13. },
  14. data() {
  15. return {
  16. options: {
  17. grid: {},
  18. legend: {
  19. textStyle: {
  20. color: "#fff"
  21. }
  22. },
  23. xAxis: {
  24. type: "category",
  25. data: [],
  26. axisLabel: {
  27. show: true,
  28. textStyle: {
  29. color: "#fff"
  30. }
  31. }
  32. },
  33. yAxis: {
  34. type: "value",
  35. data: [],
  36. axisLabel: {
  37. show: true,
  38. textStyle: {
  39. color: "#fff"
  40. }
  41. }
  42. },
  43. series: [
  44. {
  45. data: [],
  46. type: "bar",
  47. barGap: "0%",
  48. itemStyle: {
  49. borderRadius: null
  50. }
  51. }
  52. ]
  53. },
  54. optionsStyle: {}, // 样式
  55. optionsData: {}, // 数据
  56. optionsSetup: {},
  57. flagInter: null
  58. };
  59. },
  60. computed: {
  61. styleObj() {
  62. return {
  63. position: this.ispreview ? "absolute" : "static",
  64. width: this.optionsStyle.width + "px",
  65. height: this.optionsStyle.height + "px",
  66. left: this.optionsStyle.left + "px",
  67. top: this.optionsStyle.top + "px",
  68. background: this.optionsSetup.background
  69. };
  70. }
  71. },
  72. watch: {
  73. value: {
  74. handler(val) {
  75. this.optionsStyle = val.position;
  76. this.optionsData = val.data;
  77. this.optionsCollapse = val.setup;
  78. this.optionsSetup = val.setup;
  79. this.editorOptions();
  80. },
  81. deep: true
  82. }
  83. },
  84. mounted() {
  85. this.optionsStyle = this.value.position;
  86. this.optionsData = this.value.data;
  87. this.optionsCollapse = this.value.setup;
  88. this.optionsSetup = this.value.setup;
  89. this.editorOptions();
  90. },
  91. methods: {
  92. // 修改图标options属性
  93. editorOptions() {
  94. this.setOptionsTitle();
  95. this.setOptionsX();
  96. this.setOptionsY();
  97. this.setOptionsTop();
  98. this.setOptionsTooltip();
  99. this.setOptionsMargin();
  100. this.setOptionsLegend();
  101. this.setOptionsColor();
  102. this.setOptionsData();
  103. },
  104. // 标题修改
  105. setOptionsTitle() {
  106. const optionsSetup = this.optionsSetup;
  107. const title = {};
  108. title.text = optionsSetup.titleText;
  109. title.show = optionsSetup.isNoTitle;
  110. title.left = optionsSetup.textAlign;
  111. title.textStyle = {
  112. color: optionsSetup.textColor,
  113. fontSize: optionsSetup.textFontSize,
  114. fontWeight: optionsSetup.textFontWeight
  115. };
  116. title.subtext = optionsSetup.subText;
  117. title.subtextStyle = {
  118. color: optionsSetup.subTextColor,
  119. fontWeight: optionsSetup.subTextFontWeight,
  120. fontSize: optionsSetup.subTextFontSize
  121. };
  122. this.options.title = title;
  123. },
  124. // X轴设置
  125. setOptionsX() {
  126. const optionsSetup = this.optionsSetup;
  127. const xAxis = {
  128. type: "category",
  129. show: optionsSetup.hideX, // 坐标轴是否显示
  130. name: optionsSetup.xName, // 坐标轴名称
  131. nameTextStyle: {
  132. color: optionsSetup.xNameColor,
  133. fontSize: optionsSetup.xNameFontSize
  134. },
  135. nameRotate: optionsSetup.textAngle, // 文字角度
  136. inverse: optionsSetup.reversalX, // 轴反转
  137. axisLabel: {
  138. show: true,
  139. interval: optionsSetup.textInterval, // 文字角度
  140. rotate: optionsSetup.textAngle, // 文字角度
  141. textStyle: {
  142. color: optionsSetup.Xcolor, // x轴 坐标文字颜色
  143. fontSize: optionsSetup.fontSizeX
  144. }
  145. },
  146. axisLine: {
  147. show: true,
  148. lineStyle: {
  149. color: optionsSetup.lineColorX
  150. }
  151. },
  152. splitLine: {
  153. show: optionsSetup.isShowSplitLineX,
  154. lineStyle: {
  155. color: optionsSetup.splitLineColorX
  156. }
  157. }
  158. };
  159. this.options.xAxis = xAxis;
  160. },
  161. // Y轴设置
  162. setOptionsY() {
  163. const optionsSetup = this.optionsSetup;
  164. const yAxis = {
  165. type: "value",
  166. scale: optionsSetup.scale,
  167. splitNumber: optionsSetup.splitNumber,// 均分
  168. show: optionsSetup.isShowY, // 坐标轴是否显示
  169. name: optionsSetup.textNameY, // 坐标轴名称
  170. nameTextStyle: {
  171. color: optionsSetup.nameColorY,
  172. fontSize: optionsSetup.nameFontSizeY
  173. },
  174. inverse: optionsSetup.reversalY, // 轴反转
  175. axisLabel: {
  176. show: true,
  177. rotate: optionsSetup.ytextAngle, // 文字角度
  178. textStyle: {
  179. color: optionsSetup.colorY, // x轴 坐标文字颜色
  180. fontSize: optionsSetup.fontSizeY
  181. }
  182. },
  183. axisLine: {
  184. show: true,
  185. lineStyle: {
  186. color: optionsSetup.lineColorY
  187. }
  188. },
  189. splitLine: {
  190. show: optionsSetup.isShowSplitLineY,
  191. lineStyle: {
  192. color: optionsSetup.splitLineColorY
  193. }
  194. }
  195. };
  196. this.options.yAxis = yAxis;
  197. },
  198. // 数值设定 or 柱体设置
  199. setOptionsTop() {
  200. const optionsSetup = this.optionsSetup;
  201. const series = this.options.series;
  202. if (series[0].type == "bar") {
  203. if (optionsSetup.verticalShow) {
  204. series[0].label = {
  205. show: optionsSetup.isShow,
  206. position: 'right',
  207. distance: optionsSetup.distance,
  208. textStyle: {
  209. fontSize: optionsSetup.fontSize,
  210. color: optionsSetup.subTextColor,
  211. fontWeight: optionsSetup.fontWeight
  212. }
  213. }
  214. } else {
  215. series[0].label = {
  216. show: optionsSetup.isShow,
  217. position: "top",
  218. distance: optionsSetup.distance,
  219. fontSize: optionsSetup.fontSize,
  220. color: optionsSetup.subTextColor,
  221. fontWeight: optionsSetup.fontWeight
  222. }
  223. }
  224. }
  225. series[0].barWidth = optionsSetup.maxWidth;
  226. series[0].barMinHeight = optionsSetup.minHeight;
  227. },
  228. // tooltip 设置
  229. setOptionsTooltip() {
  230. const optionsSetup = this.optionsSetup;
  231. const tooltip = {
  232. trigger: "item",
  233. show: true,
  234. textStyle: {
  235. color: optionsSetup.lineColor,
  236. fontSize: optionsSetup.tipFontSize
  237. }
  238. };
  239. this.options.tooltip = tooltip;
  240. },
  241. // 边距设置
  242. setOptionsMargin() {
  243. const optionsSetup = this.optionsSetup;
  244. const grid = {
  245. left: optionsSetup.marginLeft,
  246. right: optionsSetup.marginRight,
  247. bottom: optionsSetup.marginBottom,
  248. top: optionsSetup.marginTop,
  249. containLabel: true
  250. };
  251. this.options.grid = grid;
  252. },
  253. // 图例操作 legend
  254. setOptionsLegend() {
  255. const optionsSetup = this.optionsSetup;
  256. const legend = this.options.legend;
  257. legend.show = optionsSetup.isShowLegend;
  258. legend.left = optionsSetup.lateralPosition == "left" ? 0 : "auto";
  259. legend.right = optionsSetup.lateralPosition == "right" ? 0 : "auto";
  260. legend.top = optionsSetup.longitudinalPosition == "top" ? 0 : "auto";
  261. legend.bottom =
  262. optionsSetup.longitudinalPosition == "bottom" ? 0 : "auto";
  263. legend.orient = optionsSetup.layoutFront;
  264. legend.textStyle = {
  265. color: optionsSetup.lengedColor,
  266. fontSize: optionsSetup.fontSize
  267. };
  268. legend.itemWidth = optionsSetup.lengedWidth;
  269. },
  270. // 图例颜色修改
  271. setOptionsColor() {
  272. const optionsSetup = this.optionsSetup;
  273. const customColor = optionsSetup.customColor;
  274. if (!customColor) return;
  275. const arrColor = [];
  276. for (let i = 0; i < customColor.length; i++) {
  277. arrColor.push(customColor[i].color);
  278. }
  279. const itemStyle = {
  280. normal: {
  281. color: params => {
  282. return arrColor[params.dataIndex];
  283. },
  284. barBorderRadius: optionsSetup.radius
  285. }
  286. };
  287. for (const key in this.options.series) {
  288. if (this.options.series[key].type == "bar") {
  289. this.options.series[key].itemStyle = itemStyle;
  290. }
  291. }
  292. this.options = Object.assign({}, this.options);
  293. },
  294. // 数据解析
  295. setOptionsData() {
  296. const optionsSetup = this.optionsSetup;
  297. const optionsData = this.optionsData; // 数据类型 静态 or 动态
  298. optionsData.dataType == "staticData"
  299. ? this.staticDataFn(optionsData.staticData)
  300. : this.dynamicDataFn(
  301. optionsData.dynamicData,
  302. optionsData.refreshTime,
  303. optionsSetup
  304. );
  305. },
  306. // 静态数据
  307. staticDataFn(val) {
  308. const optionsSetup = this.optionsSetup;
  309. const series = this.options.series;
  310. let axis = [];
  311. let data = [];
  312. for (const i in val) {
  313. axis[i] = val[i].axis;
  314. data[i] = val[i].data
  315. }
  316. // x轴
  317. if (optionsSetup.verticalShow) {
  318. this.options.xAxis.data = [];
  319. this.options.yAxis.data = axis;
  320. this.options.xAxis.type = "value";
  321. this.options.yAxis.type = "category";
  322. } else {
  323. this.options.xAxis.data = axis;
  324. this.options.yAxis.data = [];
  325. this.options.xAxis.type = "category";
  326. this.options.yAxis.type = "value";
  327. }
  328. if (series[0].type == "bar") {
  329. series[0].data = data;
  330. }
  331. },
  332. // 动态数据
  333. dynamicDataFn(val, refreshTime, optionsSetup) {
  334. if (!val) return;
  335. if (this.ispreview) {
  336. this.getEchartData(val, optionsSetup);
  337. this.flagInter = setInterval(() => {
  338. this.getEchartData(val, optionsSetup);
  339. }, refreshTime);
  340. } else {
  341. this.getEchartData(val, optionsSetup);
  342. }
  343. },
  344. getEchartData(val, optionsSetup) {
  345. const data = this.queryEchartsData(val);
  346. data.then(res => {
  347. this.renderingFn(optionsSetup, res);
  348. });
  349. },
  350. renderingFn(optionsSetup, val) {
  351. // x轴
  352. if (optionsSetup.verticalShow) {
  353. this.options.xAxis.data = [];
  354. this.options.yAxis.data = val.xAxis;
  355. this.options.xAxis.type = "value";
  356. this.options.yAxis.type = "category";
  357. } else {
  358. this.options.xAxis.data = val.xAxis;
  359. this.options.yAxis.data = [];
  360. this.options.xAxis.type = "category";
  361. this.options.yAxis.type = "value";
  362. }
  363. // series
  364. const series = this.options.series;
  365. for (const i in series) {
  366. if (series[i].type == "bar") {
  367. series[i].data = val.series[i].data;
  368. }
  369. }
  370. }
  371. }
  372. };
  373. </script>
  374. <style scoped lang="scss">
  375. .echarts {
  376. width: 100%;
  377. height: 100%;
  378. overflow: hidden;
  379. }
  380. </style>