Jfreechart:是否可以更改条形颜色?
是否可以更改条形颜色?
我编写了一个简单的计数程序。
我还想实现一件事:如果计数大于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
神奇的是 getItemPaint BarRenderer 类。
示例位于 http:// javabeanz.wordpress.com/2007/07/04/creating-barcharts-with-custom-colours-using-jfreechart/
您想要做的事情是这样的:
然后在调用 ChartFactory 之后。创建条形图,你做的
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:
And then after your call to ChartFactory.createBarChart, you do
获取 BarRenderer 并对其调用 setSeriesPaint(int series, java.awt.Paint Paint) 。
Get a handle to the BarRenderer and call setSeriesPaint(int series, java.awt.Paint paint) on it.
看一下这个链接
JFreeChart:条形图演示 3:内部的不同颜色系列
Take a look at this link
JFreeChart: Bar Chart Demo 3: different colors within a series