本文目录一览:
- 1、java怎么生成折线图,传入月份(1,2,3)生产数量(100,200,300),然后生成一个折线图,最好是曲线图,谢
- 2、如何用java 画折线图
- 3、java折线图echarts 怎么用
- 4、已知两个坐标轴上的数值点怎么用java做一个折线图?
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折线图echarts 怎么用
可以去官网下载它的资源包,然后根据自己想要的图,在官网找到它的js,一般的数据都是改它js里面的一段json数组,你只要在后台通过sql语句拿到数据,用json数组格式传到前台就可以了。
已知两个坐标轴上的数值点怎么用java做一个折线图?
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class t3 extends JFrame {
t3()
{
this.setSize(200, 200);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
public static void main(String[] args)throws Exception{
new t3();
}
Point[] p = new Point[]{new Point(1,1),new Point(11,31),new Point(31,21),new Point(99,100)};
public void paint(Graphics g)
{
Image img = (Image)new BufferedImage(200,200,BufferedImage.SCALE_DEFAULT);
Graphics gg = img.getGraphics();
for(int i=0;ip.length-1;i++)
{
gg.drawLine(p[i].x, 200-p[i].y,p[i+1].x, 200-p[i+1].y);
}
g.drawImage(img, 0, 0, null);
}
}