parseChartOption.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /**
  2. * 将请求返回的图表展示数据解析为前台展示用的config
  3. */
  4. import moment from 'moment'
  5. import EllipsisTooltip from '../components/common/ellipsisTooltip/index'
  6. import { deepAssign } from '../utils/baseUtils'
  7. import STATISTICS_OPTION from '../components/chartDesigner/sections/statisticsOption.json'
  8. import themes from './theme/index'
  9. export default function(viewType, data, chartConfig, themeName, styleConfig) {
  10. if(!data) {
  11. return {};
  12. }
  13. try {
  14. let theme = themes[themeName] || themes['default']
  15. let o, themeConfig;
  16. switch(viewType) {
  17. case 'bar': {
  18. themeConfig = Object.assign({}, theme.base, theme.bar);
  19. o = barOption(data, chartConfig, themeConfig, styleConfig);
  20. break;
  21. }
  22. case 'pie': {
  23. themeConfig = Object.assign({}, theme.base, theme.pie);
  24. o = pieOption(data, chartConfig, themeConfig, styleConfig);
  25. break;
  26. }
  27. case 'line': {
  28. themeConfig = Object.assign({}, theme.base, theme.line);
  29. o = lineOption(data, chartConfig, themeConfig, styleConfig);
  30. break;
  31. }
  32. case 'scatter': {
  33. themeConfig = Object.assign({}, theme.base, theme.scatter);
  34. o = scatterOption(data, chartConfig, themeConfig, styleConfig);
  35. break;
  36. }
  37. case 'aggregateTable': {
  38. themeConfig = theme.aggregateTable;
  39. o = aggregateTableOption(data, chartConfig, themeConfig, styleConfig);
  40. break;
  41. }case 'dataView' : {
  42. themeConfig = theme.dataView;
  43. o = dataViewOption(data, chartConfig, themeConfig, styleConfig);
  44. break;
  45. }
  46. default:{
  47. o = {};
  48. break;
  49. }
  50. }
  51. return o;
  52. }catch(e) {
  53. console.error(e);
  54. }
  55. }
  56. function barOption(data, barConfig, themeConfig, styleConfig) {
  57. let xTitle = barConfig.xAxis?`${barConfig.xAxis.column.label}${barConfig.xAxis.granularity.value?'('+barConfig.xAxis.granularity.label+')':''}`:null
  58. let yTitle = barConfig.yAxis?`${barConfig.yAxis.column.label}${barConfig.yAxis.gauge.value?'('+barConfig.yAxis.gauge.label+')':''}`:null
  59. data.serieses = data.serieses || [];
  60. let option = deepAssign({
  61. tooltip : {
  62. trigger: "axis",
  63. axisPointer: {
  64. type: "cross",
  65. label: {
  66. backgroundColor: "#6a7985"
  67. }
  68. }
  69. },
  70. legend: {
  71. show: data.serieses.length > 1
  72. },
  73. grid: {
  74. left: '10%',
  75. right: '10%',
  76. top: 60,
  77. bottom: 60,
  78. containLabel: true
  79. },
  80. xAxis: [{
  81. type: 'category',
  82. data: data.xAxis.map(d => {
  83. let gv= barConfig.xAxis.granularity.value;
  84. let xv = d || '空';
  85. if(gv === 'halfYear') {
  86. let arr = d.split('-H');
  87. xv = arr[0] + ['上半年', '下半年'][arr[1] - 1]
  88. }else if(gv === 'month') {
  89. xv = d.replace('-M', '-');
  90. }else if(gv === 'quarter') {
  91. let arr = d.split('-Q');
  92. xv = arr[0] + '-' + ['一', '二', '三', '四'][arr[1] - 1] + '季度'
  93. }else if(gv === 'week') {
  94. let arr = d.split('-W');
  95. xv = arr[0] + '-' + arr[1] + '周'
  96. }
  97. return xv;
  98. }),
  99. name: xTitle || '横轴',
  100. }],
  101. yAxis: [{
  102. name: yTitle || '纵轴',
  103. type: 'value'
  104. }],
  105. series: data.serieses.map(s => {
  106. return {
  107. name: barConfig.groupBy ? s.name : (barConfig.yAxis.column.label || s.name),
  108. type: 'bar',
  109. data: s.value,
  110. // stack: s.stack
  111. }
  112. }),
  113. dataZoom: {
  114. show: false
  115. }
  116. }, themeConfig, styleConfig);
  117. return option;
  118. }
  119. function pieOption(data, pieConfig, themeConfig, styleConfig) {
  120. let columnName = pieConfig.xAxis.column.label + (pieConfig.xAxis.granularity.value ? '('+pieConfig.xAxis.granularity.label+')' : '');
  121. let option = deepAssign({
  122. tooltip : {
  123. trigger: 'item',
  124. formatter: "{a} <br/>{b} : {c} ({d}%)"
  125. },
  126. legend: {
  127. show: true,
  128. orient: 'vertical',
  129. right: '30%',
  130. top: '100',
  131. data: data.xAxis.map(d => {
  132. let gv= pieConfig.xAxis.granularity.value;
  133. let xv = d || '空';
  134. if(gv === 'halfYear') {
  135. let arr = d.split('-H');
  136. xv = arr[0] + ['上半年', '下半年'][arr[1] - 1]
  137. }else if(gv === 'month') {
  138. xv = d.replace('-M', '-');
  139. }else if(gv === 'quarter') {
  140. let arr = d.split('-Q');
  141. xv = arr[0] + '-' + ['一', '二', '三', '四'][arr[1] - 1] + '季度'
  142. }else if(gv === 'week') {
  143. let arr = d.split('-W');
  144. xv = arr[0] + '-' + arr[1] + '周'
  145. }
  146. return xv;
  147. }),
  148. },
  149. grid: {
  150. left: '10%',
  151. right: '10%',
  152. top: 100,
  153. bottom: 60,
  154. containLabel: true
  155. },
  156. series : [
  157. {
  158. name: columnName,
  159. type: 'pie',
  160. center: ['30%', '50%'],
  161. radius: [0, '65%'],
  162. // radius : '55%',
  163. // center: ['50%', '60%'],
  164. // data: (data.serieses || [{ value: [] }])[0].value,
  165. // data: (data.serieses || [{ value: [] }])[0].value.map(v => ({ ...v, name: v.name || '空' })),
  166. data: (data.serieses || [{ value: [] }])[0].value.map(v => {
  167. let obj = { ...v };
  168. if(!v.name) {
  169. obj.name = '空'
  170. }else {
  171. let gv= pieConfig.xAxis.granularity.value;
  172. if(gv === 'halfYear') {
  173. let arr = v.name.split('-H');
  174. obj.name = arr[0] + ['上半年', '下半年'][arr[1] - 1]
  175. }else if(gv === 'month') {
  176. obj.name = v.name.replace('-M', '-');
  177. }else if(gv === 'quarter') {
  178. let arr = v.name.split('-');
  179. obj.name = arr[0] + '-' + ['一', '二', '三', '四'][arr[1] - 1] + '季度'
  180. }else if(gv === 'week') {
  181. let arr = v.name.split('-');
  182. obj.name = arr[0] + '-' + arr[1] + '周'
  183. }
  184. }
  185. return obj;
  186. }),
  187. itemStyle: {
  188. emphasis: {
  189. shadowBlur: 10,
  190. shadowOffsetX: 0,
  191. shadowColor: 'rgba(0, 0, 0, 0.5)'
  192. }
  193. }
  194. }
  195. ]
  196. }, themeConfig, styleConfig);
  197. return option;
  198. }
  199. function lineOption(data, lineConfig, themeConfig, styleConfig) {
  200. let xTitle = lineConfig.xAxis?`${lineConfig.xAxis.column.label}${lineConfig.xAxis.granularity.value?'('+lineConfig.xAxis.granularity.label+')':''}`:null
  201. let yTitle = lineConfig.yAxis?`${lineConfig.yAxis.column.label}${lineConfig.yAxis.gauge.value?'('+lineConfig.yAxis.gauge.label+')':''}`:null
  202. data.serieses = data.serieses || [];
  203. let option = deepAssign({
  204. tooltip: {
  205. trigger: 'axis',
  206. axisPointer: {
  207. type: 'cross'
  208. }
  209. },
  210. legend: {
  211. show: data.serieses.length > 1
  212. },
  213. xAxis: {
  214. name: xTitle || '横轴',
  215. type: 'time'
  216. },
  217. yAxis: {
  218. name: yTitle || '纵轴',
  219. type: 'value'
  220. },
  221. series: (data.serieses || []).map(s => {
  222. return {
  223. name: s.name,
  224. type: 'line',
  225. data: s.mdata.map(m => {
  226. return [m.date, m.value]
  227. }).sort((a, b) => {return new Date(a[0]).getTime() - new Date(b[0]).getTime()} )
  228. }
  229. }),
  230. dataZoom: {
  231. show: false
  232. }
  233. }, themeConfig, styleConfig);
  234. return option;
  235. }
  236. function scatterOption(data, scatterConfig, themeConfig, styleConfig) {
  237. let xTitle = scatterConfig.xAxis?`${scatterConfig.xAxis.column.label}${scatterConfig.xAxis.granularity.value?'('+scatterConfig.xAxis.granularity.label+')':''}`:null
  238. let yTitle = scatterConfig.yAxis?`${scatterConfig.yAxis.column.label}${scatterConfig.yAxis.gauge.value?'('+scatterConfig.yAxis.gauge.label+')':''}`:null
  239. let option = deepAssign({
  240. tooltip : {
  241. showDelay : 0,
  242. axisPointer:{
  243. show: true,
  244. type : 'cross',
  245. lineStyle: {
  246. type : 'dashed',
  247. width : 1
  248. }
  249. }
  250. },
  251. legend: {
  252. show: true
  253. },
  254. xAxis : [
  255. {
  256. type : 'value',
  257. name: xTitle || '横轴',
  258. scale:true,
  259. splitLine: {
  260. show: true
  261. }
  262. }
  263. ],
  264. yAxis : [
  265. {
  266. type : 'value',
  267. name: yTitle || '纵轴',
  268. scale:true,
  269. splitLine: {
  270. show: true
  271. }
  272. }
  273. ],
  274. series : (data.serieses || []).map(s => {
  275. return {
  276. name: s.name,
  277. type: 'scatter',
  278. data: s.mdata.map(m => {
  279. return [m.date, m.value]
  280. })
  281. }
  282. }),
  283. dataZoom: {
  284. show: false
  285. }
  286. }, themeConfig, styleConfig);
  287. return option;
  288. }
  289. function aggregateTableOption( data, aggregateTableConfig, themeConfig, styleConfig) {
  290. const resData = data.valueList;
  291. const { targetColumn, statistics: statisticsNames } = aggregateTableConfig;
  292. const { direction } = styleConfig;
  293. let statistics = STATISTICS_OPTION.filter(o => statisticsNames.indexOf(o.value) !== -1);
  294. let option = {
  295. targetColumn: targetColumn,
  296. statistics: statistics.map(s => ({
  297. name: s.value,
  298. label: s.label,
  299. value: resData[s.value]
  300. })),
  301. direction: direction || 'vertical'
  302. };
  303. return option;
  304. }
  305. function dataViewOption(data, dataViewConfig, themeConfig, styleConfig) {
  306. const { list, pageNum, pageSize, pages, total } = data.valueList;
  307. let columns = dataViewConfig.viewColumns || [];
  308. let dataSource = list || [];
  309. let option = {
  310. columns: columns.map(c => {
  311. let obj = {
  312. title: c.label,
  313. dataIndex: c.name,
  314. }
  315. if(c.type === 'time') {
  316. obj.render = (v, r, i) => {
  317. let text = v === null ? '空' : moment(v).isValid() ? moment(v).format('YYYY-MM-DD') : v
  318. return <EllipsisTooltip title={text}>{text}</EllipsisTooltip>
  319. }
  320. }else {
  321. obj.render = v => {
  322. let text = v === null ? '空' : v
  323. return <EllipsisTooltip title={text}>{text}</EllipsisTooltip>
  324. }
  325. }
  326. return obj;
  327. }),
  328. dataSource: dataSource.map((d, i) => {
  329. return { ...d, key: i}
  330. }),
  331. page: pageNum,
  332. pageSize,
  333. pages,
  334. total,
  335. };
  336. return option;
  337. }