chart.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // 获取Canvas元素
  2. const ctx = document.getElementById('colorChart').getContext('2d');
  3. // 准备数据 - 与Angular组件中的colorData结构相同
  4. const colorData = {
  5. labels: ['海洋蓝', '活力红', '森林绿', '阳光黄', '梦幻紫', '珊瑚橙'],
  6. datasets: [{
  7. label: '使用次数',
  8. data: [1850, 1620, 1540, 1420, 1360, 1280],
  9. backgroundColor: [
  10. '#3498db',
  11. '#e74c3c',
  12. '#2ecc71',
  13. '#f1c40f',
  14. '#9b59b6',
  15. '#ff7f50'
  16. ],
  17. borderColor: [
  18. '#2980b9',
  19. '#c0392b',
  20. '#27ae60',
  21. '#f39c12',
  22. '#8e44ad',
  23. '#ff6347'
  24. ],
  25. borderWidth: 1,
  26. borderRadius: 10
  27. }]
  28. };
  29. // 创建图表
  30. const colorChart = new Chart(ctx, {
  31. type: 'bar',
  32. data: colorData,
  33. options: {
  34. responsive: true,
  35. maintainAspectRatio: false,
  36. plugins: {
  37. legend: {
  38. display: false
  39. },
  40. title: {
  41. display: true,
  42. text: '颜色使用频率统计',
  43. font: {
  44. size: 16
  45. }
  46. }
  47. },
  48. scales: {
  49. y: {
  50. beginAtZero: true,
  51. grid: {
  52. color: 'rgba(0, 0, 0, 0.05)'
  53. }
  54. },
  55. x: {
  56. grid: {
  57. display: false
  58. }
  59. }
  60. }
  61. }
  62. });