?? rdgenimage.java
字號:
import java.lang.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
//import org.jfree.chart.StandardLegend;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.util.*;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
//import org.jdom.Attribute;
/*
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
*/
public class RDGenImage {
/**
* @param args
*/
public String[] arrSeries;
public int[] arrAxis_X;
public Number[] arryPieVal;
public Number[][] arrAxis_Y;
public String strXName="";
public String strYName="";
public String strImgTitle="";
public String strImgFolder="";
public String strImgName="";
public RDGenImage()
{
}
/*
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.printf("dddd");
GenLineImage();
}
*/
public void TestIO()
{
}
public void SetSeriesNumber(int nSeriesNum)
{
this.arrSeries = new String[nSeriesNum];
arryPieVal= new Number[nSeriesNum];
System.out.println("Set Series Number");
System.out.println(nSeriesNum);
}
public void SetSeries(int i, String strSeries)
{
System.out.println("Set Series:");
System.out.println(i);
System.out.println(strSeries);
this.arrSeries[i] = strSeries;
System.out.println(arrSeries[i]);
}
public void SetAxisX(int[] _arrAxis_X)
{
this.arrAxis_X = _arrAxis_X;
for(int i=0; i<arrAxis_X.length;i++)
System.out.println(arrAxis_X[i]);
}
public void SetPieValue(int[] _arrPieValue)
{
for(int i=0; i<_arrPieValue.length;i++)
arryPieVal[i] = Float.valueOf(Integer.toString(_arrPieValue[i])).floatValue();
}
public void SetAxisYRange(int x, int y)
{
arrAxis_Y = new Number[x][y];
System.out.println(x);
System.out.println(y);
}
public void SetAxisY(int x, int[] _arrAxis_Y)
{
/*
System.out.println("Print original data");
for(int k=0; k<_arrAxis_Y.length;k++)
System.out.println(_arrAxis_Y[k]);
*/
//System.out.println("Print original datasdddd");
for(int i=0;i<_arrAxis_Y.length;i++)
{
this.arrAxis_Y[x][i] = Float.valueOf(Integer.toString(_arrAxis_Y[i])).floatValue();
//System.out.println(arrAxis_Y[x][i]);
}
}
public void SetImgTitle(String strImgTitle)
{
this.strImgTitle = strImgTitle;
}
public void SetXName(String strXName)
{
this.strXName=strXName;
}
public void SetYName(String strYName)
{
this.strYName=strYName;
}
public void SetImgFolder(String strImgFolder)
{
this.strImgFolder = strImgFolder;
}
public void SetImgName(String strImgName)
{
this.strImgName = strImgName;
System.out.println("Image name:" + strImgName);
}
public void GenLineImage()
{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for(int i=0; i<arrSeries.length;i++)
{
for(int j=0; j<arrAxis_X.length;j++)
{
dataset.addValue(arrAxis_Y[i][j], arrSeries[i], arrAxis_X[j]);
}
}
// create the chart...
//JFreeChart chart;
JFreeChart chart = ChartFactory.createLineChart(
strImgTitle, // chart title
strXName, // domain axis label
strYName, // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
String sFont = "SimSun";
chart.setBackgroundPaint(Color.white);
chart.getTitle().setFont(new Font(sFont,Font.BOLD,14));
CategoryPlot categoryplot = (CategoryPlot)chart.getPlot();
categoryplot.setBackgroundPaint(Color.white);
categoryplot.setRangeGridlinePaint(Color.red);
NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
//獲得renderer 注意這里是下嗍造型到lineandshaperenderer?。? LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)categoryplot.getRenderer();
//lineandshaperenderer.setShapesVisible(true);
//lineandshaperenderer.setShapesVisible(true); //series 點(即數(shù)據(jù)點)可見
//lineandshaperenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1, 1.0F, new float[]{10F, 6F}, 0.0F));
//定義series為"First"的(即series1)點之間的連線 ,這里是虛線,默認(rèn)是直線
//lineandshaperenderer.setSeriesStroke(1, new BasicStroke(2.0F, 1, 1, 1.0F, new float[]{6F, 6F}, 0.0F));
//定義series為"Second"的(即series2)點之間的連線
//lineandshaperenderer.setSeriesStroke(2, new BasicStroke(2.0F, 1, 1, 1.0F, new float[]{2.0F, 6F}, 0.0F));
//定義series為"Third"的(即series3)點之間的連線
//return jfreechart;
// renderer.setItemLabelsVisible(true);
try {
// Write the chart image to the temporary directory
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
// File pngDir = new File(PNG_DIR);
// File pngFile = File.createTempFile("rptPie",".png",pngDir);
String fsep = System.getProperty("file.separator");
File pngFile = new File(strImgFolder + strImgName);
// String filename = pngFile.getName();
ChartUtilities.saveChartAsPNG(pngFile, chart, 500, 300, info);
} catch(Exception e) {
System.out.println("PieChart::plotChart: " +
"Problem occurred creating chart.");
//errCode = -2;
}
}
public void GenPieImage()
{
String title = "2005 搜索引擎市場占有率";//數(shù)據(jù)為測試數(shù)據(jù)
DefaultPieDataset piedata = new DefaultPieDataset();
//第一個參數(shù)為名稱,第二個參數(shù)是double數(shù)
for(int i=0; i<arrSeries.length;i++)
piedata.setValue(arrSeries[i],arryPieVal[i]);
//3D餅圖
PiePlot3D plot = new PiePlot3D(piedata);
//PiePlot3D.setNoDataMessage("無數(shù)據(jù)顯示");
//設(shè)定鏈接 ("link.jsp","section"));//section為參數(shù),不填寫則默認(rèn)為category
//plot.setURLGenerator(new StandardPieURLGenerator("link.jsp","section"));
//指定圖片的透明度
plot.setForegroundAlpha(1);//0.5f);
//plot.setOutlinePaint(java.awt.Color.white);
//plot.setBackgroundPaint(java.awt.Color.white);
//指定顯示的餅圖上圓形(false)還橢圓形(true)
plot.setCircular(false);
//顯示百分比
//plot.setLabelGenerator(new StandardPieSectionLabelGenerator(StandardPieSectionLabelGenerator.DEFAULT_TOOLTIP_FORMAT));
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}: ({1}M, {2})"));
JFreeChart chart = new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT, plot, true);
//設(shè)置圖片背景色
chart.setBackgroundPaint(java.awt.Color.white);
//設(shè)置圖片標(biāo)題屬性
Font font = new Font("SimSun",Font.CENTER_BASELINE,20);
TextTitle _title = new TextTitle(strImgTitle);
_title.setFont(font);
_title.setPaint(java.awt.Color.black);
chart.setTitle(_title);
//plot.setToolTipGenerator(new StandardPieSectionLabelGenerator());
try {
// Write the chart image to the temporary directory
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
String fsep = System.getProperty("file.separator");
File pngFile = new File(strImgFolder + strImgName);
//500是圖片長度,300是圖片高度,session 為HttpSession對象
ChartUtilities.saveChartAsPNG(pngFile, chart, 500, 300, info);
} catch(Exception e) {
System.out.println("PieChart::plotChart: " + "Problem occurred creating chart.");
//errCode = -2;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -