java代码绘制折线图 java绘制曲线

如何在netbeans6.8中用Java语言绘制类型于下图的折线图,要有详细代码

public class XYJFrame extends javax.swing.JFrame {

创新互联是专业的白塔网站建设公司,白塔接单;提供做网站、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行白塔网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

/**

* Creates new form XYJFrame

*/

public XYJFrame() {

initComponents();

iniCoordinate();

}

/**

* Netbeans 生成的代码,用于界面设计

*/

@SuppressWarnings("unchecked")

// editor-fold defaultstate="collapsed" desc="Generated Code"                          

private void initComponents() {

lblXCoordinate = new javax.swing.JLabel();

lblYCoordinate = new javax.swing.JLabel();

txtXCoordinate = new javax.swing.JTextField();

txtYCoordinate = new javax.swing.JTextField();

btnCalculateY = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

lblXCoordinate.setText("X 坐标");

lblYCoordinate.setText("Y 坐标");

btnCalculateY.setText("计算 Y 坐标");

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

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

btnCalculateYActionPerformed(evt);

}

});

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

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

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

.addGroup(layout.createSequentialGroup()

.addGap(37, 37, 37)

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

.addComponent(lblXCoordinate)

.addComponent(lblYCoordinate))

.addGap(29, 29, 29)

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

.addComponent(txtXCoordinate)

.addComponent(txtYCoordinate, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)

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

.addComponent(btnCalculateY)

.addGap(30, 30, 30)))

.addContainerGap(131, Short.MAX_VALUE))

);

layout.setVerticalGroup(

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

.addGroup(layout.createSequentialGroup()

.addGap(70, 70, 70)

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

.addComponent(lblXCoordinate)

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

.addGap(24, 24, 24)

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

.addComponent(lblYCoordinate)

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

.addGap(28, 28, 28)

.addComponent(btnCalculateY)

.addContainerGap(103, Short.MAX_VALUE))

);

pack();

}// /editor-fold                       

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

this.txtYCoordinate.setText(this.calculateY() + "");

}                                            

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "

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

* For details see  

*/

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(XYJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (InstantiationException ex) {

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

} catch (IllegalAccessException ex) {

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

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

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

}

///editor-fold

/* Create and display the form */

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

public void run() {

new XYJFrame().setVisible(true);

}

});

}

private void iniCoordinate() {

// 转折点 X 坐标

dbXCoordinate = new double[] { 0, 0.1, 0.3, 0.5, 1 };

// 转折点 Y 坐标

dbYCoordinate = new double[] { 0, 0.4, 0.7, 0.85, 1 };        

}

// 计算 Y 坐标

private double calculateY() {

double xCoordinate = Double.parseDouble(this.txtXCoordinate.getText());

int xPos = getXPositon(xCoordinate);

return xPos == -1 ? -1 : (dbYCoordinate[xPos] * xCoordinate) / dbXCoordinate[xPos];

}

// 得到输入的 X 值位置,确定比率

private int getXPositon(double xCoordinate) {

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

if(xCoordinate = dbXCoordinate[i]) {

return i;

}

}

return -1;

}

private double[] dbXCoordinate = new double[5];

private double[] dbYCoordinate = new double[5];

private double[] rate = new double[5];

// Variables declaration - do not modify                     

private javax.swing.JButton btnCalculateY;

private javax.swing.JLabel lblXCoordinate;

private javax.swing.JLabel lblYCoordinate;

private javax.swing.JTextField txtXCoordinate;

private javax.swing.JTextField txtYCoordinate;

// End of variables declaration                   

}

java怎么生成折线图,传入月份(1,2,3)生产数量(100,200,300),然后生成一个折线图,最好是曲线图,谢

按照你的要求编写的折线图程序如下:生成的图片放在D盘根目录下,文件名是testline.png

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartPanel;

import org.jfree.chart.ChartUtilities;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.NumberAxis;

import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.data.category.DefaultCategoryDataset;

import org.jfree.ui.ApplicationFrame;

import org.jfree.ui.RefineryUtilities;

public class LineCharts extends ApplicationFrame {

public LineCharts(String s) {

super(s);

setContentPane(createDemoLine());

}

public static void main(String[] args) {

LineCharts fjc = new LineCharts("折线图");

fjc.pack();

RefineryUtilities.centerFrameOnScreen(fjc);

fjc.setVisible(true);

}

// 生成显示图表的面板 public static JPanel createDemoLine() {

JFreeChart jfreechart = createChart(createDataset());

saveAsFile(jfreechart, "D://testline.png", 500, 300);

return new ChartPanel(jfreechart);

}

