189 8069 5689

java绘制折线代码 Java画折线图

java 中如何绘制90度折线?

90度直角弯线其实就是两条直线,第一条线的两个顶点坐标其实你是知道的,然后第二条线的第一个顶点就是你第一条线的第二个顶点,第二条线的第二个顶点可以根据第一个顶点跟长度计算出来。

10年积累的成都网站设计、成都网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有泉州免费网站建设让你可以放心的选择与我们合作。

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

public class XYJFrame extends javax.swing.JFrame {

/**

* 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 画折线图

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中用jframechart如何实现柱状折线组合图,请教高手,最好有个例子给我看看,谢谢!!

是jFreeChart吧?

这是Struts中结合 jFreeChart 画3种图,是我自己一个项目用到的 你自己看看吧 其中为生成List数据 我是在数据库用取出来的 jFreeChart 的接受数据应该是 类似于二维数组型的 这个网上有介绍

public String chartDB() {

if (style.equals("饼图")) {

drawPieChart();

} else if (style.equals("垂直柱状统计图")) {

drawBarChart();

} else if (style.equals("折线统计图")) {

drawLineChart();

}

return SUCCESS;

}

private void drawLineChart() {

MapString, Object session = ActionContext.getContext().getSession();

recordids = (String[]) session.get("recordids");

String[] field = new String[word.length]; // 获得要查询的字段

String[] rowKeys = new String[word.length]; // 各字段的中文名称

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

field[i] = word[i].split("-")[0];

rowKeys[i] = word[i].split("-")[1];

}

StringBuffer sb = new StringBuffer("SELECT ");

for (int j = 0; j field.length; j++) {

sb.append("sum(" + field[j] + "), ");

}

sb

.append("DATE_FORMAT(bas_datestart,'%Y-%m') from disasterrecord where bas_recordid in "

+ Util.typesToString(recordids)

+ " group by DATE_FORMAT(bas_datestart,'%Y-%m')");

ListObject[] list = new SearchService().findBySql(sb.toString());

DefaultCategoryDataset dataSet = new DefaultCategoryDataset();

String[] category = new String[list.size()];

for (int k = 0; k field.length; k++) {

for (int j = 0; j list.size(); j++) {

if(list.get(j)[field.length]==null){

category[j] = "";

}else{

category[j] = list.get(j)[field.length].toString();

}

if(list.get(j)[k] ==null){

dataSet.addValue(new Double(0), rowKeys[k], category[j]);

}else{

dataSet.addValue(new Double(list.get(j)[k].toString()),

rowKeys[k], category[j]);

}

}

}

chart = ChartFactory.createLineChart(title, xname, yname, dataSet,

PlotOrientation.VERTICAL, true, true, false);

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

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

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

CategoryAxis domainAxis = plot.getDomainAxis();

/*------设置X轴坐标上的文字-----------*/

domainAxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 11));

/*------设置X轴的标题文字------------*/

domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));

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

/*------设置Y轴坐标上的文字-----------*/

numberaxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 12));

/*------设置Y轴的标题文字------------*/

numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));

}

private void drawBarChart() {

MapString, Object session = ActionContext.getContext().getSession();

recordids = (String[]) session.get("recordids");

String[] field = new String[word.length]; // 获得要查询的字段

String[] rowKeys = new String[word.length]; // 各字段的中文名称

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

field[i] = word[i].split("-")[0];

rowKeys[i] = word[i].split("-")[1];

}

StringBuffer sb = new StringBuffer("SELECT ");

if (type.equals("各地区各灾害")) {

for (int j = 0; j field.length; j++) {

sb.append("sum(" + field[j] + "), ");

}

sb

.append(" bas_cityarea from disasterrecord where bas_recordid in "

+ Util.typesToString(recordids)

+ " group by bas_cityarea");

} else {

for (int j = 0; j field.length; j++) {

sb.append("sum(" + field[j] + "), ");

}

sb

.append("Year(bas_datestart) from disasterrecord where bas_recordid in "

+ Util.typesToString(recordids)

+ " group by Year(bas_datestart)");

}

ListObject[] list = new SearchService().findBySql(sb.toString());

