java饼状图_java图形(柱形图,折线图,饼状图)

/** To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.*/

packaget2;importjava.awt.Color;importjava.awt.FlowLayout;importjava.awt.Font;importjava.io.File;importjava.io.FileInputStream;importjava.text.SimpleDateFormat;importjava.util.Vector;importjavax.swing.JFrame;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;importorg.apache.poi.xslf.usermodel.BarChartDemo;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;importorg.jfree.chart.ChartPanel;importorg.jfree.chart.ChartFactory;importorg.jfree.chart.ChartFrame;importorg.jfree.chart.JFreeChart;importorg.jfree.chart.StandardChartTheme;importorg.jfree.chart.axis.CategoryAxis;importorg.jfree.chart.axis.CategoryLabelPositions;importorg.jfree.chart.axis.DateAxis;importorg.jfree.chart.axis.DateTickUnit;importorg.jfree.chart.axis.NumberAxis;importorg.jfree.chart.axis.ValueAxis;importorg.jfree.chart.labels.ItemLabelAnchor;importorg.jfree.chart.labels.ItemLabelPosition;importorg.jfree.chart.labels.StandardXYItemLabelGenerator;importorg.jfree.chart.plot.CategoryPlot;importorg.jfree.chart.plot.PlotOrientation;importorg.jfree.chart.plot.XYPlot;importorg.jfree.chart.renderer.category.LineAndShapeRenderer;importorg.jfree.chart.renderer.xy.XYItemRenderer;importorg.jfree.chart.renderer.xy.XYLineAndShapeRenderer;importorg.jfree.chart.title.TextTitle;importorg.jfree.data.category.CategoryDataset;importorg.jfree.data.category.DefaultCategoryDataset;importorg.jfree.data.general.DefaultPieDataset;importorg.jfree.data.time.TimeSeries;importorg.jfree.data.xy.XYDataset;importorg.jfree.data.xy.XYSeries;importorg.jfree.data.xy.XYSeriesCollection;importorg.jfree.ui.TextAnchor;/***

*@authorLenovo*/

public class t2 extendsjavax.swing.JFrame {/*** Creates new form t2*/

publict2() {

initComponents();

}/*** This method is called from within the constructor to initialize the form.

* WARNING: Do NOT modify this code. The content of this method is always

* regenerated by the Form Editor.*/@SuppressWarnings("unchecked")//

private voidinitComponents() {

jLabel1= newjavax.swing.JLabel();

jScrollPane1= newjavax.swing.JScrollPane();

table= newjavax.swing.JTable();

jButton1= newjavax.swing.JButton();

jButton2= newjavax.swing.JButton();

jButton3= newjavax.swing.JButton();

pane= newjava.awt.Panel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("宋体", 1, 24)); //NOI18N

jLabel1.setText("数据展示器");

table.setModel(newjavax.swing.table.DefaultTableModel(newObject [][] {

{null, null},

{null, null},

{null, null},

{null, null},

{null, null},

{null, null},

{null, null},

{null, null},

{null, null},

{null, null},

{null, null},

{null, null},

{null, null}

},newString [] {"名称", "数据"}

));

jScrollPane1.setViewportView(table);

jButton1.setText("曲线数据");

jButton1.addActionListener(newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

}

});

jButton2.setText("饼状数据");

jButton2.addActionListener(newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {

jButton2ActionPerformed(evt);

}

});

jButton3.setText("柱状数据");

jButton3.addActionListener(newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {

jButton3ActionPerformed(evt);

}

});

javax.swing.GroupLayout paneLayout= newjavax.swing.GroupLayout(pane);

pane.setLayout(paneLayout);

paneLayout.setHorizontalGroup(

paneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 385, Short.MAX_VALUE)

);

paneLayout.setVerticalGroup(

paneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGap(0, 0, Short.MAX_VALUE)

);

javax.swing.GroupLayout layout= newjavax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,false)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,0, Short.MAX_VALUE)

.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(pane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addContainerGap())

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE,35, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(1, 1, 1)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,262, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jButton1)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jButton2)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jButton3))

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addComponent(pane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addContainerGap())))

);

pack();

}//

