Scientific charts

Scientific charts

There are plenty of Java libraries to create charts and graphics, but I haven't seen any specifically designed for science. What I mean is to draw subscripts and superscripts in the titles or axis labels, and also to draw greek letters easily. This is an important requirement because a scientist usually needs to draw units of any kind or mathematical formulae. Formulae can be generated using JMathText, an implementation of LATEX formulae in Java, but this approach is impossible to generate labels in different charting libraries. Since these libraries are quite complex I didn't find a solution until recently. I found a simple 2D graphics package written by Leigh Brookshaw that inspired me. I took one of his classes called TextLabel and rewrote it adapted to my needs. Then I navigated into the code of some powerful graphics libraries to support the new features. First I modified JCommons library to have everything available in JFreeChart, but now I have also modified SGT and JMathPlot to add these new features. These are the results.

JFreeChart

I modified org.jfree.text.TextUtilities and added class TextLabel in the same package. To change the way Strings are drawn I simply commented the drawString method and added the other calls. For details the new classes are located in jcommon-1.0.16.jar, which is a dependency for the current version of JFreeChart (1.0.13).

TextLabel t = new TextLabel(text, g.getFont(), g.getColor(), TextLabel.LEFT);
 
/* tl.draw(g, textX, textY); */
 
t.draw(g, (int) textX, (int) textY);

Resulting charts are shown below. The new implementation is not very elegant, but allows to draw subscript and superscripts, greek letters in lower and upper case, and to change the size and even the color of the text.

 

SGT

JFreeChart supports surface charts, but it seems it is not still capable of drawing contour lines. For this purpose the SGT library developed at NOAA is more adequate. This library has some bugs and limitations. Since most libraries supports rotating and zooming with the mouse I added these features, mainly in JPARSEC. The main bug I found is that the color bar that shows intensity levels is inverted when it is shown in vertical orientation. So I corrected this bug and also added the new features in the same way as before for JFreeChart. Modified classes are gov.noaa.pmel.sgt.ColorKey, gov.noaa.pmel.sgt.LabelDrawer1, gov.noaa.pmel.sgt.LabelDrawer2, gov.noaa.pmel.sgt.swing.JPlotLayout, and gov.noaa.pmel.sgt.swing.JGraphicLayout. In this case TextLabel is not required since I compiled SGT by adding that dependency from JPARSEC.

 

JMathPlot

JMathPlot is a new library to show 3D charts. I also decided to modify it to change the mouse behaviour in zooms and translations, since it was very easy to 'loose' the chart and then impossible to center it again. After adding the new features for subscripts and superscripts I obtained the following 3d version of the previous chart.

 

In the last case I modified org.math.plot.plotObjects.Axis, org.math.plot.canvas.PlotCanvas, org.math.plot.render.Projection, org.math.plot.render.Projection3D, org.math.plot.render.AWTDrawer, and added org.math.plot.render.TextLabel.

NOTE: If you want to use these advanced features for charting in Java I've upploaded some code that reduces JPARSEC to the minimum for that purpose, based on version 1.83 of my library. Download this file and use also my own version of jcommon-1.0.16.jar, or put jparsec.graph.TextLabel class inside jcommon and you can forget about the rest of the jfreechart binding code. Relevant classes are jparsec.graph.TextLabel and jparsec.graph.CreateChart. This code is released under LGPL license, so it can be used even in a commercial project. For jmathplot and SGT libraries the corresponding files are jmathplot.jar and sgt_v30.jar, and recently I also did this for the SurfacePlotter library.

Discussion

Jorge Fdez., 2016/02/13 15:24

Dear Alonso,

I am writing to you because I am using the jparsec.graph.TextLabel class in my jfreeChart code, and I have a small issue that I would like to ask you. First of all, I'd like to thank you for sharing your code. I am using the command @LATEX{formula} to include a formula in the title of the final pdf file, but the problem is that the quality of the title is very low and the symbols cannnot be clearly distinguished. On the other hand, the quality of the final pdf file is fine if I make use of special characters like @alpha. Do you know how could I solve this minor issue?

Thank you very much in advance.

Jorge.

Tomás Alonso Albi, 2016/02/16 18:28

Hi Jorge. Very, very tricky to do it.

1. After creating the chart with CreateChart class, use something like this

TextTitle tt = ch2.getChart().getTitle(); Font biggerTitle = tt.getFont().deriveFont(tt.getFont().getSize2D()*2f); tt.setFont(biggerTitle); ch2.getChart().setTitle(tt);

to increase the area allowed for the title. Don't use CreateChart.updateChart method later.

2. In case you obtain a parsing error in JMathTex, increase the width of the chart in pixels when defining the ChartElement object, or use a factor less than 2 to increment the text size in the title in the previous code.

3. In case the formula gets out of the vertical bounds of the title, locate the following code in TextLabel

do { lf = new LATEXFormula(f, current.f.getStyle(), initS); initS = initS - 2; } while(lf.getAsImage().getHeight() > maxH);

and replace it by something like

do { lf = new LATEXFormula(f, current.f.getStyle(), initS); initS = initS - 2; } while(lf.getAsImage().getHeight() > maxH*0.8);

It doesn't work very well, but its something to start. I'll add to my todo list to improve the current version of jparsec (not the code posted in this page) when having time.

Hope it helps, regards Tomas

Jorge Fernandez, 2016/02/24 16:30

Dear Tomas,

we are using the jfrechart library version 1.19. We have checked that it depends on

<groupId>org.jfree</groupId> <artifactId>jcommon</artifactId> <version>1.0.23</version> </dependency>

We would like to make use of your jcommon-1.0.16.jar instead. Do you know whether there could be any problem with it since your version is older?

Cheers,

J.

Tomás Alonso Albi, 2016/02/24 18:14

jcommon is probably more stable, so I don't think so, just try it. Anyway, if you do a diff command or use a tool to see file differences you can add yourself the few line changes described above to 1.0.23.

Regards, Tomas

 
blog/mail_name_date_scientific_charts.txt · created: 2009/12/23 13:26 (Last modified 2017/01/17 17:57) by Tomás Alonso Albi
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki