0

使用 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>

//我想要不同颜色的条,具体取决于数据集的值。

4

1 回答 1

0

我没有直接在jsp中使用jfreechart代码。运行您的 jsp 代码加载CustomRenderer类失败时出现错误。

要更改条的颜色,您可以使用包中的GradientPaintjava.awt。如果您有四个系列,则创建四个不同的对象GradientPaint,将 RGB 代码传递给java.awt.Color. 要获取颜色代码,您可能会发现此站点很有用。
最后调用BarRenderer的setSeriesPaint方法

GradientPaint gradientpaint0 = new GradientPaint(0.0F, 0.0F, new Color(250, 12, 8), 0.0F, 0.0F, new Color(250, 12, 8));   
GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, new Color(85, 19, 230), 0.0F, 0.0F, new Color(85, 19, 230));  
GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F, new Color(26, 189, 20), 0.0F, 0.0F, new Color(26, 189, 20));
GradientPaint gradientpaint3 = new GradientPaint(0.0F, 0.0F, new Color(15, 233, 193), 0.0F, 0.0F, new Color(15, 233, 193)); //15,233,193 is RGB
renderer.setSeriesPaint(0, gradientpaint0);  
renderer.setSeriesPaint(1, gradientpaint1);  
renderer.setSeriesPaint(2, gradientpaint2);
renderer.setSeriesPaint(3, gradientpaint3); 

希望这可以帮助你。

  1. 有用的链接1
  2. 有用的链接2
于 2013-09-26T09:16:34.287 回答