private voidjButton1ActionPerformed(java.awt.event.ActionEvent evt) {//TODO add your handling code here:

Vector vector=newVector();

Vector vector2=newVector();try{

File file= new File("D:\\曲线图数据.xlsx");

FileInputStream fis= newFileInputStream(file);

Workbook wb=newXSSFWorkbook(file);

Sheet filesheet=(Sheet)wb.getSheet("Sheet1");int numrows =filesheet.getLastRowNum()-filesheet.getFirstRowNum();for(int i=0;i<=numrows;i++){

Row row=filesheet.getRow(i);//获取每列的数据

String title1=row.getCell(0).getStringCellValue();

vector.add(title1);int title2=(int)row.getCell(1).getNumericCellValue();

vector2.add(title2);// table.getModel().setValueAt(title1,i,0);

table.getModel().setValueAt(title2,i,1);

}

DefaultCategoryDataset mDataset= newDefaultCategoryDataset();for(int i=0;i

mDataset.addValue((int)vector2.get(i),String.valueOf(vector.get(i)), String.valueOf(vector.get(i)));

StandardChartTheme mChartTheme= new StandardChartTheme("CN");

mChartTheme.setLargeFont(new Font("黑体", Font.BOLD, 20));

mChartTheme.setExtraLargeFont(new Font("宋体", Font.PLAIN, 15));

mChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15));

ChartFactory.setChartTheme(mChartTheme);

JFreeChart chart=ChartFactory.createLineChart("曲线图",//图名字

"名称",//横坐标

"数量",//纵坐标

mDataset,//数据集

PlotOrientation.VERTICAL,true, //显示图例

true, //采用标准生成器

false);//是否生成超链接

chart.setTextAntiAlias(false);

chart.setBackgroundPaint(Color.WHITE);//设置图标题的字体重新设置title

Font font = new Font("隶书", Font.BOLD, 25);

TextTitle title= new TextTitle("曲线图");

title.setFont(font);

chart.setTitle(title);//设置面板字体

Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);

chart.setBackgroundPaint(Color.WHITE);

CategoryPlot categoryplot=(CategoryPlot) chart.getPlot();//x轴//分类轴网格是否可见

categoryplot.setDomainGridlinesVisible(true);//y轴//数据轴网格是否可见

categoryplot.setRangeGridlinesVisible(true);

categoryplot.setRangeGridlinePaint(Color.WHITE);//虚线色彩

categoryplot.setDomainGridlinePaint(Color.WHITE);//虚线色彩

categoryplot.setBackgroundPaint(Color.lightGray);//设置轴和面板之间的距离//categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));

CategoryAxis domainAxis =categoryplot.getDomainAxis();

domainAxis.setLabelFont(labelFont);//轴标题

domainAxis.setTickLabelFont(labelFont);//轴数值

domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); //横轴上的//Lable//45度倾斜//设置距离图片左端距离

domainAxis.setLowerMargin(0.0);//设置距离图片右端距离

domainAxis.setUpperMargin(0.0);

NumberAxis numberaxis=(NumberAxis) categoryplot.getRangeAxis();

numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

numberaxis.setAutoRangeIncludesZero(true);

LineAndShapeRenderer lineandshaperenderer=(LineAndShapeRenderer) categoryplot

.getRenderer();

lineandshaperenderer.setBaseShapesVisible(true); //series 点(即数据点)可见

lineandshaperenderer.setBaseLinesVisible(true); //series 点(即数据点)间有连线可见

ChartPanel chartPanel= newChartPanel(chart);

chartPanel.setSize(pane.getSize());

pane.removeAll();

pane.add(chartPanel);

pane.validate();

}

}catch(Exception ex)