// 生成图表主对象JFreeChart public static JFreeChart createChart(DefaultCategoryDataset linedataset) {

//定义图表对象

JFreeChart chart = ChartFactory.createLineChart("LineChart", // chart title

"Time", // domain axis label

"Quantity", // range axis label

linedataset, // data

PlotOrientation.VERTICAL, // orientation

true, // include legend

true, // tooltips

false // urls

);

CategoryPlot plot = chart.getCategoryPlot();

// customise the range axis...

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();

rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

rangeAxis.setAutoRangeIncludesZero(true);

rangeAxis.setUpperMargin(1);

rangeAxis.setLabelAngle(Math.PI / 2.0);

return chart; }

//生成数据 public static DefaultCategoryDataset createDataset() {

DefaultCategoryDataset linedataset = new DefaultCategoryDataset();

// 各曲线名称

String series1 = "car";

// 横轴名称(列名称)

String type1 = "Jan";

String type2 = "Feb";

String type3 = "Mar";

linedataset.addValue(100, series1, type1); linedataset.addValue(200, series1, type2);

linedataset.addValue(300, series1, type3);

return linedataset; }

public static void saveAsFile(JFreeChart chart, String outputPath,

int weight, int height) {

FileOutputStream out = null;

try {

File outFile = new File(outputPath);

if (!outFile.getParentFile().exists()) {

outFile.getParentFile().mkdirs();

}

out = new FileOutputStream(outputPath);

// 保存为PNG文件

ChartUtilities.writeChartAsPNG(out, chart, 600, 350);

out.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (out != null) {

try {

out.close();

} catch (IOException e) {

// do nothing

}

}

}

}

}

如何用java 画折线图

package com.lei.jfreechart;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartPanel;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.NumberAxis;

import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.data.category.DefaultCategoryDataset;

import org.jfree.ui.ApplicationFrame;

import org.jfree.ui.RefineryUtilities;

public class LineCharts extends ApplicationFrame {

/**

*/

private static final long serialVersionUID = 1L;

public LineCharts(String s) {

super(s);

setContentPane(createDemoLine());

}

public static void main(String[] args) {

LineCharts fjc = new LineCharts("折线图");

fjc.pack();

RefineryUtilities.centerFrameOnScreen(fjc);

fjc.setVisible(true);

}

// 生成显示图表的面板

public static JPanel createDemoLine() {

JFreeChart jfreechart = createChart(createDataset());

return new ChartPanel(jfreechart);

}

// 生成图表主对象JFreeChart

public static JFreeChart createChart(DefaultCategoryDataset linedataset) {

// 定义图表对象

JFreeChart chart = ChartFactory.createLineChart("一季度销售曲线", //折线图名称

"时间", // 横坐标名称

"销售额(百万)", // 纵坐标名称

linedataset, // 数据

PlotOrientation.VERTICAL, // 水平显示图像

true, // include legend

true, // tooltips

false // urls

);

CategoryPlot plot = chart.getCategoryPlot();

plot.setRangeGridlinesVisible(true); //是否显示格子线

plot.setBackgroundAlpha(0.3f); //设置背景透明度

NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();

rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

rangeAxis.setAutoRangeIncludesZero(true);

rangeAxis.setUpperMargin(0.20);

rangeAxis.setLabelAngle(Math.PI / 2.0);

return chart;

}

// 生成数据

public static DefaultCategoryDataset createDataset() {

DefaultCategoryDataset linedataset = new DefaultCategoryDataset();

// 各曲线名称

String series1 = "冰箱";

String series2 = "彩电";

String series3 = "洗衣机";

// 横轴名称(列名称)

String type1 = "1月";

String type2 = "2月";

String type3 = "3月";

linedataset.addValue(0.0, series1, type1);

linedataset.addValue(4.2, series1, type2);

linedataset.addValue(3.9, series1, type3);

linedataset.addValue(1.0, series2, type1);

linedataset.addValue(5.2, series2, type2);

linedataset.addValue(7.9, series2, type3);

linedataset.addValue(2.0, series3, type1);

linedataset.addValue(9.2, series3, type2);

linedataset.addValue(8.9, series3, type3);

return linedataset;

}

}

网友分享,看看是否能帮到你


文章名称:java代码绘制折线图 java绘制曲线
标题来源:http://scjbc.cn/article/doheisg.html

其他资讯