Real time rendering using JSPlot - html

I have started using JQPlot to draw the graph in real time as well. I have got one sample example working in which it will draw the graph with two data only-
var storedData = [70, 10];
Then I have added a button, if I click on that button, it will start rendering some more data. Basically, you can say just like real time data from the server-
But the problem that I am facing currently is, as soon as I clicked on Start Updates button, it doesn't draw anything. So I believe, I messed up something the button click thing. It's been a long time I worked on Javascript and HTML thing so I have forgotten lot of things.
Below is my code-
<!DOCTYPE html>
<html>
<head>
<title>Line Charts and Options</title>
<link class="include" rel="stylesheet" type="text/css" href="../jquery.jqplot.min.css" />
<link rel="stylesheet" type="text/css" href="examples.min.css" />
<link type="text/css" rel="stylesheet" href="syntaxhighlighter/styles/shCoreDefault.min.css" />
<link type="text/css" rel="stylesheet" href="syntaxhighlighter/styles/shThemejqPlot.min.css" />
<!--[if lt IE 9]><script language="javascript" type="text/javascript" src="../excanvas.js"></script><![endif]-->
<script class="include" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<div class="colmask leftmenu">
<div class="colleft">
<div class="col1" id="example-content">
<div id="chart1" style="height: 300px; width: 500px; position: relative;"></div>
<button>Start Updates</button>
<script class="code" type="text/javascript">
$(document).ready(function(){
var storedData = [70, 10];
var plot1;
renderGraph();
$('button').click( function(){
doUpdate();
$(this).hide();
});
function renderGraph() {
if (plot1) {
plot1.destroy();
}
plot1 = $.jqplot('chart1', [storedData]);
}
var newData = [9, 1, 4, 6, 8, 2, 5];
function doUpdate() {
if (newData.length) {
var val = newData.shift();
$.post('/echo/html/', {
html: val
}, function(response) {
var newVal = new Number(response); /* update storedData array*/
storedData.push(newVal);
renderGraph();
log('New Value '+ newVal+' added')
setTimeout(doUpdate, 3000)
})
} else {
log("All Done")
}
}
function log(msg) {
$('body').append('<div>'+msg+'</div>')
}
});
</script>
<script class="include" type="text/javascript" src="../jquery.jqplot.min.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shCore.min.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushJScript.min.js"></script>
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushXml.min.js"></script>
<script class="include" type="text/javascript" src="../plugins/jqplot.canvasTextRenderer.min.js"></script>
<script class="include" type="text/javascript" src="../plugins/jqplot.canvasAxisLabelRenderer.min.js"></script>
</body>
</html>
I am not able to make the button click event work. As soon as I click on Start Update button, it does not draw anything. Correct behavior should be that it should start rendering on the graph with new datasets.
I tried to make that real-time rendering thing work from this jsfiddle
I copied same bunch of code from the above JSFiddle in my HTML but it doesn't working for me. Can anybody help me with this?

Here you go - you will have to update to your jqplot location in script tags. Also, I had to change the doUpdate to doUpdate2 as $.post will not work without jfiddle as far as I know.
<!DOCTYPE html>
<html>
<head>
<title>Line Charts and Options</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="jqplot/jquery.jqplot.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.canvasAxisTickRenderer.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.canvasTextRenderer.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.canvasAxisLabelRenderer.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.cursor.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.pointLabels.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.highlighter.min.js"></script>
<script type="text/javascript">
$(function(){
/* store empty array or array of original data to plot on page load */
var storedData = [70, 10];
/* initialize plot*/
var plot1;
renderGraph();
$('button').click( function(){
doUpdate2();
$(this).hide();
});
function renderGraph() {
if (plot1) {
plot1.destroy();
}
plot1 = $.jqplot('chart1', [storedData]);
}
var newData = [9, 1, 4, 6, 8, 2, 5];
var cp = 0;
function doUpdate2() {
var newVal = new Number(newData[cp]); /* update storedData array*/
storedData.push(newVal);
renderGraph();
log('New Value '+ newVal+' added')
if (cp < newData.length-1) setTimeout(doUpdate2, 3000)
cp++;
}
function doUpdate() {
if (newData.length) {
var val = newData.shift();
$.post('/echo/html/', {
html: val
}, function(response) {
var newVal = new Number(response); /* update storedData array*/
storedData.push(newVal);
renderGraph();
log('New Value '+ newVal+' added')
setTimeout(doUpdate, 3000)
})
} else {
log("All Done")
}
}
function log(msg) {
$('body').append('<div>'+msg+'</div>')
}
});
</script>
</head>
<body>
<div id="chart1" style="height: 300px; width: 500px; position: relative;"></div>
<button>Start Updates</button>
</body>
</html>

Related

JS not calling function

