本文介绍了JFreeChart的LogAxis上未显示小刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JFreeChart绘制一些数据,并使用LogAxis作为范围轴.我很困惑为什么我似乎无法使较小的刻度线出现在轴上,但是网格线显示得很好.

I'm using JFreeChart to plot some data and using a LogAxis for the range axis. I'm stumped as to why I can't seem to get the minor ticks to show up on the axis, but the grid lines show up fine.

如果我不使用LogAxis,则可以使用setMinorTickMarksVisible()打开和关闭次刻度.下面的示例:

If I don't use the LogAxis I can turn the minor ticks on and off fine using setMinorTickMarksVisible(). Example below:

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轴效果的示例(右).

Note current x axis (left) and example of the desired x axis effect with minor ticks (right).

推荐答案

LogAxis 支持网格线的较小刻度线,如您的片段所示.相反, 允许完全自定义刻度线,如下所示.刻度线的颜色和笔划被夸大了.

LogAxis supports minor ticks for gridlines, as your fragment shows. In contrast LogarithmicAxis allows full customization of tick marks, as shown below. The color and stroke of the tick marks have been exaggerated for emphasis.

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);
        });
    }
}

这篇关于JFreeChart的LogAxis上未显示小刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 13:20