Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Triggering a JS function on an AJAX write back 1

Status
Not open for further replies.

BigRed1212

Technical User
Mar 11, 2008
550
US
Hello,

I have these includes:
Code:
        <script src="raphael.js" type="text/javascript" charset="utf-8"></script>
        <script src="g.raphael.js" type="text/javascript" charset="utf-8"></script>
        <script src="g.pie.js" type="text/javascript" charset="utf-8"></script>

followed by this function:
Code:
        <script type="text/javascript" charset="utf-8">
            window.onload = function () {
                var r = Raphael("holder");
                r.g.txtattr.font = "12px Tahoma, Arial, Helvetica, sans-serif";
                
                r.g.text(125, 30, "Racial/Ethnic Percentages").attr({"font-size": 12});
                
                var pie = r.g.piechart(225, 120, 75, [<%=a%>, <%=b%>, <%=c%>, <%=d%>, <%=e%>, <%=f%>], {legend: ["%%.% – White", "%%.% - Black",  "%%.% - Hispanic",  "%%.% - Asian",  "%%.% - Native Amer.", "%%.% - Multi"], legendpos: "west", href: ["[URL unfurl="true"]http://raphaeljs.com",[/URL] "[URL unfurl="true"]http://g.raphaeljs.com"[/URL]]});
                pie.hover(function () {
                    this.sector.stop();
                    this.sector.scale(1.1, 1.1, this.cx, this.cy);
                    if (this.label) {
                        this.label[0].stop();
                        this.label[0].scale(1.5);
                        this.label[1].attr({"font-weight": 800});
                    }
                }, function () {
                    this.sector.animate({scale: [1, 1, this.cx, this.cy]}, 500, "bounce");
                    if (this.label) {
                        this.label[0].animate({scale: 1}, 500, "bounce");
                        this.label[1].attr({"font-weight": 400});
                    }
                });
                
            };
        </script>
This works great and draws a nice pie graph that I want.
is where it is from.

As you can see from the ASP inserted above, I was playing around with using ASP to dynamically write the values that make the size of the pie slices.

What I want to do is put this function on an ASP page that would return data when hit with an AJAX call. (There would other stuff and the data for the graph would come in from a database query.)

My problem is that I don't know how to make the function fire and draw the graph after it is returned by the AJAX request. Obviously onload isn't going to do it. I don't know how to name and call it another way so that it will fire.

I thought about posting this in the AJAX room/forum, but I think this one is higher traffic so I posted here.

Guidance?
 
Hi

BigRed1212 said:
I can not access raphaeljs.com and http://g.raphaeljs.com/]Down for everyone or just me?[/url] says the same. Do you know other repositories or the Raphael libraries ?
BigRed1212 said:
I thought about posting this in the AJAX room/forum, but I think this one is higher traffic so I posted here.
Posting the questions in the related forum is also helpful for future surfers who will search AJAX-related questions in the AJAX forum.


Feherke.
 
Posting the questions in the related forum is also helpful for future surfers who will search AJAX-related questions in the AJAX forum.

As luck would have it, another poster had a similiar question at a similiar time and posted it in the AJAX forum. He hasn't had any more luck in responses than I have.

Oh well.
 
Oh and BTW,

"It's just you, g.raphaeljs.com is up."



 
Hi

Ok, I downloaded it now.

