简体   繁体   English

ChartJS:具有多个数据集的水平条不显示条

[英]ChartJS: Horizontal Bar with multiple datasets not showing Bars

I have Horizontal Bar comparing Rooms & Guests target vs achieved.我有单杠比较房间和客人目标与实现。 Rooms has 3 data values (Available, Budget, Actual) and Guests has only 2 (Budget, Actual).房间有 3 个数据值(可用、预算、实际),客人只有 2 个(预算、实际)。

Below is the code generates the chart:下面是生成图表的代码:

 data = [{
        label: 'Available',
        backgroundColor: '#3366ff',
        data: [5580]
    }, {
        label: 'Budget',
        backgroundColor: '#009999',
        data: [5000, 6500]
    }, {
        label: 'Actual',
        backgroundColor: '#92d400',
        data: [5200, 7245]
    }];

 var myChart = new Chart(ctx, {
    type: 'horizontalBar',
    data: {
        labels: ["Rooms", "Guests"],
        datasets: data
    }
}

Issue with the above code is, Rooms Budget bar is not showing up.上面代码的问题是,房间预算栏没有显示。

It shows only when I add [5580,0] to the Guests Available data value;它仅在我将[5580,0]添加到Guests Available 数据值时显示; however there is no such Data for Guests like Rooms.但是没有像房间这样的客人的数据。

Here is the JSFiddle ( https://jsfiddle.net/kingBethal/vtf17k84/8/ ).这是 JSFiddle ( https://jsfiddle.net/kingBethal/vtf17k84/8/ )。

5000 is the lowest value in your dataset and Chart.js is creating its scale starting at 5000: 5000是数据集中的最低值,而 Chart.js 正在创建从 5000 开始的比例:

在此处输入图片说明

Set the beginAtZero property to true and you'll see the expected bar:beginAtZero属性设置为true ,您将看到预期的栏:

 let data = [{ label: 'Available', backgroundColor: '#3366ff', data: [5580] }, { label: 'Budget', backgroundColor: '#009999', data: [5000, 6500] }, { label: 'Actual', backgroundColor: '#92d400', data: [5200, 7245] }]; let myChart = new Chart(document.getElementById('chart'), { type: 'horizontalBar', data: { labels: ["Rooms", "Guests"], datasets: data }, options: { scales: { xAxes: [{ ticks: { beginAtZero: true } }] } } });
 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script> <canvas id="chart"></canvas>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM