第二个系列中的项目宽度太宽

时间:2014-12-21 03:16:42

标签: java jfreechart

我试图在一个图表/ seriesCollection中显示2个OHLCSeries。第一个系列似乎没问题,但是在第二个系列中有一些带有奇怪宽度的项目。看图:

enter image description here

也许问题来自系列中的项目数 - 第一个系列有20个项目,第二个系列只有1个。

以下是完整的应用代码:

import java.awt.Dimension;
import java.text.SimpleDateFormat;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.data.time.Minute;
import org.jfree.data.time.ohlc.OHLCSeries;
import org.jfree.data.time.ohlc.OHLCSeriesCollection;

public class sample8 {

    private ChartPanel chartPanel;
    private JFreeChart chart;
    private final OHLCSeriesCollection seriesCollection = new OHLCSeriesCollection();

    public sample8() {
        chart = ChartFactory.createCandlestickChart(null, "Time", "Price", seriesCollection, false);
        chart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);
        chart.setAntiAlias(false);

        // renderer
        CandlestickRenderer renderer = new CandlestickRenderer();
        chart.getXYPlot().setRenderer(renderer);

        // Y-axis
        chart.getXYPlot().setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
        NumberAxis numberAxis = (NumberAxis) chart.getXYPlot().getRangeAxis();
        numberAxis.setAutoRangeIncludesZero(false);
        numberAxis.setAutoRangeStickyZero(false);

        // X-axis
        DateAxis dateAxis = (DateAxis) chart.getXYPlot().getDomainAxis();
        dateAxis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));
        dateAxis.setTickUnit(new DateTickUnit(DateTickUnit.MINUTE, 1), true, true);
        dateAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);

        // chartPanel
        chartPanel = new ChartPanel(chart);
        chartPanel.getChartRenderingInfo().setEntityCollection(null);
        chartPanel.setPreferredSize(new Dimension(1000, 500));

        // series
        addSeries1();
        addSeries2();
    }

    private void addSeries1() {
        OHLCSeries series = new OHLCSeries("");
        series.add(new Minute(0, 12, 1, 1, 2014), 97.23D, 98.47D, 97.15D, 97.43);
        series.add(new Minute(1, 12, 1, 1, 2014), 96.47D, 98.81D, 96.27D, 97.49);
        series.add(new Minute(2, 12, 1, 1, 2014), 95.88D, 96.87D, 96.11D, 96.75);
        series.add(new Minute(3, 12, 1, 1, 2014), 96.05D, 96.94D, 95.89D, 96.59);
        series.add(new Minute(4, 12, 1, 1, 2014), 95.92D, 97.00D, 95.69D, 96.92);
        series.add(new Minute(5, 12, 1, 1, 2014), 96.34D, 96.71D, 95.68D, 96.70);
        series.add(new Minute(6, 12, 1, 1, 2014), 96.65D, 96.87D, 94.91D, 96.38);
        series.add(new Minute(7, 12, 1, 1, 2014), 97.75D, 98.20D, 96.90D, 97.24);
        series.add(new Minute(8, 12, 1, 1, 2014), 97.72D, 98.47D, 97.35D, 97.64);
        series.add(new Minute(9, 12, 1, 1, 2014), 98.30D, 98.77D, 97.56D, 97.99);
        series.add(new Minute(10, 12, 1, 1, 2014), 100.09D, 99.79D, 99.17D, 99.25);
        series.add(new Minute(11, 12, 1, 1, 2014), 100.39D, 100.24D, 99.66D, 100.13);
        series.add(new Minute(12, 12, 1, 1, 2014), 97.99D, 98.63D, 97.67D, 98.54);
        series.add(new Minute(13, 12, 1, 1, 2014), 99.33D, 99.37D, 97.96D, 98.34);
        series.add(new Minute(14, 12, 1, 1, 2014), 98.00D, 98.16D, 97.17D, 97.52);
        series.add(new Minute(15, 12, 1, 1, 2014), 98.77D, 97.92D, 97.37D, 97.70);
        series.add(new Minute(16, 12, 1, 1, 2014), 97.33D, 97.94D, 97.15D, 97.67);
        series.add(new Minute(17, 12, 1, 1, 2014), 99.59D, 98.33D, 97.89D, 98.22);
        series.add(new Minute(18, 12, 1, 1, 2014), 96.67D, 97.09D, 95.66D, 97.07);
        series.add(new Minute(19, 12, 1, 1, 2014), 96.65D, 97.58D, 96.79D, 97.00);
        seriesCollection.addSeries(series);
    }

    private void addSeries2() {
        OHLCSeries series = new OHLCSeries("");
        series.add(new Minute(0, 12, 1, 1, 2014), 96D, 96.5D, 95.5D, 96.2D);
        seriesCollection.addSeries(series);
    }

    public ChartPanel getChartPanel() {
        return chartPanel;
    }

    public static void main(String[] args) {
        sample8 app = new sample8();
        JFrame frame = new JFrame("sample8");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.add(app.getChartPanel());
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

为什么JFreeChart并没有自动设置所有项目的宽度相同?

1 个答案:

答案 0 :(得分:3)

您可能希望尝试setAutoWidthMethod()WIDTHMETHOD_SMALLEST。两项似乎是最低限度的;五个系列如下所示"两个"。

CandlestickRenderer r = (CandlestickRenderer) chart.getXYPlot().getRenderer();
r.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST);

