0
votes

Can anybody please help me with this issue i.e. Am trying to plot a Multicolor graph using android plot with single LineAndPointFormatter. According to the Range values LineAndPointFormatter color will change i.e. Suppose range values lies in between 0-50 then Line color will be blue, if range values lies 50-100 then color will be green, if range values lies 100-200 then color will be black and above 100 it will be gray.

Check and let me know if below solution is fine or not i.e.

LineAndPointFormatter formatter; 

formatter = new LineAndPointFormatter(Color.rgb(50, 143, 222),
                null, null, null);

Paint paint = formatter.getLinePaint();
paint.setStrokeWidth(10); // Set the formatter width
paint.setColor(Color.BLUE); // Set the formatter color
formatter.setLinePaint(paint);

But am facing problem how to get the range values and change the color, if somehow I will get the range values then accordingly I can change the color using paint.setColor(Color.BLUE);

Let me know if any solution is available.

1

1 Answers

1
votes

Assuming this is a static chart, it should be as simple as finding the largest yVal in your series data (getMaxY() method below) and doing a lookup (getColorForMaxVal() method below). Replace the code above with something like this:

formatter = new LineAndPointFormatter(getColorForMaxVal(getMaxY(series1)),
    null, null, null);

/**
 *
 * @param maxVal
 * @return A color value appropriate for maxVal.
 */
int getColorForMaxVal(Number maxVal)  {
    double max = maxVal.doubleValue();
    if(max > 50) {
        return Color.GREEN;
    } else if(max > 100) {
        return Color.BLACK;
    } else if(max > 200) {
        return Color.GRAY;
    } else {
        return Color.BLUE;
    }
}

/**
 *
 * @param series
 * @return The largest yVal in the series or null if the series contains no non-null yVals.
 */
Number getMaxY(XYSeries series) {
    Number max = null;
    for(int i = 0; i < series.size(); i++) {
        Number thisNumber = series.getY(i);
        if(max == null || thisNumber != null && thisNumber.doubleValue() >  max.doubleValue()) {
            max = thisNumber;
        }
    }
    return max;
}

Ive not tried to compile or run this code so there might be a bug somewhere, but you get the idea