package realtimeMonitor;
import java.awt.Color;
import java.awt.Font;
import java.awt.RenderingHints;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.json.JSONException;
public class SwapSpacePie {
public static void createPieChart() throws NumberFormatException, JSONException, IOException {
//用工厂类创建饼图
JFreeChart pieChart = ChartFactory.createPieChart("SwapSpacePie:",
createDataset(), true, true, false);
// RenderingHints做文字渲染参数的修改
// VALUE_TEXT_ANTIALIAS_OFF表示将文字的抗锯齿关闭.
pieChart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
//得到饼图的Plot对象
PiePlot piePlot = (PiePlot) pieChart.getPlot();
setSection(piePlot);
setLabel(piePlot);
setNoDataMessage(piePlot);
setNullAndZeroValue(piePlot);
//将饼图显示在图像界面上
FileOutputStream ous = null;
String filePath="";//绝对路径
String webPath="WarnImages/swap_space_pie.png";//"+request.getSession().getId()+"
try {
filePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
filePath =filePath.replace("WEB-INF/classes/", "");
filePath += webPath;
// System.out.println(filePath);
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
if (null != pieChart) {
ous = new FileOutputStream(filePath);
ChartUtilities.writeChartAsPNG(ous, pieChart, 400, 200);
//ous.flush();
}
} catch (IOException e) {
e.printStackTrace();
} // 生成图片
finally {
try {
ous.close();// 最后关闭文件流
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static DefaultPieDataset createDataset() throws NumberFormatException, JSONException, IOException {
//设置数据
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("已使用", Integer.parseInt(ResolveJSONForShell.resolveJSONForSwapSpace()[0]));
pieDataset.setValue("未使用", Integer.parseInt(ResolveJSONForShell.resolveJSONForSwapSpace()[1]));
return pieDataset;
}
public static void setSection(PiePlot pieplot) {
//设置扇区颜色
pieplot.setSectionPaint("已使用", new Color(160, 160, 255));
pieplot.setSectionPaint("未使用", new Color(128, 128, 223));
//设置扇区分离显示
pieplot.setExplodePercent("已使用", 0.2D);
//设置扇区边框不可见
pieplot.setSectionOutlinesVisible(false);
}
public static void setLabel(PiePlot pieplot) {
//设置扇区标签显示格式:关键字:值(百分比)
pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}:{1}({2} percent)"));
//设置扇区标签颜色
pieplot.setLabelBackgroundPaint(new Color(220, 220, 220));
pieplot.setLabelFont((new Font("宋体", Font.PLAIN, 12)));
}
public static void setNoDataMessage(PiePlot pieplot) {
//设置没有数据时显示的信息
pieplot.setNoDataMessage("无数据");
//设置没有数据时显示的信息的字体
pieplot.setNoDataMessageFont(new Font("宋体", Font.BOLD, 14));
//设置没有数据时显示的信息的颜色
pieplot.setNoDataMessagePaint(Color.red);
}
public static void setNullAndZeroValue(PiePlot piePlot) {
//设置是否忽略0和null值
piePlot.setIgnoreNullValues(true);
piePlot.setIgnoreZeroValues(true);
}
private static int randomNum()
{
System.out.println((Math.random()*10+5));
return (int)(Math.random()*10+5);
}
}
jfreechart 饼图
最新推荐文章于 2021-11-09 18:54:40 发布