PDA

View Full Version : JMSL Chart Antialiasing?



cbanack
12-09-2008, 02:30 PM
Hi,

Was wondering if there is a way to enabled anti-aliasing in the "Chart" classes in the latest version of JMSL? I'm producing some charts for our application, but the lines and markers are all jaggy and aliased. :)

Thanks!

ed
12-10-2008, 07:01 AM
I haven't found an effective way to turn on anti-aliasing for charts yet.

The most likely candidate is:


JPanelChart panel = new JPanelChart(chart);
Graphics2D g2 = (Graphics2D)panel.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);


but it doesn't work.

To enable AA graphics in the IMSL C# charting library, we had to modify the source code to add an API that allows setting the "SmoothingMode (http://msdn.microsoft.com/en-us/library/z714w2y9.aspx)". The Java equivalent appears to be RenderingHints, but it's possible we need to grab a handle to the Graphics2D object a little deeper, for example inside the Draw class source.

This isn't the first request we've had for this, so we'll continue to research.

If anyone out there has had success turning on AA for custom components, I'd love to hear any hints.

cbanack
12-10-2008, 10:10 AM
Well, the method call panel.getGraphics() in your "most likely candidate" produces a NEW Graphics object, which can indeed be used to draw on that panel...but now it is just one of potentially several Graphics objects that can do so.

The code you've written will produce anti-aliased drawing effects if you were to use that new Graphics to do some drawing--i.e. if you were to call g2.drawLine() or something like that.

But what you really want to do is enable anti-aliasing on the specifc Graphics object that is being used by Swing to render the panel onto the screen, rather than enabling anti-aliasing on the new Graphics that you've just instantiated.

This works, at least for a JChartPanel:


JPanelChart panelchart = new JPanelChart() {
public void paint( Graphics g ) {
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASI NG,
RenderingHints.VALUE_ANTIALIAS_ON);
super.paint( g );
}
};


It should be easy enough to enable some similar code right inside your JPanelChart, so that JMSL users don't have to override it themselves. I'd recommend turning on anti-aliasing by default; I'm not even sure there's much reason to give users the ability to turn it off, since nowadays there's probably no good reason NOT to do anti-aliasing. It used to be a performance concern, but now I think even the oldest, lamest computer has more than enough horsepower to handle it. (Maybe underpowered devices like cellphones or blackberries need to turn it off...?)

There is also:


g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIA LIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);


But I don't think that is necessary, since I think text anti-aliasing gets turned on automatically when you enable the general anti-aliasing.

ed
12-10-2008, 10:26 AM
Ah, so that's not using the underlying Graphics2D object. That makes sense as to why it doesn't work.

I'll try out your suggestion inside the JChartPanel class when I get a chance. If it works, we'll certainly roll this into the next release.

Thanks!

ed
12-10-2008, 06:07 PM
I started digging into the source and found we are already processing RenderingHints in the ChartNode hierarchy. So you just need to use setAttribute() on any ChartNode instance to turn on anti-aliasing.

Here's an example using the LineEx1 (http://www.vni.com/products/imsl/jmsl/v501/manual/api/com/imsl/chart/LineEx1.html) source:



AxisXY axis = new AxisXY(chart);

RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
axis.setAttribute("RenderingHints", hints);


This turns on AA for everything drawn on that axis now. In the two attached images, the first is the default without these extra lines and the second is the result of adding them.

We will still look into enabling this by default -- but luckily this can be done simply on a per-node basis without much additional coding.

Jacky Jacson
10-27-2014, 05:01 AM
If anyone out there has had success turning on AA for custom components, I'd love to hear any hints.