Creating a graph based on form data

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

eddparsons
Ebase User
Posts: 53
Joined: Wed Jan 02, 2013 4:23 pm
Location: Lyndhurst, Hampshire
Contact:

Creating a graph based on form data

#1

Postby eddparsons » Fri Mar 01, 2013 3:59 pm

I'm trying to generate a graph in Ebase using data from a table.

I've setup jQuery and JQPlot and have after some trial and error produced a graph with some dummy data but I'm struggling to use tables in Javascript.

Any code I use that relates to tables not only doesn't do anything but stops any other code from working - i.e. I added the line
tables.TOTALS.fetchTable();
to the function I was using for plotting the dummy graph and it stops it from working.

I have also tried the below to create an array for the graph, without any success:

var data2 = new Array();
var rows = tables.TOTALS.rows;
var i = 0;
while ( rows.next() )
{
data2 = tables.TOTALS.TOTAL.value;
i++
}

Can someone tell me where I'm going wrong?

Also, I'm not getting any error messages - is there a way of displaying these to give me a clue what the problem is?

Regards,
Edd Parsons
0 x

User avatar
dvanhussel
Ebase User
Posts: 161
Joined: Fri Oct 19, 2007 12:45 pm
Location: Haarlem, the Netherlands

#2

Postby dvanhussel » Sat Mar 02, 2013 1:13 pm

Are you sure that that there is actually data in your table after you fetched it?
If you put the table on your page, does it show any rows? If this table is backed by, for example, a database resource, you can turn on debugging in this resource to see which queries are performed to fetch the data.

In order to do some debugging in JavaScript, use the log() function. And I think you have a missing semicolon after i++

An example of what I would log:

Code: Select all

 
var data2 = new Array(); 
var rows = tables.TOTALS.rows; 
var i = 0; 
log('start of loop');
while ( rows.next() ) 
{ 
  data2[i] = tables.TOTALS.TOTAL.value; 
  log(i+': '+data2[i]);
  i++;
}
When this script is run, you should see the values that are logged in the Excecution Log in the Ebase Desginer
0 x

eddparsons
Ebase User
Posts: 53
Joined: Wed Jan 02, 2013 4:23 pm
Location: Lyndhurst, Hampshire
Contact:

#3

Postby eddparsons » Mon Mar 04, 2013 9:31 am

Hi, thanks for your reply.

Yes there was data in the table that was being fetched via an FPL script.

I think I have a more fundamental issue though - I've gone back to my dummy data script and added an extra log line as described but this also stops the graph from being produced and doesn't log anything.

My script is as follows:

$(document).ready(function(){
});

showchart = function(){

var data2 = new Array();
for(var i=0; i<5; i++) {
data2 = i;
// log(i+': '+data2);
}

var plot1 = jQuery.jqplot ('chart1', [data2],
{
seriesDefaults: {
renderer: jQuery.jqplot.PieRenderer,
rendererOptions: {
showDataLabels: true
}
},
legend: { show:true, location: 'e' }
}
);
};

There is a panel in the form with a HTML element ID of chart1 that has an on click event of showchart();

This works as is but breaks when I uncomment the log line.
0 x

User avatar
Wai
Moderator
Moderator
Posts: 165
Joined: Wed Sep 12, 2007 9:04 am
Location: Sandy, UK
Contact:

#4

Postby Wai » Mon Mar 04, 2013 10:19 am

Hi Edd,

You are mixing Client-side JavaScript and Server-side JavaScript here.

