Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix wrong value displayed in cursor label #805

Open
bennyabc opened this issue Dec 20, 2023 · 1 comment
Open

Fix wrong value displayed in cursor label #805

bennyabc opened this issue Dec 20, 2023 · 1 comment

Comments

@bennyabc
Copy link

bennyabc commented Dec 20, 2023

I solved the issue with the wrong cursor label content.
Hope that someone adds the fix to the branch.

File: PlotContent_XY.java

public void doPaint(Graphics2D g) {
//+++++
    cursor.clearData();
//+++++

File: Cursor.java

//+++++
import java.util.Arrays;
import java.util.Collections;
//+++++

//- - - - - 
 private void calculateMatchingDataPoints() {

    List<DataPoint> dataPoints = new ArrayList<>();
    for (DataPoint dataPoint : dataPointList) {
      if (dataPoint.shape.contains(mouseX, dataPoint.shape.getBounds().getCenterY())
          && chart.plot.plotContent.getBounds().getY() < mouseY
          && chart.plot.plotContent.getBounds().getY()
                  + chart.plot.plotContent.getBounds().getHeight()
              > mouseY) {
        dataPoints.add(dataPoint);
      }
    }

    if (dataPoints.size() > 0) {
      Map<String, DataPoint> map = new HashMap<>();
      String seriesName = "";
      for (DataPoint dataPoint : dataPoints) {
        seriesName = dataPoint.seriesName;
        if (map.containsKey(seriesName)) {
          if (Math.abs(dataPoint.x - mouseX) < Math.abs(map.get(seriesName).x - mouseX)) {
            map.put(seriesName, dataPoint);
          }
        } else {
          map.put(seriesName, dataPoint);
        }
      }
      matchingDataPointList.clear();
      matchingDataPointList.addAll(map.values());
    }
  }
//- - - - -
//+++++
 void clearData()   {

      dataPointList.clear();
  }
//+++++
    
//+++++
  private void calculateMatchingDataPoints()    {

      // find all points on y axis
      List<DataPoint> dataPoints = new ArrayList<>();
      for (DataPoint dataPoint : dataPointList)  {
          if (dataPoint.shape.contains(mouseX, dataPoint.shape.getBounds().getCenterY())
                  && chart.plot.plotContent.getBounds().getY() < mouseY
                  && chart.plot.plotContent.getBounds().getY()  + chart.plot.plotContent.getBounds().getHeight() > mouseY)  {
              dataPoints.add(dataPoint);
          }
      }

      if (dataPoints.size() > 0)   {
          // group data points by seriesName
          // remove double values
          Map<String, DataPoint> map = new HashMap<>();
          HashMap<String, ArrayList<String>> sameSeriesPoints = new HashMap<String, ArrayList<String>>();
          for (DataPoint dataPoint : dataPoints)  {
              String seriesName = dataPoint.seriesName;
              DataPoint matchingPoint = map.get(seriesName);
              if (matchingPoint == null) {
                  // first point of series
                  map.put(seriesName, dataPoint);
              }  else if ( !matchingPoint.yValue.equals(dataPoint.yValue) )  {
                  // another point of series
                  ArrayList<String> list = sameSeriesPoints.get(seriesName);
                  if ( list == null )  {
                      list = new ArrayList<String>();
                      sameSeriesPoints.put(seriesName, list);
                  }
                  // add only different values
                  if ( !list.contains(dataPoint.yValue) )  {
                      list.add(dataPoint.yValue);
                  }
              }
          }
          
          if ( sameSeriesPoints.size() > 0 )  {
              // create combined yValue
              for ( String seriesName : sameSeriesPoints.keySet() )  {
                  DataPoint dp = map.get(seriesName);
                  // create sorted list
                  ArrayList<String> list = sameSeriesPoints.get(seriesName);
                  list.add(dp.yValue);
                  Collections.sort(list);
                  // build yValue String
                  StringBuilder sb = new StringBuilder();
                  boolean first = true;
                  for ( String s : list ) {
                      if ( !first ) {
                          sb.append(",");
                      }
                      first = false;
                      sb.append(s);
                  }
                  
                  // replace DataPoint
                  map.put(seriesName, new DataPoint(dp.x, dp.y, dp.xValue, sb.toString(), seriesName));
              }
          }
        
          matchingDataPointList.clear();
          matchingDataPointList.addAll(map.values());
      }
  }
//+++++

EDITED by @mccartney to fix the formatting

@bennyabc
Copy link
Author

bennyabc commented Jan 1, 2024

Hi
Small fix.

void clearData() {
if ( dataPointList != null )
dataPointList.clear();
}

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants