程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java類中生成jfreechart,前往圖表的url地址 代碼分享

java類中生成jfreechart,前往圖表的url地址 代碼分享

編輯:關於JAVA

java類中生成jfreechart,前往圖表的url地址 代碼分享。本站提示廣大學習愛好者:(java類中生成jfreechart,前往圖表的url地址 代碼分享)文章只能為提供參考,不一定能成為您想要的結果。以下是java類中生成jfreechart,前往圖表的url地址 代碼分享正文


web.xml中設置:

<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet >
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/DisplayChart</url-pattern>
</servlet-mapping>

java類中辦法:

public String getChart(String series[],double score[][],String type[],String name){
final int num=8;
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
for(int i=0;i<type.length;i++){ 
type[i]=type[i].substring(0, (type[i].length()<num)?type[i].length():num);
}

for(int j=0;j<series.length;j++){
int i=0;
for( i=0;i<type.length;i++){
defaultcategorydataset.addValue(score[j][i], series[j], type[i]); 
}
}

JFreeChart jfreechart = ChartFactory.createLineChart(name,null,null,defaultcategorydataset,PlotOrientation.VERTICAL,true,true,false);

jfreechart.getLegend().setPosition(RectangleEdge.RIGHT);

jfreechart.setBackgroundPaint(Color.white);

CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
categoryplot.setNoDataMessage("有數據可供顯示!");
categoryplot.setBackgroundPaint(Color.white);
categoryplot.setDomainGridlinesVisible(true);
categoryplot.setRangeGridlinesVisible(true);
categoryplot.setRangeGridlinePaint(Color.gray);
categoryplot.setDomainGridlinePaint(Color.gray);
categoryplot.setBackgroundAlpha(0.8f);
Font font1 = new Font("黑體",Font.BOLD, 14);
jfreechart.getTitle().setFont(font1);

Font font3 = new Font("隸書",Font.BOLD, 12);
jfreechart.getLegend().setItemFont(font3);

CategoryAxis categoryaxis = categoryplot.getDomainAxis();
//  categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryaxis.setMaximumCategoryLabelLines(10);//行數,依據須要本身設
categoryaxis.setMaximumCategoryLabelWidthRatio(0.5f);//每行寬度,這裡設一個漢字寬

NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
numberaxis.setRangeWithMargins(0, 3);

numberaxis.setUpperMargin(0.8);////設置最高的一個 Item 與圖片頂真個間隔
numberaxis.setUpperBound(3.5);//縱坐標最年夜值

categoryaxis.setTickLabelFont(new Font("宋體", Font.BOLD, 12));
numberaxis.setTickLabelFont(new Font("隸書", Font.BOLD, 12));
Font font2 = new Font("SimSun", Font.BOLD, 16);
categoryaxis.setLabelFont(font2);
numberaxis.setLabelFont(font2);
categoryplot.setAxisOffset(new RectangleInsets(0D, 0D,0D, 10D));//設置曲線圖與xy軸的間隔
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)categoryplot.getRenderer();
lineandshaperenderer.setShapesVisible(true); //數據點可見

lineandshaperenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
10F, 6F
}, 0.0F)); //界說series點之間的連線 ,這裡是虛線,默許是直線
lineandshaperenderer.setSeriesStroke(1, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
6F, 6F
}, 0.0F));

lineandshaperenderer.setBaseItemLabelsVisible(true);
lineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());

ChartRenderingInfo info=new ChartRenderingInfo(new StandardEntityCollection());
String fileName = null;
try
{
fileName = ServletUtilities.saveChartAsPNG(jfreechart, 700,300, info, null);//生成圖片
}
catch (IOException e)
{
e.printStackTrace();
}

String graphURL = "/projectname/DisplayChart?filename=" + fileName; //projectname為對應項目標途徑path,普通就是項目稱號

//jsp中如許應用: String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
return graphURL;//前往生成圖片的地址
}

挪用上述辦法獲得生成的chart的url:

   getChart(stus,score_field,type,"總分圖");

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved