2014-07-21 86 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; 
} 

香港專業教育學院沒有嘗試編譯或運行該代碼,所以有可能是某處的錯誤,但你的想法