public?class?XYJFrame?extends?javax.swing.JFrame?{
創(chuàng)新互聯(lián)是專業(yè)的白塔網站建設公司,白塔接單;提供做網站、網站制作,網頁設計,網站設計,建網站,PHP網站建設等專業(yè)做網站服務;采用PHP框架,可快速的進行白塔網站開發(fā)網頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網站,專業(yè)的做網站團隊,希望更多企業(yè)前來合作!
/**
*?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???????????????????
}
按照你的要求編寫的折線圖程序如下:生成的圖片放在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; }
//生成數(shù)據(jù) 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
}
}
}
}
}
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,?//?數(shù)據(jù)
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;
}
//?生成數(shù)據(jù)
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;
}
}
網友分享,看看是否能幫到你