Canvas not shown on page using Raphael.js in web2py - raphael

Goal: use Raphael.js to draw data chart/lines in web2py framework.
What I have learned from http://raphaeljs.com/ is that, I just need to include the JS into the HTML page and then I can use the APIs. But here is what I have done:
(1) First, I copied both raphael.js and raphael-min.js to myapp\static\js folder.
(2) Then I wrote these in the HTML file:
<script type="text/javascript" src="{{=URL('static','js/raphael.js')}}">
window.onload = function() {
var paper = new Raphael(0, 0, 100, 100);
var circle = paper.circle(50, 50, 50);
circle.attr("fill", "#f00");
circle.attr("stroke", "#fff");
}
</script>
This file is periodically loaded to a div in the index.html.
I expected a circle could be shown but nope, though I was very sure this page was periodically loaded.
Did I do anything wrong here (of course I did)? Thanks for your help!

Try using separate script tags to load the library and then run your code:
<script type="text/javascript" src="{{=URL('static','js/raphael.js')}}"></script>
<script>
window.onload = function() {
var paper = new Raphael(0, 0, 100, 100);
var circle = paper.circle(50, 50, 50);
circle.attr("fill", "#f00");
circle.attr("stroke", "#fff");
}
</script>

I'm not sure if this is exactly your question but instead of importing just one and then writing the other in the file, you can do it all in one src assigning like so:
<script type="text/javascript" src="{{=URL('static','js/raphael.js')}} {{=URL('static', 'other_function.whatever_extension}}">
This will import both scripts. You can add as many as you wish and even change the paths.

Related

Wkhtmltopdf does not render Chart.JS 2.5.0 graph

