Rename x and y-axis labels [AttributedString error]

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
AjitPS
Posts: 49
Joined: Tue Oct 22, 2013 10:08 am
antibot: No, of course not.
Location: UK

Rename x and y-axis labels [AttributedString error]

Post by AjitPS » Mon Jul 07, 2014 10:26 am

Hi, I'm using JFreeChart to create XYLineCharts. However, I am facing a few issues related to the Chart's x-axis and y-axis labels, around renaming them.

Firstly, after the chart is drawn, I open its popup menu & click on "Properties" to bring up the Properties menu for the Chart. However, if I then go to the Domain Axis or Range Axis within Properties, the value for axes Labels shows an AttributedString value instead of the actual axis labels that are shown on the Chart. The chart title field however, shows the correct String value. How can I fix this so as to have the axis labels also show their actual values instead of AttributedString values ?

Secondly, when I try to rename these x, y-axis labels, the new value is shown correctly within "Properties" but is shown as its AttributedString value on the main chart. I want to be able to rename the x and y-axis labels on the Chart and have it show them correctly on the Chart and within the "Properties" menu. e.g., if I currently draw a Chart & rename its x-axis label within "Properties", it now shows it as:

Code: Select all

java.text.AttributedString@364adedd
Thirdly, when I rename the Chart title or domain/ range axis labels and click "Ok", I want to be able to get the new label values and display them below the chart in a table. How can I get these values from the chart ? Which JFreeChart method would I need to override ?

Kindly let me know the cause of this AttributedString error and how can it be fixed and how can I detect when user clicks "Ok" after renaming a Chart's title or its x or y-axis labels & get those values ? Thanks.

AjitPS
Posts: 49
Joined: Tue Oct 22, 2013 10:08 am
antibot: No, of course not.
Location: UK

Re: Rename x and y-axis labels [AttributedString error]

Post by AjitPS » Mon Jul 07, 2014 10:27 am

Note: I had asked a similar question before at http://www.jfree.org/phpBB2/viewtopic.p ... utedString but did not get a solution to my problem then.

Any suggestions on how to resolve this would be very helpful.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Rename x and y-axis labels [AttributedString error]

Post by david.gilbert » Tue Jul 08, 2014 5:57 am

AjitPS wrote:Firstly, after the chart is drawn, I open its popup menu & click on "Properties" to bring up the Properties menu for the Chart. However, if I then go to the Domain Axis or Range Axis within Properties, the value for axes Labels shows an AttributedString value instead of the actual axis labels that are shown on the Chart. The chart title field however, shows the correct String value. How can I fix this so as to have the axis labels also show their actual values instead of AttributedString values ?
This patch fixes the problem:

Code: Select all

# This patch file was generated by NetBeans IDE
# It uses platform neutral UTF-8 encoding and \n newlines.
--- HEAD
+++ Modified In Working Tree
@@ -2,7 +2,7 @@
  * JFreeChart : a free chart library for the Java(tm) platform
  * ===========================================================
  *
- * (C) Copyright 2000-2012, by Object Refinery Limited and Contributors.
+ * (C) Copyright 2000-2014, by Object Refinery Limited and Contributors.
  *
  * Project Info:  http://www.jfree.org/jfreechart/index.html
  *
@@ -27,7 +27,7 @@
  * ----------------------
  * DefaultAxisEditor.java
  * ----------------------
- * (C) Copyright 2005-2012, by Object Refinery Limited and Contributors.
+ * (C) Copyright 2005-2014, by Object Refinery Limited and Contributors.
  *
  * Original Author:  David Gilbert;
  * Contributor(s):   Andrzej Porebski;
@@ -70,6 +70,7 @@
 import org.jfree.chart.ui.LCBLayout;
 import org.jfree.chart.ui.PaintSample;
 import org.jfree.chart.ui.RectangleInsets;
+import org.jfree.chart.util.AttributedStringUtils;
 import org.jfree.chart.util.ResourceBundleWrapper;
 
 /**
@@ -200,7 +201,9 @@
         JPanel interior = new JPanel(new LCBLayout(5));
         interior.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
         interior.add(new JLabel(localizationResources.getString("Label")));
-        this.label = new JTextField(axis.getLabel().toString());
+        String axisLabel = AttributedStringUtils.toString(
+                axis.getLabel().getIterator());
+        this.label = new JTextField(axisLabel);
         interior.add(this.label);
         interior.add(new JPanel());
 
The chart editing code hasn't been worked on in a long time, and only covers a tiny subset of the chart properties. Note that I'm considering removing all of it from JFreeChart. If that happens, anyone that wants to use property editing will have to maintain it separately.
AjitPS wrote:Secondly, when I try to rename these x, y-axis labels, the new value is shown correctly within "Properties" but is shown as its AttributedString value on the main chart. I want to be able to rename the x and y-axis labels on the Chart and have it show them correctly on the Chart and within the "Properties" menu. e.g., if I currently draw a Chart & rename its x-axis label within "Properties", it now shows it as:

Code: Select all

java.text.AttributedString@364adedd
I'm not seeing this issue.
AjitPS wrote:Thirdly, when I rename the Chart title or domain/ range axis labels and click "Ok", I want to be able to get the new label values and display them below the chart in a table. How can I get these values from the chart ? Which JFreeChart method would I need to override ?
To get the text from the chart title:

Code: Select all

chart.getTitle().getText();
To get the text from the axis label:

Code: Select all

String axisLabel = AttributedStringUtils.toString(axis.getLabel().getIterator());
I should probably add convenience methods for each of these to the JFreeChart and Axis classes.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked