3

1.My bar chart have one serie.

2.In serie has 10 value.

3.When chart is shown it's has 10 bar but all bar is same color

4.How to paint different color of bar?

I'm try to do this but it's not work

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);
        }
    }
}
user2955394
  • 1,063
  • 4
  • 17
  • 34

1 Answers1

3

My code looks almost identical to yours, and it worked for me. Here is the rest of my code:

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

To use these two classes, I invoke the following code:

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

Here's what it looks like, alternating between 2 different colors:

enter image description here

Also, to add the dashed horizontal line:

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);
Luke
  • 2,187
  • 1
  • 18
  • 29