0

在这里,我有一个带有十字准线的条形图;当我右键单击时,我会看到一些菜单,例如“LimitBuy @ price”。我想要的是每当我单击右键单击菜单时,我希望在我单击的那个坐标处出现一个箭头。现在,每当我右键单击时,我都可以“SOPln”;这意味着我的听众工作正常,但我如何得到一个箭头和它下面的文字说“买”。我尝试使用注释,但这似乎不起作用。请帮我解决一下这个。

图片

public class MouseListenerDemo4 extends ApplicationFrame implements
        ChartMouseListener {

    private JFreeChart chart;

    private ChartPanel chartPanel;
    private JLabel priceLabel;
    private double chartX;
    private double chartY;
    public XYPlot plot1;

    public MouseListenerDemo4(String title) {
        super(title);

        priceLabel = new JLabel("");
        priceLabel.setBounds(5000, 5, 50, 20);
        String chartTitle = "Mouse Listener Demo 4";
        String file = "D:\\CL.txt";
        OHLCDataset data2 = createPriceDataset(file);
        System.out.println(data2.getItemCount(0));
        HighLowRenderer renderer1 = new HighLowRenderer();
        renderer1.setTickLength(3);
        renderer1.setBaseToolTipGenerator(new StandardXYToolTipGenerator(
                StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
        renderer1.setSeriesPaint(0, Color.blue);

        DateAxis domainAxis = new DateAxis("Date");
        NumberAxis rangeAxis = new NumberAxis("Price");
        rangeAxis.setNumberFormatOverride(new DecimalFormat("$0.00"));
        rangeAxis.setAutoRange(true);
        rangeAxis.setAutoRangeIncludesZero(false);
        plot1 = new XYPlot(data2, domainAxis, rangeAxis, renderer1);
        plot1.getRangeCrosshairPaint();
        plot1.setBackgroundPaint(Color.white);
        plot1.setDomainGridlinePaint(Color.magenta);
        plot1.setRangeGridlinePaint(Color.magenta);
        plot1.setRangePannable(true);

        plot1.setRangeCrosshairVisible(true);
        plot1.setRangeCrosshairValue(4000, true);
        plot1.setRangeCrosshairLockedOnData(true);
        plot1.setRangeCrosshairVisible(true);

        this.chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot1,
                false);
        chartPanel = new ChartPanel(this.chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(1000, 500));
        chartPanel.setMouseZoomable(true, false);
        chartPanel.addChartMouseListener(this);
        setContentPane(chartPanel);

        priceLabel.setBackground(Color.cyan);
        chartPanel.add(priceLabel);
        priceLabel.setVisible(true);
        priceLabel.setText("");


    }

    private static OHLCDataset createPriceDataset(String filename) {
        // the following data is taken from http://finance.yahoo.com/
        // for demo purposes...

        OHLCSeries s1 = new OHLCSeries(filename);

        try {
            BufferedReader in = new BufferedReader(new FileReader(filename));
            DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
            String inputLine;
            in.readLine();
            while ((inputLine = in.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(inputLine, ",");
                Date date = df.parse(st.nextToken());
                double open = Double.parseDouble(st.nextToken());
                double high = Double.parseDouble(st.nextToken());
                double low = Double.parseDouble(st.nextToken());
                double close = Double.parseDouble(st.nextToken());
                double volume = Double.parseDouble(st.nextToken());

                s1.add(new Day(date), open, high, low, close);
                // t1.add(new Day(date), close);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        OHLCSeriesCollection dataset = new OHLCSeriesCollection();
        dataset.addSeries(s1);

        return dataset;
    }

    public void chartMouseClicked(ChartMouseEvent event) {
        int mouseX = event.getTrigger().getX();
        int mouseY = event.getTrigger().getY();
        System.out.println("x = " + mouseX + ", y = " + mouseY);
        Point2D p = chartPanel
                .translateScreenToJava2D(new Point(mouseX, mouseY));
        XYPlot plot = (XYPlot) chart.getPlot();
        Rectangle2D plotArea = this.chartPanel.getChartRenderingInfo()
                .getPlotInfo().getDataArea();
        ValueAxis domainAxis = plot.getDomainAxis();
        RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();
        ValueAxis rangeAxis = plot.getRangeAxis();
        RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
         chartX = domainAxis.java2DToValue(p.getX(), plotArea,
                domainAxisEdge);
         chartY = rangeAxis.java2DToValue(p.getY(), plotArea,
                rangeAxisEdge);
        this.chartPanel.setHorizontalAxisTrace(true);
        this.chartPanel.setVerticalAxisTrace(true);
        System.out.println("Chart: x = " + chartX + ", y = " + (int) chartY);
        priceLabel.setText("" + (int) chartY);

        JPopupMenu popup = new JPopupMenu();
        JMenuItem limitbuy = new JMenuItem("Limit Buy @ " + (int) chartY);
        limitbuy.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                XYTextAnnotation textannotation = new XYTextAnnotation("Label", (int)chartX, chartY);
                XYShapeAnnotation annotation = new XYShapeAnnotation(
                        new Ellipse2D.Float(1000.0f, 120.0f, 10.0f, 10.0f),
                        new BasicStroke(5.0f), Color.blue);
                XYPointerAnnotation pointer = new XYPointerAnnotation("arrow",
                        0.5, 0.5, 0.0);
                pointer.setPaint(Color.GREEN);
                pointer.setText("mark");
                chart.getXYPlot().addAnnotation(pointer);
                chart.getXYPlot().addAnnotation(annotation);
                chart.getXYPlot().addAnnotation(textannotation);


            }
        });
        popup.add(limitbuy);
        popup.add(new JMenuItem("Limit Sell @ " + (int) chartY));
        popup.add(new JMenuItem("Stop Buy @ " + (int) chartY));
        popup.add(new JMenuItem("Stop Sell @ " + (int) chartY));
        popup.add(new JMenuItem("Market Buy @ " + (int) chartY));
        popup.add(new JMenuItem("Market Sell @ " + (int) chartY));
        chartPanel.setPopupMenu(popup);

    }

    /**
     * Receives chart mouse moved events.
     * 
     * @param event
     *            the event.
     */
    public void chartMouseMoved(ChartMouseEvent event) {
        // ignore
    }

    public static void main(String[] args) {
        MouseListenerDemo4 demo = new MouseListenerDemo4(
                "Mouse Listener Demo 4");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

}
4

0 回答 0