没有在变量中获取jfreechart数据集的值

时间:2013-09-26 07:42:07

标签: jsp jfreechart

使用jfreechart创建条形图的JSP代码我没有从数据集中获取系列的值,并且所有条形都显示为蓝色。 PLZ任何人都可以帮助我

<%@ 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>

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

1 个答案:

答案 0 :(得分:0)

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

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

BarRenderer方法
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. Useful link1
  2. Useful link2