开发者

Jfreechart: Is it possible to change the bar color?

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", "subjec开发者_Go百科t","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>


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


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.


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];
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