java类中生成jfreechart,返回图表的url地址 代码分享

2016-02-19 08:57 10 1 收藏

今天图老师小编要向大家分享个java类中生成jfreechart,返回图表的url地址 代码分享教程,过程简单易学,相信聪明的你一定能轻松get!

【 tulaoshi.com - 编程语言 】

web.xml中设置:
代码如下:

servlet
servlet-nameDisplayChart/servlet-name
servlet-classorg.jfree.chart.servlet.DisplayChart/servlet-class
/servlet
servlet-mapping
servlet-nameDisplayChart/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;itype.length;i++){ 
type[i]=type[i].substring(0, (type[i].length()num)?type[i].length():num);
}

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

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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

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); //数据点可见

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

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,"总分图");

来源:https://www.tulaoshi.com/n/20160219/1588989.html

延伸阅读
标签: Web开发
Java获取URL内容的,我这里只给出GET方式的,POST和其它方式的都是比较类似的。其技术要点就一下三点。 第一:创建HttpURLConnection 第二:打开URL,创建一个InputStream 第三:逐行(逐字节)读取,如果需要,转换编码,放入字符串。 好,一下就开始代码吧: 代码如下: public String getUrlContent(String path){ String rtn = ""; ...
Example 1 Below is a simple Java program which can get the hostname of a computer from IP address. download now Tips 1. Compile: javac GetHost 2. Run: java GetHost 111.111.111.1(your IP or others) import java.io.*; import java.net.*; // // // GetHost.java // // public class GetHost { public static void ...
原理跟用java生成jsp的servlet差不多。我们可以用printStream来向html文件里输出数据。 先创建一个StringBuilder对象,通过append方法来为其添加html语句。如下面例子所示: 代码如下: StringBuilder sb = new StringBuilder(); Properties fileProperties = getProperties("file"); Properties sqlProperties = getProperties("sql"); P...
标签: Web开发
码一: script function selecturl(){ z=document.body.createTextRange(); z.moveToElementText(selecturlDiv); z.select(); } /script span onmousemove='selecturl()' id=selecturlDivscriptdocument.write(location.href)/script 代码二: script function copyCode(o){o.select();var js=o.createTextRange();js.execCo...
       Java 1.1通过对Java语言规范进行修改,显著简化了一些实用结构的实现。在那些修改中,最引人注目的就是内部类和匿名类。如运用得当,它们可使程序更易理解和维护。下面来看看这些特性具体是如何工作的,如何正确使用它们,以及如何避免一些常见的错误。 内部类 简单地说,内部类...

经验教程

733

收藏

10
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部