i'm trying to add a logarithmic scaled axis to a StackedBarChart. I already found a way, using the class LogAxis and setting an instance as rangeaxis to the plot.
The Problem: If the Axis is added, the first Color isn't rendered anymore (usually the red bar). Maybe the problem is already known? Here's the code:
Code: Select all
package UI;
import java.awt.Font;
import java.awt.Paint;
import java.text.NumberFormat;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.LogAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class JFreePanel extends JFrame {
private static final long serialVersionUID = 1L;
ChartPanel chartPanel;
public JFreePanel(boolean logScaled) {
super("");
super.setDefaultCloseOperation(EXIT_ON_CLOSE);
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1, "A", "X");
dataset.addValue(2, "B", "X");
dataset.addValue(3, "C", "X");
dataset.addValue(4, "D", "X");
dataset.addValue(5, "A", "Y");
dataset.addValue(6, "B", "Y");
dataset.addValue(7, "C", "Y");
dataset.addValue(8, "D", "Y");
dataset.addValue(9, "A", "Z");
dataset.addValue(10, "B", "Z");
dataset.addValue(11, "C", "Z");
dataset.addValue(12, "D", "Z");
JFreeChart chart = ChartFactory.createStackedBarChart("", "", "",
dataset, PlotOrientation.VERTICAL, true, true, false);
if (logScaled) {
scaleRangeAxisLogarithmic(chart);
}
chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(400, 300));
getContentPane().add(chartPanel);
pack();
setVisible(true);
}
private void scaleRangeAxisLogarithmic(JFreeChart chart) {
CategoryPlot plot = (CategoryPlot) chart.getPlot();
// Accessing current axis format
Font font = plot.getDomainAxis().getTickLabelFont();
Paint paint = plot.getDomainAxis().getTickLabelPaint();
// Creating new Axis
LogAxis logaxis = new LogAxis();
logaxis.setBase(10);
logaxis.setSmallestValue(1);
logaxis.setTickLabelFont(font);
logaxis.setTickLabelPaint(paint);
logaxis.setNumberFormatOverride(NumberFormat.getInstance());
// Adding new Axis to plot
plot.setRangeAxis(logaxis);
}
public static void main(String[] args) {
new JFreePanel(true);
new JFreePanel(false);
}
}
Thank you very much in advance for your answers.