⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chartservlet.java

📁 JWebChart is a simple package to allow anyone to render complex charts using a Java Servlet. The pro
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    	PieChart pie = new PieChart();
    	
    	// set the title for the Pie Chart
    	if (data.containsKey("Title")) {
    		pie.title = req.getParameter("Title");
    		data.remove("Title");
    	}
    	
    	// what type?
    	if (data.containsKey("Type")) {
    		String type = req.getParameter("Type");
    		if (type.equalsIgnoreCase("2D")) {
            	pie.type = PieChart.TYPE2D;
            } else if (type.equalsIgnoreCase("3D")) {
            	pie.type = PieChart.TYPE3D;
            } else if (type.equalsIgnoreCase("Ring")) {
            	pie.type = PieChart.TYPERING;
            }
    		data.remove("Type");
    	}
    	
    	// get the chart data from an XML file or...
        if (data.containsKey("XMLFile")) {
        	URL url = getClass().getResource(req.getParameter("XMLFile"));
            try
            {
                java.io.InputStream inputstream = url.openStream();
                pie.dataset = (DefaultPieDataset) DatasetReader.readPieDatasetFromXML(inputstream);
            }
            catch(IOException ioexception)
            {
                System.out.println(ioexception.getMessage());
            }
        	data.remove("XMLFile");
        } else {
        	// get the data from the map
        	for (Iterator<Entry<String, String[]>> it=data.entrySet().iterator(); it.hasNext(); ) {
        		Map.Entry entry = it.next();
        		String key = (String) entry.getKey();
        		String value[] = (String[]) entry.getValue();
        		pie.dataset.setValue(key,new Double(value[0]));
        		//out.println("<br>Key : " + key);
        		//out.println("  Value : " + value[0]);
        	}
        }
    	
    	// create pie chart
    	JFreeChart chart = pie.getChart();
    	return chart;
    }
    
    /**
     * Create category chart method.
     * 
     * @param map which can consist of elements such as:
     * <ul>
     * <li> Type - the type of chart: either Bar, Bar3D, Area, Line, or Line3D.</li>
     * <li> Title - the title of the chart.</li>
     * <li> XLabel - the domain title.</li>
     * <li> YLabel - the range title.<li>
     * <li> Orientation - the orientation of the chart, either H or V.<li>
     * <li> Range - the upper and lower value of the range. Format is Range=lower,upper, e.g. Range=68,72.</li>
     * <li> XMLFile - the XML file name if applicable.</li>
     * <li> data - the data to plot. Format is name=value&name=value, eg: Fish=70&Chips=66.</li>
     * </ul>
     * @param req which is the Http Servlet Request	
     *
     * @return JFreeChart
     * @throws ServletException
     * 
     */
    private JFreeChart createCategory(Map<String, String[]> map, HttpServletRequest req) 
    		throws ServletException {
    	CategoryChart bar = new CategoryChart();
    	
    	// set the title for the Category Chart
    	if (map.containsKey("Title")) {
    		bar.title = req.getParameter("Title");
    		map.remove("Title");
    	}
    	
    	// what type? (default is Bar)
    	if (map.containsKey("Type")) {
    		String type = req.getParameter("Type");
    		if (type.equalsIgnoreCase("Bar")) {
            	bar.type = CategoryChart.TYPEBAR;
            } else if (type.equalsIgnoreCase("Bar3D")) {
            	bar.type = CategoryChart.TYPEBAR3D;
            } else if (type.equalsIgnoreCase("Area")) {
            	bar.type = CategoryChart.TYPEAREA;
            } else if (type.equalsIgnoreCase("Line")) {
            	bar.type = CategoryChart.TYPELINE;
            } else if (type.equalsIgnoreCase("Line3D")) {
            	bar.type = CategoryChart.TYPELINE3D;
            }
    		map.remove("Type");
    	}
        
    	// set the range
    	if (map.containsKey("XLabel")) {
    		bar.range = req.getParameter("XLabel");
        	map.remove("XLabel");
        }
    	// set the domain
        if (map.containsKey("YLabel")) {
        	bar.domain = req.getParameter("YLabel");
        	map.remove("YLabel");
        }
        
        // set the orientation
        if (map.containsKey("Orientation")) {
        	String orientation = req.getParameter("Orientation");
        	if (orientation.equalsIgnoreCase("V")) {
    			bar.orientation = PlotOrientation.VERTICAL;
    		} else if (orientation.equalsIgnoreCase("H")) {
    			bar.orientation = PlotOrientation.HORIZONTAL;
    		}
        	map.remove("Orientation");
        	//out.println("<br>Orientation : " + orientation);
        }
        
        // set the lower and upper range
        if (map.containsKey("Range")) {
        	String rangeArray[] = (String[]) req.getParameter("Range").split(",");
    		bar.lower = new Double(rangeArray[0]);
    		bar.upper = new Double(rangeArray[1]);
        	map.remove("Range");
        	//out.println("<br>Range : " + range);
        }
    	
    	// get the chart data from an XML file or...
        if (map.containsKey("XMLFile")) {
        	URL url = getClass().getResource(req.getParameter("XMLFile"));
            try
            {
                java.io.InputStream inputstream = url.openStream();
                bar.dataset = (DefaultCategoryDataset) DatasetReader.readCategoryDatasetFromXML(inputstream);
            }
            catch(IOException ioexception)
            {
                System.out.println(ioexception.getMessage());
            }
        	map.remove("XMLFile");
        } else {
        	// get the data
            for (Iterator<Entry<String, String[]>> it=map.entrySet().iterator(); it.hasNext(); ) {
            	Map.Entry entry = it.next();
            	String key = (String) entry.getKey();
            	//out.println("<br>Key : " + key);
            	String value[] = (String[]) entry.getValue();
            	for (int i=0;i != value.length;i++) {
            		String data[] = value[i].split(",");
            		bar.dataset.setValue(new Double(data[1]),key,data[0]);
            		//out.println("  Value : " + value[i] + "Data : " + data[0]);
            	}
            }
        }
    	
    	JFreeChart chart = bar.getChart();
    	return chart;
    }
    
    /**
     * Create XY chart method.
     * 
     * @param map which can consist of elements such as:
     * <ul>
     * <li> Type - the type of chart: either Bar, Step, Area, Line, Scatter, or Time.</li>
     * <li> Title - the title of the chart.</li>
     * <li> XLabel - the domain title.</li>
     * <li> YLabel - the range title.<li>
     * <li> Orientation - the orientation of the chart, either H or V.<li>
     * <li> Range - the upper and lower value of the range. Format is Range=lower,upper, e.g. Range=68,72.</li>
     * <li> data - the data to plot. Format is name=value&name=value, eg: Fish=70&Chips=66.</li>
     * </ul>
     * @param req which is the Http Servlet Request	
     * 
     * @return JFreeChart
     * @throws ServletException
     *
     */
    private JFreeChart createXY(Map<String, String[]> map, HttpServletRequest req)
    		throws ServletException {
    	XYChart xy = new XYChart();
    	// set the title for the Category Chart
    	if (map.containsKey("Title")) {
    		xy.title = req.getParameter("Title");
    		map.remove("Title");
    	}
    	
    	// what type? (default is Bar)
    	if (map.containsKey("Type")) {
    		String type = req.getParameter("Type");
    		if (type.equalsIgnoreCase("Bar")) {
            	xy.type = XYChart.TYPEBAR; 
            } else if (type.equalsIgnoreCase("Step")) {
            	xy.type = XYChart.TYPESTEPAREA;
            } else if (type.equalsIgnoreCase("Area")) {
            	xy.type = XYChart.TYPEAREA;
            } else if (type.equalsIgnoreCase("Line")) {
            	xy.type = XYChart.TYPELINE;
            } else if (type.equalsIgnoreCase("Scatter")) {
            	xy.type = XYChart.TYPESCATTER;
            //} else if (xyType.equalsIgnoreCase("Polar")) {
            	//xy.type = XYChart.TYPEPOLAR;
            } else if (type.equalsIgnoreCase("Time")) {
            	xy.type = XYChart.TYPETIME;
            }
    		map.remove("Type");
    	}
        
    	// set the range
    	if (map.containsKey("XLabel")) {
    		xy.xLabel = req.getParameter("XLabel");
        	map.remove("XLabel");
        }
    	// set the domain
        if (map.containsKey("YLabel")) {
        	xy.yLabel = req.getParameter("YLabel");
        	map.remove("YLabel");
        }
        
        // set the orientation
        if (map.containsKey("Orientation")) {
        	String orientation = req.getParameter("Orientation");
        	if (orientation.equalsIgnoreCase("V")) {
    			xy.orientation = PlotOrientation.VERTICAL;
    		} else if (orientation.equalsIgnoreCase("H")) {
    			xy.orientation = PlotOrientation.HORIZONTAL;
    		}
        	map.remove("Orientation");
        	//out.println("<br>Orientation : " + orientation);
        }
        
        // set the lower and upper range
        if (map.containsKey("Range")) {
        	String rangeArray[] = (String[]) req.getParameter("Range").split(",");
    		xy.lower = new Double(rangeArray[0]);
    		xy.upper = new Double(rangeArray[1]);
        	map.remove("Range");
        	//out.println("<br>Range : " + range);
        }
    	        	
        // get the data
        for (Iterator<Entry<String, String[]>> it=map.entrySet().iterator(); it.hasNext(); ) {
        	Map.Entry entry = it.next();
        	String key = (String) entry.getKey();
        	//out.println("<br>Key : " + key);
        	
        	String value[] = (String[]) entry.getValue();
        	if (xy.type == XYChart.TYPETIME) {
        		TimeSeries timeSeries = new TimeSeries(key,Second.class);
        		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        		Date date = null;
        		for (int i=0;i != value.length;i++) {
            		String data[] = value[i].split(",");
            		try {
            			date = sdf.parse(data[0]);
            		} catch (ParseException pe) {
            			throw new ServletException(pe.getMessage()); 
            		}
            		timeSeries.add(new Second(date),new Double(data[1]));
            		//out.println("  Value : " + value[i]);
        		}
        		xy.timeset.addSeries(timeSeries);
        	} else {
        		XYSeries xySeries = new XYSeries(key);
        		for (int i=0;i != value.length;i++) {
            		String data[] = value[i].split(",");
            		xySeries.add(new Double(data[0]),new Double(data[1]));
            		//out.println("  Value : " + value[i]);
            	}
        		xy.dataset.addSeries(xySeries);
        	}
        }
    	
    	JFreeChart chart = xy.getChart();
		return chart;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -