Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove automatic axis label stretching?

I am using JFreeChart 1.0.14. The labels of my axis stretch really strangely if the plot is too small/big. I want to turn off that behaviour and want the axis labels always printed at the same width:height ratio.

stretched

Here is an SSCCE:

import java.awt.BorderLayout;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());

        XYSeries series = new XYSeries("series a");
        for (int i = 0; i < 100; i++)
            series.add(i, Math.sin(i / 2.0) * Math.cos(i / (2.0 + Math.random())));
        XYSeriesCollection dataset = new XYSeriesCollection(series);
        JFreeChart chart = ChartFactory.createXYLineChart("", "x-axis", "y-axis", dataset, PlotOrientation.VERTICAL, false, false, false);
        ChartPanel panel = new ChartPanel(chart);

        frame.add(panel, BorderLayout.CENTER);

        frame.setSize(400, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

It produces the output marked with the OK. When I change the size of the frame in either direction to a certain amount, the axis labels (and tick labels) are starting to get distorted (as can be seen by the two frames marked with stretched). And their "stretching factor" is synced.

I can not figure out how to disable this "feature" and always display the the axis labels at a fixed width:height ratio. Do you know how to do this?

like image 670
brimborium Avatar asked Nov 28 '12 10:11

brimborium


People also ask

How do I remove axis labels in axes?

Now, let's take a look at how to remove the spines, tick labels, ticks, grid and axis labels. The easiest way to turn off everything axis-related is via a convenience function - axis ('off') on your Axes or Axes3D instance:

How to turn off the axis and spines of a graph?

You can use the get_yaxis () or get_xaxis () to get the respective axes and turn them off by using set_visible (False): This turns off the tick labels and ticks themselves, but leaves the frame (spines) on: To turn the spines off - you can access them via the ax.spines dictionary.

How do I remove a title from an axis?

(1) Select the Axis. (2) Upper right-hand corner of the Axis, a very, very small drop-down arrow will appear. Click on it and select Edit Axis: Remove the Title or Subtitle however it's arranged. Try right-clicking those names and then uncheck 'Show Header'.

How do I Turn Off the axis in a plot?

Alternatively, you can use the ax.set_axis_off () function, in conjecture with the ax.set_axis_on () function, which reverses the former's effects. This is a very useful set of functions to use when updating plots, such as when animating them, or whenever you might want to turn the axis off and on, rather than just disabling it:\


1 Answers

This is an example of scaling.

You need to set the maximum and minimum draw height and width on the ChartPanel. You can either set them once:

ChartPanel panel = new ChartPanel(chart);
panel.setMaximumDrawHeight(1000);
panel.setMaximumDrawWidth(1000);
panel.setMinimumDrawWidth(10);
panel.setMinimumDrawHeight(10);

using some suitable values or add a ComponentListener:

    frame.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            panel.setMaximumDrawHeight(e.getComponent().getHeight());
            panel.setMaximumDrawWidth(e.getComponent().getWidth());
            panel.setMinimumDrawWidth(e.getComponent().getWidth());
            panel.setMinimumDrawHeight(e.getComponent().getHeight());
        }
    });
like image 66
GrahamA Avatar answered Oct 14 '22 20:10

GrahamA