Resize Canvas Chart

Asked

Viewed 1,144 times

3

I’m using Chartjs and I have a radar graph.
See the code below: Also have that fiddle.

var radarChartData = {
		labels: ["Item1", "Item2", "Item3", "Item4"],
    
    datasets: [
			{
				label: "Linha1",
				fillColor: "rgba(220,220,220,0)",
				strokeColor: "rgba(220,220,220,1)",
				pointColor: "rgba(220,220,220,1)",
				pointStrokeColor: "#fff",
				pointHighlightFill: "#fff",
				pointHighlightStroke: "rgba(220,220,220,1)",
				data: [2,2,2,2]
			},
			{
				label: "Linha2",
				fillColor: "rgba(151,187,205,0)",
				strokeColor: "rgba(151,187,205,1)",
				pointColor: "rgba(151,187,205,1)",
				pointStrokeColor: "#fff",
				pointHighlightFill: "#fff",
				pointHighlightStroke: "rgba(151,187,205,1)",
				data: [8,8,8,8]
			}
		]
	};

	window.myRadar = new Chart(document.getElementById("canvas").getContext("2d")).Radar(radarChartData, {
            responsive: true,
        //scaleShowLabels : true,
        animationSteps: 80,
        animationEasing: "easeOutQuart",
        scaleOverride: true,
        scaleSteps: 10,
        scaleStepWidth: 1,
        scaleStartValue: 0,
        angleShowLineOut : false,
        scaleLineColor: "rgba(0, 0, 0, 1)",
        
        legendTemplate : '<% for (var i=0; i<datasets.length; i++) { %>'
                    +'<h3 style=\"color:<%=datasets[i].strokeColor%>\">.'
                    +'<% if (datasets[i].label) { %><%= datasets[i].label %><% }%>'
                +'<% } %></h3>',
        
        //Number - Pixel width of the angle line
    angleLineWidth : 100,

    //String - Point label font declaration
    pointLabelFontFamily : "Arial",

    //String - Point label font weight
    pointLabelFontStyle : "normal",

    //Number - Point label font size in pixels
    pointLabelFontSize : 20,

    //String - Point label font colour
    pointLabelFontColor : "black",
         // String - Template string for single tooltips
            tooltipTemplate: "<%if (label){%><%=label %>: <%}%><%= value %>",
            // String - Template string for multiple tooltips
            multiTooltipTemplate: "<%= datasetLabel %> : <%= value %>",
        });

document.getElementById("legendDiv").innerHTML = window.myRadar.generateLegend();
#canvas-container {
        width: 100%;
        text-align: center;
    }

    canvas {
        display: inline;
        background-color: green;
    }
<script src="http://www.chartjs.org/assets/Chart.js"></script>
<div id="canvas-container">
            <canvas id="canvas"></canvas>
        </div>
<div id="legendDiv"></div>

The point is, I’d like the chart to take advantage of all the horizontal space, but I can’t change the width.
What I got is this: inserir a descrição da imagem aqui


But I would like this result (if possible without losing quality, since we are talking about canvas): inserir a descrição da imagem aqui

responsive: true has not solved my problem.
Could someone help me?

  • Try using version 2.0-alpha3 in "Releases". Contains "samples" as well.

1 answer

1

What you can do is use the canvas transformation methods to stretch the graph, but then you lose the graph mouse events (they get "crooked")

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

ctx.translate(-canvas.width*0.5, 0);
ctx.scale(canvas.width / (myRadar.scale.size), 1);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.