private void drawPie(Chart chart, List<GraphicDataset> data) {
PieChart elements = new PieChart();
elements.setGradientFill(true);
elements.setTooltip("#val# / #total#<br> #percent#");
List<String> lstColor = new ArrayList<String>();
int sliceIndex = 0;
for (GraphicDataset ds : data) {
elements.addSlice(ds.getValue(), ds.getCategoryName());
lstColor.add(colors[sliceIndex++]);
}
elements.setColours(lstColor);
chart.addElements(elements);
}
private void drawLine(Chart chart, Map<String, List<GraphicDataset>> dataMap) {
double max = 0;
int barIndex = 0;
java.util.Iterator<String> keyIter = dataMap.keySet().iterator();
String seriesName;
List<GraphicDataset> lstData = null;
while (keyIter.hasNext()) {
seriesName = keyIter.next();
LineChart elements = new LineChart();
elements.setGradientFill(true);
elements.setText(seriesName);
lstData = dataMap.get(seriesName);
for (GraphicDataset ds : lstData) {
LineChart.Dot dot = new LineChart.Dot(ds.getValue(), colors[barIndex]);
elements.addDots(dot);
max = Math.max(max, ds.getValue().doubleValue());
}
elements.setColour(colors[barIndex++]);
chart.addElements(elements);
}
if (lstData != null) {
XAxis xas = new XAxis();
for (GraphicDataset ds : lstData) {
xas.addLabels(ds.getCategoryName());
}
chart.setXAxis(xas);
}
YAxis y = new YAxis();
double step = max / 10;
y.setSteps(step);
y.setMax(max + step);
chart.setYAxis(y);
}
private void drawBar(Chart chart, Map<String, List<GraphicDataset>> dataMap) {
double max = 0;
int barIndex = 0;
java.util.Iterator<String> keyIter = dataMap.keySet().iterator();
String seriesName;
List<GraphicDataset> lstData = null;
while (keyIter.hasNext()) {
seriesName = keyIter.next();
BarChart elements = new BarChart(Style.GLASS);// BarChart(Style.THREED);
elements.setGradientFill(true);
elements.setText(seriesName);
lstData = dataMap.get(seriesName);
for (GraphicDataset ds : lstData) {
BarChart.Bar bar = new BarChart.Bar(ds.getValue(), colors[barIndex]);
elements.addBars(bar);
max = Math.max(max, ds.getValue().doubleValue());
}
elements.setColour(colors[barIndex++]);
chart.addElements(elements);
}
if (lstData != null) {
XAxis xas = new XAxis();
for (GraphicDataset ds : lstData) {
xas.addLabels(ds.getCategoryName());
}
chart.setXAxis(xas);
}
YAxis y = new YAxis();
double step = max / 10;
y.setSteps(step);
y.setMax(max + step);
chart.setYAxis(y);
}
private void drawHorizontalBar(Chart chart, Map<String, List<GraphicDataset>> dataMap) {
double max = 0;
int barIndex = 0;
java.util.Iterator<String> keyIter = dataMap.keySet().iterator();
String seriesName;
List<GraphicDataset> lstData = null;
while (keyIter.hasNext()) {
seriesName = keyIter.next();
HorizontalBarChart elements = new HorizontalBarChart();
elements.setText(seriesName);
elements.setGradientFill(true);
lstData = dataMap.get(seriesName);
for (GraphicDataset ds : lstData) {
// new HorizontalBarChart.Bar(ds.getValue())
elements.addBar(0, ds.getValue());
max = Math.max(max, ds.getValue().doubleValue());
}
elements.setColour(colors[barIndex++]);
chart.addElements(elements);
break;
}
if (lstData != null) {
YAxis yas = new YAxis();
for (GraphicDataset ds : lstData) {
yas.addLabels(ds.getCategoryName());
}
yas.setOffset(true);
chart.setYAxis(yas);
}
XAxis xas = new XAxis();
double step = max / 10;
xas.setSteps(step);
xas.setMax(max + step);
chart.setXAxis(xas);
System.out.println(chart.toDebugString());
}
private void drawStackBar(Chart chart, Map<String, List<GraphicDataset>> dataMap) {
double max = 0, tempNum = 0;
int barIndex = 0;
// 将Map的key由seriesName变为categoryName
java.util.Iterator<String> keyIter = dataMap.keySet().iterator();
List<String> keyNames = new ArrayList<String>();
String seriesName, categoryName;
Map<String, List<GraphicDataset>> tempMap =
new LinkedHashMap<String, List<GraphicDataset>>();
List<GraphicDataset> lstData = null;
while (keyIter.hasNext()) {
seriesName = keyIter.next();
keyNames.add(seriesName);
lstData = dataMap.get(seriesName);
for (GraphicDataset ds : lstData) {
categoryName = ds.getCategoryName();
if (!tempMap.containsKey(categoryName)) {
tempMap.put(categoryName, new ArrayList<GraphicDataset>());
}
tempMap.get(categoryName).add(ds);
}
}
StackedBarChart elements = new StackedBarChart();
elements.setGradientFill(true);
chart.addElements(elements);
for (String keyName : keyNames) {
StackedBarChart.Key key = new StackedBarChart.Key(colors[barIndex++], keyName, 12);
elements.addKeys(key);
}
XAxis xas = new XAxis();
keyIter = tempMap.keySet().iterator();
while (keyIter.hasNext()) {
categoryName = keyIter.next();
xas.addLabels(categoryName);
StackedBarChart.Stack stack = new StackedBarChart.Stack(); // 一个堆
elements.addStack(stack);
lstData = tempMap.get(categoryName);
tempNum = 0;
barIndex = 0;
for (GraphicDataset ds : lstData) {
StackedBarChart.StackValue stackValue =
new StackedBarChart.StackValue(ds.getValue(), colors[barIndex++]);
stack.addStackValues(stackValue);
tempNum += ds.getValue().doubleValue();
}
max = Math.max(max, tempNum);
}
chart.setXAxis(xas);
YAxis y = new YAxis();
double step = max / 10;
y.setSteps(step);
y.setMax(max + step);
chart.setYAxis(y);
tempMap = null;
lstData = null;
}