JfreeChart生成折線圖

 用到的jar包有:

jcommon-1.0.16.jar,jfreechart-1.0.13.jar

 

代碼如下:

package com.take.test;

 

import java.awt.Color;

import java.awt.Font;

 

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartFrame;

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.chart.title.LegendTitle;

import org.jfree.chart.title.TextTitle;

import org.jfree.data.category.CategoryDataset;

import org.jfree.data.general.DatasetUtilities;

 

public class JFr {

public static void main(String[] args) {

JFr j=new JFr();

j.makeLineChart();

}

private static CategoryDataset getDataset() {

double[][] data = new double[][] { { 751, 800, 260, 600, 200 },

{ 400, 560, 240, 300, 150 }, { 600, 450, 620, 220, 610 } };

String[] rowKeys = { "CPU", "硬盤", "內存" };

String[] columnKeys = { "北京", "上海", "廣州", "南京", "深圳" };

CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);

return dataset;

}

 

public static void makeLineChart() {

String title = "電腦配件三月銷量"; // 獲得數據集

CategoryDataset dataset = getDataset();

org.jfree.chart.JFreeChart chart = ChartFactory.createLineChart(title, // 圖表標題

"配件", // 目錄軸的顯示標籤

"銷量", // 數值軸的顯示標籤

dataset, // 數據集

PlotOrientation.VERTICAL, // 圖表方向:水平、垂直

true, // 是否顯示圖例

true, // 是否生成工具(提示)

false // 是否生成URL鏈接

);

chart.setTextAntiAlias(false); // 設置背景色

chart.setBackgroundPaint(Color.WHITE); // 設置圖標題的字體

Font font = new Font("宋體", Font.BOLD, 20);

TextTitle textTitle = new TextTitle(title);

textTitle.setFont(font);

chart.setTitle(textTitle); // 設置X軸Y軸的字體

Font labelFont = new Font("宋體", Font.BOLD, 16);

chart.setBackgroundPaint(Color.WHITE); // 設置圖例字體

LegendTitle legend = chart.getLegend(0);

legend.setItemFont(new Font("宋體", Font.TRUETYPE_FONT, 14)); // 獲得plot

CategoryPlot categoryplot = (CategoryPlot) chart.getPlot(); // x軸

// 分類軸網格是否可見

categoryplot.setDomainGridlinesVisible(true); // y軸 數據軸網格是否可見

categoryplot.setRangeGridlinesVisible(true); // 虛線色彩

categoryplot.setRangeGridlinePaint(Color.WHITE); // 虛線色彩

categoryplot.setDomainGridlinePaint(Color.WHITE); // 設置背景色

categoryplot.setBackgroundPaint(Color.lightGray); // 設置軸和麪板之間的距離

CategoryAxis domainAxis = categoryplot.getDomainAxis(); // 設置橫軸標籤標題字體

domainAxis.setLabelFont(labelFont); // 設置橫軸數值標籤字體

domainAxis.setTickLabelFont(new Font("宋體", Font.TRUETYPE_FONT, 14)); // 橫軸上的

domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD); // 設置距離圖片左端距離

domainAxis.setLowerMargin(0.0); // 設置距離圖片右端距離

domainAxis.setUpperMargin(0.0);

NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); // 設置縱軸顯示標籤的字體

numberaxis.setLabelFont(labelFont);

numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

numberaxis.setAutoRangeIncludesZero(true); // 獲得renderer

LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot

.getRenderer(); // series 點(即數據點)可見

lineandshaperenderer.setBaseShapesVisible(true); // series 點(即數據點)間有連線可見

lineandshaperenderer.setBaseLinesVisible(true);

/*******************************************************/

ChartFrame frame = new ChartFrame(title, chart, true);

frame.pack();

frame.setVisible(true);

}

}

 

直接放到Java類裏面引入包就能運行。

本文出自 “Just do it” 博客,請務必保留此出處http://davenzeng.blog.51cto.com/3896952/991451

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章