String[] columnKeys = new String[list.size()]; // 获得 各地区名 或者 年份

for (int k = 0; k list.size(); k++) {

columnKeys[k] = list.get(k)[field.length].toString();

}

double[][] data = new double[rowKeys.length][columnKeys.length];

for (int m = 0; m rowKeys.length; m++) {

for (int n = 0; n columnKeys.length; n++) {

if (list.get(n)[m] == null) {

data[m][n] = new Double(0);

} else {

data[m][n] = new Double(list.get(n)[m].toString());

}

}

}

CategoryDataset dataset = DatasetUtilities.createCategoryDataset(

rowKeys, columnKeys, data);

chart = ChartFactory.createBarChart3D(title, xname, yname, dataset,

PlotOrientation.VERTICAL, true, true, false);

CategoryPlot plot = chart.getCategoryPlot();// 获取图表区域对象

CategoryAxis domainAxis = plot.getDomainAxis();

// 水平底部列表

domainAxis.setLabelFont(new Font("黑体", Font.BOLD, 14));

// 水平底部标题

domainAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 12));

// 垂直标题

ValueAxis rangeAxis = plot.getRangeAxis();// 获取柱状

rangeAxis.setLabelFont(new Font("黑体", Font.BOLD, 15));

TextTitle textTitle = chart.getTitle();

textTitle.setFont(new Font("黑体", Font.BOLD, 15));

BarRenderer3D renderer = new BarRenderer3D();

renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());

renderer.setBaseItemLabelsVisible(true);

renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));

renderer.setItemLabelAnchorOffset(10D);

renderer.setItemLabelFont(new Font("宋体", Font.PLAIN, 12));

renderer.setItemLabelsVisible(true);

//设置每个地区所包含的平行柱的之间距离

//renderer.setItemMargin(0.3);

plot.setRenderer(renderer);

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

}

public void drawPieChart() {

MapString, Object session = ActionContext.getContext().getSession();

recordids = (String[]) session.get("recordids");

String sql = "";

String field[] = word[0].split("-");

if (type.equals("各地区各灾害")) {

sql = "SELECT sum("

+ field[0]

+ ") +'', bas_cityarea from disasterrecord where bas_recordid in "

+ Util.typesToString(recordids) + " group by bas_cityarea";

} else {

sql = "SELECT sum("

+ field[0]

+ ") +'', Year(bas_datestart) from disasterrecord where bas_recordid in "

+ Util.typesToString(recordids)

+ " group by Year(bas_datestart)";

}

ListObject[] list = new SearchService().findBySql(sql);

DefaultPieDataset data = new DefaultPieDataset();

for (Object[] o : list) {

if(o[0] == null){

data.setValue(o[1].toString(), new Double(0));

}

data.setValue(o[1].toString(), new Double(o[0].toString()));

}

chart = ChartFactory.createPieChart(title, data, true, true, false);

Font font = new Font("SimSun", 10, 20);

TextTitle txtTitle = null;

txtTitle = chart.getTitle();

txtTitle.setFont(font);

PiePlot pieplot = (PiePlot)chart.getPlot();

pieplot.setLabelFont(font);

chart.getLegend().setItemFont(font);

}

Java开发jfreechart的折线图时,想要实现鼠标移向每个折点时只显示Y轴对应的数值,应该怎么做?

jfreechart只能产生静态图表,鼠标移上去显示的数值,实际上是靠html 的map来实现的,因此那个矩形框是浏览器显示的,与代码无关,所以也改不了的。

至于只显示Y轴的值,请从chart得到plot,然后再从plot得到renderer,再自定义renderer的ToolTipGenerator,从而实现自定义的tooltip.

如果想要更动态的图表,可以使用flash图表,如fusioncharts

java折线图数据库

用这个插件可以实现,只需要将你从数据库中得到的数据封装成xml格式的数据,就可以在页面上显示,显示的效果就像excel中的统计图,如果需要我可以给你发一下这个插件FusionChartsFree


本文标题:java绘制折线代码 Java画折线图
转载源于:http://cdxtjz.cn/article/dosddcc.html

其他资讯