如何在股票条形图上绘制十字准线

时间:2013-04-30 12:05:42

标签: java jfreechart

你好我想在这个barchar上绘制十字线。可以帮助我。我想让十字线打印X和y坐在鼠标指向或点击的地方。即使十字线打印X和我也很好Y坐在控制台上。这是我的代码

import java.awt.Color;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.FileReader;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import javax.swing.JPanel;
import org.jfree.chart.ChartColor;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.Marker;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.HighLowRenderer;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.ohlc.OHLCSeries;
import org.jfree.data.time.ohlc.OHLCSeriesCollection;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.OHLCDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class PriceVolumeChart2 extends ApplicationFrame {

    final static String filename    = "D:\\CL.txt";

    /**
     * Default constructor
     */
    public PriceVolumeChart2(String title) {
        super(title);
        JPanel panel = createDemoPanel();
        panel.setPreferredSize(new Dimension(1200, 800));
        setContentPane(panel);
    }

    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());


                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;
    }

    private static JFreeChart createCombinedChart() {
        OHLCDataset data1 = createPriceDataset(filename);
        System.out.println(data1.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);
        ChartColor chartColor=new ChartColor(0, 0, 0);

        XYPlot plot1 = new XYPlot(data1, domainAxis, rangeAxis, renderer1);
        plot1.getRangeCrosshairPaint();

        plot1.setRangePannable(true);

        JFreeChart chart = new JFreeChart("NSE NIFTY", JFreeChart.DEFAULT_TITLE_FONT, plot1, false);

        //ChartUtilities.applyCurrentTheme(chart);

        return chart;
    }

    // create a panel
    public static JPanel createDemoPanel() {
        JFreeChart chart = createCombinedChart();
        return new ChartPanel(chart);
    }

    public static void main(String[] args) {
        // TODO code application logic here
        PriceVolumeChart2 demo = new PriceVolumeChart2("JFreeChart");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
    // Download data from web
}

这是我的输出。我希望十字准线根据X轴和Y轴的给定值告诉我x和y坐标

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以启用跟踪功能以跟随鼠标,如图here所示。您可以添加ChartMouseListener以查看鼠标下的ChartEntity,如图here所示。

附录:鉴于这些情况,

JFreeChart chart = new JFreeChart(…);
chartPanel = new ChartPanel(chart);

这个完整的example启用了跟踪:

chartPanel.setHorizontalAxisTrace(true);
chartPanel.setVerticalAxisTrace(true);

这个完整的example添加了ChartMouseListener

chartPanel.addChartMouseListener(new ChartMouseListener() {

    @Override
    public void chartMouseClicked(ChartMouseEvent e) {
        final ChartEntity entity = e.getEntity();
        System.out.println(entity + " " + entity.getArea());
    }

    @Override
    public void chartMouseMoved(ChartMouseEvent e) {
    }
});

答案 1 :(得分:0)

嘿伙计们我找到了这个答案..谢谢你帮我出去了。下面的代码有助于绘制一个带有移动十字线的条形图,y坐标显示在JLabel上.CL.txt文件包含只有日期,开放,关闭,高,低(都是双值)。

import java.awt.Color;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.BufferedReader;
import java.io.FileReader;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;

import javax.swing.JLabel;

import org.jfree.chart.ChartColor;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.HighLowRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.ohlc.OHLCSeries;
import org.jfree.data.time.ohlc.OHLCSeriesCollection;
import org.jfree.data.xy.OHLCDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;

/**
* An example showing how to convert the mouse location to chart coordinates.
*/
public class MouseListenerDemo4 extends ApplicationFrame  
                                implements ChartMouseListener {

    private JFreeChart chart;

    private ChartPanel chartPanel;
    private JLabel priceLabel;


    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);
        ChartColor chartColor=new ChartColor(0, 0, 0);

        XYPlot 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;
    }


    /**
     * Creates a sample dataset.
     *
     * @return The dataset.
     */


    /**
     * Receives chart mouse click events.
     *
     * @param event  the event.
     */
    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();
        double chartX = domainAxis.java2DToValue(p.getX(), plotArea, 
                domainAxisEdge);
        double 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);
    }

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

/**
 * Starting point for the demonstration application.
 *
 * @param args  ignored.
 */
public static void main(String[] args) {
    MouseListenerDemo4 demo = new MouseListenerDemo4(
        "Mouse Listener Demo 4");
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);
}

}