Jfreechart: Is it possible to change the bar color?
Asked Answered
R

3

10

Is it possible to change the bar color?

I have coded a simple program for counting.

I want to implement one more thing: if the count number is greater than 200, use blue color to draw the bar. If not, use yellow color to do so.

Currently, all bar color is in red.

So, I would like to ask, is it possible to change the bar color?

If yes, can someone give me some guide to realize?

Thanks in advance!

attached is my coding:

<%@page contentType="text/html"%>
<%@page import="java.io.*" %>
<%@page import="java.sql.*" %>
<%@page import="org.jfree.data.category.*" %>
<%@page import="org.jfree.chart.*" %>
<%@page import="org.jfree.chart.plot.*" %>

<html>
<body>

<%
       DefaultCategoryDataset dataset = new DefaultCategoryDataset();
       try
        {

            Class.forName("com.mysql.jdbc.Driver");
            java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/delivery","root","root");
            Statement sta = con.createStatement();
            ResultSet res = sta.executeQuery("SELECT inventory, subject from statistics");
            int count;
            String subject;

            while (res.next())
            {
                count = res.getInt("inventory");
                subject = res.getString("subject");
                dataset.addValue(count,"enrollment count statistics", subject);  
            }
        }
        catch (Exception e) { 
            System.err.println(e.getMessage());
        }   

        JFreeChart bar = ChartFactory.createBarChart("Enrollment Chart", "subject","Count",dataset, PlotOrientation.HORIZONTAL,true, false, false);   
        //BarRenderer renderer = (BarRenderer) bar.getCategoryPlot().getRenderer();

        String fileName = "/bar.png";
        String file = application.getRealPath("/") + fileName;

        try
        {
            FileOutputStream fileOut = new FileOutputStream(file);
            ChartUtilities.writeChartAsPNG(fileOut, bar, 300, 300);
        }
        catch (IOException e)
        {
             out.print(e);
        }


%>
<img src="/delivery/bar.png" alt="subject Bar Chart" />
</body>
</html>
Rancell answered 10/2, 2011 at 2:58 Comment(0)
C
5

The magic is the getItemPaint(int,int) method of the BarRenderer class.

An example is at http://javabeanz.wordpress.com/2007/07/04/creating-barcharts-with-custom-colours-using-jfreechart/

What you're trying to do would be something like:

class CustomRenderer extends BarRenderer
{

   public CustomRenderer()
   {
   }

   public Paint getItemPaint(final int row, final int column)
   {
      // returns color depending on y coordinate.
      return (row > 200) ? Color.blue : Color.yellow ;
   }
}

And then after your call to ChartFactory.createBarChart, you do

final CategoryPlot plot = chart.getCategoryPlot();
CategoryItemRenderer renderer = new CustomRenderer();
plot.setRenderer(renderer);
Capsulate answered 10/2, 2011 at 3:16 Comment(0)
M
4
Plot plot = bar.getPlot();
BarRenderer barRenderer = (BarRenderer)plot.getRenderer();
barRenderer.setSeriesPaint(0, Color.gray);

Get a handle to the BarRenderer and call setSeriesPaint(int series, java.awt.Paint paint) on it.

Machree answered 10/2, 2011 at 3:8 Comment(1)
This method cannot be used to set the color of bars within a series, which is what OP wants to accomplish. Use Stobor's answer insteadSegregate
S
2

Take a look at this link

JFreeChart: Bar Chart Demo 3: different colors within a series

 /**
 * A custom renderer that returns a different color for each item in a single series.
 */
class CustomRenderer extends BarRenderer {

    /** The colors. */
    private Paint[] colors;

    /**
     * Creates a new renderer.
     *
     * @param colors  the colors.
     */
    public CustomRenderer(final Paint[] colors) {
        this.colors = colors;
    }

    /**
     * Returns the paint for an item.  Overrides the default behaviour inherited from
     * AbstractSeriesRenderer.
     *
     * @param row  the series.
     * @param column  the category.
     *
     * @return The item color.
     */
    public Paint getItemPaint(final int row, final int column) {
        return this.colors[column % this.colors.length];
    }
}
Sackcloth answered 23/4, 2013 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.