Ⅰ Java如何调用数据库中的数据画图
你需要下载导入JFREECHART的jar包
然后下面是一个例子
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class XYSeriesDemo extends ApplicationFrame {
/**
* A demonstration application showing an XY series containing a null value.
*
* @param title the frame title.
*/
public XYSeriesDemo(final String title) {
super(title);
final XYSeries series = new XYSeries("Random Data");
series.add(1.0, 500.2);
series.add(5.0, 694.1);
series.add(4.0, 100.0);
series.add(12.5, 734.4);
series.add(17.3, 453.2);
series.add(21.2, 500.2);
series.add(21.9, null);
series.add(25.6, 734.4);
series.add(30.0, 453.2);
final XYSeriesCollection data = new XYSeriesCollection(series);
final JFreeChart chart = ChartFactory.createXYLineChart(
"XY Series Demo",
"X",
"Y",
data,
PlotOrientation.VERTICAL,
true,
true,
false
);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
public static void main(final String[] args) {
final XYSeriesDemo demo = new XYSeriesDemo("XY Series Demo");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
series.add(1.0, 500.2);1.0,500.2分别是横坐标和纵坐标数值 它会自动产生横竖坐标的
Ⅱ 如何用jfreechart显示饼状图,数据来自我的数据库当中。求具体代码
/*
*生成3D饼状图
*/
(Stringtitle,
List<TimeSeriesDataBean>list){
FontCtl.setFont();//解决乱码问题
List<String[]>dataList=newArrayList<String[]>();
String[]ss;
for(TimeSeriesDataBeanbean:list){
ss=newString[2];
ss[0]=bean.getNam();
ss[1]=bean.getVal()+"";
dataList.add(ss);
}
DefaultPieDatasetdataset=DataSetBuilder.buildPieChartDataSet(title,
dataList);
JFreeChartchart=ChartFactory.createPieChart3D(title,dataset,true,
true,false);
//chart.addSubtitle(newTextTitle("SUBTITLE测试"));
PiePlotpieplot=(PiePlot)chart.getPlot();
//{0}:({1})%代表显示格式:0=名称1=值比如:名称:19%
=(
"{0}:({1})",NumberFormat.getNumberInstance(),NumberFormat
.getPercentInstance());
pieplot.setLabelGenerator(standarPieIG);
//没有数据的时候显示的内容
pieplot.setNoDataMessage("无数据显示");
pieplot.setLabelGap(0.02D);
PiePlot3Dpieplot3d=(PiePlot3D)chart.getPlot();
//设置开始角度
pieplot3d.setStartAngle(120D);
//设置方向为”顺时针方向“
pieplot3d.setDirection(Rotation.CLOCKWISE);
//设置透明度,0.5F为半透明,1为不透明,0为全透明
pieplot3d.setForegroundAlpha(0.7F);
ChartFrameframe=newChartFrame(title,chart);
frame.pack();
frame.setVisible(true);
returnchart;
}