Ok , here it comes...
First, I think you'll need some more information on what I've done. The chart which I posted above is a barchart3D with multiple axes using multiple datasets. To get the bars plottet side by side, the datasets are filled with "dummy" series in the way that every xth dataset has its "real" values in the xth series, the other series in that dataset are null. The second dataset for example would look like this: series1 : nullvalues; series2:data; seriesn:nullvalues; and so on.
For our Test, i created a barchart with 2 datasets and 2 NumberAxes. The first dataset has its data in the first series (the second is the "dummy") .
The second dataset has its values in the second series, here, the first one is null;
basicly this is the same as in the MultipleAxisBarDemo
The rest should be clear by looking at the code. I tried to keep the structure of my "real world" chart, so don't blame me if something could be coded easier for this example
There are two classes. The first one ist BarChart3D, this is the whole issue.
The second one is ChartPrinter, a class that just instantiates the BarChart, and then prints it to a file named "chart.png" on C:\\temp\.
Puh, long story...
So here comes the code:
Code: Select all
import java.awt.Color;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.*;
import org.jfree.util.SortOrder;
public class BarChart3D extends JFreeChart
{
private CategoryPlot plot = null;
private DefaultCategoryDataset dataset1 = null;
private DefaultCategoryDataset dataset2 = null;
private CategoryAxis3D xAxis = null;
private NumberAxis3D yAxis1 = null;
private NumberAxis3D yAxis2 = null;
private BarRenderer3D renderer1 = null;
private BarRenderer3D renderer2 = null;
public BarChart3D(String title)
{
super(title, new CategoryPlot());
createChart();
}
/**
* creates the chart
*/
public void createChart()
{
initChart();
fillChart();
//X-Achse erzeugen
createXAxis();
//Y-Achsen erzeugen
createYAxis();
//Legende erzeugen
//createLegend();
}
/**
* creates and fills the datasets
*/
public void fillChart()
{
String[] categories = { "A", "B", "C","D","E","F",
"G", "H", "I","J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T"
};
double[] data = { 8.8, 9.7, 11.6, 5.0, 15.5,10,
19.5,12, 23.8, 25, 29.8,
35.6, 45.8, 70.7, 98.6, 100.0, 100.0, 100.0,
100.0, 100.0 };
double[] data2 = { 10.2, 11.5, 13.4, 5, 17.5,6,
21.5, 7, 25.8, 10, 31.8,
37.6, 47.8, 73.7, 100.0, 100.0, 100.0,
100.0, 100.0, 100.0 };
//create Datasets
dataset1 = new DefaultCategoryDataset();
for (int i=0; i < categories.length; i++)
{
dataset1.addValue(data[i], "series1", categories[i]);
//to get the bars plottet side by side, we add a "dummy" series
dataset1.addValue(null, "series2", categories[i]);
}
dataset2 = new DefaultCategoryDataset();
for (int i=0; i < categories.length; i++)
{
// same as above but the other way round
dataset2.addValue(null, "series1", categories[i]);
dataset2.addValue(data2[i], "series2", categories[i]);
}
}
/**
* Makes all the initial settings
*/
public void initChart()
{
//we won't need a Legend
this.removeLegend();
plot = (CategoryPlot) this.getPlot();
//------------------------- Renderer -----------------------------//
renderer1 = new BarRenderer3D();
renderer2 = new BarRenderer3D();
//-------------------------- Plot -------------------------------//
plot.setOrientation(PlotOrientation.VERTICAL);
double offset = 0.0;
plot.setAxisOffset(new RectangleInsets(offset, offset, offset, offset));
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.darkGray);
plot.setRangeGridlinePaint(Color.darkGray);
//try to get the right overlapping
plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
plot.setColumnRenderingOrder(SortOrder.ASCENDING);
plot.setRowRenderingOrder(SortOrder.ASCENDING);
}
/**
* creates the xAxis
*/
public void createXAxis()
{
xAxis = new CategoryAxis3D("xAxis");
xAxis.setMaximumCategoryLabelLines(5);
xAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90);
xAxis.setLowerMargin(0.0);
xAxis.setUpperMargin(0.0);
plot.setDomainAxis(xAxis);
}
/**
* creates the yAxis.
*/
public void createYAxis()
{
yAxis1 = new NumberAxis3D("yAxis1");
yAxis2 = new NumberAxis3D("yAxis2");
yAxis1.setAutoRange(true);
yAxis2.setAutoRange(true);
plot.setRangeAxis(0, yAxis1);
plot.setRangeAxis(1, yAxis2);
plot.setDataset(0, dataset1);
plot.setDataset(1, dataset2);
plot.mapDatasetToRangeAxis(0, 0);
plot.mapDatasetToRangeAxis(1, 1);
plot.setRenderer(0, renderer1);
plot.setRenderer(1, renderer2);
}
}
and the ChartPrinter...
Code: Select all
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import org.jfree.chart.ChartUtilities;
/**
* This class produces an image named "Chart.png".
*
* @author th
* @date 09/07/2007
*
* @version 1.0
*/
public class ChartPrinter
{
public ChartPrinter()
{
}
public static void main(String[] args)
{
File file = new File("C:\\temp\\chart.png");
FileOutputStream fos;
BarChart3D chart = new BarChart3D("Test");
chart.setBackgroundPaint(new Color(255,255,255));
chart.setBorderPaint(new Color(255,255,255,255));
try
{
fos = new FileOutputStream(file);
ChartUtilities.writeChartAsPNG(fos, chart, 600, 400);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
At the end, the result looks like this:
Thanks for reading this long post,
Tom