ChartJS标签沿边缘沿着

pxy2qtax  于 2023-10-18  发布在  Chart.js
关注(0)|答案(1)|浏览(146)

是否有可能按比例分布X标签,强制性地将它们放在最左和最右的边缘?

scales: {
    xAxes: {
      grid: { display: false },
      ticks: { autoSkip: true, maxTicksLimit: 5, maxRotation: 0, minRotation: 0 },
    },

scales: {
    xAxes: {
      grid: { display: false },
      ticks: { autoSkip: true, maxTicksLimit: 5, maxRotation: 0, minRotation: 0 },
      type: 'time',
      time: {
        unit: 'month',
      },
    },

bnlyeluc

bnlyeluc1#

你可以使用axes回调来设置不常见/未覆盖的属性,但这样做会失去scales代码中内置的一些服务和保护。特别是,如果图表是静态的,并且您确定tick标签永远不会重叠,您可以使用afterBuildTicks回调来设置时间刻度的tick位置-tick的位置是其value属性,在unix timestamp中。
下面是一个示例:

const d0 = new Date('2021-09-01').valueOf(),
    d1 = new Date('2023-09-10').valueOf(),
    N = 100;

const labels1 = Array.from({length: N}, (_, i) => {
    return d0 + (d1-d0)/(N-1)*i;
});
const data = {
    labels: labels1,
    datasets: [
        {
            label: 'Bars',
            data: Array.from({length: N}, (_, i) => Math.random() < 0.4 && i !== 0 && i !== N-1 ? 0 : 5 + Math.random() * 10),
            backgroundColor: 'rgba(128, 196, 32, 0.85)',
        },
        {
            label: 'Line',
            type: 'line',
            data: Array.from({length: N}, (_, i) => 5 + i/N * 10 + Math.random() / 2),
            fill: true,
            pointRadius: 0,
            backgroundColor: 'rgba(196, 196, 196, 0.5)',
        }
    ]
};

const config = {
    type: 'bar',
    data: data,
    options: {
        responsive: true,
        scales:{
            x:{
                type: 'time',
                grid: { display: false },
                time:{
                    unit: "day",
                    displayFormats: {
                        day: 'MMM yyyy'
                    }
                },
                  ticks: {autoSkip: true, maxTicksLimit: 5, //stepSize: (d1-d0)/4/3600/1000/24,
                    maxRotation: 0, minRotation: 0
                },
                min: d0 - 5*24*36e5, // add a margin of 5 days to make sure the data fits
                max: d1 + 5*24*36e5,

                afterBuildTicks(scale){
                    const nTicks = scale.options.ticks.maxTicksLimit ?? 10;
                    scale.ticks = Array.from({length: nTicks}, (_, i)=>({value: (d1-d0)/(nTicks-1)*i+d0}));
                }
            },
            y:{
                display: false
            }
        },
        plugins: {
            legend: {
                display: false
            },
            title: {
                display: true,
                text: ''
            }
        }
    },
};

new Chart('chart1', config);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.0/chart.umd.js" integrity="sha512-6HrPqAvK+lZElIZ4mZ64fyxIBTsaX5zAFZg2V/2WT+iKPrFzTzvx6QAsLW2OaLwobhMYBog/+bvmIEEGXi0p1w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script src="https://cdn.jsdelivr.net/npm/luxon@^3"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-luxon@^1"></script>

  <div style="min-height: 60vh">
    <canvas id="chart1">
    </canvas>
  </div>

相关问题