我正在使用JFreeChart
API在我的Java应用程序中生成一些图表。在我的一个图表中,我尝试使用LogAxis
对象通过以下代码使我的y轴成为对数刻度轴(图中的A):
LogAxis logAxis = new LogAxis("Price($)");
logAxis.setMinorTickMarksVisible(true);
logAxis.setAutoRange(true);
xyplot.setRangeAxis(logAxis);
然后我得到了一个对数刻度的y轴,刻度为10 ^ n(如图A)。我想让它像B一样,对用户来说更直观,并且每个间隔代表不同的值,如图所示,2-> 4,4-> 8,8-> 16,间隔增长为2 ^ n。不足之处在于,即使它们代表不同的值,间隔也会显示相同的宽度。但是,当O尝试通过以下代码实现此目的时:
LogAxis logAxis = new LogAxis("Price($)");
logAxis.setBase(2);
logAxis.setTickUnit(new NumberTickUnit(2));
logAxis.setMinorTickMarksVisible(true);
logAxis.setAutoRange(true);
xyplot.setRangeAxis(logAxis);
我得到的是图C。
如何实现图B?
答案 0 :(得分:5)
我认为您需要的是logAxis.setNumberFormatOverride(NumberFormat format);
编辑:由于需要进一步的帮助...试试这个:
logAxis.setBase(10);
LogFormat format = new LogFormat(logAxis.getBase(), "", "", true);
logAxis.setNumberFormatOverride(format);
这是一个可以用来玩...的整个方法:
public static void main(String[] args) {
XYSeries series = new XYSeries("");
series.add(1, 10);
series.add(2, 100);
series.add(3, 1000);
series.add(4, 10000);
series.add(5, 100000);
series.add(6, 1000000);
// NumberAxis yAxis = new NumberAxis("");
LogAxis yAxis = new LogAxis("");
yAxis.setBase(10);
LogFormat format = new LogFormat(yAxis.getBase(), "", "", true);
yAxis.setNumberFormatOverride(format);
XYPlot plot = new XYPlot(
new XYSeriesCollection(series),
new NumberAxis(""),
yAxis,
new XYLineAndShapeRenderer(true, false));
JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
JFrame frame = new JFrame("LogAxis Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new ChartPanel(chart));
frame.pack();
frame.setVisible(true);
}
答案 1 :(得分:5)
即使您使用LogAxis
,也可以指定整数刻度单位,如下面@ amaidment示例的变体所示。
/** @see http://stackoverflow.com/a/10353270/230513 */
private static void createFrame() {
XYSeries series = new XYSeries("Series");
for (int i = 0; i <= N; i++) {
series.add(i, Math.pow(2, i));
}
NumberAxis xAxis = new NumberAxis("X");
xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
LogAxis yAxis = new LogAxis("Y");
yAxis.setBase(2);
yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
XYPlot plot = new XYPlot(new XYSeriesCollection(series),
xAxis, yAxis, new XYLineAndShapeRenderer(true, false));
JFreeChart chart = new JFreeChart(
"Chart", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
JFrame frame = new JFrame("LogAxis Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new ChartPanel(chart));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
createFrame();
}
});
}
答案 2 :(得分:0)
恕我直言,你应该使用ChartFactory来创建你想要的图表类型,而不是直接创建JFreeChart构造函数。因此,这是一个比之前发布的更好的解决方案:
public class MyLogChart
{
public static void main(String[] args)
{
XYSeries series = new XYSeries("First");
series.add(1.0, 1.0);
series.add(2.0, 10.0);
series.add(3.0, 100.0);
series.add(4.0, 1000.0);
series.add(5.0, 10000.0);
series.add(6.0, 100000.0);
series.add(7.0, 1000000.0);
series.add(8.0, 10000000.0);
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series);
JFreeChart chart = ChartFactory.createXYLineChart(
"XY Chart",
"x-axis",
"y-axis",
dataset,
PlotOrientation.VERTICAL,
false,
false,
false
);
LogarithmicAxis yAxis = new LogarithmicAxis("Y");
XYPlot plot = chart.getXYPlot();
plot.setRangeAxis(yAxis);
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)plot.getRenderer();
renderer.setSeriesShapesVisible(0, true);
ChartFrame frame = new ChartFrame("My Chart", chart);
frame.pack();
frame.setVisible(true);
}
}