就像在下面的示例中所做的一样,我希望图表域上的刻度线标签像该图表中的一样旋转45度:
http://left.subtree.org/2007/08/14/rotate-labels-jfreechart/
区别在于,我想在具有数字轴的散点图上执行此操作。我在类NumberAxis中找不到与setCategoryLabelPositions()等效的对象。
最佳答案
方法setVerticalTickLabels()
可能是替代方法。如果没有,我只能覆盖refreshTicksHorizontal()
。另请参见此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);
}
}