Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create stylish charts in Java eg with JFreeChart

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?

like image 757
user1043466 Avatar asked Nov 25 '11 04:11

user1043466


People also ask

How do you create a pie chart in Java?

To create a pie chart in your JavaFX application, at a minimum, you must instantiate the PieChart class, define the data, assign the data items to the PieChart object, and add the chart to the application. When creating the chart data, define as many PieChart. Data objects for as many slices you want to appear.

How do you create a line graph in Java?

To create a line chart, at a minimum, you must define two axes, create the LineChart object by instantiating the LineChart class, create one or more series of data by using the XYChart. Series class, and assign the data to the chart.


2 Answers

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

like image 133
Rob Audenaerde Avatar answered Sep 27 '22 16:09

Rob Audenaerde


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

like image 29
herrtim Avatar answered Sep 27 '22 17:09

herrtim