java – 如何在JFreeChart中的数字轴的域上旋转刻度线标签?

就像在下面的示例中所做的那样,我希望图表域上的刻度线标签旋转45度,如下图所示:
http://left.subtree.org/2007/08/14/rotate-labels-jfreechart/

区别在于,我想在带有数字轴的散点图上执行此操作.我找不到NumberAxis类中的setCategoryLabelPositions()的等价物.

方法 setVerticalTickLabels()可以是替代方案.如果没有,我没有看到任何选择,但覆盖refreshTicksHorizo​​ntal().另见本 example.

import java.awt.Color;
import java.awt.Dimension;
import java.util.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * @see https://stackoverflow.com/questions/7208657
 * @see https://stackoverflow.com/questions/7071057
 */
public class ScatterTickLabels extends ApplicationFrame {

    public ScatterTickLabels(String s) {
        super(s);
        final ChartPanel chartPanel = createDemoPanel();
        chartPanel.setPreferredSize(new Dimension(640, 480));
        this.add(chartPanel);
    }

    public static ChartPanel createDemoPanel() {
        JFreeChart jfreechart = ChartFactory.createScatterPlot(
            "Scatter Plot Demo", "X", "Y", samplexydataset(),
            PlotOrientation.VERTICAL, true, true, false);
        XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
        xyPlot.setDomainCrosshairVisible(true);
        xyPlot.setRangeCrosshairVisible(true);
        XYItemRenderer renderer = xyPlot.getRenderer();
        renderer.setSeriesPaint(0, Color.blue);
        NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
        domain.setVerticalTickLabels(true);
        return new ChartPanel(jfreechart);
    }

    private static XYDataset samplexydataset() {
        int cols = 20;
        int rows = 20;
        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        XYSeries series = new XYSeries("Random");
        Random rand = new Random();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                double x = rand.nextGaussian();
                double y = rand.nextGaussian();
                series.add(x, y);
            }
        }
        xySeriesCollection.addSeries(series);
        return xySeriesCollection;
    }

    public static void main(String args[]) {
        ScatterTickLabels demo = new ScatterTickLabels("Scatter Plot Demo");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
}
相关文章
相关标签/搜索