javascript - 如何在 Angular chart.js 中更改 Canvas 高度和宽度

标签 javascript html css angularjs canvas

我正在使用 angular chart.js 开发我的第一个示例代码,我无法更改自定义高度和宽度,如何更改 Canvas 的高度和宽度。

代码:

CSS
#我的图表{ 宽度:500px; 高度:500px;

HTML

<body ng-controller="LineCtrl">
<canvas id="myChart" ></canvas>
<div class="chart-container">
        <linechart data="data" options="options" mode=""></linechart>
</div>

Javascript

var data = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [
        {
            label: "My First dataset",
            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: [65, 59, 80, 81, 56, 55, 40]
        },
        {
            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: [28, 48, 40, 19, 86, 27, 90]
        }
    ]
};
var options = {
        scaleOverlay : false,
        scaleOverride : false,
        scaleSteps : null,
        scaleStepWidth : null,
        scaleStartValue : null,
        scaleLineColor : "rgba(0,0,0,.1)",
        scaleLineWidth : 1,
        scaleShowLabels : true,
        scaleLabel : "<%=value%>",
        scaleFontFamily : "'proxima-nova'",
        scaleFontSize : 10,
        scaleFontStyle : "normal",
        scaleFontColor : "#909090", 
        scaleShowGridLines : true,
        scaleGridLineColor : "rgba(0,0,0,.05)",
        scaleGridLineWidth : 1, 
        bezierCurve : true,
        pointDot : true,
        pointDotRadius : 3,
        pointDotStrokeWidth : 1,
        datasetStroke : true,
        datasetStrokeWidth : 2,
        datasetFill : true,
        animation : true,
        animationSteps : 60,
        animationEasing : "easeOutQuart",
        onAnimationComplete : null
    }
    var ctx=document.getElementById("myChart").getContext("2d");
var myLineChart = new Chart(ctx).Line(data, options);
//console.log(myLineChart)

最佳答案

我通过更改一些选项属性并在上下文中设置宽度和高度来做到这一点:-

var options = { 
    responsive: false,
    maintainAspectRatio: false
}

在 Js 中

var ctx=document.getElementById("myChart").getContext("2d");

ctx.canvas.width = 100;
ctx.canvas.height = 100;

var myLineChart = new Chart(ctx).Line(data, options);

关于javascript - 如何在 Angular chart.js 中更改 Canvas 高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28250375/

相关文章:

javascript - 如何将窗口的Y中心设置为元素的Y中心?

javascript - Angular 通用和翻译

html - 如何为引导登录页面创建一个简单的纯 CSS 图库/幻灯片?

javascript - LESS/CSS 在输入中添加第二个类以覆盖第一个类的宽度

html - CSS 格式问题?

css - 多类样式链接

html - 如何使用 CSS 创建平铺背景按钮

javascript - jQuery UI slider 在 DIV 下方被选中

javascript - jQuery 获得某些类而不是其他类

jquery - 如何在 ios 上获取 jQuery 警报?