Jfreechart:是否可以更改条形颜色?

发布于 2024-10-16 20:53:46 字数 2004 浏览 6 评论 0原文

是否可以更改条形颜色?

我编写了一个简单的计数程序。

我还想实现一件事:如果计数大于 200,则使用蓝色绘制条形。如果没有,请使用黄色。

目前,所有条形颜色均为红色。

所以我想问一下,可以改变bar的颜色吗?

如果是,有人可以给我一些指导来实现吗?

提前致谢!

附件是我的编码:

<%@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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬花如无物 2024-10-23 20:53:46

神奇的是 getItemPaint BarRenderer 类

示例位于 http:// javabeanz.wordpress.com/2007/07/04/creating-barcharts-with-custom-colours-using-jfreechart/

您想要做的事情是这样的:

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

然后在调用 ChartFactory 之后。创建条形图,你做的

final CategoryPlot plot = chart.getCategoryPlot();
CategoryItemRenderer renderer = new CustomRenderer();
plot.setRenderer(renderer);

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);
青柠芒果 2024-10-23 20:53:46
Plot plot = bar.getPlot();
BarRenderer barRenderer = (BarRenderer)plot.getRenderer();
barRenderer.setSeriesPaint(0, Color.gray);

获取 BarRenderer 并对其调用 setSeriesPaint(int series, java.awt.Paint Paint) 。

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.

葬花如无物 2024-10-23 20:53:46

看一下这个链接

JFreeChart:条形图演示 3:内部的不同颜色系列

 /**
 * 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];
    }
}

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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文