2014-07-21 34 views
0

任何人都可以请帮我解决这个问题,也就是说,我试图用单个LineAndPointFormatter使用android plot绘制一个多色图。根据范围值LineAndPointFormatter颜色将会改变,即假设范围值在0-50之间,那么线颜色将是蓝色,如果范围值在50-100,那么颜色将是绿色,如果范围值在100-200,那么颜色将是黑色和100以上它将是灰色的。AndroidPlot多色LineAndPointFormatter

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); 

但现在面临的问题是如何获得的数值范围,改变颜色,如果不知何故,我会得到的范围值,则相应地,我可以改变使用paint.setColor(Color.BLUE)的颜色;

让我知道是否有解决方案。

回答

1

假设这是一个静态图表,它应该像查找系列数据中最大的yVal(下面的getMaxY()方法)并执行查找(下面的getColorForMaxVal()方法)一样简单。像这样的东西替换上面的代码:

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; 
} 

香港专业教育学院没有尝试编译或运行该代码,所以有可能是某处的错误,但你的想法