JFreeChart------------它是一種組件技術(shù),專用于在java中實(shí)現(xiàn)圖形報(bào)表
成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設(shè)、做網(wǎng)站與策劃設(shè)計(jì),東方網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:東方等地區(qū)。東方做網(wǎng)站價(jià)格咨詢:13518219792
----------餅圖,柱狀圖,折線圖
JFreeChart它是一種獨(dú)立圖表技術(shù),它與struts2本身并無(wú)關(guān)系,只不過(guò),它可以與struts2配合使用
餅圖的步驟:
1、創(chuàng)建一個(gè)web工程
2、導(dǎo)入struts2框架(core,struts2-jfreeChart)
3、向工程導(dǎo)入(jfreechart.jar,jcommon.jar)
4、創(chuàng)建BaseAction繼承于ActionSupport,并且,在類中,聲明一個(gè)屬性 JFreeChart chart;并且生成set,get方法
5、編寫ChartAction類,繼承于BaseAction
6、在ChartAction類中,編寫 showPie()用于顯示餅圖
7、在struts.xml配置當(dāng)前action
8、在index.jsp配置,通過(guò)img src="chart!showPie.action"進(jìn)入到指定方法
=================================================================================================================
9、編寫ChartDao類,模擬從數(shù)據(jù)庫(kù)查詢數(shù)據(jù)
//得到餅圖需要的數(shù)據(jù)集
public DefaultPieDataset getPieDataset(){
DefaultPieDataset dp = new DefaultPieDataset();
dp.setValue("聯(lián)想",321);
dp.setValue("華碩",189);
dp.setValue("戴爾",98);
dp.setValue("IBM",213);
dp.setValue("Apple",287);
dp.setValue("惠普",120);
dp.setValue("SONY",87);
return dp;
}
==============================================================================================================
10、在ChartAction中的showPie方法,生成餅圖
public String showPie(){
//得以要顯示的數(shù)據(jù)集,根據(jù)數(shù)據(jù),生成餅圖
DefaultPieDataset dp = dao.getPieDataset();
//chart = ChartFactory.createPieChart("標(biāo)題",要顯示在餅圖中的數(shù)據(jù)集,是否顯示顏色說(shuō)明,"是否顯示工具提示","是否顯示網(wǎng)絡(luò)地址 ");
chart = ChartFactory.createPieChart("一季度各電腦品牌銷售匯總",dp,true,false,false);
return "success";
}
=============================================================================================================
11、在struts.xml文件,進(jìn)行相關(guān)配置
package name="struts2" extends="struts-default"
result-types
result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"/
/result-types
action name="chart" class="org.java.web.ChartAction"
result name="success" type="chart"
param name="width"800/param
param name="height"600/param
/result
/action
/package
=================================================================================================================
12、解決亂碼
標(biāo)題區(qū)的亂碼
Font f1 = new Font("隸書",Font.BOLD,40);
TextTitle tt = new TextTitle("一季度各電腦品牌銷售匯總",f1);
chart.setTitle(tt);//綁定標(biāo)題
顏色提示區(qū)亂碼
Font f2 = new Font("隸書",Font.BOLD,20);
LegendTitle lt = chart.getLegend();
lt.setItemFont(f2);//設(shè)置該區(qū)域的字體
解決餅圖中的亂碼
PiePlot pp = (PiePlot) chart.getPlot();//得到餅圖區(qū)域
pp.setLabelFont(f2);//設(shè)置餅圖區(qū)域中的字體
=============================================================================================================
13、設(shè)置餅圖中要顯示的數(shù)據(jù)格式
//該對(duì)象,用于指定餅圖要顯示的數(shù)據(jù)格式 //0:key //1:value 2:百分比
StandardPieSectionLabelGenerator sc = new StandardPieSectionLabelGenerator("{0},{1}臺(tái),{2}");
//把顯示的格式,綁定餅圖中
pp.setLabelGenerator(sc);
需要完整版請(qǐng)聯(lián)系我
1.環(huán)境搭建
整個(gè)項(xiàng)目的結(jié)構(gòu)圖
2.編寫DetectFaceDemo.java,代碼如下:
[java] view plaincopy
package com.njupt.zhb.test;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;
//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
public class DetectFaceDemo {
public void run() {
System.out.println("\nRunning DetectFaceDemo");
System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());
// Create a face detector from the cascade file in the resources
// directory.
//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());
//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());
//注意:源程序的路徑會(huì)多打印一個(gè)‘/’,因此總是出現(xiàn)如下錯(cuò)誤
/*
* Detected 0 faces Writing faceDetection.png libpng warning: Image
* width is zero in IHDR libpng warning: Image height is zero in IHDR
* libpng error: Invalid IHDR data
*/
//因此,我們將第一個(gè)字符去掉
String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);
CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);
Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
// Draw a bounding box around each face.
for (Rect rect : faceDetections.toArray()) {
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}
// Save the visualized detection.
String filename = "faceDetection.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, image);
}
}
3.編寫測(cè)試類:
[java] view plaincopy
package com.njupt.zhb.test;
public class TestMain {
public static void main(String[] args) {
System.out.println("Hello, OpenCV");
// Load the native library.
System.loadLibrary("opencv_java246");
new DetectFaceDemo().run();
}
}
//運(yùn)行結(jié)果:
//Hello, OpenCV
//
//Running DetectFaceDemo
///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
//Detected 8 faces
//Writing faceDetection.png
完整代碼如下:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class Face extends JFrame ?{
/**
*
*/
private static final long serialVersionUID = 1L;
public Face(){
setSize(500, 500);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit()
.getScreenSize();
Dimension frameSize = getSize();
setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
setVisible(true);
}
//下面的是關(guān)鍵的繪圖代碼
public void paint(Graphics g){
//畫頭
g.drawOval(100, 50, 300, 400);
//畫眼睛
g.drawOval(140, 150, 100, 50);
g.drawOval(260, 150, 100, 50);
//畫鼻子
g.drawArc(140, 150, 100, 150, -90, 90);
g.drawArc(260, 150, 100, 150, 180, 90);
//畫嘴巴
g.drawOval(170, 320, 150, 50);
}
public static void main(String args[]){
new Face();
}
}
主要是用了幾個(gè)java的畫圖函數(shù),如果有用的話,希望采納