enter image description here

此外,

  • 应在event dispatch threa d

  • 上构建和操作的Swing GUI对象。

  • 当您真正想要覆盖setPreferredSize()时,请不要使用getPreferredSize(),正如所讨论的here

  • 如上所述hereDateTickUnit已被弃用;请改用DateTickUnitType

经测试:

import java.awt.Dimension;
import java.awt.EventQueue;
import java.text.SimpleDateFormat;
import javax.swing.JFrame;
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.DateTickMarkPosition;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.DateTickUnitType;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.data.time.Minute;
import org.jfree.data.time.ohlc.OHLCSeries;
import org.jfree.data.time.ohlc.OHLCSeriesCollection;

/** @see https://stackoverflow.com/a/27611246/230513 */
public class Sample8 {

    private final ChartPanel chartPanel;
    private final OHLCSeriesCollection seriesCollection = new OHLCSeriesCollection();

    public Sample8() {
        JFreeChart chart = ChartFactory.createCandlestickChart(
                "Sample8", "Time", "Price", seriesCollection, true);
        chart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);
        CandlestickRenderer r = (CandlestickRenderer) chart.getXYPlot().getRenderer();
        r.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST);

        // Y-axis
        NumberAxis numberAxis = (NumberAxis) chart.getXYPlot().getRangeAxis();
        numberAxis.setAutoRangeIncludesZero(false);
        numberAxis.setAutoRangeStickyZero(false);

        // X-axis
        DateAxis dateAxis = (DateAxis) chart.getXYPlot().getDomainAxis();
        dateAxis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));
        dateAxis.setTickUnit(new DateTickUnit(DateTickUnitType.MINUTE, 1));
        dateAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);

        // chartPanel
        chartPanel = new ChartPanel(chart){

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(800, 400);
            }
        };

        // series
        addSeries1();
        addSeries2();
    }

    private void addSeries1() {
        OHLCSeries series = new OHLCSeries("One");
        series.add(new Minute(0, 12, 1, 1, 2014), 97.23D, 98.47D, 97.15D, 97.43);
        series.add(new Minute(1, 12, 1, 1, 2014), 96.47D, 98.81D, 96.27D, 97.49);
        series.add(new Minute(2, 12, 1, 1, 2014), 95.88D, 96.87D, 96.11D, 96.75);
        series.add(new Minute(3, 12, 1, 1, 2014), 96.05D, 96.94D, 95.89D, 96.59);
        series.add(new Minute(4, 12, 1, 1, 2014), 95.92D, 97.00D, 95.69D, 96.92);
        series.add(new Minute(5, 12, 1, 1, 2014), 96.34D, 96.71D, 95.68D, 96.70);
        series.add(new Minute(6, 12, 1, 1, 2014), 96.65D, 96.87D, 94.91D, 96.38);
        series.add(new Minute(7, 12, 1, 1, 2014), 97.75D, 98.20D, 96.90D, 97.24);
        series.add(new Minute(8, 12, 1, 1, 2014), 97.72D, 98.47D, 97.35D, 97.64);
        series.add(new Minute(9, 12, 1, 1, 2014), 98.30D, 98.77D, 97.56D, 97.99);
        seriesCollection.addSeries(series);
    }

    private void addSeries2() {
        OHLCSeries series = new OHLCSeries("Two");
        series.add(new Minute(0, 12, 1, 1, 2014), 96D, 96.5D, 95.5D, 96.2D);
        series.add(new Minute(1, 12, 1, 1, 2014), 100.39D, 100.24D, 99.66D, 100.13);
        series.add(new Minute(2, 12, 1, 1, 2014), 97.99D, 98.63D, 97.67D, 98.54);
        series.add(new Minute(3, 12, 1, 1, 2014), 99.33D, 99.37D, 97.96D, 98.34);
        series.add(new Minute(4, 12, 1, 1, 2014), 98.00D, 98.16D, 97.17D, 97.52);
//        series.add(new Minute(5, 12, 1, 1, 2014), 98.77D, 97.92D, 97.37D, 97.70);
//        series.add(new Minute(6, 12, 1, 1, 2014), 97.33D, 97.94D, 97.15D, 97.67);
//        series.add(new Minute(7, 12, 1, 1, 2014), 99.59D, 98.33D, 97.89D, 98.22);
//        series.add(new Minute(8, 12, 1, 1, 2014), 96.67D, 97.09D, 95.66D, 97.07);
//        series.add(new Minute(9, 12, 1, 1, 2014), 96.65D, 97.58D, 96.79D, 97.00);
        seriesCollection.addSeries(series);
    }

    public ChartPanel getChartPanel() {
        return chartPanel;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                Sample8 app = new Sample8();
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(app.getChartPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}