Here is my calling html code
<!DOCTYPE html>
<html>
<head>
<title>Display Card Type</title>
</head>
<script src='Card.js'; ></script>
<body>
<script>
var card = new myCardType();
var value = new myCardType();
card.setCard('I am a club');
value.setFace('I am an Ace');
card.showSuit();
value.showFace();
</script>
</body>
</html>
And here is the separate function (saved in same folder).
function myCardType(){
this.suit = suit;
this.showSuit = function(){
alert(this.suit);
}
this.face = value;
this.showFace = function() {
alert(this.face);
}
this.setCard = function (newSuit) {
this.suit = newSuit;
}
this.setFace = function (newValue) {
this.face = newValue;
}
}
Spent ages reviewing this but just cannot see what is going on, grateful for any clues.

Html5 Div Data atribute bot showing

I am trying to set atributes for my div from Json, but i cannot get it to work. Writing the values manually works, but not when setting them. i cannot figure out what i am doing wrong.
This is my code:
<html>
<head>
<title>ThingSpeak Live Colours</title>
<link rel="stylesheet" href="css/jquery.mobile.structure-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
var Temp;
var Humidity;
var Light;
$(document).ready(function () {
$.getJSON('http://api.thingspeak.com/channels/200101/feed/last.json', null, function (data) {
Temp = data["field1"];
Humidity = data["field2"];
Light = data["field3"];
var PreviewGaugeMeter_1 = document.getElementById("PreviewGaugeMeter_1");
var Data_percent_1 = PreviewGaugeMeter_1.getAttribute("data-percent");
PreviewGaugeMeter_1.setAttribute("data-percent", Temp);
});
});
</script>
</head>
<body>
<div class="GaugeMeter" id="PreviewGaugeMeter_1" data-percent="" data-append="%" data-size="90" data-theme="Green-Gold-Red" data-animate_gauge_colors="1" data-animate_text_colors="1" data-width="9" data-label="" data-style="Arch" data-label_color="fff"></div>
</html>
And this my JSON output:
{"created_at":"0016-12-12T00:00:00Z","entry_id":37,"field1":" 4","field2":" 7","field3":"4"}
I found a workaround, by getting "$.getJSON" to send variables to another webpage, from there i used $_GET, to retrieve them from the url, and it works.

Image not rendering on PDF using jsPDF?

I have included the following files:
<script type="text/javascript" src="libs/png_support/zlib.js"></script>
<script type="text/javascript" src="libs/png_support/png.js"></script>
<script type="text/javascript" src="jspdf.plugin.addimage.js"></script>
<script type="text/javascript" src="jspdf.plugin.png_support.js"></script>
<script type="text/javascript" src="jspdf/jspdf.plugin.standard_fonts_metrics.js"></script>
<script type="text/javascript" src="jspdf/jspdf.plugin.split_text_to_size.js"></script>
<script type="text/javascript" src="jspdf/jspdf.plugin.from_html.js"></script>
<script type="text/javascript" src="jspdf.js"></script>
And I am just testing if it could render a image through html:
var doc = new jsPDF();
var elementHandler = {
'#ignorePDF': function (element, renderer) {
return true;
}
};
var source = '<img src="/assets/common/image/BG.jpg"/>';
doc.fromHTML(
source,
15,
15,
{
'width': 180,'elementHandlers': elementHandler
});
doc.output("dataurlnewwindow");
It throws this error on console which says:
jsPDF Warning: rendering issues? provide a callback to fromHTML! (anonymous function)
I have used PNG format, since it was not working I also tried with JPG format, still no luck!
What am I doing wrong?
Thanks in advance.
Have one more argument for fromHTML()
doc.fromHTML(
source,
15,
15, {
'width': 180,
'elementHandlers': elementHandler
},
function(dispose) {
// dispose: object with X, Y of the last line add to the PDF
// this allow the insertion of new lines after html
// pdf.save('Test.pdf');
if (navigator.msSaveBlob) {
var string = doc.output('datauristring');
} else {
var string = doc.output('bloburi');
}
$('.previewIFRAME').attr('src', string);
})

show data in listview pulling from DB

