Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jfreechart: Is it possible to change the bar color?

Tags:

jsp

jfreechart

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>
like image 940
Jfreechart Avatar asked Feb 10 '11 02:02

Jfreechart


2 Answers

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);
like image 55
Stobor Avatar answered Sep 21 '22 14:09

Stobor


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.

like image 23
Piyush Mattoo Avatar answered Sep 23 '22 14:09

Piyush Mattoo