Chartjs:使一些刻度(星期日,星期六)变红

yb3bgrhw  于 2022-11-06  发布在  Chart.js
关注(0)|答案(1)|浏览(412)

我试着让勾号变成红色。但它不起作用。当标签没有标签- Sa/Su时它起作用。所有标签-灰色,但当我有Su或Sa标签时,它只是黑色

  1. ticks: {
  2. fontSize: 9,
  3. fontColor: curLabels.map((item) => {
  4. if(item === 'Su' || item === 'Sa')
  5. return 'red';
  6. return 'rgba(0, 0, 0, 0.4)';
  7. }),
  8. maxRotation: 0,
  9. },

编辑:

  1. <div style={{height: '100%'}} className='position-relative'>
  2. <Line
  3. key='lineChart17'
  4. data={dataForChart}
  5. options={lineOptions}
  6. style={{height: '90px', padding: '5px', width: '100%'}}
  7. redraw
  8. />
  9. </div>

图表的选项。我得到错误- ticks没有属性major。在控制台中它只有字符串- Mo/Th/Fr/We/Su...:

  1. const lineOptions = {
  2. animation: {
  3. duration: 0
  4. },
  5. layout:{
  6. padding:{
  7. left: 0,
  8. right: 0,
  9. top: 5,
  10. bottom: 0
  11. }
  12. },
  13. maintainAspectRatio: false,
  14. scales: {
  15. xAxes: [{
  16. display: true,
  17. gridLines: {
  18. display: true,
  19. tickMarkLength: 1,
  20. },
  21. ticks: {
  22. fontSize: 9,
  23. fontColor: 'rgba(0, 0, 0, 0.4)',
  24. maxRotation: 0,
  25. major: {
  26. enabled: true,
  27. fontStyle: 'bold',
  28. fontColor: 'red'
  29. },
  30. },
  31. afterBuildTicks: (scale, ticks) => {
  32. ticks.forEach(t => {
  33. t.major = (t === 'Su');
  34. });
  35. return ticks;
  36. },
  37. }],
  38. yAxes: [{
  39. display: false,
  40. gridLines: {
  41. display: false,
  42. tickMarkLength: 1,
  43. },
  44. }],
  45. },
  46. };
bejyjqdl

bejyjqdl1#

根据Chart.js文档,选项ticks.fontColor不接受array,而接受简单的string
但是,您可以通过ticks.major配置定义周末ticks的风格,如下所示。

  1. ticks: {
  2. major: {
  3. enabled: true,
  4. fontStyle: 'bold',
  5. fontColor: 'red'
  6. }
  7. },

此外,您还需要通过afterBuildTicks回调将所需的分笔成交点标记为major

  1. afterBuildTicks: (scale, ticks) => {
  2. ticks.forEach(t => {
  3. const day = new Date(t.value).getDay();
  4. t.major = (day == 0 || day == 6);
  5. });
  6. return ticks;
  7. }

请看一下下面的可运行代码片段。
第一个

展开查看全部

相关问题