As I understand, you need a function like this :
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]makepie[/color][teal]([/teal]which[teal])[/teal]
[teal]{[/teal]
  [b]var[/b] http[teal]=[/teal][b]var[/b] [COLOR=darkgoldenrod]XMLHttpRequest[/color][teal]();[/teal]
  http[teal].[/teal][COLOR=darkgoldenrod]open[/color][teal]([/teal][green][i]'GET'[/i][/green][teal],[/teal][green][i]'data.asp?pie='[/i][/green][teal]+[/teal][COLOR=darkgoldenrod]escape[/color][teal]([/teal]which[teal]),[/teal][b]false[/b][teal])[/teal]
  http[teal].[/teal][COLOR=darkgoldenrod]send[/color][teal]([/teal][b]null[/b][teal])[/teal]

  [b]if[/b] [teal]([/teal]http[teal].[/teal]readyState[teal]!=[/teal][purple]4[/purple] [teal]||[/teal] http[teal].[/teal]status[teal]!=[/teal][purple]200[/purple][teal])[/teal] [b]return[/b]

  [b]var[/b] data[teal]=[/teal]JSON[teal].[/teal][COLOR=darkgoldenrod]parse[/color][teal]([/teal]http[teal].[/teal]responseText[teal])[/teal]

  [b]var[/b] r [teal]=[/teal] [COLOR=darkgoldenrod]Raphael[/color][teal]([/teal][green][i]"holder"[/i][/green][teal]);[/teal]
  r[teal].[/teal]g[teal].[/teal]txtattr[teal].[/teal]font [teal]=[/teal] [green][i]"12px Tahoma, Arial, Helvetica, sans-serif"[/i][/green][teal];[/teal]

  r[teal].[/teal]g[teal].[/teal][COLOR=darkgoldenrod]text[/color][teal]([/teal][purple]125[/purple][teal],[/teal] [purple]30[/purple][teal],[/teal] [green][i]"Racial/Ethnic Percentages"[/i][/green][teal]).[/teal][COLOR=darkgoldenrod]attr[/color][teal]([/teal][teal]{[/teal][green][i]"font-size"[/i][/green][teal]:[/teal] [purple]12[/purple][teal]}[/teal][teal]);[/teal]

  [b]var[/b] pie [teal]=[/teal] r[teal].[/teal]g[teal].[/teal][COLOR=darkgoldenrod]piechart[/color][teal]([/teal]data[teal][[/teal][purple]0[/purple][teal]],[/teal] data[teal][[/teal][purple]1[/purple][teal]],[/teal] data[teal][[/teal][purple]2[/purple][teal]],[/teal] data[teal][[/teal][purple]3[/purple][teal]],[/teal] data[teal][[/teal][purple]4[/purple][teal]]);[/teal]
  pie[teal].[/teal][COLOR=darkgoldenrod]hover[/color][teal]([/teal][b]function[/b] [teal]()[/teal] [teal]{[/teal]
    [b]this[/b][teal].[/teal]sector[teal].[/teal][COLOR=darkgoldenrod]stop[/color][teal]();[/teal]
    [b]this[/b][teal].[/teal]sector[teal].[/teal][COLOR=darkgoldenrod]scale[/color][teal]([/teal][purple]1.1[/purple][teal],[/teal] [purple]1.1[/purple][teal],[/teal] [b]this[/b][teal].[/teal]cx[teal],[/teal] [b]this[/b][teal].[/teal]cy[teal]);[/teal]
    [b]if[/b] [teal]([/teal][b]this[/b][teal].[/teal]label[teal])[/teal] [teal]{[/teal]
      [b]this[/b][teal].[/teal]label[teal][[/teal][purple]0[/purple][teal]].[/teal][COLOR=darkgoldenrod]stop[/color][teal]();[/teal]
      [b]this[/b][teal].[/teal]label[teal][[/teal][purple]0[/purple][teal]].[/teal][COLOR=darkgoldenrod]scale[/color][teal]([/teal][purple]1.5[/purple][teal]);[/teal]
      [b]this[/b][teal].[/teal]label[teal][[/teal][purple]1[/purple][teal]].[/teal][COLOR=darkgoldenrod]attr[/color][teal]([/teal][teal]{[/teal][green][i]"font-weight"[/i][/green][teal]:[/teal] [purple]800[/purple][teal]}[/teal][teal]);[/teal]
    [teal]}[/teal]
  [teal]}[/teal][teal],[/teal] [b]function[/b] [teal]()[/teal] [teal]{[/teal]
    [b]this[/b][teal].[/teal]sector[teal].[/teal][COLOR=darkgoldenrod]animate[/color][teal]([/teal][teal]{[/teal]scale[teal]:[/teal] [teal][[/teal][purple]1[/purple][teal],[/teal] [purple]1[/purple][teal],[/teal] [b]this[/b][teal].[/teal]cx[teal],[/teal] [b]this[/b][teal].[/teal]cy[teal]][/teal][teal]}[/teal][teal],[/teal] [purple]500[/purple][teal],[/teal] [green][i]"bounce"[/i][/green][teal]);[/teal]
    [b]if[/b] [teal]([/teal][b]this[/b][teal].[/teal]label[teal])[/teal] [teal]{[/teal]
      [b]this[/b][teal].[/teal]label[teal][[/teal][purple]0[/purple][teal]].[/teal][COLOR=darkgoldenrod]animate[/color][teal]([/teal][teal]{[/teal]scale[teal]:[/teal] [purple]1[/purple][teal]}[/teal][teal],[/teal] [purple]500[/purple][teal],[/teal] [green][i]"bounce"[/i][/green][teal]);[/teal]
      [b]this[/b][teal].[/teal]label[teal][[/teal][purple]1[/purple][teal]].[/teal][COLOR=darkgoldenrod]attr[/color][teal]([/teal][teal]{[/teal][green][i]"font-weight"[/i][/green][teal]:[/teal] [purple]400[/purple][teal]}[/teal][teal]);[/teal]
    [teal]}[/teal]
  [teal]}[/teal][teal]);[/teal]

[teal]}[/teal]
And be able to run it like this :
HTML:
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]""[/i][/green] [maroon]onclick[/maroon][teal]=[/teal][green][i]"makepie('one'); return false"[/i][/green][b]>[/b]Make Pie 1[b]</a>[/b]
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]""[/i][/green] [maroon]onclick[/maroon][teal]=[/teal][green][i]"makepie('two'); return false"[/i][/green][b]>[/b]Make Pie 2[b]</a>[/b]
No idea how you can code the data provider in ASP, for the test I use this PHP code :
PHP:
[teal]<?php[/teal]