Using:
WKpdftohml version 0.12.3.2
PHPwkhtmltopdf version 2.2.0
Chart.JS version 2.5.0
I'm trying to print a line graph using the above libraries. I can reproduce a pdf using the shell command: wkhtmltopdf --javascript-delay 5000 " http://netdna.webdesignerdepot.com/uploads7/easily-create-stunning-animated-charts-with-chart-js/chartjs-demo.html" test2.pdf
So there is no problem with WKhtmltopdf.
The problem is when I do it in my app, using the PHPwkhtmltopdf library. I get a blank page.
From my research these are the things I tried:
Added 'javascript-delay' => 500 to Chart.JS options;
Added animation:{onComplete: function () {window.JSREPORT_READY_TO_START =true} to Chart.JS options;
Added <div style="width:800px;height:200;font-size:10px;"> to the parent div of canvas html tag
Added ctx.canvas.width = 800;ctx.canvas.height = 200; to javascript initialization of the chart.
Well nothing worked. I love Chart.JS and WKhtmltopdf, but if I can't print I'll have to drop one of them. Is there any solution?
This is my php code for the PHPwkhtmltopdf:
public function imprimir ($request, $response)
{
// include_once 'config/constants.php';
// include_once 'resources/auxiliar/helpers.php';
$folha = $_POST['printit'];
$variaveis = explode(',', $folha);
$nomeFicheiro = $variaveis[0];
$printName = substr($nomeFicheiro, 5);
if (isset($variaveis[2])) {
$_SESSION['mesNumero'] = $variaveis[2];
$_SESSION['mes'] = $variaveis[1];
} else {
$mesNumero = 0;
$mes = '';
}
ob_start();
if ($nomeFicheiro == 'printPpiam') {
require ('C:/xampp/htdocs/.../'.$nomeFicheiro.'.php');
} else {
require ('C:/xampp/htdocs/.../'.$nomeFicheiro.'.php');
}
$content = ob_get_clean();
// You can pass a filename, a HTML string, an URL or an options array to the constructor
$pdf = new Pdf($content);
// On some systems you may have to set the path to the wkhtmltopdf executable
$pdf->binary = 'C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf';
$pdf -> setOptions(['orientation' => 'Landscape',
'javascript-delay' => 500,
// 'enable-javascript' => true,
// 'no-stop-slow-scripts' => true]
]);
if (!$pdf->send($printName.'.pdf')) {
throw new Exception('Could not create PDF: '.$pdf->getError());
}
$pdf->send($printName.'.pdf');
}
# Update 1
Made a php file with the page output. Run it in the browser and the graph rendered. When I do it in the console it renders everything except the graph!
How can it be wkhtmltopdf renders the graphics in this page : http://netdna.webdesignerdepot.com/uploads7/easily-create-stunning-animated-charts-with-chart-js/chartjs-demo.html but not my own?!
# Update 2
After Quince's comment, I tried just turning the animations off, but I'm not sure on how to do that. I tried:
$pdf -> setOptions(['orientation' => 'Landscape',
'javascript-delay' => 500,
// 'window-status' => 'myrandomstring ',
'animation' => false,
'debug-javascript',
'no-stop-slow-scripts',
]);
But it fails.
Here's the code that works with wkhtmltopdf version 0.12.5:
chart.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<style>
.reportGraph {width:900px}
</style>
</head>
<body>
<div class="reportGraph"><canvas id="canvas"></canvas></div>
<script type="text/javascript">
// wkhtmltopdf 0.12.5 crash fix.
// https://github.com/wkhtmltopdf/wkhtmltopdf/issues/3242#issuecomment-518099192
'use strict';
(function(setLineDash) {
CanvasRenderingContext2D.prototype.setLineDash = function() {
if(!arguments[0].length){
arguments[0] = [1,0];
}
// Now, call the original method
return setLineDash.apply(this, arguments);
};
})(CanvasRenderingContext2D.prototype.setLineDash);
Function.prototype.bind = Function.prototype.bind || function (thisp) {
var fn = this;
return function () {
return fn.apply(thisp, arguments);
};
};
function drawGraphs() {
new Chart(
document.getElementById("canvas"), {
"responsive": false,
"type":"line",
"data":{"labels":["January","February","March","April","May","June","July"],"datasets":[{"label":"My First Dataset","data":[65,59,80,81,56,55,40],"fill":false,"borderColor":"rgb(75, 192, 192)","lineTension":0.1}]},
"options":{}
}
);
}
window.onload = function() {
drawGraphs();
};
</script>
</body>
</html>
Run:
$ wkhtmltopdf chart.html chart.pdf:
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
Found the answer. After I created a separate file, outside the framework, i did some tests again. It rendered the graph in the browser so I tried to use the command tool WKhtmltopdf, and it did not worked, when it did with other examples (see Update #1). So there is something wrong with my php page.
Ran the same tests that I did in the framework, and got the answer for my problem. By introducing a parent div tag width dimensions in the canvas tag it made the graph render in the page.
<div style="width:800px;height:200;">
<canvas id="myChart" style="width:800px;height:200;"></canvas>
</div>
The proposition was found in this site: Github, so thanks laguiz.
Try adding this, as according to this github source
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.0.0/polyfill.min.js"></script>
Solved it by downgrading wkhtmltopdf: 0.12.4 > 0.12.2.1
chart.js version seemed to have no influence. I used 2.7.0.
Fixed width and height seem to be required as well.
Edit: Since wkhtmltopdf is dead, I switched to Puppeteer recently.
I was dealing with the same issue using rotativa to export my ASP.NET MVC page with Chart.JS to PDF with no luck.
After a couple of days I finally found a super-easy solution to achieve my goal. What I did is simply to use the .toBase64Image() method of Chart.JS to encode the chart to a base64 string variable in Javascript. Then I saved this string into a model and then on the PDF html page a used tag where i put the base64encoded string to a scr property and the result is great :-)
javascript:
//save Chart as Image
var url_base64 = document.getElementById('myChart').toDataURL('image/png');
//set the string as a value of a hidden element
document.getElementById('base64graph').value = url_base64;
PDF view:
<img style='display:block; width:900px;height:400px;position:relative;margin:auto;text-align:center;' id='base64image'
src='#Model.base64graph' />
I'm trying to improve on the answer by temuri, which is great, but a bit bloated. I ran into the OP's issues (even same WKpdftohml version) and this did the trick for me:
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<div style="width: 400px;"><canvas id="canvas"></canvas></div>
<script type="application/javascript">
// wkhtmltopdf 0.12.5 crash fix.
// https://github.com/wkhtmltopdf/wkhtmltopdf/issues/3242#issuecomment-518099192
Function.prototype.bind = Function.prototype.bind || function (thisp) {
const fn = this;
return function () {
return fn.apply(thisp, arguments);
};
};
new Chart(
document.getElementById("canvas"), {
"responsive": false,
"type":"line",
"data":{"labels":["January","February","March","April","May","June","July"],"datasets":[{"label":"My First Dataset","data":[65,59,80,81,56,55,40],"fill":false,"borderColor":"rgb(75, 192, 192)","lineTension":0.1}]},
"options":{}
}
);
</script>
I'm yet to figure out how to get the chart library via ordinary tools like npm, instead of getting it via ajax like here in the first line. Note that this can impact your chart resolution.
I was strugling with that too and you self-answer did not help my case. I am using symfony 3.3 and Chart.js 2 and whatever I did, did not work properly. So I have solved it in a different manner (maybe not a clean one) and I wanted to post it here for inspiration to others.
I needed to export a page, that I was presenting to the user in a browser. In browser, I used Javascript to get picture out of the rendered graph with
animation: {
onComplete: function(animation) {
console.log('done');
var url=document.getElementById("barChartByCountryWeight{{ part }}{{ subsetKey }}").toDataURL();
$.ajax({
url: 'saveChartImages',
type: 'POST',
data: { 'barChartByCountryWeight{{ part }}{{ subsetKey }}': url },
success: function(result) {
console.log(result);
console.log('the request was successfully sent to the server');
},
error: function (request, error) {
console.log(arguments[0]['responseText']);
console.log(" Can't do because: " + error);
}
});
}
}
And on server side I put it in session and in a controller for the PDF export, I have taken the image from session and put the image in the HTML, that is converted to PDF.
Hope that helps.
I have implemented the working code for this issue. You can check out the working code here.
NOTE: For generating pdf you must disable the Chart JS animation or add the option javascript-delay=>1000 to the wkhtmltopdf options.
I have solved this problem when I tried to use Chartjs 1 instead of a new chart js. The reason for this is because laravel snappy uses wkhtmltopdf, which doesn't support css animation, while new chartjs uses css animation.
This github issue shows that.
The solution i found is to use google chart instead. It also uses svg, so you can get high resolution charts.

How to save and show Mapbox directions?

I am using Mapbox and Leaflet.
I have a create view, where I create walking directions.
But I also need detail view, where I can show created directions.
Now I save only originMarker and destinationMarker coordinates into database;
This way I try to show routing:
<script>
var start = JSON.parse($('#start_field').val());
var finish = JSON.parse($('#finish_field').val());
L.mapbox.accessToken = 'qwerty';
var map = L.mapbox.map('map', 'mapbox.streets', {
zoomControl: false
}).setView([42.8580536, 74.6224754], 12);
L.Routing.control({
waypoints: [
L.latLng(start.lat, start.lng),
L.latLng(finish.lat, finish.lng)
]
}).addTo(map);
var start_marker = L.marker([start.lat, start.lng], {
draggable: false
}).addTo(map);
var finish_marker = L.marker([finish.lat, finish.lng], {
draggable: false
}).addTo(map);
</script>
But browser gives me an error:
Uncaught TypeError: Cannot read property 'control' of undefined
Without the full code it's kind of hard to answer but my first guess is that you didn't load the routing plugin's assets or not in the proper order (first the Leaflet script then the routing plugin, the other way around doesn't work):
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<link rel="stylesheet" href="leaflet-routing-machine.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<script src="leaflet-routing-machine.js"></script>

