javascript - Add element to existing Canvas -
i have this canvas, using chartjs , make legend merge in graphic canvas.
see code:
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 : '<% (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 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 single tooltips tooltiptemplate: "<%if (label){%><%=label %>: <%}%><%= value %>", // string - template string multiple tooltips multitooltiptemplate: "<%= datasetlabel %> : <%= value %>", }); document.getelementbyid("legenddiv").innerhtml = window.myradar.generatelegend();
#canvas-container { width: 100%; text-align: center; } canvas { display: inline; }
<script src="http://www.chartjs.org/assets/chart.js"></script> <div id="canvas-container"> <canvas id="canvas"></canvas> </div> <div id="legenddiv"></div>
i entering legend in div
. there way insert canvas
?
wish both graph legend part of single canvas
.
me?
i managed solve problem in this fiddle.
generated new canvas html2canvas.
code:
$("#newcanvas").click(function () { var $conteudo = $("#canvas-container"); html2canvas($conteudo[0], { onrendered: function (canvas) { $('#canvas-container').after(canvas); var base64 = canvas.todataurl('image/jpeg'); console.log(base64); }, logging: true }); }); 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 : '<% (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 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 single tooltips tooltiptemplate: "<%if (label){%><%=label %>: <%}%><%= value %>", // string - template string multiple tooltips multitooltiptemplate: "<%= datasetlabel %> : <%= value %>", }); document.getelementbyid("legenddiv").innerhtml = window.myradar.generatelegend();
#canvas-container { width: 100%; border: 2px solid #8ac007; background-color: white; } canvas { display: inline; background-color: white; } #legenddiv { text-align: left; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <script src="http://html2canvas.hertzen.com/build/html2canvas.js"></script> <script src="http://www.chartjs.org/assets/chart.js"></script> <div id="canvas-container"> <canvas id="canvas"></canvas> <div id="legenddiv"></div> </div> <br/> <input id="newcanvas" type="button" value="new canvas now!"></input>
Comments
Post a Comment