JFreeChart畫折線圖


package lb;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.text.DecimalFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.RectangleInsets;
public class Line {

public static void main(String[] args) {
StandardChartTheme mChartTheme = new StandardChartTheme("CN");
mChartTheme.setLargeFont(new Font("黑體", Font.BOLD, 20));
mChartTheme.setExtraLargeFont(new Font("宋體", Font.PLAIN, 15));
mChartTheme.setRegularFont(new Font("宋體", Font.PLAIN, 15));
ChartFactory.setChartTheme(mChartTheme);
CategoryDataset mDataset = GetDataset();
JFreeChart mChart = ChartFactory.createLineChart(
"折線圖",//圖名字
"鄰居用戶數",//橫坐標
"RMSE",//縱坐標
mDataset,//數據集
PlotOrientation.VERTICAL,
true, // 顯示圖例
true, // 采用標准生成器
false);// 是否生成超鏈接
//.LIGHT_GRAY
CategoryPlot mPlot = (CategoryPlot)mChart.getPlot();
mPlot.setBackgroundPaint(Color.white);
mPlot.setRangeGridlinePaint(Color.BLUE);//背景底部橫虛線
mPlot.setOutlinePaint(Color.RED);//邊界線


/*
* Y軸設置
*/
NumberAxis vn = (NumberAxis) mPlot.getRangeAxis();
// DecimalFormat df = new DecimalFormat("#0.00");
// vn.setNumberFormatOverride(df); // 數據軸數據標簽的顯示格式
vn.setUpperMargin(0.1);
vn.setLowerMargin(0.1);
vn.setAutoRangeMinimumSize(0.01);//最小跨度
vn.setLowerBound(0.70);//最小值顯示
vn.setUpperBound(1.10);
LineAndShapeRenderer lasp = (LineAndShapeRenderer) mPlot.getRenderer();// 獲取顯示線條的對象
lasp.setBaseShapesVisible(true);// 設置拐點是否可見/是否顯示拐點
lasp.setDrawOutlines(true);// 設置拐點不同用不同的形狀
lasp.setUseFillPaint(true);// 設置線條是否被顯示填充顏色
lasp.setBaseFillPaint(Color.BLACK);//// 設置拐點顏色

/*
* X軸
*/
CategoryAxis domainAxis = mPlot.getDomainAxis();
//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
domainAxis.setLowerMargin(-0.08);

// domainAxis.setCategoryMargin(0.5);;
//System.out.println(domainAxis.getCategoryMargin());;


// domainAxis.setLabelFont(new Font("宋書", Font.PLAIN, 15)); // 設置橫軸字體
// domainAxis.setTickLabelFont(new Font("宋書", Font.PLAIN, 15));// 設置坐標軸標尺值字體
// domainAxis.setLowerMargin(0.01);// 左邊距 邊框距離
// domainAxis.setUpperMargin(0.06);// 右邊距 邊框距離,防止最后邊的一個數據靠近了坐標軸。
// domainAxis.setMaximumCategoryLabelLines(10);
// domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);// 橫軸 lable 的位置 橫軸上的 Lable 45度傾斜 DOWN_45
// domainAxis.setm

try {
File file = new File("c:/student.png");
ChartUtilities.saveChartAsPNG(file,mChart,400,300);//把報表保存為文件
}catch (Exception e) {
String s = e.getLocalizedMessage();
s = e.getMessage();
s = e.toString();
}
ChartFrame mChartFrame = new ChartFrame("折線圖", mChart);
mChartFrame.pack();
mChartFrame.setVisible(true);

}
public static CategoryDataset GetDataset()
{
DefaultCategoryDataset mDataset = new DefaultCategoryDataset();
mDataset.addValue(1.10, "First", "5");
mDataset.addValue(1.05, "First", "10");
mDataset.addValue(1.00, "First", "15");
mDataset.addValue(0.95, "First", "20");
mDataset.addValue(0.90, "First", "25");

mDataset.addValue(1.05, "Second", "5");
mDataset.addValue(1.00, "Second", "10");
mDataset.addValue(0.96, "Second", "15");
mDataset.addValue(0.91, "Second", "20");
mDataset.addValue(0.88, "Second", "25");

mDataset.addValue(1.02, "Third", "5");
mDataset.addValue(0.90, "Third", "10");
mDataset.addValue(0.88, "Third", "15");
mDataset.addValue(0.85, "Third", "20");
mDataset.addValue(0.7, "Third", "25");
return mDataset;
}

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM