Identify the grid of a plotted point in AndroidPlot

168 views Asked by At

I have an ecg graph plotting application and the graph looks like this.

enter image description here

What I need to know is,is it possible to know the subgrid in which the point is plotted... say in a format like (row_index,column_index) or something like this. Actually I don't know whether it is a possible scenario. So if there is no way to do this please let me know. Given below is my graph configuring method.

  private void configureGraph() {
/**
     * ecgPlot corresponds to XYPlot
     */
    XYGraphWidget graph = ecgPlot.getGraph();

    /**
     * Paint to denote line color
     */
    Paint paint = new Paint();
    paint.setColor(Color.WHITE);
    paint.setStrokeWidth(3.0f);
    /**
     * Setting graph x and y boundary values
     */
    ecgPlot.setRangeBoundaries(-40, 40, BoundaryMode.FIXED);
    ecgPlot.setDomainBoundaries(0, 1500, BoundaryMode.FIXED);

    ecgPlot.setPlotPadding(-10, 0, 0, 0);

    /**
     * Removes default bkg - ie; black
     */
    ecgPlot.setBackgroundPaint(null);
    graph.setBackgroundPaint(null);
    graph.setGridBackgroundPaint(null);
    /**
     * Adjusting grid line width
     */
    graph.getDomainGridLinePaint().setStrokeWidth(4.0f);
    graph.getRangeGridLinePaint().setStrokeWidth(4.0f);
    graph.getDomainSubGridLinePaint().setStrokeWidth(1.0f);
    graph.getRangeSubGridLinePaint().setStrokeWidth(1.0f);

    /**
     * Removes border
     */
    ecgPlot.setBorderPaint(null);
    /**
     * Setting grid color
     */
    graph.getDomainGridLinePaint().setColor(getResources().getColor(R.color.colorECGGrid));
    graph.getRangeGridLinePaint().setColor(getResources().getColor(R.color.colorECGGrid));
    graph.getRangeSubGridLinePaint().setColor(getResources().getColor(R.color.colorECGGrid));
    graph.getDomainSubGridLinePaint().setColor(getResources().getColor(R.color.colorECGGrid));
    /**
     * Setting number of sub grid lines per grid
     */
    graph.setLinesPerDomainLabel(5);
    graph.setLinesPerRangeLabel(5);

    ecgPlot.setRangeStep(StepMode.INCREMENT_BY_VAL, 1);
    ecgPlot.setDomainStepValue(75);
    ecgPlot.setLinesPerDomainLabel(5);
    ecgPlot.setDomainLabel(null);
    ecgPlot.setRangeLabel(null);

    Paint paintTest = new Paint();
    paintTest.setColor(Color.TRANSPARENT);
    paintTest.setStrokeWidth(3.0f);

    ecgLinePointFormatter.setLegendIconEnabled(false);
    // PointLabelFormatter pointLabelFormatter = new PointLabelFormatter();
    // pointLabelFormatter.setTextPaint(paint);

}                                                                                                            

Thanks in advance

1

There are 1 answers

1
Nick On

Unfortunately I'm not at a place where I can actually test whether this code works, but here's a general idea on how you can convert from real XY values into subgrid coords:

double subX(Number x) {
    // calculate the value each subgrid represents:
    double stepVal = (plot.getBounds().getWidth().doubleValue() / 75) * 5;

    // find the value of x relative to the left edge of the screen
    double xOff = x.doubleValue() - plot.getBounds().getMinX().doubleValue();     
    return xOff / stepVal;
}

double subY(Number y) {
    double stepVal = plot.getBounds().getHeight().doubleValue() / 5;

    double yOff = y.doubleValue() - plot.getBounds().getMinY().doubleValue();
    return yOff / stepVal;
}

Then, given Number x and Number y that you want to convert into subgrid coords:

Number x = subX(realX);
Number y = subY(realY);

If you need pixel values instead of real values, you can use the XYPlot.seriesToScreenX/Y(Number) and XYPlot.screenToSeriesX/Y(Number) methods to convert back and forth.