java類中生成jfreechart,返回圖表的url地址 代碼分享
web.xml中設(shè)置:
<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("無(wú)數(shù)據(jù)可供顯示!");
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("隸書(shū)",Font.BOLD, 12);
jfreechart.getLegend().setItemFont(font3);
CategoryAxis categoryaxis = categoryplot.getDomainAxis();
// categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryaxis.setMaximumCategoryLabelLines(10);//行數(shù),根據(jù)需要自己設(shè)
categoryaxis.setMaximumCategoryLabelWidthRatio(0.5f);//每行寬度,這里設(shè)一個(gè)漢字寬
NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
numberaxis.setRangeWithMargins(0, 3);
numberaxis.setUpperMargin(0.8);////設(shè)置最高的一個(gè) Item 與圖片頂端的距離
numberaxis.setUpperBound(3.5);//縱坐標(biāo)最大值
categoryaxis.setTickLabelFont(new Font("宋體", Font.BOLD, 12));
numberaxis.setTickLabelFont(new Font("隸書(shū)", 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));//設(shè)置曲線圖與xy軸的距離
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)categoryplot.getRenderer();
lineandshaperenderer.setShapesVisible(true); //數(shù)據(jù)點(diǎn)可見(jiàn)
lineandshaperenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
10F, 6F
}, 0.0F)); //定義series點(diǎn)之間的連線 ,這里是虛線,默認(rèn)是直線
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為對(duì)應(yīng)項(xiàng)目的路徑path,一般就是項(xiàng)目名稱
//jsp中這樣使用: String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
return graphURL;//返回生成圖片的地址
}
調(diào)用上述方法得到生成的chart的url:
getChart(stus,score_field,type,"總分圖");
相關(guān)文章
springboot實(shí)現(xiàn)用戶名查找用戶功能
本文主要介紹了springboot實(shí)現(xiàn)用戶名查找用戶功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
在java中由類名和方法名字符串實(shí)現(xiàn)其調(diào)用方式
這篇文章主要介紹了在java中由類名和方法名字符串實(shí)現(xiàn)其調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
spring boot 1.5.4 集成shiro+cas,實(shí)現(xiàn)單點(diǎn)登錄和權(quán)限控制
這篇文章主要介紹了spring boot 1.5.4 集成shiro+cas,實(shí)現(xiàn)單點(diǎn)登錄和權(quán)限控制,需要的朋友可以參考下2017-06-06
Trie樹(shù)(字典樹(shù))的介紹及Java實(shí)現(xiàn)
Trie樹(shù),又稱字典樹(shù)或前綴樹(shù),關(guān)于它的結(jié)構(gòu)就不詳細(xì)介紹了。Trie樹(shù)在單詞統(tǒng)計(jì)、前綴匹配等很多方面有很大用處。下面這篇文章主要介紹了Trie樹(shù),以及Java實(shí)現(xiàn)如何Trie樹(shù),有需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-02-02

