「java多组数据柱状图」多组数据并列柱状图

博主:adminadmin 2022-11-28 18:17:07 53

今天给各位分享java多组数据柱状图的知识,其中也会对多组数据并列柱状图进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

怎么用java做柱形图!!!

首先要有jfreechart.jar和jcommon-1.0.12.jar两个包然后在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

最后是jsp代码:

%@ page contentType="text/html;charset=GBK"%

%@ page import="org.jfree.chart.ChartFactory,

org.jfree.chart.JFreeChart,

org.jfree.chart.plot.PlotOrientation,

org.jfree.chart.servlet.ServletUtilities,

org.jfree.data.category.CategoryDataset,

org.jfree.data.general.DatasetUtilities"%

%

double[][] data = new double[][] {{1310}, {720}, {1130}, {440}};

String[] rowKeys = {"猪肉", "牛肉","鸡肉", "鱼肉"};

String[] columnKeys = {""};

CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);

JFreeChart chart = ChartFactory.createBarChart3D("广州肉类销量统计图", "肉类",

"销量",

dataset,

PlotOrientation.VERTICAL,

true,

false,

false);

String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);

String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;

%

img src="%= graphURL %"width=500 height=300 border=0 usemap="#%= filename %"

怎么用java的相关类去画一个柱状图

可以使用java第三方JFreeChart的api:具体

import java.awt.Color;

import java.awt.GradientPaint;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.CategoryAxis;

import org.jfree.chart.axis.CategoryLabelPositions;

import org.jfree.chart.axis.NumberAxis;

import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.chart.renderer.category.BarRenderer;

import org.jfree.chart.renderer.category.BarRenderer3D;

import org.jfree.data.category.CategoryDataset;

import org.jfree.data.category.DefaultCategoryDataset;

import org.jfree.data.general.DatasetUtilities;

/**

* 柱状图测试

* @author xjh

*

*/