trying to use data-filter attribute of listview but its working like a plain html listview getting some errors regarding "refresh" of listview
HTML code:
<html lang="en" dir="ltr">
<head>
<link rel="stylesheet" href="css/app/app-style.css"/>
<link rel="stylesheet" href="css/lib/jquery.mobile.structure-1.1.1.css"/>
<link rel="stylesheet" href="css/lib/jquery.mobile.theme-1.1.1.css"/>
<link rel="stylesheet" href="css/lib/simpledialog.min.css"/>
<link rel="stylesheet" href="css/lib/jquery-ui-1.10.2.custom.css"/>
<link href="css/lib/mobiscroll.css" rel="stylesheet" type="text/css"/>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script type="text/javascript" src="js/lib/jquery.js"></script>
<script type="text/javascript" src="js/lib/jquery-ui-1.10.2.custom.js"></script>
<!-- <script type="text/javascript" src="js/lib/jquery.mobile-1.1.1.js"></script> -->
<script type="text/javascript" src="js/lib/jquery.validate.js"></script>
<script type="text/javascript" src="js/lib/additional-methods.js"></script>
<script type="text/javascript" src="js/lib/cordova-2.0.0.js"></script>
<script type="text/javascript" src="js/lib/jqm.page.params.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script type="text/javascript" defer>
$(document).ready(function()
{ $('#viewtag').on('click',function(e)
{
e.preventDefault();
getRegisterdUser();
$('#viewinformation').show();
});
});
</script>
</head>
<body>
<div data-role="page" id="logininfo" data-theme="b">
<div data-role="content">
<div class="ui-grid-a" data-theme="a">
<div class="ui-block-a" data-theme="e" style="width:50%;">VIEW</div>
</div>
</div>
</div>
<div data-role="page" id="viewinformation" data-theme="d">
<div data-role="header"> </div>
<div data-role="content">
<ul id="list" data-role="listview" data-filter="true" data-filter-placeholder="Search Student Name..." data-filter-theme="e" style="margin-top:20%;">
</ul>
</div>
</div>
</body>
</html>
(have written in alldatabsae.js file) JQUERY CODE to pull data from localDB and display only username in listview
var db;
function getRegisterdUser()
{
var query = 'SELECT username FROM loginTable';
console.log("query for username selection:" + query);
db.transaction(function(tx)
{
tx.executeSql(query,[],successCBofUsername,errorCBofUsername)
},errorCB,successCB);
}
function successCBofUsername(tx,results)
{
var resultslength = results.rows.length;
if(results!= null && results.rows.length > 0 && results.rows != null)
{
for(var i = 0; i <resultslength; i++)
{
var name = results.rows.item(i);
$('<li>'+ name.username +'</li>').appendTo($('#list'));
}
$('#list').listview('refresh');
//$('#displayinfo').empty().append('html').trigger('create');
}
else
{alert("no records exists");}
}
function errorCBofUsername()
{
alert("error of usernmae query");
}
but getting errors:
1. Uncaught Error: cannot call methods on listview prior to initialization; attempted to call method 'refresh' at http://code.jquery.com/jquery-1.8.2.min.js:2
2. Uncaught Error: cannot call methods on listview prior to initialization; attempted to call method 'refresh' at file:///android_asset/www/js/lib/jquery.js:506
getting error in this line of code
$('#list').listview('refresh');
Just replace below function in your code.
function successCBofUsername(tx,results)
{ var listdata='';
var resultslength = results.rows.length;
if(results!= null && results.rows.length > 0 && results.rows != null)
{
for(var i = 0; i <resultslength; i++)
{
var name = results.rows.item(i);
listdata+='<li>'+ name.username +'</li>';
}
$("#list").append(listdata).listview('refresh');
}
else
{alert("no records exists");}
}
You should use:
$('#list').listview().listview('refresh');
Because listview is dynamically created and don't exist in reality it first must be initialized before its markup can be enhanced.
To find out why take a look at my other answer: jQuery Mobile: Markup Enhancement of dynamically added content. Search for chapter: Markup enhancement problems:

Separate localstorages

I have 1 site under foo.com/test1/test1.html and another at foo.com/test2/test2.html
test1.html looks like this:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
document.ready = function(){
var name = localStorage.getItem("name");
if(name){
console.log("name is defined");
}else{
console.log("name is not defined");
localStorage.setItem("name", "test1");
}
$('#name').text(name);
}
</script>
</head>
<body>
<p id="name"></p>
</body>
</html>
And test2.html is like this:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
document.ready = function(){
var name = localStorage.getItem("name");
if(name){
console.log("name is defined");
}else{
console.log("name is not defined");
localStorage.setItem("name", "test2");
}
$('#name').text(name);
}
</script>
</head>
<body>
<p id="name"></p>
</body>
</html>
My question is that is it possible to achieve that that if I go to test1, the value of name is "test1" and if I go to test2 the name will be test2. Basically I suspect that I need to name them differently but I rather not do that. Is there any good work around for this?
Thank you in advance.
local storage is defined per domain. You should keep objects in localstorage to maintain page context.
Here is solution to do it.
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
document.ready = function(){
var thispage = JSON.parse(localStorage.getItem("page1"));
if(thispage.name){
console.log("name is defined");
}else{
console.log("name is not defined");
var obj={name : "test1"}
localStorage.setItem("page1", JSON.stringify(obj));
}
}
</script>
</head>
<body>
<p id="name"></p>
</body>
</html>
similar to all pages.