statistics_utils.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. <?php
  2. /**
  3. * 统计分析工具函数
  4. *
  5. * 包含所有统计模块共用的工具函数和日期处理逻辑
  6. */
  7. // 确保直接访问时需要先登录
  8. require_once 'conn.php';
  9. if (!isset($_SESSION['employee_id'])) {
  10. checkLogin();
  11. }
  12. //检查是否管理员
  13. checkPermissionDie(1,2);
  14. /**
  15. * 获取和处理日期范围参数
  16. *
  17. * @return array 包含开始日期、结束日期和其他日期相关参数
  18. */
  19. function getDateRangeParams() {
  20. // 计算日期范围
  21. $current_month_start = date('Y-m-01');
  22. $current_month_end = date('Y-m-t');
  23. $last_month_start = date('Y-m-01', strtotime('-1 month'));
  24. $last_month_end = date('Y-m-t', strtotime('-1 month'));
  25. $current_year_start = date('Y-01-01');
  26. $current_year_end = date('Y-12-31');
  27. // 可选的日期范围筛选
  28. $date_range = isset($_GET['date_range']) ? $_GET['date_range'] : 'current_month';
  29. $custom_start = isset($_GET['start_date']) ? $_GET['start_date'] : '';
  30. $custom_end = isset($_GET['end_date']) ? $_GET['end_date'] : '';
  31. $period = isset($_GET['period']) ? $_GET['period'] : 'day';
  32. // 设置日期范围
  33. if ($date_range == 'custom' && !empty($custom_start) && !empty($custom_end)) {
  34. $start_date = $custom_start;
  35. $end_date = $custom_end;
  36. } else {
  37. switch ($date_range) {
  38. case 'last_month':
  39. $start_date = $last_month_start;
  40. $end_date = $last_month_end;
  41. break;
  42. case 'current_year':
  43. $start_date = $current_year_start;
  44. $end_date = $current_year_end;
  45. break;
  46. case 'last_30_days':
  47. $start_date = date('Y-m-d', strtotime('-30 days'));
  48. $end_date = date('Y-m-d');
  49. break;
  50. case 'last_90_days':
  51. $start_date = date('Y-m-d', strtotime('-90 days'));
  52. $end_date = date('Y-m-d');
  53. break;
  54. case 'current_month':
  55. default:
  56. $start_date = $current_month_start;
  57. $end_date = $current_month_end;
  58. break;
  59. }
  60. }
  61. // 格式化日期用于SQL查询
  62. $start_date_sql = date('Y-m-d', strtotime($start_date));
  63. $end_date_sql = date('Y-m-d', strtotime($end_date)) . ' 23:59:59';
  64. return [
  65. 'date_range' => $date_range,
  66. 'custom_start' => $custom_start,
  67. 'custom_end' => $custom_end,
  68. 'period' => $period,
  69. 'start_date' => $start_date,
  70. 'end_date' => $end_date,
  71. 'start_date_sql' => $start_date_sql,
  72. 'end_date_sql' => $end_date_sql
  73. ];
  74. }
  75. /**
  76. * 生成图表颜色数组
  77. *
  78. * @param int $count 需要的颜色数量
  79. * @param bool $transparent 是否透明
  80. * @return array 背景色和边框色数组
  81. */
  82. function generateChartColors($count = 10, $transparent = true) {
  83. $colors = [
  84. ['rgba(255, 99, 132, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(255, 99, 132, 1)'],
  85. ['rgba(54, 162, 235, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(54, 162, 235, 1)'],
  86. ['rgba(255, 206, 86, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(255, 206, 86, 1)'],
  87. ['rgba(75, 192, 192, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(75, 192, 192, 1)'],
  88. ['rgba(153, 102, 255, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(153, 102, 255, 1)'],
  89. ['rgba(255, 159, 64, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(255, 159, 64, 1)'],
  90. ['rgba(199, 199, 199, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(199, 199, 199, 1)'],
  91. ['rgba(83, 102, 255, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(83, 102, 255, 1)'],
  92. ['rgba(40, 159, 64, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(40, 159, 64, 1)'],
  93. ['rgba(210, 199, 199, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(210, 199, 199, 1)']
  94. ];
  95. $result = [];
  96. // 确保有足够的颜色
  97. while (count($result) < $count) {
  98. foreach ($colors as $color) {
  99. $result[] = $color;
  100. if (count($result) >= $count) {
  101. break;
  102. }
  103. }
  104. }
  105. return array_slice($result, 0, $count);
  106. }
  107. /**
  108. * 格式化数值,处理空值和小数位数
  109. *
  110. * @param mixed $value 要格式化的值
  111. * @param int $decimals 小数位数
  112. * @return string 格式化后的数值
  113. */
  114. function formatNumber($value, $decimals = 2) {
  115. if ($value === null || $value === '') {
  116. return '0';
  117. }
  118. return number_format((float)$value, $decimals);
  119. }
  120. /**
  121. * 获取时间粒度对应的MySQL DATE_FORMAT格式
  122. *
  123. * @param string $period 时间粒度 (day/week/month)
  124. * @return string MySQL DATE_FORMAT格式字符串
  125. */
  126. function getPeriodFormat($period) {
  127. switch ($period) {
  128. case 'week':
  129. return '%x-W%v'; // ISO year and week number
  130. case 'month':
  131. return '%Y-%m';
  132. case 'day':
  133. default:
  134. return '%Y-%m-%d';
  135. }
  136. }
  137. /**
  138. * 获取新增客户详细信息
  139. *
  140. * @param mysqli $conn 数据库连接
  141. * @param string $start_date 开始日期
  142. * @param string $end_date 结束日期
  143. * @return array 新增客户详细数据
  144. */
  145. function getNewCustomersDetails($conn, $start_date, $end_date) {
  146. $sql = "SELECT
  147. c.id,
  148. c.cs_company as company_name,
  149. c.cs_code as customer_code,
  150. co.countryName as country,
  151. ct.businessType as customer_type,
  152. e.em_user as employee_name,
  153. c.cs_addtime as add_date
  154. FROM customer c
  155. LEFT JOIN country co ON c.cs_country = co.id
  156. LEFT JOIN clienttype ct ON c.cs_type = ct.id
  157. LEFT JOIN employee e ON c.cs_belong = e.id
  158. WHERE c.cs_addtime BETWEEN ? AND ?
  159. ORDER BY c.cs_addtime DESC
  160. LIMIT 30";
  161. $stmt = $conn->prepare($sql);
  162. $stmt->bind_param("ss", $start_date, $end_date);
  163. $stmt->execute();
  164. $result = $stmt->get_result();
  165. $customers = [];
  166. while ($row = $result->fetch_assoc()) {
  167. $customers[] = $row;
  168. }
  169. return $customers;
  170. }
  171. /**
  172. * 获取各业务员新增客户统计
  173. *
  174. * @param mysqli $conn 数据库连接
  175. * @param string $start_date 开始日期
  176. * @param string $end_date 结束日期
  177. * @return array 业务员新增客户统计数据
  178. */
  179. function getNewCustomersByEmployee($conn, $start_date, $end_date) {
  180. $sql = "SELECT
  181. e.id as employee_id,
  182. e.em_user as employee_name,
  183. COUNT(c.id) as customer_count
  184. FROM employee e
  185. LEFT JOIN customer c ON e.id = c.cs_belong AND c.cs_addtime BETWEEN ? AND ?
  186. WHERE e.em_role IS NOT NULL
  187. GROUP BY e.id
  188. ORDER BY customer_count DESC";
  189. $stmt = $conn->prepare($sql);
  190. $stmt->bind_param("ss", $start_date, $end_date);
  191. $stmt->execute();
  192. $result = $stmt->get_result();
  193. $data = [];
  194. while ($row = $result->fetch_assoc()) {
  195. $data[] = $row;
  196. }
  197. return $data;
  198. }
  199. /**
  200. * 渲染新增客户图表
  201. *
  202. * @param array $customers 新增客户数据
  203. * @return void
  204. */
  205. function renderNewCustomersChart($customers) {
  206. if (empty($customers)) {
  207. echo '<div class="alert alert-info">该时间段内没有新增客户数据</div>';
  208. return;
  209. }
  210. ?>
  211. <div class="chart-container">
  212. <div class="table-responsive">
  213. <table class="data-table">
  214. <thead>
  215. <tr>
  216. <th>客户名称</th>
  217. <th>客户编码</th>
  218. <th>国家/地区</th>
  219. <th>客户类型</th>
  220. <th>负责业务员</th>
  221. <th>添加日期</th>
  222. </tr>
  223. </thead>
  224. <tbody>
  225. <?php foreach ($customers as $customer): ?>
  226. <tr>
  227. <td><?php echo htmlspecialchars($customer['company_name']); ?></td>
  228. <td><?php echo htmlspecialchars($customer['customer_code']); ?></td>
  229. <td><?php echo htmlspecialchars($customer['country']); ?></td>
  230. <td><?php echo htmlspecialchars($customer['customer_type'] ?: '未分类'); ?></td>
  231. <td><?php echo htmlspecialchars($customer['employee_name']); ?></td>
  232. <td><?php echo date('Y-m-d', strtotime($customer['add_date'])); ?></td>
  233. </tr>
  234. <?php endforeach; ?>
  235. </tbody>
  236. </table>
  237. </div>
  238. </div>
  239. <!-- 新增客户添加时间分布图 -->
  240. <div class="chart-container">
  241. <div>
  242. <canvas id="newCustomersChart"></canvas>
  243. </div>
  244. </div>
  245. <script>
  246. // 准备图表数据
  247. <?php
  248. // 按日期分组客户数量
  249. $dates = [];
  250. $counts = [];
  251. $dateGroups = [];
  252. foreach ($customers as $customer) {
  253. $date = date('Y-m-d', strtotime($customer['add_date']));
  254. if (!isset($dateGroups[$date])) {
  255. $dateGroups[$date] = 0;
  256. }
  257. $dateGroups[$date]++;
  258. }
  259. // 排序日期
  260. ksort($dateGroups);
  261. foreach ($dateGroups as $date => $count) {
  262. $dates[] = $date;
  263. $counts[] = $count;
  264. }
  265. ?>
  266. var newCustomersCtx = document.getElementById('newCustomersChart').getContext('2d');
  267. new Chart(newCustomersCtx, {
  268. type: 'bar',
  269. data: {
  270. labels: <?php echo json_encode($dates); ?>,
  271. datasets: [{
  272. label: '新增客户数量',
  273. data: <?php echo json_encode($counts); ?>,
  274. backgroundColor: 'rgba(54, 162, 235, 0.5)',
  275. borderColor: 'rgba(54, 162, 235, 1)',
  276. borderWidth: 1
  277. }]
  278. },
  279. options: {
  280. responsive: true,
  281. scales: {
  282. y: {
  283. beginAtZero: true,
  284. title: {
  285. display: true,
  286. text: '客户数量'
  287. }
  288. },
  289. x: {
  290. title: {
  291. display: true,
  292. text: '日期'
  293. }
  294. }
  295. },
  296. plugins: {
  297. title: {
  298. display: true,
  299. text: '新增客户时间分布'
  300. }
  301. }
  302. }
  303. });
  304. </script>
  305. <?php
  306. }
  307. /**
  308. * 渲染业务员新增客户图表
  309. *
  310. * @param array $employee_data 业务员新增客户数据
  311. * @return void
  312. */
  313. function renderNewCustomersByEmployeeChart($employee_data) {
  314. if (empty($employee_data)) {
  315. echo '<div class="alert alert-info">该时间段内没有业务员新增客户数据</div>';
  316. return;
  317. }
  318. // 准备图表数据
  319. $employee_names = [];
  320. $customer_counts = [];
  321. foreach ($employee_data as $row) {
  322. $employee_names[] = $row['employee_name'];
  323. $customer_counts[] = $row['customer_count'];
  324. }
  325. // 生成图表背景色
  326. $colors = generateChartColors(count($employee_data));
  327. $backgroundColors = [];
  328. $borderColors = [];
  329. foreach ($colors as $color) {
  330. $backgroundColors[] = $color[0];
  331. $borderColors[] = $color[1];
  332. }
  333. ?>
  334. <div class="analysis-grid">
  335. <div>
  336. <canvas id="employeeNewCustomersChart"></canvas>
  337. </div>
  338. <div class="table-responsive">
  339. <table class="data-table">
  340. <thead>
  341. <tr>
  342. <th>业务员</th>
  343. <th>新增客户数量</th>
  344. </tr>
  345. </thead>
  346. <tbody>
  347. <?php foreach ($employee_data as $row): ?>
  348. <tr>
  349. <td><?php echo htmlspecialchars($row['employee_name']); ?></td>
  350. <td><?php echo number_format($row['customer_count']); ?></td>
  351. </tr>
  352. <?php endforeach; ?>
  353. </tbody>
  354. </table>
  355. </div>
  356. </div>
  357. <script>
  358. var employeeNewCustomersCtx = document.getElementById('employeeNewCustomersChart').getContext('2d');
  359. new Chart(employeeNewCustomersCtx, {
  360. type: 'bar',
  361. data: {
  362. labels: <?php echo json_encode($employee_names); ?>,
  363. datasets: [{
  364. label: '新增客户数量',
  365. data: <?php echo json_encode($customer_counts); ?>,
  366. backgroundColor: <?php echo json_encode($backgroundColors); ?>,
  367. borderColor: <?php echo json_encode($borderColors); ?>,
  368. borderWidth: 1
  369. }]
  370. },
  371. options: {
  372. responsive: true,
  373. scales: {
  374. y: {
  375. beginAtZero: true,
  376. title: {
  377. display: true,
  378. text: '客户数量'
  379. }
  380. }
  381. },
  382. plugins: {
  383. title: {
  384. display: true,
  385. text: '业务员新增客户统计'
  386. }
  387. }
  388. }
  389. });
  390. </script>
  391. <?php
  392. }
  393. /**
  394. * 获取新客户购买产品明细
  395. *
  396. * @param mysqli $conn 数据库连接
  397. * @param string $start_date 开始日期
  398. * @param string $end_date 结束日期
  399. * @param int $category_id 产品分类ID,0表示所有分类
  400. * @return array 新客户购买产品数据
  401. */
  402. function getNewCustomerProductPurchases($conn, $start_date, $end_date, $category_id = 0) {
  403. // 获取在指定日期范围内首次购买的客户
  404. $new_customer_sql = "
  405. SELECT DISTINCT o.customer_id
  406. FROM orders o
  407. WHERE o.order_date BETWEEN ? AND ?
  408. AND o.order_status != 0
  409. AND NOT EXISTS (
  410. SELECT 1 FROM orders o2
  411. WHERE o2.customer_id = o.customer_id
  412. AND o2.order_date < ?
  413. AND o2.order_status != 0
  414. )
  415. ";
  416. $stmt = $conn->prepare($new_customer_sql);
  417. $stmt->bind_param("sss", $start_date, $end_date, $start_date);
  418. $stmt->execute();
  419. $new_customers_result = $stmt->get_result();
  420. $new_customer_ids = [];
  421. while ($row = $new_customers_result->fetch_assoc()) {
  422. $new_customer_ids[] = $row['customer_id'];
  423. }
  424. // 如果没有新客户,返回空数组
  425. if (empty($new_customer_ids)) {
  426. return [];
  427. }
  428. // 构建查询条件中的客户ID列表
  429. $customer_ids_str = implode(',', $new_customer_ids);
  430. // 查询这些新客户购买的产品
  431. $category_filter = "";
  432. if ($category_id > 0) {
  433. $category_filter = "AND p.category_id = " . intval($category_id);
  434. }
  435. $product_sql = "
  436. SELECT
  437. p.id,
  438. p.ProductName as product_name,
  439. pc.name as category_name,
  440. COUNT(DISTINCT o.id) as order_count,
  441. COUNT(DISTINCT o.customer_id) as customer_count,
  442. SUM(oi.quantity) as total_quantity,
  443. SUM(oi.total_price) as total_revenue,
  444. AVG(oi.unit_price) as avg_price
  445. FROM orders o
  446. JOIN order_items oi ON o.id = oi.order_id
  447. JOIN products p ON oi.product_id = p.id
  448. LEFT JOIN product_categories pc ON p.category_id = pc.id
  449. WHERE o.customer_id IN ({$customer_ids_str})
  450. AND o.order_date BETWEEN ? AND ?
  451. AND o.order_status != 0
  452. {$category_filter}
  453. GROUP BY p.id
  454. ORDER BY total_revenue DESC
  455. ";
  456. $stmt = $conn->prepare($product_sql);
  457. $stmt->bind_param("ss", $start_date, $end_date);
  458. $stmt->execute();
  459. $result = $stmt->get_result();
  460. $products = [];
  461. while ($row = $result->fetch_assoc()) {
  462. $products[] = $row;
  463. }
  464. return [
  465. 'new_customer_count' => count($new_customer_ids),
  466. 'products' => $products
  467. ];
  468. }
  469. /**
  470. * 渲染新客户产品购买明细
  471. *
  472. * @param array $product_data 产品购买数据
  473. * @return void
  474. */
  475. function renderNewCustomerProductPurchases($product_data) {
  476. if (empty($product_data) || empty($product_data['products'])) {
  477. echo '<div class="alert alert-info">该时间段内没有新客户购买产品数据</div>';
  478. return;
  479. }
  480. $products = $product_data['products'];
  481. $new_customer_count = $product_data['new_customer_count'];
  482. ?>
  483. <div class="section-intro">
  484. <p>本期间共有 <strong><?php echo number_format($new_customer_count); ?></strong> 名新客户进行了购买。以下是他们购买的产品明细:</p>
  485. </div>
  486. <div class="table-responsive">
  487. <table class="data-table">
  488. <thead>
  489. <tr>
  490. <th>产品名称</th>
  491. <th>产品分类</th>
  492. <th>订单数</th>
  493. <th>购买客户数</th>
  494. <th>销售数量</th>
  495. <th>销售金额</th>
  496. <th>平均单价</th>
  497. </tr>
  498. </thead>
  499. <tbody>
  500. <?php foreach ($products as $product): ?>
  501. <tr>
  502. <td><?php echo htmlspecialchars($product['product_name']); ?></td>
  503. <td><?php echo htmlspecialchars($product['category_name'] ?: '未分类'); ?></td>
  504. <td><?php echo number_format($product['order_count']); ?></td>
  505. <td><?php echo number_format($product['customer_count']); ?></td>
  506. <td><?php echo number_format($product['total_quantity']); ?></td>
  507. <td><?php echo formatCurrency($product['total_revenue']); ?></td>
  508. <td><?php echo formatCurrency($product['avg_price']); ?></td>
  509. </tr>
  510. <?php endforeach; ?>
  511. </tbody>
  512. </table>
  513. </div>
  514. <!-- 新客户产品购买分布图 -->
  515. <div class="chart-container">
  516. <div>
  517. <canvas id="newCustomerProductChart"></canvas>
  518. </div>
  519. </div>
  520. <script>
  521. <?php
  522. // 准备图表数据
  523. $product_names = [];
  524. $product_quantities = [];
  525. $product_revenues = [];
  526. // 只取前10个产品用于图表显示
  527. $top_products = array_slice($products, 0, 10);
  528. foreach ($top_products as $product) {
  529. $product_names[] = $product['product_name'];
  530. $product_quantities[] = $product['total_quantity'];
  531. $product_revenues[] = $product['total_revenue'];
  532. }
  533. // 生成图表背景色
  534. $colors = generateChartColors(count($top_products));
  535. $backgroundColors = [];
  536. foreach ($colors as $color) {
  537. $backgroundColors[] = $color[0];
  538. }
  539. ?>
  540. var newCustomerProductCtx = document.getElementById('newCustomerProductChart').getContext('2d');
  541. new Chart(newCustomerProductCtx, {
  542. type: 'bar',
  543. data: {
  544. labels: <?php echo json_encode($product_names); ?>,
  545. datasets: [
  546. {
  547. label: '销售数量',
  548. data: <?php echo json_encode($product_quantities); ?>,
  549. backgroundColor: 'rgba(54, 162, 235, 0.7)',
  550. borderColor: 'rgba(54, 162, 235, 1)',
  551. borderWidth: 1,
  552. yAxisID: 'y-quantity'
  553. },
  554. {
  555. label: '销售金额',
  556. data: <?php echo json_encode($product_revenues); ?>,
  557. backgroundColor: 'rgba(255, 99, 132, 0.7)',
  558. borderColor: 'rgba(255, 99, 132, 1)',
  559. borderWidth: 1,
  560. yAxisID: 'y-revenue',
  561. type: 'line',
  562. fill: false
  563. }
  564. ]
  565. },
  566. options: {
  567. responsive: true,
  568. scales: {
  569. 'y-quantity': {
  570. type: 'linear',
  571. position: 'left',
  572. title: {
  573. display: true,
  574. text: '销售数量'
  575. },
  576. beginAtZero: true
  577. },
  578. 'y-revenue': {
  579. type: 'linear',
  580. position: 'right',
  581. title: {
  582. display: true,
  583. text: '销售金额'
  584. },
  585. beginAtZero: true,
  586. grid: {
  587. drawOnChartArea: false
  588. }
  589. }
  590. },
  591. plugins: {
  592. title: {
  593. display: true,
  594. text: '新客户热门购买产品 (Top 10)'
  595. }
  596. }
  597. }
  598. });
  599. </script>
  600. <!-- 新客户产品类别分布图 -->
  601. <div class="chart-container">
  602. <div>
  603. <canvas id="newCustomerCategoryChart"></canvas>
  604. </div>
  605. </div>
  606. <script>
  607. <?php
  608. // 按产品类别分组
  609. $categories = [];
  610. $category_data = [];
  611. foreach ($products as $product) {
  612. $category = $product['category_name'] ?: '未分类';
  613. if (!isset($category_data[$category])) {
  614. $category_data[$category] = [
  615. 'quantity' => 0,
  616. 'revenue' => 0
  617. ];
  618. }
  619. $category_data[$category]['quantity'] += $product['total_quantity'];
  620. $category_data[$category]['revenue'] += $product['total_revenue'];
  621. }
  622. $category_names = array_keys($category_data);
  623. $category_quantities = array_column($category_data, 'quantity');
  624. $category_revenues = array_column($category_data, 'revenue');
  625. // 计算占比
  626. $total_revenue = array_sum($category_revenues);
  627. $revenue_percentages = [];
  628. foreach ($category_revenues as $revenue) {
  629. $revenue_percentages[] = round(($revenue / $total_revenue) * 100, 1);
  630. }
  631. // 生成图表背景色
  632. $category_colors = generateChartColors(count($category_data), false);
  633. $category_bg_colors = array_column($category_colors, 0);
  634. ?>
  635. var newCustomerCategoryCtx = document.getElementById('newCustomerCategoryChart').getContext('2d');
  636. new Chart(newCustomerCategoryCtx, {
  637. type: 'pie',
  638. data: {
  639. labels: <?php echo json_encode($category_names); ?>,
  640. datasets: [{
  641. data: <?php echo json_encode($revenue_percentages); ?>,
  642. backgroundColor: <?php echo json_encode($category_bg_colors); ?>,
  643. borderWidth: 1
  644. }]
  645. },
  646. options: {
  647. responsive: true,
  648. plugins: {
  649. title: {
  650. display: true,
  651. text: '新客户产品类别销售占比'
  652. },
  653. tooltip: {
  654. callbacks: {
  655. label: function(context) {
  656. return context.label + ': ' + context.raw + '%';
  657. }
  658. }
  659. }
  660. }
  661. }
  662. });
  663. </script>
  664. <?php
  665. }