utils.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import Decimal from 'decimal.js';
  2. /**
  3. * 将查询结果的表头信息格式化
  4. * */
  5. export const formatColumns=(props)=>{
  6. let columns = [];
  7. try {
  8. if (props && props.length>0){
  9. columns = props.map(item=> (
  10. {
  11. title: item.name||item.nameEn,
  12. dataIndex: item.nameEn,
  13. key: item.nameEn,
  14. width: 100,
  15. }
  16. ))
  17. }
  18. }catch(e){}
  19. return columns
  20. }
  21. /**
  22. * 查询结果的维度信息格式化
  23. * */
  24. export const formatDimensions=(props)=>{
  25. let dimensions = [];
  26. try {
  27. if (props && props.length>0){
  28. dimensions = props.map(item=> (
  29. {
  30. title: item.name,
  31. value: item.nameEn,
  32. }
  33. ))
  34. }
  35. }catch (e) {}
  36. return dimensions
  37. }
  38. /**
  39. * 查询结果的维度信息格式化
  40. * */
  41. export const formatMetrics=(props)=>{
  42. let metrics = [];
  43. if (props && props.length>0){
  44. metrics = props.map(item=> (
  45. {
  46. title: item.name,
  47. value: item.nameEn,
  48. dataType: item.dataFormatType
  49. }
  50. ))
  51. }
  52. return metrics
  53. }
  54. /**
  55. * 格式化索引
  56. * */
  57. export const formatIndex=(props)=> ( props && props.length>0 && props.map(item=> item.nameEn) ) || []
  58. /**
  59. * 数据格式化
  60. * */
  61. export const formatData=(data, dim, mtc, cat)=>(
  62. data && data.length>0 && mtc &&
  63. data.map(item=> {
  64. const mtcType = mtc.dataType;
  65. const mtcData = item[mtc?.value];
  66. const metric = mtcType==="PERCENT" && mtcData
  67. ? new Decimal(parseFloat(mtcData.replace('%', '')) / 100).toDP(4).toNumber()
  68. : new Decimal(mtcData||'0').toNumber()
  69. return {
  70. dimension:item[dim?.value],
  71. category:item[cat?.value],
  72. metric,
  73. }
  74. })
  75. ) || []