Re: Escape html in charts

170 views
Skip to first unread message

asgallant

unread,
Nov 14, 2012, 11:07:07 AM11/14/12
to google-visua...@googlegroups.com
You can't use HTML in the labels.  To encode non standard characters, you have to use the javascript UTF code, e.g. "\u00E0" for "à".  The linebreak character in javascript is "\n", but I suspect the API strips out extra whitespace characters, as the linebreaks are not preserved when the chart is drawn.  So, short answer, no you cannot use linebreaks in your title or labels in the charts.

On Wednesday, November 14, 2012 6:03:21 AM UTC-5, Rita wrote:
Hello everybody.
How can I escape html in my charts?
I have accented characters and in the DataTable I use the property allowHtml:true that allow me to see correctly accented character. For example "à" is converted in "à".
But the chart shows me "à", without converting it. I cannot find a property like allowHtml for the charts, can you help me? Could me suggest a way to visualize
 both in DataTable and in the chart accented characthers?
Moreover the escape for html could be useful to format properly the title of charts for example to insert <br>. Is it possibile?
 I have tried to use any kind of escape without success.

Thank you
Rita

Rita

unread,
Nov 14, 2012, 12:11:16 PM11/14/12
to google-visua...@googlegroups.com
Thanks for your reply, but it does not resolve my problem.
The javascript UTF code both in charts as in table is not converted. So I see \u00E0 not  à.

Any other ideas?

Thanks
Rita

Rita

unread,
Nov 14, 2012, 1:56:03 PM11/14/12
to google-visua...@googlegroups.com
Your solution works in Google Visualization Playgroung tool, but not in my app. I write on my own the JSON file with data for the chart, so do I have to use particular method to embed the javascript code? I tried with different escape methods with success.

I wait any suggestion, thanks


Rita


Il giorno mercoledì 14 novembre 2012 17:07:07 UTC+1, asgallant ha scritto:

asgallant

unread,
Nov 14, 2012, 2:13:11 PM11/14/12
to google-visua...@googlegroups.com
Can you post you code and a sample JSON string?

Rita

unread,
Nov 14, 2012, 3:48:52 PM11/14/12
to google-visua...@googlegroups.com
Thak you,
sorry but does not appear the button to format the code:
I load my json file;

function load() {
var chart_json_data = getUrlVars()["result"];
if (!(typeof chart_json_data === 'undefined')){
  downloadUrl("" + chart_json_data, drawChart);
 }
 }
function getUrlVars()
{ var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
 function(m,key,value) {
vars[key] = value; });
 return vars; }

 function createXmlHttpRequest() {
 try { if (typeof ActiveXObject != 'undefined') {
 return new ActiveXObject('Microsoft.XMLHTTP'); }
else if (window["XMLHttpRequest"]){
return new XMLHttpRequest();
 }
 } catch (e) {
alert(e); } return null; }

 function downloadUrl(url, callback) {
 var status = -1;
 var request = createXmlHttpRequest();
if (!request) {
return false;
 }
request.onreadystatechange = function() {
 if (request.readyState == 4) {
 try { status = request.status;
 } catch (e) {
 // Usually indicates request timed out in FF.
 }
 if ((status == 200) || (status == 0)) {
callback(request.responseText, request.status);
 request.onreadystatechange = function() {}; }
 }
} request.open('GET', url, true);
 try {
 request.send(null);
 } catch (e) {
 alert(e);
 }
 };


// Load the Visualization API the chart package and the table chart package.
google.load('visualization', '1.0', {'packages':['corechart','table']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(load);
 drawChart = function(doc) {
  if (!(typeof doc === 'undefined')) { jsonData = eval('(' + doc + ')'); }
...



and then I draw my chart and table





A little example of my json is :

{"cols":[{"id":"c1","label":"Cat","type":"string"},{"id":"c2","label":"N.","type":"number"}],
"title":"Chart",
"rows":[
{"c":[{"v":"Pubblicit\\u00E0"},{"v":11}]},
{"c":[{"v":"Trasporti"},{"v":5}]}
]
}


Rita

asgallant

unread,
Nov 14, 2012, 4:05:30 PM11/14/12
to google-visua...@googlegroups.com
You've got an extra "\" in your string:

"Pubblicit\\u00E0"

should be:

"Pubblicit\u00E0"

Rita

unread,
Nov 14, 2012, 5:03:57 PM11/14/12
to google-visua...@googlegroups.com
Yes thank you!!! That's the problem.
I am trying to resolve. It's the generation of the json file that put another \ in front of the code, and I did not realize. I use the toJSONString() method of the json-simple-1.1.1.jar
Reply all
Reply to author
Forward
0 new messages