ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  URL-Encoding in image maps?
Posted by Michael Opitz on Jan-20-2025 23:59
Hello!
I have a Java application and I noticed, that I have an encoding issue with image map generation since Java21.
I use non ascii characters and therefore I URL-encode them in the queryFormat parameter for getHtmlImageMap.

I used to use the default encoding, which is windows-1252 on my machine for URL encoding and it worked nicely with the Hotspot-parameters in the ChartViewer. I could see the URL/windows1252 decoded parameters.

Sind Java21, the default encoding changed to UTF-8 and now I have strange parameter values in the Hotspot-Events of the ChartViewer.
Example:
I used to urlEncode the letter ä to %E4 and it resulted in a proper ä in the Hotspot-Event.
With UTF-8, the letter ä results in %C3%A4 and I see a strange character in the Hotspot-Event (ä).

Do I need to configure something on the ChartViewer to URLdecode the values with UTF-8 instead of windows-1252?

Kind regards,
Michael

  Re: URL-Encoding in image maps?
Posted by Peter Kwan on Jan-21-2025 01:32
Hi Michael,

From your previous messages, I assume you are using ChartDirector for Java in a desktop application (as opposed to a web application).

All Java text strings must use UTF-16 encoding (2-byte unicode). It is not possible to use another encoding. See:

https://docs.oracle.com/javase/10/docs/api/java/lang/String.html

Your source code can use any encoding, as your source code is for human reading and cannot be executed by java. You need a java compiler to convert it to Java byte code first. The java compiler assumes your source code is in a certain encoding, and convert all text strings to UTF-16 as mandatory in Java.

If the Java compiler assumes your source code is in UTF-8, but it is actually in another encoding, it will convert the text strings in the source code incorrectly. The source code encoding is determined by the program that creates the source code file (such as a text editor).

In this case, you can configure your text editor to output UTF-8, or configure your compiler to use the encoding of your text editor:

https://docs.oracle.com/en/java/javase/22/docs/specs/man/javac.html

In any case, it is not possible for ChartDirector to see the text strings in your source code. ChartDirector can only see the text strings in the compiled code, which is always UTF-16 encoded.

Best Regards
Peter Kwan

  Re: URL-Encoding in image maps?
Posted by Michael Opitz on Jan-21-2025 16:19
I think, there is a misunderstanding.

I am not talking about source code. I am talking about url encoded data in the href section of the image maps:
See the "sumByName" parameter:

<area shape="rect" coords="75,123,126,162"  title="Prozessdurchlaufzeit [Tage] = 77,7
Händler = USA-Seattle
Farbe = blau
Relevanz = 491
Anteil = 12,3 %" href="value?sumByName=H%C3%A4ndler&category=blau&categoryName=Farbe&categoryKey=D_COLOR&valueName=Prozessdurchlaufzeit+%5BTage%5D&value=77,7&sumBy=USA-Seattle&sumByKey=D_DEALER&valueKey=PDLZ">

Let me give you a quick and dirty example:

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

BaseChart chart = new XYChart();
ChartViewer chartViewer = new ChartViewer()
...

String href = "http://myurl";
String[] data = {"key",  URLEncoder.encode("Händler", StandardCharsets.UTF-8)};
String queryFormat = data[0] + "=" + data[1];
String toolTip = "tooltip";

String imageMap = chart.getHTMLImageMap(href, queryFormat, toolTip, 0, 0)
// Todo: add more data to the imageMap

chartViewer.setChart(chart);
chartViewer.setImageMap(imageMap);

If you now trigger a HotSpotEvent, the parameter "key" results in "Händler" instead of Händler.

I think there is a bug in ChartDirector: the parsing of the resulting query of the "href" element of the image map "area" (here: sumByName=H%C3%A4ndler)  does not take into consideration, that the string might be UTF-8 URL-encoded.
It assumes, that the "ä" is only one character (%E4).
Therefore, instead of decoding "%C3%A4" to "ä", it decodes "%C3%A4" to "ä"

The code works fine with:
String[] data = {"key",  URLEncoder.encode("Händler", StandardCharsets.ISO-8859-1)};

Unfortunately, nowadays, UTF-8 becomes more and more the standard for URL encoding.

  Re: URL-Encoding in image maps?
Posted by Peter Kwan on Jan-21-2025 18:19
Attachments:
Hi Michael,

You should not need to use URLEncoder. Instead, ChartDirector will automatically encode the URL if necessary.

In a chart, there can be many parameters, such as the x-axis labels, data set names, the percentage sign sometimes used to display units, etc.. All of them can contain characters forbidden in URL. If the programmer needs to prepare two versions of these labels, one for display on screen, and one for URL usage, it would be quite unfriendly.

Instead, when ChartDirector generates the URL, it will automatically escape those characters "if necessary". Your code should not escape the characters yourself, otherwise it may end up being "double escaped" or have some unknown behaviour.

For web applications, it is necessary to escape URL reserve characters (like & and ?). It is also necessary to escape non-ASCII characters (due to HTTP protocol requirement).

For desktop applications, it is only necessary to escape URL reserve character (like & and ?), but not non-ASCII characters.

For your case, have you try something like:

String[] data = {"key",  "Händler"};

I have attached my test code with some screenshots for your reference. I started with the "Simple Bar Chart" SWING sample code that comes with ChartDirector. I modified it to use "Händler" as one of the labels. In my test, everything seems to work as expected.

If the above still cannot solve the problem, is it possible to provide an sample that I can compile and run (perhaps modifying the Simple Bar Chart sample code), so I can reproduce the problem?

Best Regards
Peter Kwan
javaw_20250121172405.png
javaw_20250121172434.png
simplebar.java
simplebar.java

1.94 Kb