2014-01-31 125 views
2

1.我的條形圖有一個系列。如何在androidplot條形圖中繪製不同顏色的酒吧?

2.在系列中有10個值。

3.當圖顯示它有10條,但所有的酒吧是相同的顏色

4.How畫不同的顏色吧?

我嘗試這樣做,但它不是工作

class MyBarRenderer extends BarRenderer<MyBarFormatter> { 
    public MyBarRenderer(XYPlot plot) { 
     super(plot); 
    } 

    protected BarFormatter getFormatter(int index, XYSeries series) { 
     //return getFormatter(series); 
     if(index % 2 == 1) { 
      return new MyBarFormatter(Color.BLUE, Color.TRANSPARENT); 
     } else { 
      return new MyBarFormatter(Color.RED, Color.TRANSPARENT); 
     } 
    } 
} 

回答

2

我的代碼看起來幾乎相同,你的,它爲我工作。這裏是我的代碼的其餘部分:

class MyBarFormatter extends BarFormatter 
{ 
    public MyBarFormatter(int fillColor, int borderColor) 
    { 
     super(fillColor, borderColor); 
    } 

    @Override public Class<? extends SeriesRenderer> getRendererClass() 
    { 
     return MyBarRenderer.class; 
    } 

    @Override public SeriesRenderer getRendererInstance(XYPlot plot) 
    { 
     return new MyBarRenderer(plot); 
    } 
} 

class MyBarRenderer extends BarRenderer<MyBarFormatter> 
{ 
    public MyBarRenderer(XYPlot plot) 
    { 
     super(plot); 
    } 

    public MyBarFormatter getFormatter(int index, XYSeries series) 
    { 
     // return getFormatter(series); 
     if(index % 2 == 1) 
     { 
      return new MyBarFormatter(Color.BLUE, Color.TRANSPARENT); 
     } 
     else 
     { 
      return new MyBarFormatter(Color.RED, Color.TRANSPARENT); 
     } 
    } 
} 

要使用這兩個類,我調用下面的代碼:

// Create an array of y-values to plot: 
Number[] values = { 380, 1433, 1965, 3200, 3651, 3215, 3217, 1000, 500, 4300, 3000, 2100 }; 

// Turn the above array into XYSeries': 
XYSeries series1 = new SimpleXYSeries(Arrays.asList(values), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series1"); 

// Add a new series to the xyplot: 
MyBarFormatter formatter1 = new MyBarFormatter(Color.argb(200, 100, 150, 100), Color.LTGRAY); 
mySimpleXYPlot.addSeries(series1, formatter1); 

// Give each bar a fixed width 
MyBarRenderer renderer = ((MyBarRenderer) mySimpleXYPlot.getRenderer(MyBarRenderer.class)); 
renderer.setBarWidthStyle(BarRenderer.BarWidthStyle.FIXED_WIDTH); 
renderer.setBarWidth(50); 

這裏的是什麼樣子,2種不同的顏色之間交替:

enter image description here

此外,添加的水平虛線:

YValueMarker yValueMarker = new YValueMarker(2300, "", new XPositionMetric(PixelUtils.dpToPix(0), XLayoutStyle.ABSOLUTE_FROM_RIGHT), Color.BLACK, Color.BLACK); 

DashPathEffect dashPathEffect = new DashPathEffect(new float[] { PixelUtils.dpToPix(16), PixelUtils.dpToPix(8) }, 0); 

yValueMarker.getLinePaint().setPathEffect(dashPathEffect); 

yValueMarker.getLinePaint().setStrokeWidth(PixelUtils.dpToPix(1)); 

yValueMarker.getLinePaint().setColor(Color.BLACK); 

addMarker(yValueMarker); 
+0

你是如何繪製2000 - 2500範圍內的水平虛線? –

+1

@ManuelLopera我只是將代碼添加到我的答案底部。 – Luke

相關問題