Javascript图表填满整个浏览器窗口

时间:2016-08-05 12:03:53

标签: javascript html charts chart.js

我正在使用chart.js创建我的第一个图表。

图表显示方式与我想要的一样,只是它总是填满整个浏览器屏幕。

我尝试将图表尺寸设置为100,但它似乎无法正常工作。

我创建了一个展示我的问题的jsFiddle:

https://jsfiddle.net/1q5oej4q/

此外,如果您想在此处查看代码,请输入以下代码:

使用Javascript:

$(function() {
    displayLineChart();

    function displayLineChart() {
        var data = {
            labels: ['first', 2, 3, 4, 5, 6, 7, 8, 9, 10],
            datasets: [{
                label: "Prime and Fibonacci",
                fillColor: "rgba(220,220,220,0.2)",
                strokeColor: "rgba(220,220,220,1)",
                pointColor: "rgba(220,220,220,1)",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(220,220,220,1)",
                data: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
            }, {
                label: "My Second dataset",
                fillColor: "rgba(151,187,205,0.2)",
                strokeColor: "rgba(151,187,205,1)",
                pointColor: "rgba(151,187,205,1)",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(151,187,205,1)",
                data: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
            }]
        };

        var chart = document.getElementById("lineChart");
        chart.width = 100;
        chart.height = 100;

        var ctx = document.getElementById("lineChart").getContext("2d");
        ctx.canvas.width = 100;
        ctx.canvas.height = 100;
        var options = {};
        var lineChart = new Chart(ctx, {
            type: 'line',
            data: data,
        });
    }
});

HTML:

<canvas id="lineChart">
</canvas>

2 个答案:

答案 0 :(得分:1)

将其包裹在<div>中,然后调整大小:

CSS:

#wrapper {
  width: 400px;
  height: 500px;
}

HTML:

<div id="wrapper">
    <canvas id="lineChart">
    </canvas>
</div>

jsFiddle

答案 1 :(得分:0)

您需要填写options并将其作为参数传递给图表。

$(function() {
    displayLineChart();

    function displayLineChart() {
        var data = {
            labels: ['first', 2, 3, 4, 5, 6, 7, 8, 9, 10],
            datasets: [{
                label: "Prime and Fibonacci",
                fillColor: "rgba(220,220,220,0.2)",
                strokeColor: "rgba(220,220,220,1)",
                pointColor: "rgba(220,220,220,1)",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(220,220,220,1)",
                data: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
            }, {
                label: "My Second dataset",
                fillColor: "rgba(151,187,205,0.2)",
                strokeColor: "rgba(151,187,205,1)",
                pointColor: "rgba(151,187,205,1)",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(151,187,205,1)",
                data: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
            }]
        };

        var chart = document.getElementById("lineChart");
        chart.width = 500;
        chart.height = 500;

        var ctx = document.getElementById("lineChart").getContext("2d");
        ctx.canvas.width = 500;
        ctx.canvas.height = 500;
        var options = {
            responsive: false,
            maintainAspectRatio: true
        };
        var lineChart = new Chart(ctx, {
            type: 'line',
            data: data,
            options: options
        });
    }
});