using a famo.us surface to link to another URL

Seems like this should be obvious but...How can you use a famo.us surface as a link to another webpage?
I've tried:
this.fooSurface.on("click", function(){
window.location.replace("www.foo.com");
});
but this doesn't replace the URL, it just puts the new URL on the end of the address currently in the URL bar. window.location.href = "www.foo.com" has the same result.
EDIT: window.location.assign("www.foo.com") and window.location = ("foo") also have the same result. I think this has something to do with this script in the boilerplate index.html:
<script type="text/javascript">
require.config({baseUrl: 'src/'});
require(['main']);
</script>
Use window.location.assign("http://www.foo.com"); instead.
I probably wouldn't use the replace() method personally, as replace() switches the current page's place in the document history with that of the one you provide to the method, which I can't say I've ever found beneficial as a user unless there's a blank intermediary login page or something very specific (and temporary).
Or you can even just use window.location = "http://www.foo.com";
https://developer.mozilla.org/en-US/docs/Web/API/Window.location
I was able to get things working just fine with the boilerplate generator-famous gives you.
The script tag has nothing to do with it. That's configuration for RequireJS to load in the famo.us library with AMD.
var logo = new ImageSurface({
size: [200, 200],
content: '/content/images/famous_logo.png',
classes: ['backfaceVisibility']
});
logo.on('click', function() {
window.location.href ='http://www.google.com';
});
This problem you're having is also not a famo.us problem. It's your Javascript...

Using an external Template in KnockoutJS

