Problems while adding LogAxis to a StackedBarChart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
4treyu
Posts: 2
Joined: Fri Jun 01, 2012 1:00 pm
antibot: No, of course not.

Problems while adding LogAxis to a StackedBarChart

Post by 4treyu » Fri Jun 01, 2012 1:22 pm

Buenos Dias =),

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);
	}
}
Any ideas?
Thank you very much in advance for your answers.

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Problems while adding LogAxis to a StackedBarChart

Post by John Matthews » Sun Jun 03, 2012 8:18 pm

The StackedBarRenderer has a default base of zero, the logarithm of which is negative infinity. A base of one works:

Code: Select all

private void scaleRangeAxisLogarithmic(JFreeChart chart) {
    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    StackedBarRenderer r = (StackedBarRenderer) plot.getRenderer();
    r.setBase(1);
    ...
}
Image

4treyu
Posts: 2
Joined: Fri Jun 01, 2012 1:00 pm
antibot: No, of course not.

Re: Problems while adding LogAxis to a StackedBarChart

Post by 4treyu » Mon Jun 04, 2012 8:23 am

Thank you! It works fine =)

Locked