{

javax.swing.JOptionPane.showMessageDialog(rootPane,ex.toString());

}finally{

vector.clear();

vector2.clear();

}

}private voidjButton2ActionPerformed(java.awt.event.ActionEvent evt) {//TODO add your handling code here:

Vector vector=newVector();

Vector vector2=newVector();try{

File file= new File("D:\\饼状图数据.xlsx");

FileInputStream fis= newFileInputStream(file);

Workbook wb=newXSSFWorkbook(file);

Sheet filesheet=(Sheet)wb.getSheet("Sheet1");int numrows =filesheet.getLastRowNum()-filesheet.getFirstRowNum();for(int i=0;i<=numrows;i++){

Row row=filesheet.getRow(i);//获取每列的数据

String title1=row.getCell(0).getStringCellValue();

vector.add(title1);int title2=(int)row.getCell(1).getNumericCellValue();

vector2.add(title2);// table.getModel().setValueAt(title1,i,0);

table.getModel().setValueAt(title2,i,1);

}

DefaultPieDataset dataset= newDefaultPieDataset();for(int i=0;i

dataset.setValue(String.valueOf(vector.get(i)),(int)vector2.get(i));

JFreeChart chart= ChartFactory.createPieChart("饼状图", dataset, true, true,false);

chart.getLegend().setItemFont(new Font("宋体",Font.BOLD,15));

chart.getTitle().setFont(new Font("宋体",Font.BOLD,18));

ChartPanel chartPanel= newChartPanel(chart);

chartPanel.setSize(pane.getSize());

pane.removeAll();

pane.add(chartPanel);

pane.validate();

}

}catch(Exception ex)

{

javax.swing.JOptionPane.showMessageDialog(rootPane,ex.toString());

}finally{

vector.clear();

vector2.clear();

}

}private voidjButton3ActionPerformed(java.awt.event.ActionEvent evt) {//TODO add your handling code here:

Vector vector=newVector();

Vector vector2=newVector();try{

File file= new File("D:\\柱状图数据.xlsx");

FileInputStream fis= newFileInputStream(file);

Workbook wb=newXSSFWorkbook(file);

Sheet filesheet=(Sheet)wb.getSheet("Sheet1");int numrows =filesheet.getLastRowNum()-filesheet.getFirstRowNum();for(int i=0;i<=numrows;i++){

Row row=filesheet.getRow(i);//获取每列的数据

String title1=row.getCell(0).getStringCellValue();

vector.add(title1);int title2=(int)row.getCell(1).getNumericCellValue();

vector2.add(title2);// table.getModel().setValueAt(title1,i,0);

table.getModel().setValueAt(title2,i,1);

}

DefaultCategoryDataset dataset= newDefaultCategoryDataset();for(int i=0;i

dataset.setValue((int)vector2.get(i),String.valueOf(vector.get(i)),String.valueOf(vector.get(i)));

JFreeChart chart= ChartFactory.createBarChart("柱状图","名称","数量",dataset,PlotOrientation.VERTICAL, true, true,false);

CategoryPlot plot=(CategoryPlot) chart.getCategoryPlot();

CategoryAxis axis=plot.getDomainAxis();

axis.setLabelFont(new Font("宋体", Font.BOLD, 20));

axis.setTickLabelFont(new Font("宋体", Font.BOLD, 20));

ValueAxis rangeAxis=plot.getRangeAxis();

rangeAxis.setLabelFont(new Font("宋体", Font.BOLD, 20));

chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 20));

chart.getTitle().setFont(new Font("黑体", Font.ITALIC, 22));

ChartPanel chartPanel= newChartPanel(chart);

chartPanel.setSize(pane.getSize());

pane.removeAll();

pane.add(chartPanel);

pane.validate();

}

}catch(Exception ex)

{

javax.swing.JOptionPane.showMessageDialog(rootPane,ex.toString());

}finally{

vector.clear();

vector2.clear();

}

}/***@paramargs the command line arguments*/

public static voidmain(String args[]) {/*Set the Nimbus look and feel*/

//

/*If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details seehttp://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try{for(javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());break;

}

}

}catch(ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(t2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

}catch(InstantiationException ex) {

java.util.logging.Logger.getLogger(t2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

}catch(IllegalAccessException ex) {

java.util.logging.Logger.getLogger(t2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

}catch(javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(t2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

}//

/*Create and display the form*/java.awt.EventQueue.invokeLater(newRunnable() {public voidrun() {new t2().setVisible(true);

}

});

}//Variables declaration - do not modify

privatejavax.swing.JButton jButton1;privatejavax.swing.JButton jButton2;privatejavax.swing.JButton jButton3;privatejavax.swing.JLabel jLabel1;privatejavax.swing.JScrollPane jScrollPane1;privatejava.awt.Panel pane;privatejavax.swing.JTable table;//End of variables declaration

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值