Subscript in axis labels

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
NickDeGraeve
Posts: 3
Joined: Mon Jul 29, 2013 8:57 am
antibot: No, of course not.

Subscript in axis labels

Post by NickDeGraeve » Mon Jul 29, 2013 9:18 am

How can I use subscript in the label of the X-axis of an XYPlot?

I need to label C<sub>L</sub> and C<sub>T</sub> so I can't use Unicode. From what I learned from googling it might be possible using Annotations (XYImageAnnotation?) but I can't figure out how to (re)place the label.

Thanks,
Nick

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

Re: Subscript in axis labels

Post by david.gilbert » Wed Jul 31, 2013 7:19 am

It's not supported. Probably the best approach would be to modify the axis to use AttributedString for the labels, rather than a plain String.
David Gilbert
JFreeChart Project Leader

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

NickDeGraeve
Posts: 3
Joined: Mon Jul 29, 2013 8:57 am
antibot: No, of course not.

Re: Subscript in axis labels

Post by NickDeGraeve » Wed Jul 31, 2013 7:29 am

I've never used AttributedString before but I'll give it a try. I'll report back any success/failure.
Thanks.

As a side note, why is there such a limited support for sub-/superscript? I would suspect it would be quite a common requirement.
Is this something that's on the roadmap?

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

Re: Subscript in axis labels

Post by david.gilbert » Thu Aug 01, 2013 1:45 pm

I made the change to use AttributedString for axis labels, but for JFreeChart-FSE - here's the commit:

http://github.com/jfree/jfreechart-fse/ ... f17cdd38d4

Now I need to backport that for the upcoming JFreeChart 1.0.16 release.
David Gilbert
JFreeChart Project Leader

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

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

Re: Subscript in axis labels

Post by david.gilbert » Thu Aug 01, 2013 6:16 pm

This is the change that will be in 1.0.16:

http://sourceforge.net/p/jfreechart/code/2784/

If anyone gets a chance to test it before it is released, it will be a big help. 8)
David Gilbert
JFreeChart Project Leader

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

NickDeGraeve
Posts: 3
Joined: Mon Jul 29, 2013 8:57 am
antibot: No, of course not.

Re: Subscript in axis labels

Post by NickDeGraeve » Sun Aug 04, 2013 8:04 am

Hi

I did a test and it seems to work.

The only remark I have is that the position and font properties of the label are different when I set a normal or an attributed one. And I haven't been able to match both. But for me that isn't a showstopper. I'm just glad I'll have subscript functionality.

When can we expect the 1.0.16 release?

Test code:

Code: Select all

public class AttributeStringLabelTest {

	@Test
	public void test() throws InterruptedException {
		String xAxisLabel = "XAxis";
		String yAxisLabel = "YAxis";
		DefaultXYDataset dataset = new DefaultXYDataset();
		double[][] data = {{1, 2}, {0.5, 2}};
		dataset.addSeries("Series 1", data);
		JFreeChart chart = ChartFactory.createXYLineChart("", xAxisLabel, yAxisLabel, dataset, PlotOrientation.VERTICAL, false, false, false);
		
		showImage("Default Labels", chart, 100, 100);
		
		XYPlot xyPlot = chart.getXYPlot();
		ValueAxis domainAxis = xyPlot.getDomainAxis();
		ValueAxis rangeAxis = xyPlot.getRangeAxis();
		domainAxis.setLabel(xAxisLabel);
		rangeAxis.setLabel(xAxisLabel);
		showImage("Default Plot Labels", chart, 450, 100);
		
		AttributedString domainAxisString = new AttributedString(xAxisLabel);
		domainAxis.setAttributedLabel(domainAxisString);
		AttributedString rangeAxisString = new AttributedString(yAxisLabel);
		rangeAxis.setAttributedLabel(rangeAxisString);
		
		showImage("Default Plot AttributedText Labels", chart, 800, 100);
		
		Font font = new Font(Font.SANS_SERIF, Font.BOLD, 14);
		domainAxisString.addAttributes(font.getAttributes(), 0, xAxisLabel.length());
		domainAxisString.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB, 1, 5);
		rangeAxisString.addAttributes(font.getAttributes(), 0, yAxisLabel.length());
		rangeAxisString.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB, 1, 5);
		
		showImage("Custom Plot AttributedText Labels", chart, 1150, 100);
		
		Thread.sleep(20000);
	}
	
	private void showImage(String title, JFreeChart chart, int x, int y) {
		JFrame frame = new JFrame(title);
		frame.setLayout(new BorderLayout());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.setLocation(x, y);
		BufferedImage image = chart.createBufferedImage(300, 300);
		frame.add(new JLabel(new ImageIcon(image)), BorderLayout.CENTER);
		frame.pack();
		frame.setVisible(true);
	}

}

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

Re: Subscript in axis labels

Post by david.gilbert » Tue Aug 06, 2013 9:49 am

Thanks, I'll take a look at that.

I'd like to get JFreeChart 1.0.16 out the door next week, but you should not count on that. :)
David Gilbert
JFreeChart Project Leader

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

henriklasu
Posts: 3
Joined: Fri Sep 06, 2013 11:20 am
antibot: No, of course not.

Re: Subscript in axis labels

Post by henriklasu » Fri Sep 06, 2013 12:18 pm

do you have a preliminary date for 1.0.016?

we would realy like to be able to have scale on our plotaxis that doesnt look like this: 10^4 but with real sub/super scripts.

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

Re: Subscript in axis labels

Post by david.gilbert » Mon Sep 09, 2013 8:53 pm

1.0.16 will be released this month. The AttributedString feature is applied to the axis labels, but not the tick labels at this point. I will take a look at what would be required to add this to the tick labels, because it would improve the log axis formatting a lot.
David Gilbert
JFreeChart Project Leader

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

Locked