【问题标题】:White Space in stacked bar using chart.js使用 chart.js 堆积条形图中的空白
【发布时间】:2018-08-02 12:36:25
【问题描述】:

我正在使用 chart.js 版本 2.7.2 来绘制一些图表。 图表是条形图,我使用时间轴来设置数据集点。

如果我使用普通图,数据集显示正确,但如果我设置堆叠轴,则在第一项渲染显示一个奇怪的空白!

查看图表,stacked:false 一切正常,stacked:true 在第一个柱上有一个奇怪的空白! 有没有办法去除那个空白? 谢谢

function newDate(days) {
	return moment().add(days, 'd').toDate();
}

const lineData = {
  labels: null,
  datasets: 
  [
    {
      label: 'A',
      fill: false,
      backgroundColor: "hsl(50, 50%, 80%)",
      data: 
      [
        {
          x: newDate(1),
          y: 12
        },
        {
          x: newDate(2),
          y: 15
        }, 
      ],
    },
    {
      label: 'B',
      fill: false,
      backgroundColor: "hsl(250, 50%, 80%)",
      data: 
      [
        {
          x: newDate(0),
          y: 23
        },
        {
          x: newDate(2),
          y: 34
        },
        {
          x: newDate(3),
          y: 45
        },
      ]
    }
  ],
}

const lineOptions = {
type: 'bar',
	data: lineData,
	options: 
	{
		title: 
		{
	display: true,
  text: 'STACKED TRUE'
},
	legend: 
	{
	    display: true,
	},
	hover: 
	{
		mode: 'nearest'
	},
	fill: false,
	responsive: true,
	scales: 
	{
	xAxes: 
	[
			{
				stacked: true,
				display: true,
				ticks: 
				{							
					beginAtZero: false,
				},
				scaleLabel: 
				{
					display: true,
					labelString: "Day",
				},
				barThickness: 15,
				type: 'time',
				distribution: 'linear',
				bounds: 'ticks',
				time: 
				{
					unit: 'day',
					unitStepSize: 1,
					displayFormats: 
					{
						'day': 'DD/MM',
					},
					tooltipFormat: 'DD/MM',
				}
			}
		],
		yAxes: 
		[
			{
				ticks: 
				{
					beginAtZero: true,
				},
				display: true,
				stacked: true,
				scaleLabel: 
				{
					display: true,
					labelString: "Items",
				}
			}
		]
	},
	tooltips: 
	{
		mode: 'x',
	},
}
}
const chart = new Chart(document.getElementById("chart").getContext("2d"), lineOptions);


const lineOptions2 = {
	type: 'bar',
	data: lineData,
	options: 
	{
		title: 
		{
	  display: true,
    text: 'STACKED FALSE'
  },
	legend: 
	{
		display: true
	},
	hover: 
	{
		mode: 'nearest'
	},
	fill: false,
	responsive: true,
	scales: 
	{
		xAxes: 
		[
			{
				//stacked: true,
				display: true,
				ticks: 
				{							
					beginAtZero: false,
				},
				scaleLabel: 
				{
					display: true,
					labelString: "Day",
				},
				barThickness: 15,
				type: 'time',
				distribution: 'linear',
				bounds: 'ticks',
				time: 
				{
					unit: 'day',
					unitStepSize: 1,
					displayFormats: 
					{
						'day': 'DD/MM',
					},
					tooltipFormat: 'DD/MM',
				}
			}
		],
		yAxes: 
		[
			{
				ticks: 
				{
					beginAtZero: true,
				},
				display: true,
				//stacked: true,
				scaleLabel: 
				{
					display: true,
					labelString: "Items",
				}
			}
		]
	},
	tooltips: 
	{
		mode: 'x',
	},
}
}
const chart2 = new Chart(document.getElementById("chart2").getContext("2d"), lineOptions2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>

<body>
    <canvas id="chart" width="600" height="400"></canvas>
    <canvas id="chart2" width="600" height="400"></canvas>
</body>

https://jsfiddle.net/tgfy6q82/42/

【问题讨论】:

    标签: javascript charts chart.js stacked-chart


    【解决方案1】:

    您将xAxes 和yAxes 都设置为stacked:true。

    在yAxes 上注释掉stacked:true 即可;

    function newDate(days) {
        return moment().add(days, 'd').toDate();
    }
    
    const lineData = {
        labels: null,
        datasets: [{
                label: 'A',
                fill: false,
                backgroundColor: "hsl(50, 50%, 80%)",
                data: [{
                        x: newDate(1),
                        y: 12
                    },
                    {
                        x: newDate(2),
                        y: 15
                    },
                ],
            },
            {
                label: 'B',
                fill: false,
                backgroundColor: "hsl(250, 50%, 80%)",
                data: [{
                        x: newDate(0),
                        y: 23
                    },
                    {
                        x: newDate(2),
                        y: 34
                    },
                    {
                        x: newDate(3),
                        y: 45
                    },
                ]
            }
        ],
    }
    
    const lineOptions = {
        type: 'bar',
        data: lineData,
        options: {
            title: {
                display: true,
                text: 'STACKED TRUE'
            },
            legend: {
                display: true,
            },
            hover: {
                mode: 'nearest'
            },
            fill: false,
            responsive: true,
            scales: {
                xAxes: [{
                    stacked: true,
                    display: true,
                    ticks: {
                        beginAtZero: false,
                    },
                    scaleLabel: {
                        display: true,
                        labelString: "Day",
                    },
                    barThickness: 15,
                    type: 'time',
                    distribution: 'linear',
                    bounds: 'ticks',
                    time: {
                        unit: 'day',
                        unitStepSize: 1,
                        displayFormats: {
                            'day': 'DD/MM',
                        },
                        tooltipFormat: 'DD/MM',
                    }
                }],
                yAxes: [{
                    ticks: {
                        beginAtZero: true,
                    },
                    display: true,
                    // stacked: true,
                    scaleLabel: {
                        display: true,
                        labelString: "Items",
                    }
                }]
            },
            tooltips: {
                mode: 'x',
            },
        }
    }
    const chart = new Chart(document.getElementById("chart").getContext("2d"), lineOptions);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
    
    <body>
        <canvas id="chart" width="600" height="400"></canvas>
    </body>

    另外,由于应该设置yAxes stacked:true,因此可以更新数据,以便存在A 和B 的数据,即使其中之一是0。https://jsfiddle.net/zqrp0w76/2/

    【讨论】:

    • 好的,您的解决方案很好,但是使用 yAxes 堆叠:false,条形的高度不会计算为单日的总和,而是所有数据集仅堆叠在前面其他,我需要所有数据集一个在另一个之上。
    • @user473349 如果您想让 yAxes 堆叠:true,您可以修复数据以包含一种颜色为 0 的日期。jsfiddle.net/zqrp0w76/2 更新了答案~
    • 所以如果不编辑数据集就不可能,对于与 x,y 轴堆叠使用,数据集的长度必须相同。
    • 你的数据集应该有相同的日期,因为它只是更干净的数据。但是,是的,必须做出一些改变才能进行修复。
    • 好的,这涉及到一些工作要做,因为在生产中数据来自 ajax。进一步的工作将通过 javascript 完成,以使所有数据集具有相同的日期。我会建议对 chart.js 团队进行更改。感谢您的回复。
    猜你喜欢
    • 2018-05-04
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    相关资源
    最近更新 更多