1

I've been looking everywhere to know how to change each bar color because I do not like the gradient setting of the default bar colors.

They all suggested overriding the BarRenderer with more or less the code below:

class CustomRenderer extends BarRenderer {

    private Paint[] colors;

    // add your custom colors
    public CustomRenderer() {
        this.colors = new Paint[] { Color.red, Color.blue, Color.green,
                Color.yellow, Color.orange, Color.cyan, Color.magenta,
                Color.blue };
    }

    public Paint getItemPaint(final int row, final int column) {
        return this.colors[column % this.colors.length];
    }
}

and to use this, one should put

CategoryItemRenderer renderer = new CustomRenderer();
plot.setRenderer(renderer);

But all I'm getting is the error

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    No enclosing instance of type ChartDemo is accessible. Must qualify the allocation with an enclosing instance of type ChartDemo (e.g. x.new A() where x is an instance of ChartDemo).

I don't know how to solve this, none of those answers seem to acknowledge this problem.

Below is my full code:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.DefaultFontMapper;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

public class ChartDemo {
    public static void main(String[] args) {
        writeChartToPDF(generateBarChart(), 500, 400, "D://barchart.pdf");
    }

    public static void writeChartToPDF(JFreeChart chart, int width, int height,
            String fileName) {
        PdfWriter writer = null;
        Document document = new Document(new Rectangle(width, height));

        try {
            writer = PdfWriter.getInstance(document, new FileOutputStream(
                    fileName));
            document.open();
            PdfContentByte contentByte = writer.getDirectContent();

            PdfTemplate template = contentByte.createTemplate(width, height);
            Graphics2D graphics2d = template.createGraphics(width, height,
                    new DefaultFontMapper());
            Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
                    height);

            chart.draw(graphics2d, rectangle2d);

            graphics2d.dispose();
            contentByte.addTemplate(template, 0, 0);

        } catch (Exception e) {
            e.printStackTrace();
        }
        document.close();
    }

    class CustomRenderer extends BarRenderer {

        private Paint[] colors;

        public CustomRenderer() {
            this.colors = new Paint[] { Color.red, Color.blue, Color.green,
                    Color.yellow, Color.orange, Color.cyan, Color.magenta,
                    Color.blue };
        }

        public Paint getItemPaint(final int row, final int column) {
            return this.colors[column % this.colors.length];
        }
    }

    public static JFreeChart generateBarChart() {
        DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
        dataSet.setValue(100, "Population", "2");
        dataSet.setValue(78, "Population", "4");
        dataSet.setValue(62, "Population", "6");
        dataSet.setValue(50, "Population", "8");
        dataSet.setValue(39, "Population", "10");

        JFreeChart chart = ChartFactory.createBarChart("", "Team number",
                "Solved problems (%)", dataSet, PlotOrientation.VERTICAL,
                false, true, false);
        chart.setBackgroundPaint(Color.WHITE);

        final CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.WHITE);
        plot.setDomainGridlinePaint(new Color(204, 204, 204));
        plot.setRangeGridlinePaint(new Color(204, 204, 204));

        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        CategoryItemRenderer renderer = new CustomRenderer();
        plot.setRenderer(renderer);

        final BarRenderer renderer1 = (BarRenderer) plot.getRenderer();
        renderer1.setDrawBarOutline(false);
        renderer1.setShadowVisible(false);
        renderer1.setDrawBarOutline(false);
        renderer1.setSeriesPaint(0, Color.gray);
        renderer1.setSeriesPaint(1, Color.RED);

        return chart;
    }
}

PS. My goal is to mimic the style of flot.js as much as possible (flat/minimal design).

P A S T R Y
  • 351
  • 3
  • 20
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 04 '16 at 00:47

1 Answers1

4

Your problem is not JFreeChart related but a Java problem: you have a nested, non-static class CustomRenderer. To instantiate it you either need an instance of the enclosing class (ChartDemo) or (probably the better way in your case) you need to make it static:

static class CustomRenderer extends BarRenderer {
  // ...
}

See also this answer on SO for more details.

hth,
- martin

Community
  • 1
  • 1
Martin Höller
  • 2,714
  • 26
  • 44