chart.js在空白数据上不可见

时间:2017-04-24 12:25:24

标签: javascript chart.js

即使没有数据,我也希望显示图表。在那种情况下,x轴和y轴显示可见。这是我用过的代码。即使没有数据,如何才能看到图表。

<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.4/Chart.min.js"></script>
    <style type="text/css">

    </style>

</head>
<body>
    <canvas id="myChart"></canvas>
    <script>
        const ctx = document.getElementById('myChart');
        var barChart = new Chart(ctx,{
            type: 'line',
            data:{
                datasets: [
                    {
                        fill: false,
                        lineTension: 0.1,
                        backgroundColor: "#22a7f0",
                        borderColor: "#22a7f0",
                        borderCapStyle: 'butt',
                        borderDash: [],
                        borderDashOffset: 0.0,
                        borderJoinStyle: 'miter',
                        pointBorderColor: "rgba(75,192,192,1)",
                        pointBackgroundColor: "#fff",
                        pointBorderWidth: 1,
                        pointHoverRadius: 5,
                        pointHoverBackgroundColor: "rgba(75,192,192,1)",
                        pointHoverBorderColor: "rgba(220,220,220,1)",
                        pointHoverBorderWidth: 2,
                        pointRadius: 5,
                        pointHitRadius: 40,
                        data: []
                    }
                ]                                                                                                                                                           
            },
            options: {
                legend: {
                    display: false
                },
                tooltips: {
                    enabled: true,
                    //mode: 'single',
                    mode: 'label'
                },
                maintainAspectRatio: false,
                scales: {
                    xAxes: [{
                            gridLines: {
                                display:false
                            }
                        }],
                    yAxes: [{
                            gridLines: {
                                display:false
                            }

                        }]
                }
            }
        });
    </script>
</body>

1 个答案:

答案 0 :(得分:3)

如果您只想显示图表,则无需设置gridLines选项。它默认显示。

此外,labels对象中没有data因此,网格线未正确显示。

这是固定版本:

const ctx = document.getElementById('myChart');
var barChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: [1, 2, 3, 4, 5, 6],
        datasets: [{
            fill: false,
            lineTension: 0.1,
            backgroundColor: "#22a7f0",
            borderColor: "#22a7f0",
            borderCapStyle: 'butt',
            borderDash: [],
            borderDashOffset: 0.0,
            borderJoinStyle: 'miter',
            pointBorderColor: "rgba(75,192,192,1)",
            pointBackgroundColor: "#fff",
            pointBorderWidth: 1,
            pointHoverRadius: 5,
            pointHoverBackgroundColor: "rgba(75,192,192,1)",
            pointHoverBorderColor: "rgba(220,220,220,1)",
            pointHoverBorderWidth: 2,
            pointRadius: 5,
            pointHitRadius: 40,
            data: []
        }]
    },
    options: {
        legend: {
            display: false
        },
        tooltips: {
            enabled: true,
            mode: 'label'
        },
        maintainAspectRatio: false,
        scales: {
            yAxes: [{
                ticks: {
                    min: 0,
                    max: 500
                }
            }]
        }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<canvas id="myChart"></canvas>