[navy]$pie[/navy][teal]=[/teal][b]array[/b][teal]([/teal]
  [green][i]'one'[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal][purple]225[/purple][teal],[/teal] [purple]120[/purple][teal],[/teal] [purple]75[/purple][teal],[/teal] [b]array[/b][teal]([/teal][purple]11[/purple][teal],[/teal] [purple]22[/purple][teal],[/teal] [purple]33[/purple][teal],[/teal] [purple]44[/purple][teal],[/teal] [purple]55[/purple][teal],[/teal] [purple]66[/purple][teal]),[/teal] [b]array[/b][teal]([/teal][green][i]'legend'[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal][green][i]"%%.% - White"[/i][/green][teal],[/teal] [green][i]"%%.% - Black"[/i][/green][teal],[/teal]  [green][i]"%%.% - Hispanic"[/i][/green][teal],[/teal]  [green][i]"%%.% - Asian"[/i][/green][teal],[/teal]  [green][i]"%%.% - Native Amer."[/i][/green][teal],[/teal] [green][i]"%%.% - Multi"[/i][/green][teal]),[/teal] [green][i]'legendpos'[/i][/green] [teal]=>[/teal] [green][i]"west"[/i][/green][teal],[/teal] [green][i]'href'[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal][green][i]"[URL unfurl="true"]http://raphaeljs.com"[/URL][/i][/green][teal],[/teal] [green][i]"[URL unfurl="true"]http://g.raphaeljs.com"[/URL][/i][/green][teal]))),[/teal]
  [green][i]'two'[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal][purple]225[/purple][teal],[/teal] [purple]120[/purple][teal],[/teal] [purple]75[/purple][teal],[/teal] [b]array[/b][teal]([/teal][purple]1[/purple][teal],[/teal] [purple]22[/purple][teal],[/teal] [purple]333[/purple][teal],[/teal] [purple]4444[/purple][teal],[/teal] [purple]55555[/purple][teal],[/teal] [purple]666666[/purple][teal]),[/teal] [b]array[/b][teal]([/teal][green][i]'legend'[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal][green][i]"%%.% - White"[/i][/green][teal],[/teal] [green][i]"%%.% - Black"[/i][/green][teal],[/teal]  [green][i]"%%.% - Hispanic"[/i][/green][teal],[/teal]  [green][i]"%%.% - Asian"[/i][/green][teal],[/teal]  [green][i]"%%.% - Native Amer."[/i][/green][teal],[/teal] [green][i]"%%.% - Multi"[/i][/green][teal]),[/teal] [green][i]'legendpos'[/i][/green] [teal]=>[/teal] [green][i]"west"[/i][/green][teal],[/teal] [green][i]'href'[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal][green][i]"[URL unfurl="true"]http://raphaeljs.com"[/URL][/i][/green][teal],[/teal] [green][i]"[URL unfurl="true"]http://g.raphaeljs.com"[/URL][/i][/green][teal]))),[/teal]
[teal]);[/teal]

[b]if[/b] [teal]([/teal][COLOR=darkgoldenrod]array_key_exists[/color][teal]([/teal][navy]$_GET[/navy][teal][[/teal][green][i]'pie'[/i][/green][teal]],[/teal][navy]$pie[/navy][teal]))[/teal] [teal]{[/teal]
  [COLOR=darkgoldenrod]header[/color][teal]([/teal][green][i]'Content/type: text/javascript'[/i][/green][teal]);[/teal]
  [b]echo[/b] [COLOR=darkgoldenrod]json_encode[/color][teal]([/teal][navy]$pie[/navy][teal][[/teal][navy]$_GET[/navy][teal][[/teal][green][i]'pie'[/i][/green][teal]]]);[/teal]
[teal]}[/teal]
Works in Gecko, Presto and WebKit. In KHTML fails because the g.raphael.js seems to not support it.

If you want compatibility with Trident, add this :
JavaScript:
[b]if[/b] [teal]([/teal][b]typeof[/b] XMLHttpRequest[teal]==[/teal][green][i]'undefined'[/i][/green][teal])[/teal] [teal]{[/teal]
  XMLHttpRequest[teal]=[/teal][b]function[/b][teal]()[/teal] [teal]{[/teal]
    [b]try[/b] [teal]{[/teal] [b]return[/b] [b]new[/b] [COLOR=darkgoldenrod]ActiveXObject[/color][teal]([/teal][green][i]'Msxml2.XMLHTTP.6.0'[/i][/green][teal])[/teal] [teal]}[/teal] [b]catch[/b] [teal]([/teal]e[teal])[/teal] [teal]{[/teal] [teal]}[/teal]
    [b]try[/b] [teal]{[/teal] [b]return[/b] [b]new[/b] [COLOR=darkgoldenrod]ActiveXObject[/color][teal]([/teal][green][i]'Msxml2.XMLHTTP.3.0'[/i][/green][teal])[/teal] [teal]}[/teal] [b]catch[/b] [teal]([/teal]e[teal])[/teal] [teal]{[/teal] [teal]}[/teal]
    [b]try[/b] [teal]{[/teal] [b]return[/b] [b]new[/b] [COLOR=darkgoldenrod]ActiveXObject[/color][teal]([/teal][green][i]'Msxml2.XMLHTTP'[/i][/green][teal])[/teal] [teal]}[/teal] [b]catch[/b] [teal]([/teal]e[teal])[/teal] [teal]{[/teal] [teal]}[/teal]
    [b]try[/b] [teal]{[/teal] [b]return[/b] [b]new[/b] [COLOR=darkgoldenrod]ActiveXObject[/color][teal]([/teal][green][i]'Microsoft.XMLHTTP'[/i][/green][teal])[/teal] [teal]}[/teal] [b]catch[/b] [teal]([/teal]e[teal])[/teal] [teal]{[/teal] [teal]}[/teal]
  [teal]}[/teal]
[teal]}[/teal]
If you want compatibility with older browsers which have no JSON object, add this :
JavaScript:
JSON[teal]=[/teal][teal]{[/teal]
  parse[teal]:[/teal][b]function[/b] [teal]([/teal]what[teal])[/teal] [teal]{[/teal] [b]return[/b] [COLOR=darkgoldenrod]eval[/color][teal]([/teal]what[teal])[/teal] [teal]}[/teal]
[teal]}[/teal]
Note that the above fake-JSON is a quick & dirty replacement which may fail in certain situations. Better search the web for a suitable one.


