import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.*;
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;


public class UpdateServer extends Thread
{
	private SeverList serverlist_list = null;
	private SeverList serverlist = null;
	
	public UpdateServer()
	{
		this.serverlist = main_program.serverlist_head;	
		this.serverlist_list = this.serverlist;
	}
	
	public void run() 
	{
		//恢复内存
		try {
			//进入死循环前的初始化
			//折线图数据
	        DefaultCategoryDataset lineDataset = new DefaultCategoryDataset();
	      //柱状图数据
	         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	         //生成的柱状图
	          JFreeChart chart = ChartFactory.createBarChart3D(
	                   "虚拟服务器任务调度情况",
	                   "虚拟服务器节点",//X轴的标签 
	                   "内存使用率",//Y轴的标签 
	                   dataset, //图标显示的数据集合
	                   PlotOrientation.VERTICAL, //图像的显示形式（水平或者垂直）
	                   false,//是否显示子标题 
	                   false,//是否生成提示的标签 
	                   false); //是否生成URL链接

	            /*
	             * 处理图形上的乱码
	             */

	            //处理主标题的乱码
	            chart.getTitle().setFont(new Font("黑体",Font.BOLD,18));

	            //获取图表区域对象
	            CategoryPlot categoryPlot = (CategoryPlot)chart.getPlot();

	            //获取X轴的对象
	            CategoryAxis3D categoryAxis3D = (CategoryAxis3D)categoryPlot.getDomainAxis();

	            //获取Y轴的对象
	            NumberAxis3D numberAxis3D = (NumberAxis3D)categoryPlot.getRangeAxis();

	            //处理X轴上的乱码
	            categoryAxis3D.setTickLabelFont(new Font("黑体",Font.BOLD,10));

	            //处理X轴外的乱码
	            categoryAxis3D.setLabelFont(new Font("黑体",Font.BOLD,10));

	            //处理Y轴上的乱码
	            numberAxis3D.setTickLabelFont(new Font("黑体",Font.BOLD,10));

	            //处理Y轴外的乱码
	            numberAxis3D.setLabelFont(new Font("黑体",Font.BOLD,10));

	            //自定义Y轴上显示的刻度，以10作为1格
	            numberAxis3D.setAutoTickUnitSelection(false);
	            NumberTickUnit unit = new NumberTickUnit(10); 
	            numberAxis3D.setTickUnit(unit);

	            //获取绘图区域对象
	            BarRenderer3D barRenderer3D = (BarRenderer3D)categoryPlot.getRenderer();

	            //设置柱形图的宽度
	            barRenderer3D.setMaximumBarWidth(0.07);

	            //在图形上显示数字
	            barRenderer3D.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
	            barRenderer3D.setBaseItemLabelsVisible(true);
	            barRenderer3D.setBaseItemLabelFont(new Font("宋体",Font.BOLD,10));

	            /*
	             * 放折线图数据
	             */
	            categoryPlot.setDataset(1, lineDataset);
	            //设置折线
	            LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer();
	            lineandshaperenderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
	            categoryPlot.setRenderer(1, lineandshaperenderer);
	            // 柱状图和纵轴紧靠
	            categoryAxis3D.setLowerMargin(0.0); 

	            categoryAxis3D.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
	            //折线在柱面前面显示
	            categoryPlot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

	            //生成GUI
	            JFrame newFrame=new JFrame("funBox");       
	            newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //定义JFrame关闭时的操作（必需），有效避免不能关闭后台当前框体进程的问题    
	            newFrame.setSize(1000, 800);      //定义JFrame的相关属性
	            newFrame.setLocation(800, 600);
	            newFrame.setVisible(true); 
	          
			while(true)
			{
				
		         while(serverlist_list.getNext() != null)
		         {
		        	 serverlist_list = serverlist_list.getNext();
		        	 //第一个参数是数量,第二个参数是类别,第三个参数是横坐标
		            lineDataset.addValue(serverlist_list.print_rate(), "%", serverlist_list.get_host_name());
		            //添加数据
		            dataset.addValue(serverlist_list.print_rate(), "%", serverlist_list.get_host_name());
		         }
		            
			         /*
		              * 在E盘目录下生成图片
		              */
			            File file = new File("e:\\a.png");
		            try {
		                 ChartUtilities.saveChartAsJPEG(file, chart, 800, 600);
		                } catch (IOException e) {
		                   e.printStackTrace();
		              }
		           
		            
		           //更新图片
		            newFrame.add(new MyJPanel());     //将需要呈现的图像添加进JFrame中

		        
				//恢复server_list为开头节点
		          this.serverlist_list = this.serverlist;
				  Thread.sleep(1000);				
			}
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	

}