public class BarChartTest {

/**

* 得到2D柱状图的数据集合

*

* @return

*/

private CategoryDataset getDataset2D() {

String str1 = "First";

String str2 = "Second";

String str3 = "Third";

String str4 = "Category 1";

String str5 = "Category 2";

String str6 = "Category 3";

String str7 = "Category 4";

String str8 = "Category 5";

DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();

localDefaultCategoryDataset.addValue(1.0D, str1, str4);

localDefaultCategoryDataset.addValue(4.0D, str1, str5);

localDefaultCategoryDataset.addValue(3.0D, str1, str6);

localDefaultCategoryDataset.addValue(5.0D, str1, str7);

localDefaultCategoryDataset.addValue(5.0D, str1, str8);

localDefaultCategoryDataset.addValue(5.0D, str2, str4);

localDefaultCategoryDataset.addValue(7.0D, str2, str5);

localDefaultCategoryDataset.addValue(6.0D, str2, str6);

localDefaultCategoryDataset.addValue(8.0D, str2, str7);

localDefaultCategoryDataset.addValue(4.0D, str2, str8);

localDefaultCategoryDataset.addValue(4.0D, str3, str4);

localDefaultCategoryDataset.addValue(3.0D, str3, str5);

localDefaultCategoryDataset.addValue(2.0D, str3, str6);

localDefaultCategoryDataset.addValue(3.0D, str3, str7);

localDefaultCategoryDataset.addValue(6.0D, str3, str8);

return localDefaultCategoryDataset;

}

/**

* 创建2D柱状图

*

* @param paramCategoryDataset

* @return

*/

public JFreeChart createBarChart2D() {

//得到2D柱状图的数据集合

CategoryDataset paramCategoryDataset = getDataset2D();

//创建柱状图

JFreeChart localJFreeChart = ChartFactory.createBarChart(

"Bar Chart Demo", "Category", "Value", paramCategoryDataset,

PlotOrientation.VERTICAL, true, true, false);

//设置柱状图属性

localJFreeChart.setBackgroundPaint(Color.white);

//获取柱状图的标注点集合

CategoryPlot localCategoryPlot = localJFreeChart.getCategoryPlot();

//设置标注点的背景色

localCategoryPlot.setBackgroundPaint(Color.lightGray);

//设置绘制区域的方格线的颜色

localCategoryPlot.setDomainGridlinePaint(Color.white);

//是方格线可见

localCategoryPlot.setDomainGridlinesVisible(true);

//设置方格线变动绘制颜色

localCategoryPlot.setRangeGridlinePaint(Color.white);

//得到数值坐标轴

NumberAxis localNumberAxis = (NumberAxis) localCategoryPlot

.getRangeAxis();

//设置坐标轴单元

localNumberAxis.setStandardTickUnits(NumberAxis

.createIntegerTickUnits());

//得到柱状图渲染器

BarRenderer localBarRenderer = (BarRenderer) localCategoryPlot

.getRenderer();

//如果超出绘制区域边界将不可见

localBarRenderer.setDrawBarOutline(false);

//创建倾斜度对象

GradientPaint localGradientPaint1 = new GradientPaint(0.0F, 0.0F,

Color.blue, 0.0F, 0.0F, new Color(0, 0, 64));

GradientPaint localGradientPaint2 = new GradientPaint(0.0F, 0.0F,

Color.green, 0.0F, 0.0F, new Color(0, 64, 0));

GradientPaint localGradientPaint3 = new GradientPaint(0.0F, 0.0F,

Color.red, 0.0F, 0.0F, new Color(64, 0, 0));

//为渲染器设置倾斜度

localBarRenderer.setSeriesPaint(0, localGradientPaint1);

localBarRenderer.setSeriesPaint(1, localGradientPaint2);

localBarRenderer.setSeriesPaint(2, localGradientPaint3);

//旋转坐标轴

CategoryAxis localCategoryAxis = localCategoryPlot.getDomainAxis();

localCategoryAxis.setCategoryLabelPositions(CategoryLabelPositions

.createUpRotationLabelPositions(0.5235987755982988D));

return localJFreeChart;

}

/**

* 创建3D柱状图

*

* @return

*/

public JFreeChart createBarChart3D() {

// 设置坐标点

double[][] arrayOfDouble = { { 10.0D, 4.0D, 15.0D, 14.0D },

{ -5.0D, -7.0D, 14.0D, -3.0D }, { 6.0D, 17.0D, -12.0D, 7.0D },

{ 7.0D, 15.0D, 11.0D, 0.0D }, { -8.0D, -6.0D, 10.0D, -9.0D },

{ 9.0D, 8.0D, 0.0D, 6.0D }, { -10.0D, 9.0D, 7.0D, 7.0D },

{ 11.0D, 13.0D, 9.0D, 9.0D }, { -3.0D, 7.0D, 11.0D, -10.0D } };

// 创建图表的数据集合,第一个参数:横坐标的文字说明;第二个参数:纵坐标的文字说明;第三个参数:坐标数组

CategoryDataset paramCategoryDataset = DatasetUtilities

.createCategoryDataset("Series ", "Category ", arrayOfDouble);

// 创建3D图表

JFreeChart localJFreeChart = ChartFactory.createBarChart3D(

"3D Bar Chart Demo", "Category", "Value", paramCategoryDataset,

PlotOrientation.VERTICAL, true, true, false);

// 得到横坐标点集合对象

CategoryPlot localCategoryPlot = localJFreeChart.getCategoryPlot();

// 设置方格可见

localCategoryPlot.setDomainGridlinesVisible(true);

// 得到坐标轴

CategoryAxis localCategoryAxis = localCategoryPlot.getDomainAxis();

// 设置标注点位置

localCategoryAxis.setCategoryLabelPositions(CategoryLabelPositions

.createUpRotationLabelPositions(0.3926990816987241D));

// 创建3D柱状图渲染对象

BarRenderer3D localBarRenderer3D = (BarRenderer3D) localCategoryPlot

.getRenderer();

// 设置超出边界不可见

localBarRenderer3D.setDrawBarOutline(false);

// 返回图表对象

return localJFreeChart;

}

}

java怎么生成excel柱状图?

如果是柱状图之类的表示 是在 excel中生成的 那么你可以考虑做个excel模版引用数据利用excel内的机制自动生成想要的图,而数据可以用java利用poi的相关类 往模版里面写数据。就知道这么多 就给你说这么多吧

java 导出Excel数据如何生成图表,如柱状图,折线图

我以前做的是利用jfreechart生成图片,导出excel的时候把图片放进去。

java如何读取excel中报表(柱状图)的信息