The JQuery JPlot is Client-side. All of the stuff you are doing in $(document).ready(function() etc is for client side scripting.
You cannot use the Ebase log statement in the Client script.

To get Ebase table data into your JQuery client script you have to build an Ebase form field to contain either a String of the data, or if using 4.4, you should be able to use an Object field.

You can then substitute the field into your client script using &&
0 x

eddparsons
Ebase User
Posts: 53
Joined: Wed Jan 02, 2013 4:23 pm
Location: Lyndhurst, Hampshire
Contact:

#5

Postby eddparsons » Mon Mar 04, 2013 1:15 pm

Thanks Wai, I hadn't realised there was a distinction.

I've created a form field called chartdata and built up a string - how do I use this in client-side javascript?

I've tried the below but it's not working.

var data2 = &&Chartdata;

Regards,
Edd
0 x

eddparsons
Ebase User
Posts: 53
Joined: Wed Jan 02, 2013 4:23 pm
Location: Lyndhurst, Hampshire
Contact:

#6

Postby eddparsons » Mon Mar 04, 2013 1:58 pm

Hi again,

Just seen post http://forum.ebasetech.com/forum/viewtopic.php?t=276 - so it looks like this should work.

I have also tried

data2 = &&Chartdata;

and

data2 = $('#chartdata').val();

after setting the HMTL ID of the editor part of the field to chartdata.

The chartdata string contains

[['AUDIT_TEST',5],['CONTRACT',2],['EC_COMPLAINT',1],['GRAPHICS_ORDER_FORM',2],['LI_VEHICLE_EXAM',4],['LPEN_REGISTER',2],['PENDING_REGISTER',4],['PT_ENQ',3],['PT_TRANS_GOGREEN',8],['REQUEST_FOR_SERVICE',1],['SAP_HOS',3],['STATIONERY',1]]

which works if I copy the string into the javascript code and allocate it directly, but not if I reference the fieldname.
0 x

User avatar
Wai
Moderator
Moderator
Posts: 165
Joined: Wed Sep 12, 2007 9:04 am
Location: Sandy, UK
Contact:

#7

Postby Wai » Mon Mar 04, 2013 2:25 pm

So you have the following:

- Chartdata is a form field on your page (when this is on the page, you can see the correct string in the field value).

- Chartdata has an ID on the Editor part set as chartdata (case is important)

- Client Script is added to the page, what does alert(data2) give you?
This needs to be within the document.ready function. The page must have finished loading before the script tries to access the form field. From your earlier post it looked like document.ready was empty.
0 x

eddparsons
Ebase User
Posts: 53
Joined: Wed Jan 02, 2013 4:23 pm
Location: Lyndhurst, Hampshire
Contact:

#8

Postby eddparsons » Mon Mar 04, 2013 3:01 pm

Chartdata is a form field with a value that is set via FPL on page load - the correct string is displayed on the page. I have also tried setting the chartdata default value with the same results.

The chartdata control (I assume this is set against the control as the field itself doesn't have HTML elements) has under HTML elements, Editor Input an ID of chartdata (lower case).

With data2 = &&chartdata; alert(data2) does nothing.

With data2 = $('#chartdata').val(); alert(data2) shows an empty string.

With data2 = the string copied and pasted into the script, alert(data2) shows me the string.

I have moved my scripting into the document.ready function - so it now looks like:

$(document).ready(function(){

data2 = $('#chartdata').val();
alert(data2);
var plot1 = jQuery.jqplot ('chart1', [data2],
{
seriesDefaults: {
renderer: jQuery.jqplot.PieRenderer,
rendererOptions: {
showDataLabels: true
}
},
legend: { show:true, location: 'e' }
}
);
});
0 x

eddparsons
Ebase User
Posts: 53
Joined: Wed Jan 02, 2013 4:23 pm
Location: Lyndhurst, Hampshire
Contact:

#9

Postby eddparsons » Mon Mar 04, 2013 4:18 pm

Update - I've created a new field test_field, given it an ID of test and a default value of [['AUDITTEST',5],['CONTRACT',2]] and set my data2 = $('#test').val();

The alert function is now showing me the string although the graph still isn't plotting.

I have noticed that when using the field the alert shows the string exactly as entered but when setting the string explicitly in the javascript the alert that appears doesn't contain any of the brackets or quotes - is this relevant?
0 x

User avatar
Wai
Moderator
Moderator
Posts: 165
Joined: Wed Sep 12, 2007 9:04 am
Location: Sandy, UK
Contact:

#10

Postby Wai » Mon Mar 04, 2013 4:30 pm

Yes it is. The string that you construct must contain brackets and quotes as in the examples, this is how the JPlot javascript requires it.
0 x

Hovik
Moderator
Moderator
Posts: 184
Joined: Tue Sep 11, 2007 8:58 am

#11

Postby Hovik » Thu Mar 07, 2013 3:27 pm

The only thing missing is an eval of the data field to convert it to an array.
So the the client script code is:

Code: Select all

var v1 = $&#40;"#test"&#41;.val&#40;&#41;;
var s1 = eval&#40;v1&#41;; // this is the key
To prevent the test_field from showing on the page, put it in a panel on its own, then add display:none; to the advanced properties of the panel.
0 x

eddparsons
Ebase User
Posts: 53
Joined: Wed Jan 02, 2013 4:23 pm
Location: Lyndhurst, Hampshire
Contact:

#12

Postby eddparsons » Thu Mar 07, 2013 3:29 pm

Thanks Hovik (and others), this is now working.

Regards,
Edd
0 x

Hovik
Moderator
Moderator
Posts: 184
Joined: Tue Sep 11, 2007 8:58 am

#13

Postby Hovik » Tue Mar 12, 2013 11:37 am

So the main steps are:

Populate a form field with the data. Note the leading and trailing [ and ]. e.g.
[['AUDIT_TEST',5],['CONTRACT',2],['EC_COMPLAINT',1],['GRAPHICS_ORDER_FORM',2],['LI_VEHICLE_EXAM',4],['LPEN_REGISTER',2],['PENDING_REGISTER',4],['PT_ENQ',3],['PT_TRANS_GOGREEN',8],['REQUEST_FOR_SERVICE',1],['SAP_HOS',3],['STATIONERY',1]]

2. set the HMTL ID of the editor part of the field to chartdata

3. This field needs to be on the page (not hidden). To prevent it from showing on the page, put it in a panel on its own, then add display:none; to the advanced properties of the panel

4. Write the client script (Web Resources -> Client Scripts):

Code: Select all

$&#40;document&#41;.ready&#40;function&#40;&#41; &#123;

   $.jqplot.config.enablePlugins = true;
   var v1 = $&#40;"#chartdata"&#41;.val&#40;&#41;;
   var s1 = eval&#40;v1&#41;;
   plot1 = $.jqplot&#40;'chart1', &#91;s1&#93;, &#123;
         seriesDefaults&#58;&#123;
            renderer&#58;$.jqplot.PieRenderer,
            rendererOptions&#58; &#123; padding&#58; 8, showDataLabels&#58; true&#125;
         &#125;,
   legend&#58; &#123;show&#58;true&#125;
   &#125;
&#41;;

$&#40;'#chart1'&#41;.bind&#40;'jqplotDataHighlight', function &#40;ev, seriesIndex, pointIndex, data&#41; &#123;
   $&#40;'#info1'&#41;.html&#40;'Graph value &#40;£&#41; &#58; ' + data&#41;;
   &#125;
&#41;;

$&#40;'#chart1'&#41;.bind&#40;'jqplotDataUnhighlight', function &#40;ev&#41; &#123;
   $&#40;'#info1'&#41;.html&#40;'Nothing selected'&#41;;
   &#125;
&#41;;
});

5. Add this to the page client scripts, after all the required jQuery library scripts.
0 x


Who is online

Users browsing this forum: No registered users and 33 guests