is it possible to use an external Template in KnockoutJS like this?
<script type="text/html" id="a_template" src="templates/a_template.html">
</script>
I've tried this solution but didn't get it working.
You can use jquery to dynamically load html into a script element, and then execute knockout based on that.
<script type="text/html" id="template_holder"></script>
<script type="text/javascript">
$('#template_holder').load('templates/a_template.html', function() {
alert('Load was performed.');
//knockout binding goes here
});</script>
Your knockout binding must be done in the callback function though, otherwise there's a chance that you'll be trying to bind before the page has loaded
UPDATE Here's an example I've coded on jsfiddle to demonstrate dynamic loading: http://jsfiddle.net/soniiic/2HxPp/
You could also look at:
https://github.com/ifandelse/Knockout.js-External-Template-Engine
You can also use this Template bootstrapper for KO
Bootstrapper
https://github.com/AndersMalmgren/Knockout.Bootstrap
MVC WebAPI Demo
https://github.com/AndersMalmgren/Knockout.Bootstrap.Demo
It uses a Convention over configuration approuch using this lib
https://github.com/AndersMalmgren/Knockout.BindingConventions
Meaning it will automatically understand that MyViewModel should be matched to MyView
Its also prepared to work nicely in a SPA
Disclaimer: I'm the author behind the 3 libs mentioned above
Here's a little function building off of soniiic's answer:
function loadExternalKnockoutTemplates(callback) {
var sel = 'script[src][type="text/html"]:not([loaded])';
$toload = $(sel);
function oncomplete() {
this.attr('loaded', true);
var $not_loaded = $(sel);
if(!$not_loaded.length) {
callback();
}
}
_.each($toload, function(elem) {
var $elem = $(elem);
$elem.load($elem.attr('src'), _.bind(oncomplete, $elem));
});
}
This'll automatically load all knockout templates on your document, provided their src is set and their type is "text/html". Pass in a callback to be notified when all templates loaded. No idea what happens if any of them fails.
Example usage:
<head>
<script type="text/html" src="kot/template1.html" id="template1"></script>
</head>
<body>
<script>
$(function() {
loadExternalKnockoutTemplates(function() {
// Put your ready code here, like
ko.applyBindings();
});
});
function loadExternalKnockoutTemplates(callback) {
var sel = 'script[src][type="text/html"]:not([loaded])';
$toload = $(sel);
function oncomplete() {
this.attr('loaded', true);
var $not_loaded = $(sel);
if(!$not_loaded.length) {
callback();
}
}
_.each($toload, function(elem) {
var $elem = $(elem);
$elem.load($elem.attr('src'), _.bind(oncomplete, $elem));
});
}
</script>
</body>

Automatically Embed Youtube Videos From URI

How would you go about embedding youtube vidoes automatically from the URI.
Say for example you type in a textbox:
The quick brown fox jumped over this video http://www.youtube.com/watch?v=5qm8PH4xAss&feature=player_embedded and then fell into this http://www.youtube.com/watch?v=lSCSmpV8Nn8&feature=hp_SLN&list=SL
You press submit and the results are displayed back to you. In place of the youtube links you have the actual flash files.
(In php naturally, or any language).
I know you could use Regex (preg_replace .etc), however how would you remove the extra queries that youtube provides (i.e. &feature=blah), and turn everything into something like: http://www.youtube.com/v/5qm8PH4xAss, ready to be placed into an object to embed.
Sorry for the long question,.. I hope you understand it. :)
i think you are looking for something like this:
try it here : http://www.jsfiddle.net/feridcelik/qu6U6/2/
<script src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script src="http://jquery.lukelutman.com/plugins/flash/jquery.flash.js"></script>
<script type="text/javascript">
var yt="http://www.youtube.com/v/";
$("a[href^='http://www.youtube.com/watch?v=']").each(function(){
var key;
var href =$(this).attr("href");
var i=href.indexOf("v");
href=href.substring(i+2,href.length);
i=href.indexOf("&");
if(i>-1){
key=href.substring(0,i);
}
href=yt+key;
$(this).after("<a href='"+href+"' class='video'></a>");
});
$('.video').flash(
{ height: 200, width: 200},
{ version: 8 },
function(htmlOptions) {
$this = $(this);
htmlOptions.src = $this.attr('href');
$this.before($.fn.flash.transform(htmlOptions));
}
);
$('.video').remove();
</script>
based on the flash plugin from:
http://jquery.lukelutman.com/plugins/flash/