I'm using JFreechart to plot a graph inside a report generated with JasperReports
(actually I'm using DynamicReports which uses JasperReports).
I'm plotting a barchart
and I want to have custom strings on the labels on the axis. A picture will explain everything better.
Now I've got this:
I want to obtain this:
Some suggestions?
Here is the solution to your problem. You need to provide a new Axis with text for each value
String[] grade = new String[6];
grade[0] = "String0";
grade[1] = "String1";
grade[2] = "String2";
grade[3] = "String3";
grade[4] = "String4";
grade[5] = "";
SymbolAxis rangeAxis = new SymbolAxis("", grade);
rangeAxis.setTickUnit(new NumberTickUnit(1));
rangeAxis.setRange(0,5);
plot.setRangeAxis(rangeAxis);
here is the code in full
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Paint;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.SymbolAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class BarChartDemo7 extends ApplicationFrame {
static class CustomRenderer extends BarRenderer {
private final Paint[] colors1;
private final Paint[] colors2;
public CustomRenderer(Paint[] colors1,Paint[] colors2) {
this.colors1 = colors1;
this.colors2 = colors2;
}
@Override
public Paint getItemPaint(int series, int category) {
if (category == 4)
return this.colors2[series % this.colors1.length];
else
return this.colors1[series % this.colors1.length];
}
}
public BarChartDemo7(String title) {
super(title);
CategoryDataset dataSet=createDataset();
JFreeChart chart=createChart(dataSet);
ChartPanel panel=new ChartPanel(chart);
panel.setPreferredSize(new Dimension(800,800));
setContentPane(panel);
}
private static CategoryDataset createDataset() {
// row keys...
final String series1 = "ABC";
final String series2 = "XYZ";
// column keys...
final String category1 = "A";
final String category2 = "B";
final String category3 = "C";
final String category4 = "D";
final String category5 = "E";
final String category6 = "F";
// create the dataset...
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(0, series1, category1);
dataset.addValue(1, series1, category2);
dataset.addValue(4, series1, category3);
dataset.addValue(3, series1, category4);
dataset.addValue(2, series1, category5);
dataset.addValue(4, series1, category6);
dataset.addValue(4, series2, category1);
dataset.addValue(0, series2, category2);
dataset.addValue(0, series2, category3);
dataset.addValue(4, series2, category4);
dataset.addValue(3, series2, category1);
dataset.addValue(2, series2, category2);
dataset.addValue(1, series2, category5);
dataset.addValue(4, series2, category4);
return dataset;
}
private static JFreeChart createChart(CategoryDataset dataset) {
// create the chart...
JFreeChart chart = ChartFactory.createBarChart(
"ABC",
"",
"",
dataset,
PlotOrientation.HORIZONTAL,
true,
true,
false
);
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.WHITE);
plot.setDomainGridlinePaint(Color.BLACK);
plot.setRangeGridlinePaint(Color.BLACK);
String[] grade = new String[6];
grade[0] = "String0";
grade[1] = "String1";
grade[2] = "String2";
grade[3] = "String3";
grade[4] = "String4";
grade[5] = "";
SymbolAxis rangeAxis = new SymbolAxis("", grade);
rangeAxis.setTickUnit(new NumberTickUnit(1));
rangeAxis.setRange(0,5);
plot.setRangeAxis(rangeAxis);
Color[] color1 = new Color[]{Color.red, Color.green,Color.blue};
Color[] color2 = new Color[]{Color.gray,Color.gray.darker()};
plot.setRenderer(new CustomRenderer(color1,color2));
return chart;
}
public static void main(String[] args) {
BarChartDemo7 demo = new BarChartDemo7("ABC");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}