jFreechart三种图的测试代码
时间: 2023-11-15 07:11:52 浏览: 175
以下是 jFreeChart 中三种类型图的测试代码,包括折线图、柱状图和饼图:
折线图:
```java
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.xy.*;
public class LineChartExample {
public static void main(String[] args) {
XYSeries series = new XYSeries("XYGraph");
series.add(1, 1);
series.add(2, 3);
series.add(3, 2);
series.add(4, 5);
series.add(5, 4);
series.add(6, 7);
series.add(7, 9);
series.add(8, 6);
series.add(9, 8);
series.add(10, 10);
XYDataset xyDataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createXYLineChart("XYLine Chart", "X Axis",
"Y Axis", xyDataset, PlotOrientation.VERTICAL, true, true,
false);
ChartFrame frame = new ChartFrame("XYLine Chart", chart);
frame.setVisible(true);
frame.setSize(300, 300);
}
}
```
柱状图:
```java
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.category.*;
import org.jfree.data.general.*;
public class BarChartExample {
public static void main(String[] args) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(120, "Profit", "Jan");
dataset.setValue(135, "Profit", "Feb");
dataset.setValue(152, "Profit", "Mar");
dataset.setValue(187, "Profit", "Apr");
dataset.setValue(200, "Profit", "May");
dataset.setValue(230, "Profit", "Jun");
dataset.setValue(280, "Profit", "Jul");
dataset.setValue(300, "Profit", "Aug");
dataset.setValue(320, "Profit", "Sep");
dataset.setValue(350, "Profit", "Oct");
dataset.setValue(370, "Profit", "Nov");
dataset.setValue(390, "Profit", "Dec");
JFreeChart chart = ChartFactory.createBarChart("Monthly Profit",
"Month", "Profit", dataset, PlotOrientation.VERTICAL, true,
true, false);
ChartFrame frame = new ChartFrame("Bar Chart", chart);
frame.setVisible(true);
frame.setSize(300, 300);
}
}
```
饼图:
```java
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.general.*;
import org.jfree.util.*;
public class PieChartExample {
public static void main(String[] args) {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Java", new Double(43.2));
dataset.setValue("C++", new Double(10.0));
dataset.setValue("Python", new Double(17.5));
dataset.setValue("Ruby", new Double(32.5));
JFreeChart chart = ChartFactory.createPieChart("Programming Language",
dataset, true, true, false);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setStartAngle(290);
plot.setDirection(Rotation.CLOCKWISE);
plot.setForegroundAlpha(0.5f);
ChartFrame frame = new ChartFrame("Pie Chart", chart);
frame.setVisible(true);
frame.setSize(300, 300);
}
}
```
阅读全文