Crosshair: print a string on mouse over

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
isochor
Posts: 3
Joined: Sun Apr 09, 2017 3:48 pm
antibot: No, of course not.

Crosshair: print a string on mouse over

Post by isochor » Sun Apr 09, 2017 4:00 pm

Hello,

I would like print a stock price over the date(dd/mm/yyyy).

I have this piece of code and it works fine for the price (because it is a double).


Code: Select all

  @Override
  public void chartMouseClicked(ChartMouseEvent event) {
      // ignore
  }

  @Override
  public void chartMouseMoved(ChartMouseEvent event) {
      Rectangle2D dataArea = this.chartPanel.getScreenDataArea();
      JFreeChart chart = event.getChart();
      XYPlot plot = (XYPlot) chart.getPlot();

      
      String label = xCrosshair.getLabelGenerator().generateLabel(xCrosshair);
      double milliSeconds= x.java2DToValue(event.getTrigger().getX(), dataArea, RectangleEdge.BOTTOM);
    
     

      
      double y = DatasetUtilities.findYValue(plot.getDataset(), 0, (x.java2DToValue(event.getTrigger().getX(), dataArea, RectangleEdge.TOP)));
      xCrosshair.setValue(milliSeconds);
 
      //this.xCrosshair.setValue(milliSeconds);
      
      this.yCrosshair.setValue(y);
  }
However I face a problem by printing the corresponding date of each value. The code prints only the representation of the date as double (for example 1.472.685.946.445 instead of 01/09/2016)

I have another peace of code which transforms the unwanted double value into the desired String date.

Could someone please explain me, how I can print the corresponding sring date (which I get by an getDateAsString()method) in my plot and not the double value?

Thank you :)

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Crosshair: print a string on mouse over

Post by paradoxoff » Mon Apr 10, 2017 8:51 am

Write a new implementation of CrosshairLabelGenerator that is either returning the string or that is using an instance of DateFormat to format the value of the crosshair as a date.

isochor
Posts: 3
Joined: Sun Apr 09, 2017 3:48 pm
antibot: No, of course not.

Re: Crosshair: print a string on mouse over

Post by isochor » Sun May 14, 2017 11:53 am

Thank you for your help. Unfortunately have no idea how to begin with. I checked with google if other people have the same problem but even though my problem seems to be very easy to solve I don't find any examples. How can I write a new implementation of the CrosshairLabelGenerator and integrate the output to the CrosshairOverlay?

I would be gladful if you can give me more tips

Roman
Posts: 1
Joined: Thu May 18, 2017 9:28 am
antibot: No, of course not.

Re: Crosshair: print a string on mouse over

Post by Roman » Thu May 18, 2017 9:32 am

isochor wrote:Thank you for your help. Unfortunately have no idea how to begin with. I checked with google if other people have the same problem but even though my problem seems to be very easy to solve I don't find any examples. How can I write a new implementation of the CrosshairLabelGenerator and integrate the output to the CrosshairOverlay?

I would be gladful if you can give me more tips
Hi there

You can rewrite setLabelGenerator at creating chart function. Please check my example.

Code: Select all

        xCrosshair.setLabelGenerator(new CrosshairLabelGenerator () {
            @Override
            public String generateLabel(Crosshair crshr) {
                String converted = new SimpleDateFormat("HH:mm:ss").format(crshr.getValue());   
                return converted;
            }
        
        });

Regards,
Roman

isochor
Posts: 3
Joined: Sun Apr 09, 2017 3:48 pm
antibot: No, of course not.

Re: Crosshair: print a string on mouse over

Post by isochor » Sun Jun 25, 2017 7:39 pm

It worked perfect. Thank you very much :D

Locked