Create stylish charts in Java eg with JFreeChart
Asked Answered
L

5

8

What's the best way to create great looking charts in Java? It looks like the main option for charting is JFreeChart, but unfortunately by default they come out looking quite plain.

Compare a sample of JFreeChart: http://www.jfree.org/jfreechart/images/PriceVolumeDemo1.png with one of the Javascript charting libraries, eg http://www.highcharts.com/demo/spline-symbols/grid or http://people.iola.dk/olau/flot/examples/graph-types.html

The javascript ones look nicer - they have smooth lines, nice font by default, and just overall look good compared to JFreeChart which looks very plain.

Is there a charting library built on top of JFreeChart that looks good by default, or maybe some sample code to make a normal JFreeChart chart (eg line chart) look great?

Luting answered 25/11, 2011 at 4:30 Comment(0)
R
3

http://www.jfree.org/jfreechart/samples.html

There you can find many samples (you need to download a JFreeChart Demo (web start)). After some work with jFreeChart, I was thinking about moving to EasyChart (follow: http://www.objectplanet.com/easycharts/examples.html), but it looks very similar to jFreeChart actually. JFreeChart is quite easy to write, that I don't know about EasyChart.

But according to your question, there is no problem to change a Font, LineRenderer or anything from desing in JFreeChart, so you can modify it to look exactly like the one you've posted from JavaScript.

Resurrection answered 25/11, 2011 at 4:57 Comment(2)
I assumed there was a way to change most of the style like the font and line rendering - I guess I was hoping for some sample code of really great looking charts to save me from having to play around with it too much. The showcase charts in the web start demo of JFreeChart are a good start though, they ought to use screenshots of those on their sample page.Luting
See also this answer, as well as org.jfree.chart.annotations.Catchpenny
A
14

I had the same issue.

This code makes JFreeChart look like Highcharts (currently only barcharts are supported). It could easily be made more efficient :)

    String fontName = "Lucida Sans";
    JFreeChart chart = ChartFactory.createBarChart(null, "", "", dataset, PlotOrientation.VERTICAL, false, true, false );

    StandardChartTheme theme = (StandardChartTheme)org.jfree.chart.StandardChartTheme.createJFreeTheme();

    theme.setTitlePaint( Color.decode( "#4572a7" ) );
    theme.setExtraLargeFont( new Font(fontName,Font.PLAIN, 16) ); //title
    theme.setLargeFont( new Font(fontName,Font.BOLD, 15)); //axis-title
    theme.setRegularFont( new Font(fontName,Font.PLAIN, 11));
    theme.setRangeGridlinePaint( Color.decode("#C0C0C0"));
    theme.setPlotBackgroundPaint( Color.white );
    theme.setChartBackgroundPaint( Color.white );
    theme.setGridBandPaint( Color.red );
    theme.setAxisOffset( new RectangleInsets(0,0,0,0) );
    theme.setBarPainter(new StandardBarPainter());
    theme.setAxisLabelPaint( Color.decode("#666666")  );
    theme.apply( chart );
    chart.getCategoryPlot().setOutlineVisible( false );
    chart.getCategoryPlot().getRangeAxis().setAxisLineVisible( false );
    chart.getCategoryPlot().getRangeAxis().setTickMarksVisible( false );
    chart.getCategoryPlot().setRangeGridlineStroke( new BasicStroke() );
    chart.getCategoryPlot().getRangeAxis().setTickLabelPaint( Color.decode("#666666") );
    chart.getCategoryPlot().getDomainAxis().setTickLabelPaint( Color.decode("#666666") );
    chart.setTextAntiAlias( true );
    chart.setAntiAlias( true );
    chart.getCategoryPlot().getRenderer().setSeriesPaint( 0, Color.decode( "#4572a7" ));
    BarRenderer rend = (BarRenderer) chart.getCategoryPlot().getRenderer();
    rend.setShadowVisible( true );
    rend.setShadowXOffset( 2 );
    rend.setShadowYOffset( 0 );
    rend.setShadowPaint( Color.decode( "#C0C0C0"));
    rend.setMaximumBarWidth( 0.1);

enter image description here

Albemarle answered 12/11, 2012 at 14:47 Comment(0)
D
9

Try out XChart. XChart is a light-weight Java library for plotting data that would be a potential alternative to JFreeChart. Its focus is on simplicity and doesn't have every capability that JFreeChart has, but it offers a rich set of charting features including Themes to apply different "skins" to a chart. You can easily create your own Theme by implementing an interface and applying it to a Chart by calling chart.setTheme(myTheme). The jar is only ~86 KB as of the 2.0.0 release, and it has no dependencies. It's licensed under Apache 2.0 and is hosted on Github. Some screenshots can be found here. Disclaimer: I'm the lead devloper of the project.

enter image description here

Dicot answered 3/2, 2013 at 18:46 Comment(1)
Really impressed about xchart. Highly recommend to everyone who need simple graph tool. Thank you for your work!Nevlin
R
3

http://www.jfree.org/jfreechart/samples.html

There you can find many samples (you need to download a JFreeChart Demo (web start)). After some work with jFreeChart, I was thinking about moving to EasyChart (follow: http://www.objectplanet.com/easycharts/examples.html), but it looks very similar to jFreeChart actually. JFreeChart is quite easy to write, that I don't know about EasyChart.

But according to your question, there is no problem to change a Font, LineRenderer or anything from desing in JFreeChart, so you can modify it to look exactly like the one you've posted from JavaScript.

Resurrection answered 25/11, 2011 at 4:57 Comment(2)
I assumed there was a way to change most of the style like the font and line rendering - I guess I was hoping for some sample code of really great looking charts to save me from having to play around with it too much. The showcase charts in the web start demo of JFreeChart are a good start though, they ought to use screenshots of those on their sample page.Luting
See also this answer, as well as org.jfree.chart.annotations.Catchpenny
L
2

http://javafx.com/about-javafx/

Take a look at JavaFX 2.0

Lovett answered 25/11, 2011 at 5:28 Comment(0)
E
0

The above code works fine if you will edit a little, but without BarRenderer. The edited code also works on pie charts & line charts.

StandardChartTheme theme = new StandardChartTheme(ch.toString());

theme.setTitlePaint( Color.decode( "#4572a7" ) );
theme.setRangeGridlinePaint( Color.decode("#C0C0C0"));
theme.setPlotBackgroundPaint( Color.BLACK );
theme.setChartBackgroundPaint( Color.BLACK );
theme.setGridBandPaint( Color.red );
theme.setAxisOffset( new RectangleInsets(0,0,0,0) );
theme.setBarPainter(new StandardBarPainter());
theme.setAxisLabelPaint( Color.decode("#666666")  );
theme.apply( ch );
ch.getCategoryPlot().setOutlineVisible( false );
ch.getCategoryPlot().getRangeAxis().setAxisLineVisible( false );
ch.getCategoryPlot().getRangeAxis().setTickMarksVisible( false );
ch.getCategoryPlot().setRangeGridlineStroke( new BasicStroke() );
ch.getCategoryPlot().getRangeAxis().setTickLabelPaint( Color.decode("#666666") );
ch.getCategoryPlot().getDomainAxis().setTickLabelPaint( Color.decode("#666666") );
ch.setTextAntiAlias( true );
ch.setAntiAlias( true );
ch.getCategoryPlot().getRenderer().setSeriesPaint( 0, Color.decode( "#4572a7" ));

// Note- The ch is a chart variable.
Embed answered 13/9, 2021 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.