1、Excel 几种简单常用的图表1、 柱状图(3D 柱状图)doubledata=new double,;/柱状图数据Stringrows=new String“,“,“; /柱状图表示的含义Stringcolumns=new String“,“,“; /柱状图所区分的名字CategoryDataset dataset=DatasetUtilities.createCategoryDataset(rows, columns, data); /创建普通柱状图JFreeChart chart=ChartFactory.createBarChart(chartTitle, xName, yName,
2、dataset,PlotOrientation.VERTICAL,true,false,false);/创建3D 柱状图/*JFreeChart jc=ChartFactory.createBarChart3D(chartTitle,xName,yName, dataset, PlotOrientation.VERTICAL,true,false,false);*/设置图表字体是否模糊(true-模糊,false-不模糊 )chart.setAntiAlias(false);/图片背景色chart.setBackgroundPaint(Color.white);/设置图标题的字体Font fo
3、nt=new Font(“隶书 “,Font.BOLD,25);/重新设置titleTextTitle title=new TextTitle(chartTitle);title.setFont(font);chart.setTitle(title);/为图表设置柱子CategoryPlot plot=chart.getCategoryPlot();/设置柱子横虚线可见plot.setRangeGridlinesVisible(true);/设置柱子虚线颜色plot.setRangeGridlinePaint(Color.gray);/数据轴精度NumberAxis na=(NumberAxi
4、s)plot.getRangeAxis();/数据轴数据标签的显示格式DecimalFormat df=new DecimalFormat(“#0.00“);na.setNumberFormatOverride(df);/设置X、Y 轴标签字体Font labelFont=new Font(“SansSerif“,Font.TRUETYPE_FONT,12);/X轴设置CategoryAxis domainAxis=plot.getDomainAxis();domainAxis.setLabelFont(labelFont);domainAxis.setTickLabelFont(labelF
5、ont);/横轴上的Label 是否完全显示domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);/设置图片左端距离domainAxis.setLowerMargin(0.1);/设置图片右端距离domainAxis.setUpperMargin(0.1);plot.setDomainAxis(domainAxis);/设置柱图背景色plot.setBackgroundPaint(new Color(255,255,204);/Y轴ValueAxis rangeAxis=plot.getRangeAxis();rangeAxis.setLabel
6、Font(labelFont);rangeAxis.setTickLabelFont(labelFont);/设置最高的一个Item与图片顶端的距离rangeAxis.setUpperMargin(0.15);/设置最低的一个Item与图片底端的距离rangeAxis.setLowerMargin(0.15);plot.setRangeAxis(rangeAxis);/柱子的具体设置BarRenderer renderer=new BarRenderer();/设置柱子宽度renderer.setMaximumBarWidth(0.05);/设置柱子高度renderer.setMinimumB
7、arLength(0.2);/设置柱子边框颜色renderer.setBaseOutlinePaint(Color.black);/设置柱子颜色/*renderer.setSeriesPaint(0, new Color(204,255,255);renderer.setSeriesPaint(1, new Color(153,204,255);renderer.setSeriesPaint(2, new Color(51,204,204);*/设置每个地区所包含的平行柱之间的距离renderer.setItemMargin(0.0);/显示每个柱子的数值并修改该数值的字体属性renderer
8、.setIncludeBaseInRange(true);renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator();renderer.setBaseItemLabelsVisible(true);plot.setRenderer(renderer);/设置柱的透明度plot.setForegroundAlpha(1.0f);chart.getLegend().setItemFont(new Font(“黑体“, Font.BOLD, 15);2、 堆积柱状图(3D 堆积柱状图 )doubledata=
9、new double,;/柱状图数据Stringrows=new String“,“,“; /柱状图表示的含义Stringcolumns=new String“,“,“; /柱状图所区分的名字CategoryDataset dataset=DatasetUtilities.createCategoryDataset(rows, columns,data);JFreeChart chart=ChartFactory.createStackedBarChart(chartTitle,xName, yName,dataset,PlotOrientation.VERTICAL,true,false,f
10、alse);/图表字体清晰chart.setAntiAlias(false);/图表背景颜色chart.setBorderPaint(Color.WHITE);/设置图表标题chart.setTitle(new TextTitle(chartTitle,new Font(“隶书,Font.BOLD,25);/X、Y轴字体Font labelFont=new Font(“SansSerif“,Font.TRUETYPE_FONT,12);CategoryPlot plot=chart.getCategoryPlot();/设置横虚线可见plot.setRangeGridlinesVisible(
11、true);/设置横虚线色彩plot.setRangeGridlinePaint(Color.gray);/数据轴精度NumberAxis na=(NumberAxis)plot.getRangeAxis();/设置最大值是1na.setUpperBound(1);/设置数据格式是百分比DecimalFormat df=new DecimalFormat(“0.00%“);na.setNumberFormatOverride(df);/X轴CategoryAxis domainAxis=plot.getDomainAxis();domainAxis.setLabelFont(labelFont
12、);domainAxis.setTickLabelFont(labelFont);/横坐标的label 是否完全显示domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);plot.setDomainAxis(domainAxis);/Y轴ValueAxis va=plot.getRangeAxis();va.setLabelFont(labelFont);va.setTickLabelFont(labelFont);va.setUpperMargin(0.15);va.setLowerMargin(0.15);plot.setRangeAxis(
13、va);/Renderer图形绘制单元StackedBarRenderer renderer=new StackedBarRenderer();/设置柱子宽度renderer.setMaximumBarWidth(0.05);/设置柱子的高度renderer.setMinimumBarLength(0.1);/设置柱子的边框颜色renderer.setBaseOutlinePaint(Color.BLACK);/设置柱子的边框可见renderer.setDrawBarOutline(true);/设置柱的颜色/*renderer.setSeriesPaint(0, new Color(204,
14、255,204);renderer.setSeriesPaint(1, new Color(255,204,153);*/设置每个地区所包含的平行柱之间的距离renderer.setItemMargin(0.4);plot.setRenderer(renderer);chart.getLegend().setItemFont(new Font(“黑体“, Font.BOLD, 15);3、 饼图(3D 饼图)doubledata=,;Stringkeys=“,“,“;DefaultPieDataset dataset=new DefaultPieDataset();if(data!=nulli
15、data.length;i+)dataset.setValue(keysi, datai);/*DefaultPieDataset dataset = new DefaultPieDataset();dataset.setValue(“, new Double();dataset.setValue(“, new Double();.dataset.setValue(“, new Double();*/JFreeChart chart = ChartFactory.createPieChart(chartTitle, dataset, true, true,false);/3D饼图/*JFree
16、Chart jc=ChartFactory.createPieChart3D(chartTitle, dataset, true,true,false);*/设置图表字体是否模糊chart.setTextAntiAlias(false);/图片背景色chart.setBackgroundPaint(Color.white);/设置图标题的字体Font font=new Font(“隶书 “,Font.BOLD,25);/重新设置titleTextTitle title=new TextTitle(chartTitle);title.setFont(font);chart.setTitle(ti
17、tle);PiePlot plot=(PiePlot)chart.getPlot();/指定饼图轮廓线的颜色plot.setBaseSectionOutlinePaint(Color.black);plot.setBaseSectionPaint(Color.black);/设置无数据时的信息plot.setNoDataMessage(“无对应数据,请重新查询!“);/设置无数据时的信息显示颜色plot.setNoDataMessagePaint(Color.RED);/图片中显示百分比:自定义方式,0表示选项,1表示数值,2表示所占比例,小数点后两位plot.setLabelGenerato
18、r(new StandardPieSectionLabelGenerator(“0=1(2)“,NumberFormat.getNumberInstance(),new DecimalFormat(“0.00%“);plot.setLabelFont(new Font(“SansSerif“,Font.TRUETYPE_FONT,12);/指定图片透明度/plot.setForegroundAlpha(0.065f);/指示显示的饼图是圆形(false)还是椭圆形(true)plot.setCircular(false,true);/设置饼块section开始的位置, 默认是12点钟方向plo
19、t.setStartAngle(90);/设置分饼颜色/*plot.setSectionPaint(keys0,new Color(224,204,22);plot.setSectionPaint(keys1,new Color(11,159,33);*/chart.getLegend().setItemFont(new Font(“黑体“,Font. BOLD,15);4、 折线图(3D 折线图)doubledata=new double,;/柱状图数据Stringrows=new String“,“,“; /柱状图表示的含义Stringcolumns=new String“,“,“; /柱
20、状图所区分的名字CategoryDataset dataset=DatasetUtilities.createCategoryDataset(rows, columns,data);/普通折线图JFreeChart chart=ChartFactory.createLineChart(chartTitle,xName,yName, dataset, PlotOrientation.VERTICAL, true,false,false);/3D折线图/*JFreeChart jc=ChartFactory.createLineChart3D(chartTitle, xName,yName, da
21、taset, PlotOrientation.VERTICAL, true, false,false);*/设置图表字体是否模糊chart.setAntiAlias(false);/设置图表背景颜色chart.setBackgroundPaint(Color.white);/设置图标题的字体Font font=new Font(“隶书 “,Font.BOLD,25);/重新设置titleTextTitle title=new TextTitle(chartTitle);title.setFont(font);chart.setTitle(title);CategoryPlot plot=(Ca
22、tegoryPlot)chart.getPlot();/设置X、Y 轴显示字体Font labelFont=new Font(“SansSerif“,Font.TRUETYPE_FONT,12);/X轴/分类轴网格是否可见plot.setDomainGridlinesVisible(true);/Y轴/数据轴网格是否可见plot.setRangeGridlinesVisible(true);/虚线色彩plot.setRangeGridlinePaint(Color.WHITE);plot.setDomainGridlinePaint(Color.WHITE);CategoryAxis doma
23、inAxis=plot.getDomainAxis();/轴标题domainAxis.setLabelFont(labelFont);domainAxis.setTickLabelFont(labelFont);/横轴上的label45度倾斜domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);/设置距离图片左端的距离domainAxis.setLowerMargin(0.0);/设置距离图片右端的距离domainAxis.setUpperMargin(0.0);NumberAxis numberAxis=(Num
24、berAxis)plot.getRangeAxis();numberAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits();numberAxis.setAutoRangeIncludesZero(true);/轴标题numberAxis.setLabelFont(labelFont);numberAxis.setTickLabelFont(labelFont);/获得renderLineAndShapeRenderer lasr=(LineAndShapeRenderer)plot.getRenderer();lasr.setBaseShapesVisible(true);lasr.setBaseLinesVisible(true);/设置图表字体chart.getLegend().setItemFont(new Font(“黑体“, Font.BOLD, 15);5、 甘特图