Jfree中的自定义DateAxis值

时间:2018-10-22 15:11:19

标签: java jfreechart

我正在尝试以5年为间隔指定setTickUnit,并且我在X轴上的日期应显示2018、2023、2028、2033、2038等。 但是,如果我尝试以下选项:

DateTickUnit dateTickUnit = new DateTickUnit(DateTickUnitType.YEAR, 5, new SimpleDateFormat("yyyy"));
dateAxis.setTickUnit(dateTickUnit);

我得到以下输出:

enter image description here

日期从2020年,2025年开始,依此类推。

预期的输出:(日期应从2018年开始,然后应以5为间隔)

enter image description here

有人可以帮忙吗?

代码:

import java.awt.BasicStroke;
import java.awt.Color;
import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.swing.WindowConstants;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.DateTickUnitType;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class Layer1VariabelPensioenCopy extends ApplicationFrame {
    static JFreeChart xylineChart = null;
    public Layer1VariabelPensioenCopy( String applicationTitle, String chartTitle ) {
        super(applicationTitle);
        xylineChart = ChartFactory.createTimeSeriesChart(
                chartTitle ,
                "Jaar" ,
                "Amount in euro's (€)" ,
                createDataset() ,
                true , true , false);

        ChartPanel chartPanel = new ChartPanel( xylineChart );
        chartPanel.setPreferredSize( new java.awt.Dimension( 550 , 450 ) );
        final XYPlot plot = xylineChart.getXYPlot( );

        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer( );
        renderer.setSeriesPaint( 0 , new Color(105, 45, 150) );
        renderer.setSeriesPaint( 1 , new Color(0, 105, 180) );
        renderer.setSeriesPaint( 2 , Color.GRAY );
        renderer.setSeriesStroke( 0 , new BasicStroke( 4.0f ) );
        renderer.setSeriesStroke( 1 , new BasicStroke( 3.0f ) );
        renderer.setSeriesStroke( 2 , new BasicStroke( 2.0f ) );

        plot.setRenderer( renderer );
        plot.setBackgroundPaint(Color.WHITE);
        plot.setDomainGridlinesVisible(false);
        plot.setRangeGridlinePaint(Color.DARK_GRAY);

        DateAxis dateAxis = (DateAxis) plot.getDomainAxis();
        DateTickUnit dateTickUnit = new DateTickUnit(DateTickUnitType.YEAR, 5, new SimpleDateFormat("yyyy"));
        dateAxis.setTickUnit(dateTickUnit);

        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setRange(0, 25000.00);
        rangeAxis.setTickUnit(new NumberTickUnit(5000));

        setContentPane( chartPanel ); 
    }

    private XYDataset createDataset( ) {
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        final TimeSeries goedWeer = new TimeSeries("Goed weer");
        goedWeer.add(new Day(1, 1, 2018), 7000);
        goedWeer.add(new Day(1, 1, 2023), 9216.63637661471);
        goedWeer.add(new Day(1, 1, 2028), 12510.6772682533);
        goedWeer.add(new Day(1, 1, 2033), 17010.970019862);
        goedWeer.add(new Day(1, 1, 2038), 24006.1447412814);
        dataset.addSeries( goedWeer );
        return dataset;
    }

    public static void main( String[ ] args ) throws IOException {
        Layer1VariabelPensioenCopy layer1Layer1VariabelPensioen = new Layer1VariabelPensioenCopy("Variabel Pensioen", "Variabel Pensioen");
        layer1Layer1VariabelPensioen.pack( );
        layer1Layer1VariabelPensioen.setLocationRelativeTo(null);
        RefineryUtilities.centerFrameOnScreen( layer1Layer1VariabelPensioen );
        layer1Layer1VariabelPensioen.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        layer1Layer1VariabelPensioen.setVisible( true );

    }
}

版本: jfreechart:1.5.0 jcommon:1.0.24

0 个答案:

没有答案