barchart.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.awt.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.jfree.chart.*"%>
<%@ page import="org.jfree.chart.axis.*"%>
<%@ page import="org.jfree.chart.entity.*"%>
<%@ page import="org.jfree.chart.labels.*"%>
<%@ page import="org.jfree.chart.plot.*"%>
<%@ page import="org.jfree.chart.renderer.category.*"%>
<%@ page import="org.jfree.chart.urls.*"%>
<%@ page import="org.jfree.data.category.*"%>
<%@ page import="org.jfree.data.general.*"%>
<%@ page import="org.jfree.chart.plot.dial.StandardDialScale"%>
<%@ page import="org.jfree.chart.renderer.category.CategoryItemRenderer"%>
<%@ page import="org.jfree.chart.renderer.category.BarRenderer"%>
<%
class CustomBarRenderer extends BarRenderer {
private Paint[] m_paints = new Paint[]{Color.blue, Color.red, Color.yellow, Color.green, Color.pink};
public CustomBarRenderer() {
}
@Override
public Paint getItemPaint(int series, int column) {
CategoryDataset dataSet = getPlot().getDataset();
String rowKey = (String)dataSet.getRowKey(series);
String colKey = (String)dataSet.getColumnKey(column);
int value = dataSet.getValue(rowKey, colKey).intValue();
if (series == 1) {
return m_paints[0];
}
if (series == 2) {
return m_paints[1];
}
if (series == 3) {
return m_paints[2];
}
if (series == 4) {
return m_paints[3];
}
if (series == 5) {
return m_paints[4];
}
return null;
}
}
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
final String series1 = "Outsatnding";
final String series2 = "Excellent";
final String series3 = "Very Good";
final String series4 = "Good";
final String series5 = "Average";
dataSet.setValue(32, series3, "Attention to Details");
dataSet.setValue(40, series4, "Systematic Approach");
dataSet.setValue(25, series2, "Communication");
dataSet.setValue(40, series4, "Quality Management");
dataSet.setValue(40, series4, "Network Marketing");
dataSet.setValue(59, series5, "Photography, Videography, Cinematography");
dataSet.setValue(10, series1, "Animation, Graphic Design");
dataSet.setValue(38, series3, "Teaching, Training");
CategoryPlot plot = null;
JFreeChart chart =null;
//BarRenderer renderer= null;
chart = ChartFactory.createBarChart(
"Details for software programmer ",
"Competency Name", "Score In Percentage", dataSet,
PlotOrientation.HORIZONTAL, false, true, false);
plot = chart.getCategoryPlot();
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
chart.getPlot().setBackgroundPaint(java.awt.Color.white);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setRange(0, 100);
plot.setRangeGridlinePaint(Color.white);
plot.setRenderer(new CustomBarRenderer());
try {
OutputStream out2 = response.getOutputStream();
final CategoryAxis dAxis = plot.getDomainAxis();
dAxis.setTickLabelFont(new java.awt.Font("Arial Narrow", java.awt.Font.PLAIN, 14));
final ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
final File file1 = new File("/barchart.png");
ChartUtilities.writeChartAsPNG(out2, chart, 500, 600, info);
} catch (Exception e) {
out.println(e);
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!meta http-equiv="refresh" content="1">
<title>JSP Page</title>
</head>
<body>
<IMG SRC="barchart.png" WIDTH="600" HEIGHT="400" BORDER="0"
USEMAP="#chart">
</body>
</html>
//我根据系列的值范围得不到颜色。 我有5个系列的不同值范围,如何根据值给它们颜色。 请任何人都可以更正我的代码。
答案 0 :(得分:0)
试试这个:
删除强>
plot.setRenderer(new CustomBarRenderer());
为每个索引添加
plot.getRenderer().setSeriesPaint(0, Color.YELLOW);