0

条形图.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 系列不同的值范围,如何根据值给它们颜色。请任何人纠正我的代码。

4

1 回答 1

0

尝试这个:

消除

plot.setRenderer(new CustomBarRenderer());

为每个索引添加这个

plot.getRenderer().setSeriesPaint(0, Color.YELLOW);
于 2013-10-03T06:25:25.990 回答