ChartJS 如何在水平条形图上删除所有网格线、边框并仅显示刻度

kgsdhlau  于 2023-02-16  发布在  Chart.js
关注(0)|答案(2)|浏览(192)

this post中的答案
我有以下图表,也可以在here中找到

const d0 = new Date("2023-02-15T00:00:00.721Z").getTime()
    const d1 = new Date("2023-02-15T01:00:00.721Z").getTime()
    const d2 = new Date("2023-02-15T02:30:00.721Z").getTime()
    const d3 = new Date("2023-02-15T03:20:00.721Z").getTime()
    const d4 = new Date("2023-02-15T05:05:00.721Z").getTime()
    let values = [d0, d1, d2, d3, d4];

    let data = {
        labels: [''],
        datasets: [{
          label: 'up',
          axis: 'y',
          data: [d1],
          backgroundColor: 'red',
        },{
          label: 'down',
          axis: 'y',
          data: [d2],
          backgroundColor: 'yellow',
        },{
          label: 'out',
          axis: 'y',
          data: [d3],
          backgroundColor: 'green',
        },{
          label: 'up',
          axis: 'y',
          data: [d4],
          backgroundColor: 'red',
        }
      ]
      };

    const config = {
    data,
    type: 'bar',
        options:{
          elements: {
            bar: {
              borderWidth: 0
            }
          },
          ticks: {
              display: true
            },

          interaction: {
                mode: 'dataset'
            },
          tooltip: {
             mode: 'dataset'  
          },
          hover: {
               mode: 'dataset'            
            },
           onClick: function (e) {
                           // debugger;
                            var activePointLabel = 
                              this.getElementsAtEventForMode(e, 'dataset', { intersect: true }, false)
                            alert(activePointLabel[0].datasetIndex);
                        }
                    ,      
        plugins: {
          legend: {
            display: false,
          },
          title: {
            display: false,
          },
        },
        indexAxis: 'y',
        responsive: true,
        maintainAspectRatio: false,
        scales: {
          x: {
            grid: {
              display: true
            },
            min: d0,
            ticks: {
                callback: function(value, index, ticks) {
                    return moment(value).format('HH:mm');
                }
            },
          //  afterBuildTicks: axis => axis.ticks = values.map(v => ({ value: v }))
          },
          y: {
             grid: {
              display: false
            },
            
            stacked: true
          },
        }    
      }};
      
      new Chart(document.getElementById("chart"), config);

它产生了这个...


] 1
我想去掉所有的网格线,除了勾号,但是如果我设置网格显示为false,勾号也会消失,并且它还会在图表周围留下一个边框,etg类似于

有什么办法吗?

qeeaahzv

qeeaahzv1#

只需将border: {display: false}添加到scales配置中即可。here is the link to the documentation

...
scales: {
  x: {
    border: {
      display: false,
    },
    ...
  },
  ...
}
...
    • 更新添加的完整运行示例:**

x一个一个一个一个x一个一个二个x

bnlyeluc

bnlyeluc2#

使用v3.8.2的tickColor(参见以下配置)

scales: {
          x: {
            ...
            grid: {
              display: true,
              drawTicks: true,  //show ticks
              borderColor: "transparent", //horizontal line color above ticks (x-axis)
              color: "transparent",   //grid lines color
              tickColor: "#868e96"  //ticks color (little line above points)
            },
          },
    
          ...
     }

相关问题