Feherke.
 
Thanks for the work!

I don't want an onClick event- I want the pie to draw once the page comes back from getting the data.

I have a select box, user makes a selection, currently the form self submits because that is the only way I could make it work.

Parameter from the select box feeds the SQL which returns values for the selected record. Some of those values get dumped into a text display. Some of those values I feed to the Raphael javascript for drawing the pie.

What I would like to do is use AJAX for the select box submission so I don't have the page flash/reload (and maybe I could use a "loading please wait" things for when the server is slow).

Problem is the AJAX write back is not an onload event and won't fire the pie graph draw. I don't know how to make it fire. I don't know a good way to get javascript in general to fire on AJAX write back. I think it has to do with registrations and complexities of the DOM that are currently beyond my understanding.

I am currently using a regular form submission. If I went AJAX I would likely want to submit the form onChange of the select box using something like:

Code:
<script type="text/javascript">
$(document).ready(function(){  
	$("#selectbox").change(onSelectChange);  
});

function onSelectChange(){  
	var selected = $("#selectbox").val();
	if (selected.length > 0) {
		$.post("processing_page.asp", { thing: selected }, function(data) {
			$("#output").html(data);
		});
	}
}
</script>

I keep thinking there ought to be a way to add eval() or some jQuery command I know nothing about to something like that make this work but I am not that skilled.

Thanks for taking the time to look at this. I appreciate the response even if it may not sound like it.


 
Hi

