­
:::: MENU ::::

Bar charts with JFreeSVG

Introduction

JFreeSVG is a tool that allows us to easily generate bar charts (among other chart types) and to export the result to a SVG file.
It depends of the JFreeChart project, which allows to create different chart types for different Java applications. More information at http://www.jfree.org/jfreechart/.

Installation

This is a packaged tool, so we can manage it as a dependency of Maven. After creating a basic Maven project we only need to add the next block in our pom.xml:

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupid>org.jfree</groupid>
<artifactid>jfreechart</artifactid>
<version>1.5.0</version>
</dependency>
 
<dependency>
<groupid>org.jfree</groupid>
<artifactid>jfreesvg</artifactid>
<version>3.3</version>
</dependency>

Basic usage

After launching Maven Update to download the dependencies, we create a Main.java file with a single main method.

Then, we create two methods: the first will create a data set, and the other will configure the rendering options. from the main methog we will call there methods in order to execute the image rendering process.

Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.luisgomezcaballero.jfreesvgdemo;
 
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.StatisticalBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;
import org.jfree.graphics2d.svg.SVGGraphics2D;
import org.jfree.graphics2d.svg.SVGUtils;
 
public class Main {
 
private static CategoryDataset createDataset() {
DefaultStatisticalCategoryDataset dataset = new DefaultStatisticalCategoryDataset();
dataset.add(30.0, null, "Row Key 1", "Column Key 1");
dataset.add(50.0, null, "Row Key 2", "Column Key 2");
dataset.add(40.0, null, "Row Key 3", "Column Key 3");
dataset.add(80.0, null, "Row Key 4", "Column Key 4");
dataset.add(60.0, null, "Row Key 5", "Column Key 5");
return dataset;
}
 
private static JFreeChart createChart(CategoryDataset dataset) {
JFreeChart chart = ChartFactory.createLineChart("Title", "Category Axis Label",
"Value Axis Label", dataset);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
StatisticalBarRenderer renderer = new StatisticalBarRenderer();
plot.setRenderer(renderer);
ChartUtils.applyCurrentTheme(chart);
renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setDefaultItemLabelsVisible(true);
 
return chart;
}
 
public static void main(String[] args) {
JFreeChart chart = createChart(createDataset());
SVGGraphics2D g2 = new SVGGraphics2D(800, 600);
Rectangle r = new Rectangle(0, 0, 800, 600);
chart.draw(g2, r);
File f = new File("Result.svg");
try {
SVGUtils.writeToSVG(f, g2.getSVGElement());
} catch (IOException e) {
e.printStackTrace();
}
}
 
}

Resulto

Right click on the project and then click on Java Run As/Java Application.

This will generate a .SVG file on the root folder of our project, that will change depending of the data and configuration we specify.

It’s important to say that this is a very basic example. The possibilities of this tool are huge.

Repository

The code of this example project is available at the next repository: https://github.com/luisgomezcaballero/jfreesvg-demo.


So, what do you think ?