blog rss feed

Wrapping JFreeChart as a builder for Groovy

I’ve started looking into the possibility of wrapping up JFreeChart to make it a little groovier. I know there’s no end of web packages for graphing, but what if you don’t want to transmit sensitive data across the internet, what if you don’t want the data in the URL, what if the application in question is a Swing GUI thats not connected to the internet.

Its for all those situations that I have looked at wrapping up the excellent http://www.jfree.org/jfreechart API in a Groovy builder. My proposed name is FreeChartBuilder.

FreeChartBuilder is an early look at how a builder object using the state machine pattern could provide fairly complete support for the underlying Java API. The currently attached source archive gives an example of what can be achieved for pie and simple line charts.

Here’s an example of building a pie chart using the prototype builder:

import com.thecoderscorner.gfreechart.GFreeChartBuilder
import javax.swing.ImageIcon
import java.awt.Color
import groovy.swing.SwingBuilder
import javax.swing.JFrame
import java.awt.BorderLayout

def chart = new GFreeChartBuilder();

chart.createPie(title: "Pie Chart", legend: true, size: [400,200]) {
    dataset {
        First(20)
        Second(30)
        Third(10)
        Fourth(40)
    }
    antiAlias true
    backgroundPaint Color.WHITE

    plot {
        sectionOutlinesVisible true
        font name: 'arial', height: 15
        labelGap 0.02
        simpleGradient start: new Color(0,0,255), end: new Color(255,255,255)
    }

}
ImageIcon pieImg = chart.getIconImage()

def sb = new SwingBuilder()
def fr = sb.frame( title : 'Test Graph', size:[800,600], defaultCloseOperation: JFrame.EXIT_ON_CLOSE) {
    label(icon: pieImg, constraints: BorderLayout.CENTER)
}

fr.pack();
fr.show();

And the above code created:


generated pie chart

 

Right now, the prototype source is available from this location. Be aware that its not ready for prime time yet.

 

 

Running grails on a virtual server with limited resources

I’ve been running some tests for the past few days of how Grails performs under load on a limited memory server - actually a virtual server. Running tomcat with a max heap of 128M (-Xmx128M) and requested lots of pages using 10 simulated clients. I was pleasantly surpised, grails performed well under these conditions.

You can see more about this at http://grails.org/Deployment
and http://grails.org/Grails+Test+On+Virtual+Server

Progress with GroovyChart - the JFreeChart builder for Groovy

Its been several weeks since I last discussed wrapping JFreeChart in Groovy. Although I've been quiet - there is some good news. I have taken the java.net project groovychart and started to develop on top of it. Although this implementation is currently in Java; which means its more complex than it may have been in Groovy because of Java's strong typing, it is very comprehensive in its coverage.

Yesterday, I sent a mail to the community manager at java.net, asking for permission to work on the project. This is really needed as without this I would have to take a cut of whats there and move it to another host - I dont really want to do that.

Since I last blogged, I have got hold of the source from java.net, done some major refactoring of the way builders are stacked, then got it all working again. I also added pie chart support (my favourite chart).

 

Here's an example:

import net.java.dev.groovychart.chart.ChartBuilder
import groovy.swing.SwingBuilder
import java.awt.BorderLayout as BL
import javax.swing.JFrame
import java.awt.Color
import java.awt.Dimension
import org.jfree.chart.ChartPanel

ChartBuilder cb = new ChartBuilder();
def pieChart = cb.piechart3d(title: "Simple Pie Chart") {
    defaultPieDataset {
        Series1(40.0f)
        Series2(30.0f)
        Series3(30.0f)
    }
    antiAlias = true
    backgroundPaint(Color.WHITE)
}

def sb = new SwingBuilder()
def fr = sb.frame( title : 'Simple Pie Chart', size:[600, 400],
                   defaultCloseOperation: JFrame.EXIT_ON_CLOSE) {
    widget(new ChartPanel(pieChart.chart), constraints: BL.CENTER)
}
fr.pack();
fr.show();

 

And this code produced:

first attempt from groovychart

Ignore the "defaultPieDataset" in the chart, a fix for this is in progress.

 

Getting the source

Hopefully, I will get a reply from java.net soon, once I do I will commit my changes for inspection / download. However, if this does not happen within the next day or so, I will make the changes available as a zip on this website.

One week on - progress with JFreeChart groovy builder

Well one week has gone by since I last reported back on my progress. I have to admit I have not got as far as I wanted, but some progress is better than none.

