使用 jfreechart 创建条形图的 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"%>
<%
class CustomRenderer extends BarRenderer {
private Paint[] m_paints = new Paint[]{Color.blue, Color.orange, Color.yellow,};
public CustomRenderer() {
super();
}
@Override
public Paint getItemPaint(int series, int category) {
CategoryDataset dataSet = getPlot().getDataset();
String rowKey = (String) dataSet.getRowKey(series);
String colKey = (String) dataSet.getColumnKey(category);
int value = dataSet.getValue(rowKey, colKey).intValue();
System.out.println("value is " + value);
if (value == 5) {
System.out.println("5");
return m_paints[0];
}
if (value == 3) {
System.out.println("3");
return m_paints[1];
}
if (value == 2) {
System.out.println("2");
return m_paints[2];
}
return m_paints[0];
}
}
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
dataSet.setValue(79,"5", "Analytical Skills");
dataSet.setValue(92, "2", "Logical thinking");
dataSet.setValue(55, "4", "Attention to Details");
dataSet.setValue(79, "1", "a");
dataSet.setValue(92, "3", "b");
dataSet.setValue(55, "5", "c");
CategoryPlot plot = null;
JFreeChart chart =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 CustomRenderer());
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>
//我想要不同颜色的条,具体取决于数据集的值。