Is that so big difference ?
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]makepie[/color][teal]([/teal]which[teal])[/teal]
[teal]{[/teal]
  [highlight]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'holder'[/i][/green][teal]).[/teal]innerHTML[teal]=[/teal][green][i]'Loading, please wait...'[/i][/green][/highlight]

  [b]var[/b] http[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]XMLHttpRequest[/color][teal]();[/teal]
  http[teal].[/teal][COLOR=darkgoldenrod]open[/color][teal]([/teal][green][i]'GET'[/i][/green][teal],[/teal][green][i]'[highlight]processing_page.asp[/highlight]?[highlight]thing[/highlight]='[/i][/green][teal]+[/teal][COLOR=darkgoldenrod]escape[/color][teal]([/teal]which[teal]),[/teal][b]false[/b][teal])[/teal]
  http[teal].[/teal][COLOR=darkgoldenrod]send[/color][teal]([/teal][b]null[/b][teal])[/teal]

  [b]if[/b] [teal]([/teal]http[teal].[/teal]readyState[teal]!=[/teal][purple]4[/purple] [teal]||[/teal] http[teal].[/teal]status[teal]!=[/teal][purple]200[/purple][teal])[/teal] [b]return[/b]
  [b]var[/b] data[teal]=[/teal]JSON[teal].[/teal][COLOR=darkgoldenrod]parse[/color][teal]([/teal]http[teal].[/teal]responseText[teal])[/teal]

  [highlight]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'holder'[/i][/green][teal]).[/teal]innerHTML[teal]=[/teal][green][i]''[/i][/green][/highlight]

  [b]var[/b] r [teal]=[/teal] [COLOR=darkgoldenrod]Raphael[/color][teal]([/teal][green][i]"holder"[/i][/green][teal]);[/teal]

  r[teal].[/teal]g[teal].[/teal]txtattr[teal].[/teal]font [teal]=[/teal] [green][i]"12px Tahoma, Arial, Helvetica, sans-serif"[/i][/green][teal];[/teal]

  r[teal].[/teal]g[teal].[/teal][COLOR=darkgoldenrod]text[/color][teal]([/teal][purple]125[/purple][teal],[/teal] [purple]30[/purple][teal],[/teal] [green][i]"Racial/Ethnic Percentages"[/i][/green][teal]).[/teal][COLOR=darkgoldenrod]attr[/color][teal]([/teal][teal]{[/teal][green][i]"font-size"[/i][/green][teal]:[/teal] [purple]12[/purple][teal]}[/teal][teal]);[/teal]

  [b]var[/b] pie [teal]=[/teal] r[teal].[/teal]g[teal].[/teal][COLOR=darkgoldenrod]piechart[/color][teal]([/teal]data[teal][[/teal][purple]0[/purple][teal]],[/teal] data[teal][[/teal][purple]1[/purple][teal]],[/teal] data[teal][[/teal][purple]2[/purple][teal]],[/teal] data[teal][[/teal][purple]3[/purple][teal]],[/teal] data[teal][[/teal][purple]4[/purple][teal]]);[/teal]
  pie[teal].[/teal][COLOR=darkgoldenrod]hover[/color][teal]([/teal][b]function[/b] [teal]()[/teal] [teal]{[/teal]
    [b]this[/b][teal].[/teal]sector[teal].[/teal][COLOR=darkgoldenrod]stop[/color][teal]();[/teal]
    [b]this[/b][teal].[/teal]sector[teal].[/teal][COLOR=darkgoldenrod]scale[/color][teal]([/teal][purple]1.1[/purple][teal],[/teal] [purple]1.1[/purple][teal],[/teal] [b]this[/b][teal].[/teal]cx[teal],[/teal] [b]this[/b][teal].[/teal]cy[teal]);[/teal]
    [b]if[/b] [teal]([/teal][b]this[/b][teal].[/teal]label[teal])[/teal] [teal]{[/teal]
      [b]this[/b][teal].[/teal]label[teal][[/teal][purple]0[/purple][teal]].[/teal][COLOR=darkgoldenrod]stop[/color][teal]();[/teal]
      [b]this[/b][teal].[/teal]label[teal][[/teal][purple]0[/purple][teal]].[/teal][COLOR=darkgoldenrod]scale[/color][teal]([/teal][purple]1.5[/purple][teal]);[/teal]
      [b]this[/b][teal].[/teal]label[teal][[/teal][purple]1[/purple][teal]].[/teal][COLOR=darkgoldenrod]attr[/color][teal]([/teal][teal]{[/teal][green][i]"font-weight"[/i][/green][teal]:[/teal] [purple]800[/purple][teal]}[/teal][teal]);[/teal]
    [teal]}[/teal]
  [teal]}[/teal][teal],[/teal] [b]function[/b] [teal]()[/teal] [teal]{[/teal]
    [b]this[/b][teal].[/teal]sector[teal].[/teal][COLOR=darkgoldenrod]animate[/color][teal]([/teal][teal]{[/teal]scale[teal]:[/teal] [teal][[/teal][purple]1[/purple][teal],[/teal] [purple]1[/purple][teal],[/teal] [b]this[/b][teal].[/teal]cx[teal],[/teal] [b]this[/b][teal].[/teal]cy[teal]][/teal][teal]}[/teal][teal],[/teal] [purple]500[/purple][teal],[/teal] [green][i]"bounce"[/i][/green][teal]);[/teal]
    [b]if[/b] [teal]([/teal][b]this[/b][teal].[/teal]label[teal])[/teal] [teal]{[/teal]
      [b]this[/b][teal].[/teal]label[teal][[/teal][purple]0[/purple][teal]].[/teal][COLOR=darkgoldenrod]animate[/color][teal]([/teal][teal]{[/teal]scale[teal]:[/teal] [purple]1[/purple][teal]}[/teal][teal],[/teal] [purple]500[/purple][teal],[/teal] [green][i]"bounce"[/i][/green][teal]);[/teal]
      [b]this[/b][teal].[/teal]label[teal][[/teal][purple]1[/purple][teal]].[/teal][COLOR=darkgoldenrod]attr[/color][teal]([/teal][teal]{[/teal][green][i]"font-weight"[/i][/green][teal]:[/teal] [purple]400[/purple][teal]}[/teal][teal]);[/teal]
    [teal]}[/teal]
  [teal]}[/teal][teal]);[/teal]

[teal]}[/teal]
HTML:
[highlight][b]<form[/b] [maroon]action[/maroon][teal]=[/teal][green][i]""[/i][/green][b]>[/b][/highlight]
[highlight][b]<select[/b] [maroon]onchange[/maroon][teal]=[/teal][green][i]"[/i][/green][/highlight][green][i]makepie([/i][/green][highlight][green][i]this.value[/i][/green][/highlight][green][i])[/i][/green][highlight][green][i]"[/i][/green][b]>[/b][/highlight]
[highlight][b]<option[/b] [maroon]value[/maroon][teal]=[/teal][green][i]"[/i][/green][/highlight][green][i]one[/i][/green][highlight][green][i]"[/i][/green][b]>[/b][/highlight]Make Pie 1[highlight][b]</option>[/b][/highlight]
[highlight][b]<option[/b] [maroon]value[/maroon][teal]=[/teal][green][i]"[/i][/green][/highlight][green][i]two[/i][/green][highlight][green][i]"[/i][/green][b]>[/b][/highlight]Make Pie 2[highlight][b]</option>[/b][/highlight]
[highlight][b]</select>[/b][/highlight]
[highlight][b]</form>[/b][/highlight]

Feherke.
 
Is that so big difference ?"

With regards to this stuff and my skill level it is enormous. Huge. Colossal.

Okay. I have never done JSON.

