Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change canvas height and width in angular chart.js

I am developing my first sample code using angular chart.js,I am unable to change custom height and width, How can i change height and width of my canvas.

CODE:

CSS
#myChart{ width:500px; height: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)
like image 468
venky Avatar asked Jan 31 '15 10:01

venky


3 Answers

I did it with changing some options attribute and set width and height in context:-

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

In Js

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

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

var myLineChart = new Chart(ctx).Line(data, options);
like image 86
squiroid Avatar answered Sep 28 '22 08:09

squiroid


I did by adding via canvas style. Tested with Pie Chart. Example:

<canvas id="pie" class="chart chart-pie" style="height:40px;"
  chart-data="pieChart.data" chart-labels="pieChart.labels"
  chart-colours="pieChart.colours"
  chart-options="pieChart.options">
</canvas>

Code:

$scope.pieChart = {};
$scope.pieChart.labels = ['Label 1', 'Label 2'];
$scope.pieChart.data = [10,20];
$scope.pieChart.colours = ['#1EF9A1', '#FD1F5E'];
// This option is important!!!
$scope.pieChart.options =  {
  responsive: false,
  maintainAspectRatio: false
}
like image 22
cainhs Avatar answered Sep 28 '22 06:09

cainhs


You can actually use the width and height attribute within the canvas tag. Example:

<canvas id="bar"
     height="100"
     width="100"
     class="chart chart-bar" 
     chart-data="dataChart"
     chart-labels="labels"
     chart-click="onClick"
     chart-options="options">
</canvas>
like image 29
Kennyb Avatar answered Sep 28 '22 06:09

Kennyb