Quinn-Curtis Forums
Quinn-Curtis Forums
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Tools for Java
 QCSPCChart SPC for Java
 pushJSONChartCreate is not defined
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

aletenti

Italy
8 Posts

Posted - 06 Apr 2017 :  04:06:51  Show Profile  Reply with Quote
Hi everyone. I'm a newbie and i'm developing a web app in c# and asp.net that shows charts dinamically. I managed to pass JSON string from c# code to the asp.net one using the ViewState and I want to create my chart using "pushJSONChartCreate". to do this i have to import the nocache.js library like this:
<script language="javascript" type="text/javascript" 
src="QCSPCChartGWTWar/qcspcchartgwt/qcspcchartgwt.nocache.js"></script>

(the src is correct, because the folder "QCSPCChartGWTWar" is in the main web folder).

this is the asp.net code:
<script language="javascript" type="text/javascript">
    window.onload = function setData() {
        console.log("loaded");
        var stringa = document.getElementById('stringa_json').value;
        alert(stringa);
        pushJSONChartCreate(JSON.stringify(stringa));
    }
</script>


this is the c# code:
String JSONfinale = generaStringa(stringaJSON);
ViewState.Add("json_code", JSONfinale);
Page.ClientScript.RegisterHiddenField("stringa_json", 
ViewState["json_code"].ToString());


the asp.net code shows in an alert the JSON string passed succesfully with the ViewState, so the application enter in this block of code, but in browser console I see this error:
Uncaught ReferenceError: pushJSONChartCreate is not defined
at setData (Alessio.aspx:18)
setData @ Alessio.aspx:18


Anyone can help me?

Thank you,
Alessio

quinncurtis

1558 Posts

Posted - 06 Apr 2017 :  09:20:21  Show Profile  Reply with Quote
Assuming everything else is correct, I suspect that it is a timing issue. Everything tends to load asynchronously in a web page. So when the onload event occurs, the qcspcchartgwt.nocache.js has not yet finished loading, (it is quite large), and therefore the pushJSONChartCreate function is not present at that instant. It will be present in N milliseconds though, after the qcspcchartgwt.nocache.js has finished loading.

So get the call to pushJSONChartCreate out of the onload event.

Instead, define your chart by placing it in a defineChartUsingJSON function, as seen in our example SPCSimple.htm, and most all of our other examples. For your code it would looks something like:

function defineChartUsingJSON( )
{
var stringa = JSON.stringify(document.getElementById('stringa_json').value);
return stringa ;
}



Once loaded, our library looks for the existence of this function in the parent HTML page and if present, calls it, where it expects a valid JSON SPC Chart setup string to be returned. It is impossible to tell if your JSON script is valid, but that will result in a different error message.

For future events on the same web page you can use the pushJSONChartCreate function, because now you know the SPC chart library has been loaded.
Go to Top of Page

aletenti

Italy
8 Posts

Posted - 07 Apr 2017 :  03:57:09  Show Profile  Reply with Quote
Thanks for your reply. I temporarily set a timeout and it works. I see that the chart also appears without the defineChartUsingJSON() function. Is it essential?

Piece of my ASP.NET code

<script language="javascript" type="text/javascript">
    setTimeout(function () {
        console.log("loaded");
        var stringa = document.getElementById('stringa_json').value;
        console.log(stringa);
        pushJSONChartCreate(stringa);
    }, 2000);
</script>
Go to Top of Page

quinncurtis

1558 Posts

Posted - 07 Apr 2017 :  09:09:34  Show Profile  Reply with Quote
No, you need one or the other. Your timing approach is perfectly valid, though of indeterminate reliability since the exact delay needed for a given web page/browser loading is not known. The defineChartUsingJSON will always work, no matter the timing.

If you want to use the pushJSONChartCreate for the initial load of the chart, then before you call it, test for the functions existence (typeof pushJSONChartCreate != "undefined")at the time of the call using code similar to below. If it isn't present, delay 50 ms and then try again. The code below times out after 20 tries (1 sec). This runs asynchronously with the actual loading of the library and should catch the instant you can call pushJSONChartCreate .


if (typeof pushJSONChartCreate != "undefined") {
    pushJSONChartCreate(spcData);
} else {
    var timeoutlimitcountmax = 20;
    var timeoutinterval = 50; // milliseconds
    var timeoutcount = 0;

    var intervaltimer = setInterval(function () {
      if (typeof pushJSONChartCreate != "undefined") {
        pushJSONChartCreate(spcData);
        clearInterval(intervaltimer);
      } else {
        timeoutcount++;
        if (timeoutcount > timeoutlimitcountmax)
            clearInterval(intervaltimer);
      }
    }, timeoutinterval);
 }


Go to Top of Page

aletenti

Italy
8 Posts

Posted - 12 Apr 2017 :  03:18:08  Show Profile  Reply with Quote
Thank you so much! It works
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
Quinn-Curtis Forums © 2000-2018 Quinn-Curtis, Inc. Go To Top Of Page
Powered By: Snitz Forums 2000 Version 3.4.07