2014-01-31 70 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

相关问题