v1.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace zin;
  3. class tableChart extends wg
  4. {
  5. /**
  6. * @var mixed[]
  7. */
  8. protected static $defineProps = array(
  9. 'type:string',
  10. 'item:string',
  11. 'title:string',
  12. 'tableHeaders?:array',
  13. 'datas?:array',
  14. 'tableWidth?:string',
  15. 'chartHeight?:int',
  16. 'overflow?:bool'
  17. );
  18. private function genTableHeaders()
  19. {
  20. global $lang;
  21. $tableHeaders = $this->prop('tableHeaders');
  22. if(empty($tableHeaders))
  23. {
  24. $tableHeaders = array
  25. (
  26. 'item' => $lang->report->item,
  27. 'value' => $lang->report->value,
  28. 'percent' => $lang->report->percent
  29. );
  30. }
  31. return h::tr
  32. (
  33. setClass('text-left'),
  34. h::th($tableHeaders['item']),
  35. h::th(set::width('100px'), $tableHeaders['value']),
  36. h::th(set::width('120px'), $tableHeaders['percent'])
  37. );
  38. }
  39. protected function build()
  40. {
  41. $type = $this->prop('type');
  42. $item = $this->prop('item');
  43. $title = $this->prop('title');
  44. $datas = $this->prop('datas');
  45. $colorList = array('#5470C6', '#91CC75', '#FAC858', '#EE6666', '#73C0DE', '#3BA272', '#FC8452', '#9A60B4', '#EA7CCC');
  46. $chartOption = array();
  47. $tooltip = array('show' => true);
  48. if($type == 'pie') $tooltip['formatter'] = '{b}';
  49. shuffle($colorList);
  50. $tableTR = array();
  51. foreach($datas as $data)
  52. {
  53. $color = current($colorList);
  54. $chartOption[] = array('name' => $data->name . ($type == 'pie' ? ' : ' . $data->value : ''), 'value' => $type == 'pie' ? $data->value : array('value' => $data->value, 'itemStyle' => array('color' => $color)));
  55. $tableTR[] = h::tr
  56. (
  57. h::td(label(set::className('label-dot mr-2'), set::style(array('background-color' => $color, '--tw-ring-color' => $color))), $data->name),
  58. h::td($data->value),
  59. h::td(($data->percent * 100) . '%')
  60. );
  61. if(!next($colorList)) reset($colorList);
  62. }
  63. $tableWidth = $this->prop('tableWidth', '50%');
  64. $chartHeight = $this->prop('chartHeight', 300);
  65. $overflow = $this->prop('overflow', true);
  66. return div
  67. (
  68. set::className('flex border'),
  69. cell
  70. (
  71. set::id($item),
  72. setKey('chart'),
  73. setClass('border-r chart flex-auto'),
  74. div(set::className('center text-base font-bold py-2'), $title),
  75. echarts
  76. (
  77. set::width('100%'),
  78. set::height($chartHeight),
  79. set::color($colorList),
  80. set::tooltip($tooltip),
  81. $type != 'pie' ? set::xAxis
  82. (
  83. array
  84. (
  85. 'type' => 'category',
  86. 'data' => array_column($chartOption, 'name')
  87. )
  88. ) : null,
  89. $type != 'pie' ? set::yAxis(array('type' => 'value')) : null,
  90. set::series
  91. (
  92. array
  93. (
  94. array
  95. (
  96. 'data' => $type == 'pie' ? $chartOption : array_column($chartOption, 'value'),
  97. 'type' => $type
  98. )
  99. )
  100. )
  101. )
  102. ),
  103. cell
  104. (
  105. setKey('table'),
  106. set::width($tableWidth),
  107. div
  108. (
  109. setClass('overflow-y-auto'),
  110. $overflow ? setStyle('max-height', ($chartHeight + 50) .'px') : null,
  111. h::table
  112. (
  113. set::className('table'),
  114. $this->genTableHeaders(),
  115. $tableTR
  116. )
  117. )
  118. ),
  119. set($this->getRestProps())
  120. );
  121. }
  122. }