java 折线_java 如何实现折线图

本文介绍如何在Java中利用JFreeChart库创建折线图,展示图书馆的年度访问者统计数据。首先在Maven项目中引入JFreeChart的依赖,然后通过创建DefaultCategoryDataset填充数据,最后用ChartFactory创建折线图并显示。
摘要由CSDN通过智能技术生成

java 如何实现折线图

(博客来自本人csdn原创)

1:在maven中找到并导入jar 包

maven:添加链接描述

9be825310996625992a813eaa5419cd2.png

编写相应的代码(以图书馆访问者统计器实现)

package com.mycompany.libraryvisitor;/*

* 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.

*/

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartPanel;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.data.category.DefaultCategoryDataset;

import javax.swing.table.DefaultTableModel;

/**

*

* @author weizhijie

*/

public class VisitorForm extends javax.swing.JFrame {

/**

*创建

*/

public VisitorForm() {

initComponents();

}

@SuppressWarnings("unchecked")

//

private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();

tablevisitor = new javax.swing.JTable();

DisiplayButton = new javax.swing.JButton();

panelchart = new javax.swing.JPanel();

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

tablevisitor.setModel(new DefaultTableModel(

new Object [][] {

{null, null},

{null, null},

{null, null},

{null, null}

},

new String [] {

"年份", "访问"

}

));

jScrollPane1.setViewportView(tablevisitor);

DisiplayButton.setText("显示统计数据");

DisiplayButton.addActionListener(new java.awt.event.ActionListener() {

@Override

public void actionPerformed(java.awt.event.ActionEvent evt) {

DisiplayButtonActionPerformed(evt);

}

});

javax.swing.GroupLayout panelchartLayout = new javax.swing.GroupLayout(panelchart);

panelchart.setLayout(panelchartLayout);

panelchartLayout.setHorizontalGroup(

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

.addGap(0, 432, Short.MAX_VALUE)

);

panelchartLayout.setVerticalGroup(

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

.addGap(0, 225, Short.MAX_VALUE)

);

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

jLabel1.setText("图书馆访问者统计器");

jLabel2.setText("-----------------------------------------------------------------------");

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

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

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

.addGroup(layout.createSequentialGroup()

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

.addGroup(layout.createSequentialGroup()

.addContainerGap()

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

.addGroup(layout.createSequentialGroup()

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

.addGap(43, 43, 43)

.addComponent(panelchart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addGap(11, 11, 11)

.addComponent(DisiplayButton, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))))

.addGroup(layout.createSequentialGroup()

.addGap(34, 34, 34)

.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 521, javax.swing.GroupLayout.PREFERRED_SIZE)))

.addContainerGap(25, Short.MAX_VALUE))

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

.addGap(0, 0, Short.MAX_VALUE)

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

.addGap(233, 233, 233))

);

layout.setVerticalGroup(

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

.addGroup(layout.createSequentialGroup()

.addContainerGap()

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

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

.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)

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

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

.addGroup(layout.createSequentialGroup()

.addComponent(panelchart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(36, 36, 36)

.addComponent(DisiplayButton, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))

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

.addGap(26, 26, 26))

);

pack();

}//

private void DisiplayButtonActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

//二维数组实现数据导入

Integer[][]visitordata = {{2004,1500},{2005,1450},{2006,2000},{2007,2050},{2008,2500},{2009,2000},{2010,1600},{2011,3000},{2012,3200},{2013,4000},{2014,3500},{2015,2000},{2016,2300}};

String[] title = {"年份","访问人数"};

DefaultTableModel model = new DefaultTableModel();

model.setDataVector(visitordata, title);

tablevisitor.setModel(model);

DefaultCategoryDataset chart_dataset = new DefaultCategoryDataset();

for(int i = 0; i < 12;i++)

{

chart_dataset.addValue(visitordata[i][1],"图书馆访问人员统计表?",visitordata[i][0]);

}

JFreeChart lineChart = ChartFactory.createLineChart("图书馆人员统计表","标题","人数",chart_dataset, PlotOrientation.VERTICAL,true,true,false);

ChartPanel chartpanel = new ChartPanel(lineChart);

chartpanel.setSize(panelchart.getSize());

panelchart.removeAll();

panelchart.add(chartpanel);

panelchart.validate();

}

public static void main(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 see http://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(VisitorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (InstantiationException ex) {

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

} catch (IllegalAccessException ex) {

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

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

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

}

//

java.awt.EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

new VisitorForm().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JButton DisiplayButton;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JPanel panelchart;

private javax.swing.JTable tablevisitor;

// End of variables declaration

}

运行结果

5548d16c77e5ccf4ca9e2a3954bf21d4.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值