draw xypointerannoation using price and time from barchart

Discussion about JFreeChart related to stockmarket charts.

draw xypointerannoation using price and time from barchart

Postby maddy » Fri May 10, 2013 8:21 am

Is there a possible way to draw xypointer annotation on the specified price and time from a barchart instead of using x and y coordinates to draw the same??Right now i m able to draw xypointer on the points on which i right click but i want some already drawn xypointerannotation.Here's my code.Thanks in advance

Code: Select all
public class PriceVolumeChart extends ApplicationFrame implements
      ChartMouseListener {

   private static final long serialVersionUID = 7738515880595919749L;
   private JFreeChart chart;
   private ChartPanel chartPanel;
   private JLabel priceLabel;
   private double price;
   public CsvReading reader;
   static String filename = VolumeOneTick.filename;
   static OHLCSeries ohlcSeries = null;
   OHLCSeriesCollection dataset = null;
   TimeSeries volumeDataSeries = null;
   JPopupMenu popup;
   OHLCDataset ohldDataSet;
   ActionListener tradeActionListener;
   public double chartX;
   public double chartY;

   public PriceVolumeChart(String title) {

      super(title);
      this.filename = VolumeOneTick.filename;
      try {
         reader = new CsvReading();
      } catch (IOException e) {
         System.out.println("Exception thrown from data reader");
         e.printStackTrace();
      }
      // JPanel panel = createDemoPanel();
      JMenuBar menubar=new JMenuBar();
      JMenu menu=new JMenu("Menu");
      JMenuItem ordertable =new JMenuItem("Order Book");
      ordertable.addActionListener(new ActionListener() {
         
         @Override
         public void actionPerformed(ActionEvent arg0) {
         new ScrollableJTable();   
            
         }
      });
      menu.add(ordertable);
      menubar.add(menu);
      chartPanel = new ChartPanel(createChart());
      setJMenuBar(menubar);
      chartPanel.setLayout(new FlowLayout());
      chartPanel.setPreferredSize(new Dimension(1200, 800));
      chartPanel.setMouseZoomable(true, false);
      chartPanel.addChartMouseListener(this);
      this.chartPanel.setHorizontalAxisTrace(true);
      this.chartPanel.setVerticalAxisTrace(true);
      priceLabel = new JLabel("");
      priceLabel.setBounds(5000, 5, 50, 20);
      priceLabel.setBackground(Color.cyan);
      chartPanel.add(priceLabel);
      priceLabel.setVisible(true);
      priceLabel.setText("");

      setContentPane(chartPanel);

      tradeActionListener = new TradeActionListener();
   }

   private OHLCDataset createPriceDataset() {
      ohlcSeries = new OHLCSeries("CLF3");
      dataset = new OHLCSeriesCollection();
      dataset.addSeries(ohlcSeries);
      return dataset;
   }

   private JFreeChart createChart() {

      ohldDataSet = createPriceDataset();
      HighLowRenderer renderer1 = new HighLowRenderer();

      renderer1.setTickLength(3);
      renderer1
            .setBaseToolTipGenerator(new StandardXYToolTipGenerator(
                  StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                  new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat(
                        "0.0000")));
      renderer1.setSeriesPaint(0, Color.BLACK);
      DateAxis domainAxis = new DateAxis("Date");
      NumberAxis rangeAxis = new NumberAxis("Price");

      rangeAxis.setNumberFormatOverride(new DecimalFormat("$0.0000"));
      rangeAxis.setAutoRange(true);
      rangeAxis.setAutoRangeIncludesZero(false);

      XYPlot xyPlot = new XYPlot(ohldDataSet, domainAxis, rangeAxis,
            renderer1);
      xyPlot.getRangeCrosshairPaint();
      xyPlot.setBackgroundPaint(Color.white);
      xyPlot.setDomainGridlinePaint(Color.gray);
      xyPlot.setRangeGridlinePaint(Color.gray);
      xyPlot.setRangePannable(true);

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

      chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, xyPlot, false);

      // addMouseListener(new MousePopupListener());
      return chart;
   }

   public JPanel createDemoPanel() {
      JFreeChart chart = this.createChart();

      return new ChartPanel(chart);
   }

   @Override
   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.setPrice(chartY);
      System.out.println("Chart: x = " + chartX + ", y = " + (int) chartY);
      priceLabel.setText("" + (int) chartY);

      int listSize = ohldDataSet.getItemCount(0) - 1;

      popup = new JPopupMenu();

      JMenuItem item = null;

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

         @Override
         public void actionPerformed(ActionEvent arg0) {

            XYPointerAnnotation xypointer = new XYPointerAnnotation("",
                  chartX + 5, chartY, 0);
            XYTextAnnotation xytext = new XYTextAnnotation("LimitBuy@ "
                  + chartY, chartX, chartY - 0.001);
            xytext.setPaint(Color.ORANGE);
            xypointer.setAngle(3.14);
            xypointer.setPaint(Color.darkGray);
            xypointer.setBaseRadius(20);
            xypointer.setTipRadius(1);
            System.out.println("Base radius " + xypointer.getBaseRadius());
            System.out.println("tip radius " + xypointer.getTipRadius());
            chart.getXYPlot().addAnnotation(xytext);
            chart.getXYPlot().addAnnotation(xypointer);
            
         }
      });
      popup.add(limitbuy);
      chartPanel.setPopupMenu(popup);
   }

   @Override
   public void chartMouseMoved(ChartMouseEvent event) {

   }

   public double getPrice() {
      return price;
   }

   public void setPrice(double price) {
      this.price = price;
   }
}


and here's sample data which i m using to draw OHLC barchart

Code: Select all
date,time,ltp,volume
20121231,183000,3.6485,1
20121231,183000,3.648,1
20121231,183035,3.6485,1
20121231,183036,3.649,1
20121231,183037,3.649,1
20121231,183045,3.648,1
20121231,183055,3.648,1
20121231,183103,3.6485,1
20121231,183103,3.6485,1
20121231,183103,3.6485,1
20121231,183106,3.649,5
20121231,183106,3.649,1
20121231,183106,3.649,1
20121231,183106,3.649,1
20121231,183106,3.649,1
20121231,183106,3.649,1
20121231,183106,3.649,5
20121231,183106,3.649,1
20121231,183106,3.649,4
20121231,183107,3.648,1
maddy
 
Posts: 2
Joined: Thu May 02, 2013 1:12 pm

Return to JFreeChart - Stockmarket

Who is online

Users browsing this forum: No registered users and 4 guests