【问题标题】:Chartjs: Double x axis not having the same gridlineChartjs:双x轴没有相同的网格线
【发布时间】:2021-03-26 08:04:08
【问题描述】:

我将 Chart.js 2.9.4 版与 jQuery 3.2.1 版一起使用,并且我使用条形图。我想要实现的是在背景中显示一个数据栏(米色),在前景中显示两个数据栏(绿色和洋红色)。问题是,我有这个设置,它显示错误 - x 轴和网格线没有正确对齐(我只想显示一个,但为了调试,图片中显示了两个)。

我的数据和比例代码是:

var activityData = {
            datasets: [
                {
                    label: 'Monthly plan',
                    backgroundColor: '#a3b13a',
                    data: [14000, 12000, 14000, 15000, 15000, 16000, 12000, 10000, 15000, 16000, 17000, 20000],
                    xAxisID: 'x-axis-1',
                    barPercentage: 0.4
                }, {
                    label: 'Month sales',
                    backgroundColor: '#a51b4d',
                    data: [14200, 12800, 20100],
                    xAxisID: 'x-axis-1',
                    barPercentage: 0.4
                },{
                    label: 'Last year sales',
                    backgroundColor: '#f8f1e5',
                    data: [13250, 10965, 14520, 13789, 14085, 15796, 10367, 9513, 14302, 14985, 16997, 18622 ],
                    xAxisID: 'x-axis-2'
                }],
            labels: monthNames
        }

        var myScales = {
            xAxes: [{
                display: true,
                stacked: true,
                id: "x-axis-2",
                type: 'category',
                ticks: {
                    beginAtZero: true
                }
            }, {
                display: true,
                stacked: false,
                id: "x-axis-1",
                type: 'category',
                ticks: {
                    beginAtZero: true
                }
            }],
            yAxes: [{
                stacked: false,
                ticks: {
                    beginAtZero: true
                }
            }]
        }

如何解决这个问题以正确显示 - 背景一栏,前景两栏?

【问题讨论】:

    标签: javascript charts chart.js bar-chart


    【解决方案1】:

    我想通了,如果有人遇到同样的问题,我必须添加 offset: true。

        var myScales = {
            xAxes: [{
                display: true,
                stacked: true,
                id: "x-axis-2",
                type: 'category',
                offset: true,
                ticks: {
                    beginAtZero: true
                }
            }, {
                display: false,
                stacked: false,
                id: "x-axis-1",
                type: 'category',
                offset: true,
                gridLines: {
                    display:false
                },
                ticks: {
                    beginAtZero: true
                }
            }],
            yAxes: [{
                stacked: false,
                ticks: {
                    beginAtZero: true
                }
            }]
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多