AndroidPlot Multi color LineAndPointFormatter

孤人 提交于 2019-12-13 06:59:30

问题


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:


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



来源:https://stackoverflow.com/questions/24867520/androidplot-multi-color-lineandpointformatter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!