var data=JSON.parse(http.responseText)

What is this doing? Throwing the response text string into a variable that is being parsed? Some of the values I want to display in a data table and some go to the pie. Does this parse into an array? I see data[0], data[1], data[2], data[3], data[4] below plugged into the graph.

If I was returning fields
white, black, hispanic, asian, native, multi, low_income, drop_rate, grad_rate, per_pov_lvl, plan_full_time, expect_bd (among others)
from the processing page, do I need to structure the reponse.text string to come back as something like
var jsontext = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}
(see to see where I got that from)

or in my case
"white":"value","black":"value","hispanic":"vlauer" etc as a giant text string?

Could I split() like

output= http.responsetext.split("~~~")

display part of it like I would normally in a div with replaced content

document.getElementById('someplace').innerHTML = output(0)

and take the other part, JSON parse it, feed the pie script and draw it in it's div?

var data=JSON.parse(output(1))

Would/could I then refer to the fields as data.white, data.black, data.hispanic etc?

Am I even thinking about this remotely correctly?

Mike
 
Hi

Mike said:
I have never done JSON.
Not so sure. JSON means JavaScript Object Notation and it is JavaScript's native way to write object literals :
Code:
[b]var[/b] an_array[teal]=[[/teal][green][i]'one'[/i][/green][teal],[/teal][green][i]'two'[/i][/green][teal],[/teal][green][i]'three'[/i][/green][teal]][/teal] [gray]// array with three elements[/gray]
[b]var[/b] an_object[teal]=[/teal][teal]{[/teal][green][i]'one'[/i][/green][teal]:[/teal][green][i]'first'[/i][/green][teal],[/teal][green][i]'two'[/i][/green][teal]:[/teal][green][i]'second'[/i][/green][teal]}[/teal] [gray]// object with two properties[/gray]
You know that AJAX means Asynchronous JavaScript And XML. Where XML is the intended format in which the data is transferred. But when goes through HTTP, everything is just data, the client has to interpret it. Parsing XML is not really simple and for portable solutions we had to use JavaScript libraries in the past. But JSON is JavaScript native format, so parsing it is an elementary task. That is why I prefer it.

Because using [tt]eval()[/tt] is considered bad practice, the [tt]JSON[/tt] object was included in modern browsers, having two methods : [tt]parse()[/tt] ( string -> object ) and [tt]stringify()[/tt] ( object -> string ).

