widgetLinechart.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <template>
  2. <div :style="styleObj">
  3. <v-chart :options="options" autoresize/>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: "WidgetLinechart",
  9. components: {},
  10. props: {
  11. value: Object,
  12. ispreview: Boolean
  13. },
  14. data() {
  15. return {
  16. options: {
  17. grid: {},
  18. color: [],
  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. type: "category",
  36. data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  37. axisLabel: {
  38. show: true,
  39. textStyle: {
  40. color: "#fff"
  41. }
  42. }
  43. },
  44. yAxis: {
  45. type: "value",
  46. axisLabel: {
  47. show: true,
  48. textStyle: {
  49. color: "#fff"
  50. }
  51. }
  52. },
  53. series: [
  54. {
  55. data: [],
  56. type: "line"
  57. }
  58. ]
  59. },
  60. optionsStyle: {}, // 样式
  61. optionsData: {}, // 数据
  62. optionsCollapse: {}, // 图标属性
  63. optionsSetup: {}
  64. };
  65. },
  66. computed: {
  67. styleObj() {
  68. return {
  69. position: this.ispreview ? "absolute" : "static",
  70. width: this.optionsStyle.width + "px",
  71. height: this.optionsStyle.height + "px",
  72. left: this.optionsStyle.left + "px",
  73. top: this.optionsStyle.top + "px",
  74. background: this.optionsSetup.background
  75. };
  76. }
  77. },
  78. watch: {
  79. value: {
  80. handler(val) {
  81. this.optionsStyle = val.position;
  82. this.optionsData = val.data;
  83. this.optionsCollapse = val.collapse;
  84. this.optionsSetup = val.setup;
  85. this.editorOptions();
  86. },
  87. deep: true
  88. }
  89. },
  90. created() {
  91. this.optionsStyle = this.value.position;
  92. this.optionsData = this.value.data;
  93. this.optionsCollapse = this.value.collapse;
  94. this.optionsSetup = this.value.setup;
  95. this.editorOptions();
  96. },
  97. methods: {
  98. // 修改图标options属性
  99. editorOptions() {
  100. this.setOptionsTitle();
  101. this.setOptionsX();
  102. this.setOptionsY();
  103. this.setOptionsTop();
  104. this.setOptionsTooltip();
  105. this.setOptionsData();
  106. this.setOptionsMargin();
  107. this.setOptionsColor();
  108. },
  109. // 标题修改
  110. setOptionsTitle() {
  111. const optionsSetup = this.optionsSetup;
  112. const title = {};
  113. title.text = optionsSetup.titleText;
  114. title.show = optionsSetup.isNoTitle;
  115. title.left = optionsSetup.textAlign;
  116. title.textStyle = {
  117. color: optionsSetup.textColor,
  118. fontSize: optionsSetup.textFontSize,
  119. fontWeight: optionsSetup.textFontWeight
  120. };
  121. title.subtext = optionsSetup.subText;
  122. title.subtextStyle = {
  123. color: optionsSetup.subTextColor,
  124. fontWeight: optionsSetup.subTextFontWeight,
  125. fontSize: optionsSetup.subTextFontSize
  126. };
  127. this.options.title = title;
  128. },
  129. // X轴设置
  130. setOptionsX() {
  131. const optionsSetup = this.optionsSetup;
  132. const xAxis = {
  133. type: "category",
  134. show: optionsSetup.hideX, // 坐标轴是否显示
  135. name: optionsSetup.xName, // 坐标轴名称
  136. nameTextStyle: {
  137. color: optionsSetup.nameColorX,
  138. fontSize: optionsSetup.nameFontSizeX
  139. },
  140. nameRotate: optionsSetup.textAngle, // 文字角度
  141. inverse: optionsSetup.reversalX, // 轴反转
  142. axisLabel: {
  143. show: true,
  144. interval: optionsSetup.textInterval, // 文字间隔
  145. rotate: optionsSetup.textAngle, // 文字角度
  146. textStyle: {
  147. color: optionsSetup.Xcolor, // x轴 坐标文字颜色
  148. fontSize: optionsSetup.fontSizeX
  149. }
  150. },
  151. axisLine: {
  152. show: true,
  153. lineStyle: {
  154. color: optionsSetup.lineColorX
  155. }
  156. },
  157. splitLine: {
  158. show: optionsSetup.isShowSplitLineX,
  159. lineStyle: {
  160. color: optionsSetup.splitLineColorX
  161. }
  162. }
  163. };
  164. this.options.xAxis = xAxis;
  165. },
  166. // Y轴设置
  167. setOptionsY() {
  168. const optionsSetup = this.optionsSetup;
  169. const yAxis = {
  170. type: "value",
  171. scale: optionsSetup.scale,
  172. splitNumber: optionsSetup.splitNumber,// 均分
  173. show: optionsSetup.isShowY, // 坐标轴是否显示
  174. name: optionsSetup.textNameY, // 坐标轴名称
  175. nameTextStyle: { // 别名
  176. color: optionsSetup.nameColorY,
  177. fontSize: optionsSetup.namefontSizeY
  178. },
  179. inverse: optionsSetup.reversalY, // 轴反转
  180. axisLabel: {
  181. show: true,
  182. rotate: optionsSetup.ytextAngle, // 文字角度
  183. textStyle: {
  184. color: optionsSetup.colorY, // y轴 坐标文字颜色
  185. fontSize: optionsSetup.fontSizeY
  186. }
  187. },
  188. axisLine: {
  189. show: true,
  190. lineStyle: {
  191. color: optionsSetup.lineColorY
  192. }
  193. },
  194. splitLine: {
  195. show: optionsSetup.isShowSplitLineY,
  196. lineStyle: {
  197. color: optionsSetup.splitLineColorY
  198. }
  199. }
  200. };
  201. this.options.yAxis = yAxis;
  202. },
  203. // 折线设置
  204. setOptionsTop() {
  205. const optionsSetup = this.optionsSetup;
  206. const series = this.options.series;
  207. for (const key in series) {
  208. if (series[key].type == "line") {
  209. series[key].showSymbol = optionsSetup.markPoint;
  210. series[key].symbolSize = optionsSetup.pointSize;
  211. series[key].smooth = optionsSetup.smoothCurve;
  212. if (optionsSetup.area) {
  213. series[key].areaStyle = {
  214. opacity: optionsSetup.areaThickness / 100
  215. };
  216. } else {
  217. series[key].areaStyle = {
  218. opacity: 0
  219. };
  220. }
  221. series[key].lineStyle = {
  222. width: optionsSetup.lineWidth
  223. };
  224. series[key].label = {
  225. show: optionsSetup.isShow,
  226. position: "top",
  227. distance: 10,
  228. fontSize: optionsSetup.fontSize,
  229. color: optionsSetup.subTextColor,
  230. fontWeight: optionsSetup.fontWeight
  231. };
  232. }
  233. }
  234. this.options.series = series;
  235. },
  236. // tooltip 设置
  237. setOptionsTooltip() {
  238. const optionsSetup = this.optionsSetup;
  239. const tooltip = {
  240. trigger: "item",
  241. show: true,
  242. textStyle: {
  243. color: optionsSetup.lineColor,
  244. fontSize: optionsSetup.tipsFontSize
  245. }
  246. };
  247. this.options.tooltip = tooltip;
  248. },
  249. // 边距设置
  250. setOptionsMargin() {
  251. const optionsSetup = this.optionsSetup;
  252. const grid = {
  253. left: optionsSetup.marginLeft,
  254. right: optionsSetup.marginRight,
  255. bottom: optionsSetup.marginBottom,
  256. top: optionsSetup.marginTop,
  257. containLabel: true
  258. };
  259. this.options.grid = grid;
  260. },
  261. // 图例颜色修改
  262. setOptionsColor() {
  263. const optionsSetup = this.optionsSetup;
  264. const customColor = optionsSetup.customColor;
  265. if (!customColor) return;
  266. const arrColor = [];
  267. for (let i = 0; i < customColor.length; i++) {
  268. arrColor.push(customColor[i].color);
  269. }
  270. this.options.color = arrColor;
  271. this.options = Object.assign({}, this.options);
  272. },
  273. // 处理数据
  274. setOptionsData() {
  275. const optionsData = this.optionsData; // 数据类型 静态 or 动态
  276. optionsData.dataType == "staticData"
  277. ? this.staticDataFn(optionsData.staticData)
  278. : this.dynamicDataFn(optionsData.dynamicData, optionsData.refreshTime);
  279. },
  280. staticDataFn(val) {
  281. const series = this.options.series;
  282. let axis = [];
  283. let data = [];
  284. for (const i in val) {
  285. axis[i] = val[i].axis;
  286. data[i] = val[i].data
  287. }
  288. // x轴
  289. this.options.xAxis.data = axis;
  290. // series
  291. for (const i in series) {
  292. if (series[i].type == "line") {
  293. series[i].data = data;
  294. }
  295. }
  296. },
  297. dynamicDataFn(val, refreshTime) {
  298. if (!val) return;
  299. if (this.ispreview) {
  300. this.getEchartData(val);
  301. this.flagInter = setInterval(() => {
  302. this.getEchartData(val);
  303. }, refreshTime);
  304. } else {
  305. this.getEchartData(val);
  306. }
  307. },
  308. getEchartData(val) {
  309. const data = this.queryEchartsData(val);
  310. data.then(res => {
  311. this.renderingFn(res);
  312. });
  313. },
  314. renderingFn(val) {
  315. // x轴
  316. this.options.xAxis.data = val.xAxis;
  317. // series
  318. const series = this.options.series;
  319. for (const i in series) {
  320. if (series[i].type == "line") {
  321. series[i].data = val.series[i].data;
  322. }
  323. }
  324. }
  325. }
  326. };
  327. </script>
  328. <style scoped lang="scss">
  329. .echarts {
  330. width: 100%;
  331. height: 100%;
  332. overflow: hidden;
  333. }
  334. </style>