I've downloaded and looked over the groovychart package from dev.java.net. It turns out that this builder is written in Java, and probably not possible to fit it into FactoryBuilderSupport (FSB), although this code will come in handy for analysis, I think a clean implementation on FSB will be much better.

Following on from this, I've started to move my previous prototype over to FSB. This work is moving along now, and hopefully by the time I report back again I should have another zip of source and samples available.

Dave.

Have you tried your site without CSS and Javascript?

Have you ever tried loading up your site without CSS and Javascript? If not give it a try, in firefox its as easy as:

Turn off CSS from the menu. (View -> Page Style -> No Style )

Turn off Javascript. Firefox preferences -> Content tab -> Enable Java script by unticking the checkbox.

Personally, I try to build sites from day one so that they work in some way (maybe somewhat limited) without CSS and Javascript enabled.

If you've not tried this before, it may give you a bit of a surprise.

Why do I write this?

Sometimes I run the Firefox NoScript plugin that allows me to decide which sites can use Javascript. Many times when I first hit pages without Javascript enabled they do not render properly until javascript is enabled. Also, its worth making sure that your site looks good to users who use an alternate style sheet. Try turning off CSS on this page and you'll see that it is still quite readable.

Not only is optimising for users with alternate style sheets good for accessibility, its also good for search engines as the content you want them to index is closer to the top. Just imagine if you could not use the standard css sheet for some reason, and you had to scroll down the page just to see the content.

Here's a great site I refer to quite a lot about building a standards compliant site.

If you are using Grails or site mesh directly, then many of the things discussed here are made easier. Mainly because site mesh introduces the idea of outer templates and once these templates render correctly, its much easier to make sure that the rest of the pages render properly.

Are we prone to over optimising code?

As a developer I often feel the need to performance tune code, or write "the best piece of code ever". Sometimes this happens even before I know which sections of the code will be executed frequently? These days I make every attempt to hold back from this approach.

In applications where performance is critical I usually write it the most natural way and apply any optimisations required later. Usually in such systems I am more worried about garbage collection / not getting a cache hit for memory, than outright CPU performance. Generally speaking these days CPU time is cheap - not that I am condoning lax programming techniques.

Disk is many times slower than memory, and memory is many times slower than the cache which feeds the CPU. If you incur a memory barrier (from synchronization etc) or cache miss, that costs time while the memory is accessed. This I believe is one of todays hard to solve performance problems, but again only when in critical code sections.

I was reading a great blog article earlier today about the use of StringBuilder, which provided very valid points, to which I provided some extra points about performance of the said class. However, I would hazard a guess that in many applications the optimizations discussed would make little difference, and even in the ones that did, only a small percentage of the uses would need such treatment.

To back up this view, the hotspot JVM is based on the same pattern, only a small percentage of the code needs to be optimal, and each piece of code must show its worth to the JVM by being executed many times, before being compiled.

So my beliefs in short are to avoid over optimisation, checking the application in question actually needs to be optimised, and more importantly knowing what constitutes acceptable performance for the application in question before starting. Without knowing the acceptable limits, its hard to break out of the optimisation cycle.

Jumping through hoops to test with IE6 on Vista

In building this site, I had to make it work on IE6, which to some extent is an ongoing process. Im fairly sure there will be another page somewhere that does not render properly.

I have to admit to being a firefox user on both my Vista development box, and Linux workstation, I believe firefox is better at complying with standards, but that doesnt help you when you’re trying to make a page display properly for all.

Depending on who you believe IE6 still accounts for between 60% and 80% of all browsers. Thats too large a number to ignore. What makes things worse, is that if you have Vista as I do there’s only one real option for IE6 testing, and thats to install a Virtual PC - XP installation.

Microsoft do provide a time limited IE6 VirtualPC disk pre installed with XP and IE6. This makes testing with IE6 a possibility for Vista user. You can download this XP setup from microsoft at: this location

 

The importance of good data security

As anyone living in the UK will know, data security is a big issue, with a recent breach of very personal data for a large percentage of the population. I think the problem is that data security costs money to implement and needs very skilled staff, moreover is often hard to implement it properly.

Its because the issues involved are so complex, and on top of that most people find it hard to understand the criminal mind, that it takes a problem for people to sit up and listen.

Security is costly to implement properly and can put serious restrictions on what people can do with the system, and needs proper analysis before any changes are made to a system. At least in this latest incident we got to find out about it because it was linked to a government department. I guess in some other circumstances it would have gone un-noticed.

Archive

Consulting