Width of item in second series too wide

后端 未结 1 1071
青春惊慌失措
青春惊慌失措 2020-12-21 13:49

I am trying to show 2 OHLCSeries in one chart/seriesCollection. The first series seems to be OK but in the secord there are item(s) with weird width. Look the pic:

<

相关标签:
1条回答
  • 2020-12-21 14:21

    You may want to experiment with setAutoWidthMethod() and WIDTHMETHOD_SMALLEST. Two items appear to be a minimum; five are shown below in series "Two".

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

    enter image description here

    In addition,

    • Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

    • Don't use setPreferredSize() when you really mean to override getPreferredSize(), as discussed here.

    • As noted here, DateTickUnit is deprecated; use DateTickUnitType instead.

    As tested:

    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);
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题