在普通柱图上实现"数据下探"


/**

**QQ:252574345

**MSN:lele_love_lomboz@hotmail.com

*/

 (如遇到不能生成链接,或在对比图中设置热点链接不成功的问题,请参照另外2篇文章)

1.水果销售量

<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.awt.Color,
                org.jfree.chart.ChartFactory,
                org.jfree.chart.JFreeChart,
                org.jfree.chart.plot.PlotOrientation,
                org.jfree.chart.servlet.ServletUtilities,
                org.jfree.data.category.CategoryDataset,
                org.jfree.data.general.*,
                org.jfree.chart.plot.CategoryPlot,
                org.jfree.chart.axis.CategoryAxis,
                org.jfree.chart.axis.ValueAxis,
                org.jfree.chart.renderer.category.BarRenderer3D,
                org.jfree.chart.urls.StandardCategoryURLGenerator,
                org.jfree.chart.*,
                org.jfree.chart.entity.*,
                org.jfree.chart.labels.*"%>
<%
java.io.PrintWriter pw=new java.io.PrintWriter(out);
double[][] data = new double[][] {{672}, {325}, {332}, {440}, {550}, {330}};
String[] rowKeys = {"苹果", "梨子", "葡萄", "桔子", "西瓜", "香蕉"};
String[] columnKeys = {""};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
        rowKeys,
        columnKeys,
        data);


JFreeChart chart = ChartFactory.createBarChart3D("水果销量图统计",
                  "水果",
                  "销量",
                  dataset,
                  PlotOrientation.VERTICAL,
                  true,
                  false,
                  false);

chart.setBackgroundPaint(Color.WHITE);
CategoryPlot plot = chart.getCategoryPlot();

CategoryAxis domainAxis = plot.getDomainAxis();
plot.setDomainAxis(domainAxis);

ValueAxis rangeAxis = plot.getRangeAxis();
//设置最高的一个 Item 与图片顶端的距离
rangeAxis.setUpperMargin(0.15);
//设置最低的一个 Item 与图片底端的距离
rangeAxis.setLowerMargin(0.15);

plot.setRangeAxis(rangeAxis);

BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseOutlinePaint(Color.BLACK);
//设置 Wall 的颜色
renderer.setWallPaint(Color.gray);
//设置每种水果代表的柱的颜色
renderer.setSeriesPaint(0, new Color(0, 0, 255));
renderer.setSeriesPaint(1, new Color(200, 200, 255));
renderer.setSeriesPaint(2, Color.GREEN);
renderer.setSeriesPaint(3, Color.MAGENTA);
renderer.setSeriesPaint(4, Color.GRAY);
renderer.setSeriesPaint(5, Color.CYAN);

//设置平行柱的之间距离
renderer.setItemMargin(0.3);

//显示每个柱的数值
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);

renderer.setBaseItemLabelsVisible(true);
renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator(
        "detail.jsp", "fruit", ""));
plot.setRenderer(renderer);

//设置柱的透明度
plot.setForegroundAlpha(0.5f);
//设置地区、销量的显示位置
//plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
ChartRenderingInfo info = new ChartRenderingInfo(new
                    StandardEntityCollection());

String filename = ServletUtilities.saveChartAsPNG(chart, 800, 600, info, session);
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;

ChartUtilities.writeImageMap(pw, filename, info, true);
              pw.flush();

%>

<table width="100%">
    <tr>
        <td align="center">
            <img src="<%= graphURL %>" width=800 height=600 border=0 usemap="#<%= filename %>" alt="">
        </td>
    </tr>
</table>

 

2.水果销售量详细信息

<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.awt.Color,
                org.jfree.chart.ChartFactory,
                org.jfree.chart.JFreeChart,
                org.jfree.chart.plot.PlotOrientation,
                org.jfree.chart.servlet.ServletUtilities,
                org.jfree.data.category.CategoryDataset,
                org.jfree.data.general.*,
                org.jfree.chart.plot.CategoryPlot,
                org.jfree.chart.axis.CategoryAxis,
                org.jfree.chart.axis.ValueAxis,
                org.jfree.chart.renderer.category.BarRenderer3D,
                org.jfree.chart.labels.*"%>
<%
String fruit = new String(request.getParameter("fruit").getBytes("ISO8859_1"), "utf-8");
int count = 0;
if ("苹果".equals(fruit)) {
    count = 100;
} else if ("梨子".equals(fruit)) {
    count = 26;
} else if ("葡萄".equals(fruit)) {
    count = -20;
} else if ("桔子".equals(fruit)) {
    count = -40;
} else if ("西瓜".equals(fruit)) {
    count = 80;
}
//String address = new String(request.getParameter("address").getBytes("ISO8859_1"), "utf-8");

double[][] data = new double[][] {{672 + count}, {325 + count}, {332 + count}, {440 + count},
                                  {550 + count}, {330 + count}, {435 + count}, {553 + count},
                                  {335 + count}, {443 + count}, {378 + count}, {733 + count}};
String[] rowKeys = {"一月份", "二月份", "三月份", "四月份",
                    "五月份", "六月份", "七月份", "八月份",
                    "九月份", "十月份", "十一月份", "十二月份"};
String[] columnKeys = {""};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
        rowKeys,
        columnKeys,
        data);

JFreeChart chart = ChartFactory.createBarChart3D("2007年度" + fruit + "销量图",
                  "月份",
                  "销量",
                  dataset,
                  PlotOrientation.VERTICAL,
                  true,
                  false,
                  false);

chart.setBackgroundPaint(Color.WHITE);
CategoryPlot plot = chart.getCategoryPlot();

CategoryAxis domainAxis = plot.getDomainAxis();
plot.setDomainAxis(domainAxis);

ValueAxis rangeAxis = plot.getRangeAxis();
//设置最高的一个 Item 与图片顶端的距离
rangeAxis.setUpperMargin(0.15);
//设置最低的一个 Item 与图片底端的距离
rangeAxis.setLowerMargin(0.15);

plot.setRangeAxis(rangeAxis);

BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseOutlinePaint(Color.BLACK);
//设置 Wall 的颜色
renderer.setWallPaint(Color.gray);
//设置每种水果代表的柱的颜色
renderer.setSeriesPaint(0, new Color(0, 0, 255));
renderer.setSeriesPaint(1, new Color(200, 200, 255));
renderer.setSeriesPaint(2, Color.GREEN);
renderer.setSeriesPaint(3, Color.MAGENTA);
renderer.setSeriesPaint(4, Color.GRAY);
renderer.setSeriesPaint(5, Color.CYAN);

//设置平行柱的之间距离
renderer.setItemMargin(0.3);

//显示每个柱的数值
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);

plot.setRenderer(renderer);
//设置柱的透明度
plot.setForegroundAlpha(0.5f);

String filename = ServletUtilities.saveChartAsPNG(chart, 800, 550, null, session);
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;

%>

<table width="100%">
    <tr>
        <td align="center">
            <a href="index.jsp">返回</a>
            <br>
            <img src="<%= graphURL %>" width=800 height=550 border=0 usemap="#<%= filename %>" alt="">
        </td>
    </tr>
</table>

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页