0

I have three datasets in a chart. I want to display the x and y value with mouseover. I'm getting the values when the mouse is over a chartline. But I don't know which is dataset is displayed.

Here the code:

BatterieLeistung_resultSet = Verbinden_thread_Datenbank.DatenAbfrage3();
TimeSeriesCollection TSC = new TimeSeriesCollection();
TimeSeriesCollection TSC2 = new TimeSeriesCollection();
TimeSeriesCollection TSC3 = new TimeSeriesCollection();
//   XYSeriesCollection XYSC = new XYSeriesCollection();
TemperaturKessel_resultSet = Verbinden_thread_Datenbank.DatenAbfrage2();
Batterieladezustand_resultset = Verbinden_thread_Datenbank.DatenAbfrage();
   
TimeSeries s =   Batterieleistung(resultSet);
TSC.addSeries(s);
  
TimeSeries s2 = TemperaturKessel(TemperaturKesselResultSet);
TSC2.addSeries(s2);

TimeSeries s3 = BatterieladezustandSeries(Batterieladezustand);     
TSC3.addSeries(s3);        
 
XYDataset dataset = TSC;
final JFreeChart chart = ChartFactory.createTimeSeriesChart("PV Daten", "Uhrzeit","Batteriespannung",dataset);   ///new TimeSeriesCollection(s)
  
SimpleDateFormat f = new SimpleDateFormat("HH:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));

XYPlot plot = (XYPlot) chart.getXYPlot();
plot.setDataset(0, TSC);
plot.setDataset(1,TSC2);           
plot.setDataset(2,TSC3);         

// renderer and adding the axis 

Here the mouselistener

final ChartPanel panel = new ChartPanel(chart);
// Chartgröße hier festlegen
panel.setBounds(0, 0, 1200, 800);

panel.setRangeZoomable(true);
panel.setDomainZoomable(true);
panel.addChartMouseListener(new ChartMouseListener() {

    @Override
    public void chartMouseClicked(ChartMouseEvent cme) {
        // report(cme);
    }

    @Override
    public void chartMouseMoved(ChartMouseEvent cme) {
        report(cme);
    }

    private void report(ChartMouseEvent cme) {

        ChartEntity ce = cme.getEntity();
        if (ce instanceof XYItemEntity) {
            XYItemEntity e = (XYItemEntity) ce;
            XYDataset d = e.getDataset();

            int s = e.getSeriesIndex();
            int i = e.getItem();
            Number xValue = d.getX(s, i);   // x-value zurück nach Datum-Zeit konvertieren
            String myDateStr = new SimpleDateFormat("HH:mm:ss").format(xValue);
            System.out.println("X:" + myDateStr + ", Y:" + d.getY(s, i) + ", Seriesindex: " /*+   String.valueOf(g)*/);
        }
    }
});

panel.addMouseMotionListener(this);
//   panel.addMouseListener(new MouseMarker(panel));        <------ Mouse Marker
panel.setAutoscrolls(false);
panel.setMouseZoomable(true);

I dont´t know how to modify the ChartMouseListener correctly to get the right dataset where the mouse is over! I tried here to find some method to get the dataset.

ChartEntity ce = cme.getEntity();
    if (ce instanceof XYItemEntity) {
        XYItemEntity e = (XYItemEntity) ce;
        XYDataset d = e.getDataset();`

Where is the information for the corresponding dataset?

System.out.println("X:" + myDateStr + ", Y:"
    + d.getY(s, i) + ", Seriesindex: "
      /*+   String.valueOf(g)*/) ; 

enter image description here

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    Possible duplicate of [_Obtain series identifiers when clicking on XYPlot's series_](https://stackoverflow.com/a/35276952/230513)or [_JFreeChart simple plot (parabola)_](https://stackoverflow.com/a/20085921/230513), [_et al._](https://stackoverflow.com/search?tab=votes&q=user%3a230513%20%5bjfreechart%5d%20ChartEntity%20XYItemEntity&searchOn=3). If this is not a duplicate, please [edit] your question to include a [mre] that shows your revised approach. – trashgod Jun 08 '23 at 21:35

0 Answers0