public class Report {

/**作用:报表样式的枚举,该类的getvalue方法返回对应值。br

* Columns_2DVer:2D柱状垂直报表。br

* Columns_3DVer:3D柱状垂直报表。br

* Columns_2DHor:2D柱状水平报表。br

* Columns_3DHor:3D柱状水平报表。

* */

public enum ReportType{

Columns_2DVer("Z2DV"),

Columns_3DVer("Z3DV"),

Columns_2DHor("Z2DH"),

Columns_3DHor("Z3DH"),

LineReport_2D("Line2D"),

LineReport_3D("Line3D"),

PieReport_2D("Pie2D"),

PieReport_3D("Pie3D");

private ReportType(String a){

this.str=a;

}

private String str;

public String getvalue(){

return this.str;

}

}

/**作用:作为创建报表时的参数。br

* 成员1:报表的标题。br

* 成员2:报表的横坐标标题。br

* 成员3:报表的纵坐标标题。br

* 成员4:JSP页面的request对象。br

* 成员5:报表所保存的文件名。br

* 成员6:报表的长度。br

* 成员7:报表的宽度。br

* 成员8:报表的背景颜色,默认为白色。br

* 成员9:报表网格中竖线的颜色,默认为黑色。br

* 成员10:报表网格中横线的颜色,默认为黑色。br

* 成员11:报表横轴内容的数组。br

* 成员12:报表纵轴内容的list数组。br

* 成员13:提示信息。如果写入多个提示信息,报表就变成多柱状。br

* 成员14:报表的模式。详见reportType枚举类br

* 成员15:饼状图专用的数值数组。数组的和必须等于1.br

* 注意1:要在JSP页面引入该类型才能使用,横纵轴数组的长度必须一致。br

* 注意2:如果在提示信息数组中写入了多个提示信息,那么报表纵轴内容的

* list中就必须存放入和它数量一致的int数组。否则会出错。

* */

public class ReportClass{

public String ReportTitle;

public String xTitle;

public String yTitle;

public HttpServletRequest request;

public String filename;

public int width;

public int height;

public Color BackgroundColor=Color.WHITE;

public Color ylineColor=Color.BLACK;

public Color xlineColor=Color.BLACK;

public String[] xValues;

public ArrayListint[] yValue=new ArrayListint[]();

public String[] helpstr;

public String reportType;

public double[] PieValue;

}

/**作用:创建一个指定类型和数据的图表。br

* 参数1:ReportClass类型,各成员具体作用参见ReportClass说明br

* 返回值:String类型,在JSP页面可以直接out.println显示图形。br

* 注意1:ReportClass类型中,必须设置的成员主要有:br

* 1、ReportTitle 2、request 3、filename 4、width 5、height 6、reportTypebr

* 注意2:如果要生成饼状图,还需要设置:br

* 1、xValues 2、PieValue 这两个数组的长度必须一致br

* 注意3:如果要生成柱状图或折线图,还需要设置:br

* 1、xValues 2、yValue 3、helpstrbr

* yvalue数组总和必须等于1.即100%.br

* 如果要生成多柱状或折线图,则需要设置helpstr长度。br

* yvalue列表的长度必须和helpstr数组长度一致。br

* yvalue中的成员数组的长度必须和xvalue数组长度一致。

* */

public String CreateReport(ReportClass rc){

String s="sf";

String str="";

JFreeChart jc=null;

Font titlefont=new Font("宋体",Font.BOLD,20);

Font tickfont=new Font("宋体",0,15);

Font labelfont=new Font("宋体",Font.BOLD,15);

DefaultCategoryDataset ds=null;

DefaultPieDataset pds=null;

if (rc.ReportTitle!=null){

if (rc.reportType.indexOf("Pie")!=-1){//饼状图

pds=new DefaultPieDataset();

double[] ob=rc.PieValue;

for (int i=0;iob.length;i++){

pds.setValue(rc.xValues[i], ob[i]);

}

if (rc.ReportTitle!=null){

if (rc.reportType.equals("Pie2D")){

jc=ChartFactory.createPieChart(rc.ReportTitle,pds,true, true, false);

}

else if (rc.reportType.equals("Pie3D")){

jc=ChartFactory.createPieChart3D(rc.ReportTitle,pds,true, true, false);

}

jc.getTitle().setFont(titlefont);

jc.getLegend().setItemFont(labelfont);

jc.setBackgroundPaint(rc.BackgroundColor);//总背景色

jc.setBorderPaint(rc.BackgroundColor);

PiePlot plot=(PiePlot)jc.getPlot();

plot.setBackgroundPaint(rc.BackgroundColor);

plot.setLabelFont(new Font("宋体",0,15));

}

}

else { //柱状或折线图

ds=new DefaultCategoryDataset();

for (int i=0;irc.helpstr.length;i++){

int[] ob=rc.yValue.get(i);

for (int j=0;job.length;j++){

ds.addValue(ob[j], rc.helpstr[i], rc.xValues[j]);

}

}

if (rc.reportType.indexOf("Z2D")!=-1){

PlotOrientation po=null;

if (rc.reportType.indexOf("V")!=-1){

po=PlotOrientation.VERTICAL;

}

else if (rc.reportType.indexOf("H")!=-1){

po=PlotOrientation.HORIZONTAL;

}

jc=ChartFactory.createBarChart(rc.ReportTitle, rc.xTitle, rc.yTitle,

ds, po, true, true, false);

}

else if (rc.reportType.indexOf("Z3D")!=-1){

PlotOrientation po=null;

if (rc.reportType.indexOf("V")!=-1){

po=PlotOrientation.VERTICAL;

}

else if (rc.reportType.indexOf("H")!=-1){

po=PlotOrientation.HORIZONTAL;

}

jc=ChartFactory.createBarChart3D(rc.ReportTitle, rc.xTitle, rc.yTitle,

ds, po, true, true, false);

}

else if (rc.reportType.equals("Line2D")){

PlotOrientation po=PlotOrientation.VERTICAL;

jc=ChartFactory.createLineChart(rc.ReportTitle, rc.xTitle, rc.yTitle,

ds, po, true, true, false);

}

else if (rc.reportType.equals("Line3D")){

PlotOrientation po=PlotOrientation.VERTICAL;

jc=ChartFactory.createLineChart3D(rc.ReportTitle, rc.xTitle, rc.yTitle,

ds, po, true, true, false);

}

jc.getTitle().setFont(titlefont);

jc.getLegend().setItemFont(labelfont);

jc.setBackgroundPaint(rc.BackgroundColor);//总背景色

CategoryPlot cp=jc.getCategoryPlot();

cp.setBackgroundPaint(rc.BackgroundColor);//图形框架背景色

cp.setDomainGridlinePaint(rc.ylineColor);//图形背景网格中竖线的颜色

cp.setDomainGridlinesVisible(true);

cp.setRangeGridlinePaint(rc.xlineColor);//图形背景网格中横线的颜色

cp.getDomainAxis().setTickLabelFont(tickfont);

cp.getDomainAxis().setLabelFont(labelfont);

cp.getRangeAxis().setTickLabelFont(tickfont);

cp.getRangeAxis().setLabelFont(labelfont);

}

try {

File ff=new File(rc.request.getRealPath("/")+"file");

if (!ff.exists()){

ff.mkdir();

}

File file=new File(rc.request.getRealPath("/")+"file/"+rc.filename);

if (!file.exists()){

file.createNewFile();

}

ChartUtilities.saveChartAsJPEG(file, jc, rc.width, rc.height);

str="img src='"+file.getPath()+"'";

return str;

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

return null;

}

}

else {

return null;

}

}

}

以前写的一个报表的集成,可以生成任何形式的报表,要引入JFreeChar.你可以慢慢研究下...

怎样用JAVA来实现在网页中制作柱状图

JFreeChart是JAVA平台上的一个开放的图表绘制类库。它完全使用JAVA语言编写,是为applications, applets, servlets 以及JSP等使用所设计。JFreeChart可生成饼图(pie charts)、柱状图(bar charts)、散点图(scatter plots)、时序图(time series)、甘特图(Gantt charts)等等多种图表,并且可以产生PNG和JPEG格式的输出,还可以与PDF和EXCEL关联。

JFreeChart的主页地址为:

在这里可以找到最新版本的JFreeChart的相关信息,如说明文档、下载连接以及示例图表等。

JFreeChart目前是最好的java图形解决方案,基本能够解决目前的图形方面的需求。

IBM文档:

Javaeye社区:

java多组数据柱状图的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于多组数据并列柱状图、java多组数据柱状图的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-28,除非注明,否则均为首码项目网原创文章,转载请注明出处。