本文目錄一覽:
- 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);
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/282617.html