我正在使用JFreeChart
绘制一些数据,并使用LogAxis
作为范围轴。我很困惑为什么我似乎无法让较小的刻度显示在轴上,但是网格线显示得很好。
如果我不使用LogAxis
,则可以使用setMinorTickMarksVisible()
打开和关闭次刻度。下面的示例:
public ChartOne(){
XYSeriesCollection xysc = new XYSeriesCollection();
XYSeries x1 = new XYSeries("Series 1");
x1.add(0.5, 2);
x1.add(1, 2.2);
x1.add(2, 2.4);
x1.add(10, 2.75);
x1.add(30, 4);
x1.add(120, 7);
xysc.addSeries(x1);
XYSeries x2 = new XYSeries("Series 2");
x2.add(0.5, 6);
x2.add(1, 7);
x2.add(2, 8);
x2.add(10, 14);
x2.add(30, 18);
x2.add(120, 22);
xysc.addSeries(x2);
XYDataset xyd = xysc;
JFreeChart chart = ChartFactory.createXYLineChart(
"Data",
"Exposure",
"Percentage uptake",
xyd,
PlotOrientation.VERTICAL,
false,
true,
false
);
LogAxis xAxis = new LogAxis("exposure time");
xAxis.setBase(10);
xAxis.setTickUnit(new NumberTickUnit(1.0, NumberFormat.getInstance(Locale.ENGLISH), 9));
xAxis.setRange(0.1, 200.0);
xAxis.setMinorTickMarksVisible(true); //they don't show up
XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainAxis(xAxis);
plot.setDomainMinorGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.BLACK);
plot.setDomainMinorGridlinePaint(Color.BLACK);
plot.setBackgroundPaint(Color.WHITE);
ChartPanel chartPanel = new ChartPanel(chart);
add(chartPanel);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Chart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ChartOne(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
注意当前的x轴(左)和带有较小刻度的期望x轴效果的示例(右)。
答案 0 :(得分:1)
LogAxis
支持网格线的较小刻度线。相反,LogarithmicAxis
允许完全自定义刻度线,如下所示。刻度线的颜色和笔划被夸大了。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.BasicStroke;
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.LogarithmicAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
//** @see https://stackoverflow.com/a/54097313/230513 */
public class ChartOne {
public static ChartPanel createChart() {
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries x1 = new XYSeries("Series 1");
x1.add(0.5, 2);
x1.add(1, 2.2);
x1.add(2, 2.4);
x1.add(10, 2.75);
x1.add(30, 4);
x1.add(120, 7);
dataset.addSeries(x1);
XYSeries x2 = new XYSeries("Series 2");
x2.add(0.5, 6);
x2.add(1, 7);
x2.add(2, 8);
x2.add(10, 14);
x2.add(30, 18);
x2.add(120, 22);
dataset.addSeries(x2);
JFreeChart chart = ChartFactory.createXYLineChart(
"Data", "Exposure", "Percentage uptake", dataset,
PlotOrientation.VERTICAL, false, true, false);
LogarithmicAxis xAxis = new LogarithmicAxis("Time");
xAxis.setTickUnit(new NumberTickUnit(1.0, NumberFormat.getInstance(), 9));
xAxis.setTickMarkInsideLength(2f);
xAxis.setTickMarkOutsideLength(4f);
xAxis.setTickMarkPaint(Color.GREEN);
xAxis.setTickMarkStroke(new BasicStroke(2f));
xAxis.setMinorTickMarksVisible(true);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainAxis(xAxis);
plot.setDomainMinorGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.BLACK);
plot.setDomainMinorGridlinePaint(Color.BLACK);
plot.setBackgroundPaint(Color.WHITE);
return new ChartPanel(chart){
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 400);
}
};
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame frame = new JFrame("Chart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createChart(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}