代码
import java.awt.BasicStroke;
import java.awt.Dimension;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class MyLineChartDemo extends ApplicationFrame
{
public MyLineChartDemo(String s)
{
super(s);
JFreeChart jfreechart = createChart();
ChartPanel chartpanel = new ChartPanel(jfreechart);
chartpanel.setPreferredSize(new Dimension(600, 350));
setContentPane(chartpanel);
}
private static JFreeChart createChart()
{
String s = "家具产量";
String s1 = "居民收入";
String s3 = "2007年1月";
String s4 = "2007年2月";
String s5 = "2007年3月";
String s6 = "2007年4月";
String s7 = "2007年5月";
String s8 = "2007年6月";
String s9 = "2007年7月";
String s10 = "2007年8月";
String s11 = "2007年9月";
String s12 = "2007年10月";
String s13 = "2007年11月";
String s14 = "2007年12月";
CategoryPlot categoryplot = new CategoryPlot();
/**开始构造第一条折线*/
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
defaultcategorydataset.addValue(200, s, s3);
defaultcategorydataset.addValue(210, s, s4);
defaultcategorydataset.addValue(220, s, s5);
defaultcategorydataset.addValue(230, s, s6);
defaultcategorydataset.addValue(240, s, s7);
defaultcategorydataset.addValue(250, s, s8);
defaultcategorydataset.addValue(260, s, s9);
defaultcategorydataset.addValue(270, s, s10);
defaultcategorydataset.addValue(320, s, s11);
LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer();
categoryplot.setDataset(0, defaultcategorydataset);
categoryplot.setRenderer(0, lineandshaperenderer);/**到此为止,一条折线的设置完成*/
categoryplot.setRangeGridlinesVisible(true);
categoryplot.setDomainAxis(new CategoryAxis("Category"));
categoryplot.setRangeAxis(new NumberAxis("Value"));
categoryplot.setOrientation(PlotOrientation.VERTICAL);
/**构造第一条虚线*/
DefaultCategoryDataset defaultcategorydataset1 = new DefaultCategoryDataset();
defaultcategorydataset1.addValue(360, s, s12);
defaultcategorydataset1.addValue(400, s, s13);
defaultcategorydataset1.addValue(550, s, s14);
categoryplot.setDataset(1, defaultcategorydataset1);
LineAndShapeRenderer lineandshaperenderer1 = new LineAndShapeRenderer();
lineandshaperenderer1.setBaseShapesVisible(true);/**设置折线中的端点图形是否可见*/
lineandshaperenderer1.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {30F, 12F}, 0.0F)); //虚线生效处
lineandshaperenderer1.setBaseItemLabelsVisible(true);
lineandshaperenderer1.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
categoryplot.setRenderer(1, lineandshaperenderer1);
return new JFreeChart(categoryplot);
}
public static void main(String args[])
{
MyLineChartDemo linechartdemo5 = new MyLineChartDemo("Line Chart Demo 5");
linechartdemo5.pack();
RefineryUtilities.centerFrameOnScreen(linechartdemo5);
linechartdemo5.setVisible(true);
}
}
=========================================================================
代码
import java.awt.BasicStroke;
import java.awt.Dimension;
import java.text.NumberFormat;
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.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class MyLineChartDemo7 extends ApplicationFrame {
public MyLineChartDemo7(String s) {
super(s);
JFreeChart jfreechart = createChart();
ChartPanel chartpanel = new ChartPanel(jfreechart);
chartpanel.setPreferredSize(new Dimension(600, 350));
setContentPane(chartpanel);
}
private static XYDataset createDataset() {
TimeSeries s1 = new TimeSeries("家具产量增量", Month.class);
s1.add(new Month(1, 2001), 200);
s1.add(new Month(2, 2001), 210);
s1.add(new Month(3, 2001), 220);
s1.add(new Month(4, 2001), 230);
s1.add(new Month(5, 2001), 240);
s1.add(new Month(6, 2001), 250);
s1.add(new Month(7, 2001), 260);
s1.add(new Month(8, 2001), 270);
s1.add(new Month(9, 2001), 280);
s1.add(new Month(10, 2001), 290);
TimeSeries s2 = new TimeSeries("家具产量增量预测", Month.class);
s2.add(new Month(10, 2001), 290);
s2.add(new Month(11, 2001), 320);
s2.add(new Month(12, 2001), 340);
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(s1);
dataset.addSeries(s2);
return dataset;
}
private static JFreeChart createChart() {
XYDataset defaultcategorydataset1 = createDataset();
JFreeChart chart = ChartFactory.createTimeSeriesChart("预测图", "", "", // y-axis
// label
defaultcategorydataset1, // data
true, true, false);
XYPlot categoryplot = chart.getXYPlot();
categoryplot.setRangeGridlinesVisible(true);
categoryplot.setOrientation(PlotOrientation.VERTICAL);
categoryplot.getRenderer().setSeriesStroke(0,
new BasicStroke(2.0F, 1, 1, 1.0F, null, 0.0F));
categoryplot.getRenderer()
.setSeriesStroke(
1,
new BasicStroke(2.0F, 1, 1, 1.0F,
new float[] { 6F, 6F }, 0.0F));
XYLineAndShapeRenderer lineandshaperenderer = (XYLineAndShapeRenderer) categoryplot
.getRenderer();
lineandshaperenderer.setBaseShapesVisible(true);
/** 纵轴设置 */
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setNumberFormatOverride(NumberFormat.getPercentInstance());
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
numberaxis.setUpperMargin(0.12D);
return new JFreeChart(categoryplot);
}
public static void main(String args[]) {
MyLineChartDemo7 linechartdemo5 = new MyLineChartDemo7(
"Line Chart Demo 5");
linechartdemo5.pack();
RefineryUtilities.centerFrameOnScreen(linechartdemo5);
linechartdemo5.setVisible(true);
}
}
摘抄于http://topic.csdn.net/u/20080416/10/43f21519-f21a-4796-ad5b-fb7d56c784fa.html