chart.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import * as echarts from "echarts";
  2. export function chartBar(data) {
  3. let countryColors = [
  4. 'rgba(224,96,93)',
  5. 'rgba(231,168,79)',
  6. 'rgba(163,201,90)',
  7. 'rgba(90,201,148)',
  8. 'rgba(69,239,209)',
  9. 'rgba(69,160,239)',
  10. 'rgba(69,73,239)',
  11. 'rgba(231,79,176)',
  12. 'rgba(224,93,142)',
  13. 'rgba(155,90,201)',
  14. ]
  15. let yLabel = []
  16. let value = []
  17. data.forEach(item => {
  18. yLabel.push(item.student.student_name)
  19. value.push(Math.abs(item.score_total))
  20. })
  21. return {
  22. grid: {
  23. top: 0,
  24. bottom: 0,
  25. left: '20%',
  26. right: '10%'
  27. },
  28. xAxis: {
  29. max: 'dataMax'
  30. },
  31. yAxis: {
  32. type: 'category',
  33. boundaryGap: true,
  34. splitArea: {
  35. show: true,
  36. interval: 0,
  37. areaStyle: {
  38. color: []
  39. },
  40. },
  41. inverse: true,
  42. animationDuration: 1000,
  43. animationDurationUpdate: 1000,
  44. axisLabel: {
  45. show: true,
  46. formatter: function(value, index) {
  47. // 根据你的需求调整 formatter 函数
  48. // 这里只是一个简单的示例,假设你想在所有 Y 轴 label 前都添加一个图片
  49. return `{${index}| } {text|${data[index].student.student_name} } `;
  50. },
  51. rich: {
  52. img1: {
  53. height: 20, // 图片高度
  54. width: 20, // 图片宽度
  55. backgroundColor: {
  56. image: '', // 图片路径
  57. // 如果你使用的是模块化的方式(如 Vue),你可能需要使用 require 或 import 来引入图片
  58. // 例如:image: require('@/assets/image.png')
  59. }
  60. },
  61. text: {
  62. width: 30,
  63. marginLeft: '40px'
  64. }
  65. }
  66. }
  67. },
  68. series: [{
  69. itemStyle: {
  70. color: function(param) {
  71. return countryColors[(countryColors.length + param.dataIndex) % countryColors
  72. .length] ||
  73. '#5470c6';
  74. },
  75. height: 40
  76. },
  77. type: 'bar',
  78. data: value,
  79. label: {
  80. show: true,
  81. position: 'right',
  82. valueAnimation: true,
  83. }
  84. }],
  85. legend: {},
  86. animationDuration: 1000,
  87. animationDurationUpdate: 1000,
  88. animationEasing: 'linear',
  89. animationEasingUpdate: 'linear',
  90. };
  91. }