java 为什么在JFreeChart中不显示圆?

6pp0gazn  于 2023-03-16  发布在  Java
关注(0)|答案(1)|浏览(132)

我目前正在试用JFreeChart库来创建OHLC图表,我想在某个价格水平上画一条水平线,例如在10. 5,并在线上放置一个特定日期的点,例如“2021-01-02”,虽然我的代码成功地在10. 5画出了轴、棒和水平线,但是我试图添加的填充圆/点(point 2d)由于某种原因不可见(可能是因为坐标错误)。

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.geom.Ellipse2D;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYShapeAnnotation;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.data.xy.DefaultHighLowDataset;
import org.jfree.data.xy.OHLCDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;

public class OHLCChartDemo extends ApplicationFrame {

    public OHLCChartDemo(final String title) throws ParseException {
        super(title);
        final JFreeChart chart = createChart(createDataset());
        setContentPane(new ChartPanel(chart));
    }

    private OHLCDataset createDataset() throws ParseException {
        final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        final DefaultHighLowDataset dataset = new DefaultHighLowDataset("Series 1",
                new Date[] { new Date(df.parse("2021-01-01").getTime()), new Date(df.parse("2021-01-02").getTime()),
                        new Date(df.parse("2021-01-03").getTime()), new Date(df.parse("2021-01-04").getTime()) },
                new double[] { 11.0, 13.0, 9.0, 12.0 }, new double[] { 8.0, 9.0, 7.0, 8.5 },
                new double[] { 10.0, 11.0, 8.0, 10.5 }, new double[] { 9.0, 10.0, 8.5, 9.5 },
                new double[] { 11.0, 11.0, 9.0, 9.5 });
        return dataset;
    }

    private JFreeChart createChart(final XYDataset dataset) throws ParseException {
        final JFreeChart chart = ChartFactory.createCandlestickChart("OHLC Chart", "Date", "Price", (OHLCDataset) dataset, false);
        chart.setBackgroundPaint(Color.white);
        final XYPlot plot = (XYPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.white);
        plot.setDomainGridlinePaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.lightGray);
        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairVisible(true);
        final CandlestickRenderer renderer = new CandlestickRenderer();
        renderer.setDrawVolume(false);
        plot.setRenderer(renderer);
        final DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setAutoRangeIncludesZero(false);

        double price = 10.5;
        final ValueMarker marker = new ValueMarker(price);
        marker.setPaint(Color.blue);
        plot.addRangeMarker(marker);

        final Ellipse2D point2d = new Ellipse2D.Double(2.5, 10.5, 20, 20);
        XYShapeAnnotation annotation = new XYShapeAnnotation(point2d, new BasicStroke(2.0f), Color.BLACK, Color.BLUE);
        plot.addAnnotation(annotation);
        
        return chart;
    }

    public static void main(final String[] args) throws ParseException {
        final OHLCChartDemo demo = new OHLCChartDemo("OHLC Chart Demo");
        demo.pack();
        demo.setVisible(true);
    }
}

如何在标高(FEX)10.5处的水平线与日期(FEX)“2021-01-02”的交点处绘制实心圆?或者,如何从该点开始绘制水平线并延伸到图表的右端?

shyt4zoc

shyt4zoc1#

XYShapeAnnotation注意到“shape coordinates is specified in data space”(形状坐标在数据空间中指定)。对于DateAxis,这是自Java时代以来的毫秒数。例如,添加到示例中的以下 ad hoc 片段生成了如下所示的椭圆。它是半天宽,一个价格单位高。

double w = 12 * 60 * 60 * 1000;
double t = dataset.getX(0, 1).doubleValue() - w;
final Ellipse2D point2d =
    new Ellipse2D.Double(t + w / 2, 10, w, 1);

相关问题