It is obvious that a “half decent” application to generate reports has to include a tool for the creation of charts and graphs on the fly, so javaDocx could not do less than that.
In fact javaDocx is a pretty sophisticated tool to that regard and as we show below is very simple to generate a nice chart from some data that could be extracted from a database, a spreadsheet or elsewhere.
In this example we will concentrate in creating a pie chart that plots the following sample data:
Do you like “paella”?
Like paella: 50%
Don´t like paella: 10%
What the f*** is a paella? : 40%
Notice: paella is a typical Spanish rice dish.
import com.javadocx.*;
import java.util.*;
public class graphic {
public static void main(String[] args) {
try {
com.javadocx.cCreateDocx objDocx = new cCreateDocx("docx");
HashMap<String, Object> array = new HashMap<String, Object>();
HashMap<String, ArrayList> data = new HashMap<String, ArrayList>();
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(50);
data.put("like paella", al);
al = new ArrayList<Integer>();
al.add(10);
data.put("do not like paella", al);
al = new ArrayList<Integer>();
al.add(40);
data.put("what the f***…?", al);
array.put("data", data);
array.put("type", "pieChart");
array.put("title", "Do you like paella?");
array.put("showPercent", "1");
objDocx.fAddGraphic(array);
objDocx.fCreateDocx("simple_piechart", new HashMap());
} catch (Exception e) {
System.out.println("error " + e.toString());
}
}
}
That generates this pie chart.
If you prefer a 3D version of this pie chart you need the Pro version but the required code is not much different:
import com.javadocx.*;
import java.util.*;
public class graphic {
public static void main(String[] args) {
try {
com.javadocx.cCreateDocx objDocx = new cCreateDocx("docx");
HashMap<String, Object> array = new HashMap<String, Object>();
HashMap<String, ArrayList> data = new HashMap<String, ArrayList>();
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(50);
data.put("like paella", al);
al = new ArrayList<Integer>();
al.add(10);
data.put("do not like paella", al);
al = new ArrayList<Integer>();
al.add(40);
data.put("what the f***…?", al);
array.put("data", data);
array.put("type", "pie3DChart");
array.put("title", "Do you like paella?");
array.put("showPercent", "1");
objDocx.fAddGraphic(array);
objDocx.fCreateDocx("simple_piechart", new HashMap());
} catch (Exception e) {
System.out.println("error " + e.toString());
}
}
}