As I understood, r.g.piechart() expects five parameters, so the easiest was to send them in an array with five elements :
Code:
data[teal]=[[/teal]
  [purple]225[/purple][teal],[/teal]
  [purple]120[/purple][teal],[/teal]
  [purple]75[/purple][teal],[/teal]
  [teal][[/teal][purple]11[/purple][teal],[/teal] [purple]22[/purple][teal],[/teal] [purple]33[/purple][teal],[/teal] [purple]44[/purple][teal],[/teal] [purple]55[/purple][teal],[/teal] [purple]66[/purple][teal]],[/teal]
  [teal][[/teal][green][i]'legend'[/i][/green][teal]:[/teal] [teal][[/teal][green][i]"[highlight].% - White"[/i][/green][teal],[/teal] [green][i]"[/highlight].% - Black"[/i][/green][teal],[/teal] [green][i]"[highlight].% - Hispanic"[/i][/green][teal],[/teal] [green][i]"[/highlight].% - Asian"[/i][/green][teal],[/teal] [green][i]"[highlight].% - Native Amer."[/i][/green][teal],[/teal] [green][i]"[/highlight].% - Multi"[/i][/green][teal]],[/teal] [green][i]'legendpos'[/i][/green][teal]:[/teal] [green][i]"west"[/i][/green][teal],[/teal] [green][i]'href'[/i][/green][teal]:[/teal] [teal][[/teal][green][i]"[URL unfurl="true"]http://raphaeljs.com"[/URL][/i][/green][teal],[/teal] [green][i]"[URL unfurl="true"]http://g.raphaeljs.com"[/URL][/i][/green][teal]][/teal]
[teal]][/teal]

r[teal].[/teal]g[teal].[/teal][COLOR=darkgoldenrod]piechart[/color][teal]([/teal]data[teal][[/teal][purple]0[/purple][teal]],[/teal] data[teal][[/teal][purple]1[/purple][teal]],[/teal] data[teal][[/teal][purple]2[/purple][teal]],[/teal] data[teal][[/teal][purple]3[/purple][teal]],[/teal] data[teal][[/teal][purple]4[/purple][teal]]);[/teal]
( Note that the above assignment is the equivalent of what is done when [tt]JSON.parse()[/tt] transforms the http.responseText. )

But you can put them into an object, if you like it more :
Code:
data[teal]=[/teal][teal]{[/teal]
  [green][i]'width'[/i][/green][teal]:[/teal] [purple]225[/purple][teal],[/teal]
  [green][i]'height'[/i][/green][teal]:[/teal] [purple]120[/purple][teal],[/teal]
  [green][i]'radius'[/i][/green][teal]:[/teal] [purple]75[/purple][teal],[/teal]
  [green][i]'population'[/i][/green][teal]:[/teal] [teal][[/teal][purple]11[/purple][teal],[/teal] [purple]22[/purple][teal],[/teal] [purple]33[/purple][teal],[/teal] [purple]44[/purple][teal],[/teal] [purple]55[/purple][teal],[/teal] [purple]66[/purple][teal]],[/teal]
  [green][i]'text'[/i][/green][teal]:[/teal] [teal][[/teal][green][i]'legend'[/i][/green][teal]:[/teal] [teal][[/teal][green][i]"[highlight].% - White"[/i][/green][teal],[/teal] [green][i]"[/highlight].% - Black"[/i][/green][teal],[/teal] [green][i]"[highlight].% - Hispanic"[/i][/green][teal],[/teal] [green][i]"[/highlight].% - Asian"[/i][/green][teal],[/teal] [green][i]"[highlight].% - Native Amer."[/i][/green][teal],[/teal] [green][i]"[/highlight].% - Multi"[/i][/green][teal]],[/teal] [green][i]'legendpos'[/i][/green][teal]:[/teal] [green][i]"west"[/i][/green][teal],[/teal] [green][i]'href'[/i][/green][teal]:[/teal] [teal][[/teal][green][i]"[URL unfurl="true"]http://raphaeljs.com"[/URL][/i][/green][teal],[/teal] [green][i]"[URL unfurl="true"]http://g.raphaeljs.com"[/URL][/i][/green][teal]][/teal]
[teal]}[/teal]

r[teal].[/teal]g[teal].[/teal][COLOR=darkgoldenrod]piechart[/color][teal]([/teal]data[teal].[/teal]width[teal],[/teal] data[teal].[/teal]height[teal],[/teal] data[teal].[/teal]radius[teal],[/teal] data[teal].[/teal]population[teal],[/teal] data[teal].[/teal]text[teal]);[/teal]
I do not really understood what you asked in the last part of your post. If you mean that you want to transfer additional data beside those needed by Raphael, then just add them to the structure :
Code:
[small]data[teal]=[[/teal]
  [purple]225[/purple][teal],[/teal]
  [purple]120[/purple][teal],[/teal]
  [purple]75[/purple][teal],[/teal]
  [teal][[/teal][purple]11[/purple][teal],[/teal] [purple]22[/purple][teal],[/teal] [purple]33[/purple][teal],[/teal] [purple]44[/purple][teal],[/teal] [purple]55[/purple][teal],[/teal] [purple]66[/purple][teal]],[/teal]
  [teal][[/teal][green][i]'legend'[/i][/green][teal]:[/teal] [teal][[/teal][green][i]"[highlight].% - White"[/i][/green][teal],[/teal] [green][i]"[/highlight].% - Black"[/i][/green][teal],[/teal] [green][i]"[highlight].% - Hispanic"[/i][/green][teal],[/teal] [green][i]"[/highlight].% - Asian"[/i][/green][teal],[/teal] [green][i]"[highlight].% - Native Amer."[/i][/green][teal],[/teal] [green][i]"[/highlight].% - Multi"[/i][/green][teal]],[/teal] [green][i]'legendpos'[/i][/green][teal]:[/teal] [green][i]"west"[/i][/green][teal],[/teal] [green][i]'href'[/i][/green][teal]:[/teal] [teal][[/teal][green][i]"[URL unfurl="true"]http://raphaeljs.com"[/URL][/i][/green][teal],[/teal] [green][i]"[URL unfurl="true"]http://g.raphaeljs.com"[/URL][/i][/green][teal]],[/teal][/small]
  [green][i]'Distribution of the population'[/i][/green][teal],[/teal]
  [green][i]'Data was collected by me last year'[/i][/green]
[small][teal]][/teal]
r[teal].[/teal]g[teal].[/teal][COLOR=darkgoldenrod]piechart[/color][teal]([/teal]data[teal][[/teal][purple]0[/purple][teal]],[/teal] data[teal][[/teal][purple]1[/purple][teal]],[/teal] data[teal][[/teal][purple]2[/purple][teal]],[/teal] data[teal][[/teal][purple]3[/purple][teal]],[/teal] data[teal][[/teal][purple]4[/purple][teal]]);[/teal][/small]
document[teal].[/teal][COLOR=darkgoldenrod]getElementsByTagName[/color][teal]([/teal][green][i]'h1'[/i][/green][teal])[[/teal][purple]0[/purple][teal]].[/teal]innerHTML[teal]=[/teal]data[teal][[/teal][purple]5[/purple][teal]][/teal]
document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'detail'[/i][/green][teal]).[/teal]innerHTML[teal]=[/teal]data[teal][[/teal][purple]6[/purple][teal]][/teal]

[gray]// or[/gray]

[small]data[teal]=[/teal][teal]{[/teal]
  [green][i]'width'[/i][/green][teal]:[/teal] [purple]225[/purple][teal],[/teal]
  [green][i]'height'[/i][/green][teal]:[/teal] [purple]120[/purple][teal],[/teal]
  [green][i]'radius'[/i][/green][teal]:[/teal] [purple]75[/purple][teal],[/teal]
  [green][i]'population'[/i][/green][teal]:[/teal] [teal][[/teal][purple]11[/purple][teal],[/teal] [purple]22[/purple][teal],[/teal] [purple]33[/purple][teal],[/teal] [purple]44[/purple][teal],[/teal] [purple]55[/purple][teal],[/teal] [purple]66[/purple][teal]],[/teal]
  [green][i]'text'[/i][/green][teal]:[/teal] [teal][[/teal][green][i]'legend'[/i][/green][teal]:[/teal] [teal][[/teal][green][i]"[highlight].% - White"[/i][/green][teal],[/teal] [green][i]"[/highlight].% - Black"[/i][/green][teal],[/teal] [green][i]"[highlight].% - Hispanic"[/i][/green][teal],[/teal] [green][i]"[/highlight].% - Asian"[/i][/green][teal],[/teal] [green][i]"[highlight].% - Native Amer."[/i][/green][teal],[/teal] [green][i]"[/highlight].% - Multi"[/i][/green][teal]],[/teal] [green][i]'legendpos'[/i][/green][teal]:[/teal] [green][i]"west"[/i][/green][teal],[/teal] [green][i]'href'[/i][/green][teal]:[/teal] [teal][[/teal][green][i]"[URL unfurl="true"]http://raphaeljs.com"[/URL][/i][/green][teal],[/teal] [green][i]"[URL unfurl="true"]http://g.raphaeljs.com"[/URL][/i][/green][teal]],[/teal][/small]
  [green][i]'title'[/i][/green][teal]:[/teal] [green][i]'Distribution of the population'[/i][/green][teal],[/teal]
  [green][i]'explanation'[/i][/green][teal]:[/teal] [green][i]'Data was collected by me last year'[/i][/green]
[small][teal]}[/teal]
r[teal].[/teal]g[teal].[/teal][COLOR=darkgoldenrod]piechart[/color][teal]([/teal]data[teal].[/teal]width[teal],[/teal] data[teal].[/teal]height[teal],[/teal] data[teal].[/teal]radius[teal],[/teal] data[teal].[/teal]population[teal],[/teal] data[teal].[/teal]text[teal]);[/teal][/small]
document[teal].[/teal][COLOR=darkgoldenrod]getElementsByTagName[/color][teal]([/teal][green][i]'h1'[/i][/green][teal])[[/teal][purple]0[/purple][teal]].[/teal]innerHTML[teal]=[/teal]data[teal].[/teal]title
document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'detail'[/i][/green][teal]).[/teal]innerHTML[teal]=[/teal]data[teal].[/teal]explanation
Using the [tt]split()[/tt] method would be also a way to parse the transferred text into array. But your data is multidimensional, so you will have to use [tt]split()[/tt] multiple times and use different separators to separate the elements at different levels. So it would be uglier and much more tedious.


Feherke.
 
Ok.

"As I understood, r.g.piechart() expects five parameters, so the easiest was to send them in an array with five elements"

"header('Content/type: text/javascript'); echo json_encode($pie[$_GET['pie']]);"

I think that means that on your processing PHP page you are writing the results into an array and sending that back with a javascript header. That then gets parsed by the JSON parse thing and dumped into the pie graph script.

On my ASP processing page I'm not currently doing that. I'm sending back a mix of HTML, values to display as text, some values to use in some more ASP, and the values for the pie graph. That is why I was asking about split().

I was thinking I could continue to send my mixed return but include a delimiter so that the mixed stuff was one part and the pie graph numbers were the other part. Then maybe I could display and act on the mixed part which I know how to do and then employ your JSON parsing to build the pie.

Where I think you are going is to send it all back as a giant javascript array (including the HTML), JSON parse the whole thing, dump the pie value into the pie script and the other stuff into the different divs I want.

This makes my head swirly.

Thanks for the help. I am going to mess with it. It will be awhile (becasue I have some other stuff going), but I will post back on my adventures. Small baby steps.
 
Hi

Mike said:
I think that means that on your processing PHP page you are writing the results into an array and sending that back with a javascript header.
Yes, essentially that is there : the PHP array is transformed into text, sent, then the text is transformed into JavaScript array.
Mike said:
I'm sending back a mix of HTML, values to display as text, some values to use in some more ASP, and the values for the pie graph. That is why I was asking about split().
So you are already sending kind of structured data, but in your own format. For that you had to write your own code to handle it.

If you use a widely supported format like JSON, you will find libraries to handling it for almost every language.

While you have a single dimensional array, is quite simple to handle your own format. But that will become difficult as the data structure becomes complex.

r.g.piechart()'s 5[sup]th[/sup] parameter is an object with 3 properties, 2 of them having array values. Parsing that manually with [tt]split()[/tt] will take more time than learning JSON.
Mike said:
This makes my head swirly.
Probably because my explanation.

Take a look at the aspjson - JSON for ASP project's site, they have some simple ASP examples. Maybe looking form the "other side" will look simpler.

( Note that my code in the previous post was messed up with some [tt][ignore][highlight][/ignore][/tt] .. [tt][ignore][/highlight][/ignore][/tt] tags. Originally there were your "%%" signs in their place. )


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top