`
grape927
  • 浏览: 74885 次
  • 性别: Icon_minigender_2
  • 来自: 深圳
社区版块
存档分类
最新评论

Java: applet+jfreechart动态显示方法的执行时间

    博客分类:
  • RCP
阅读更多

public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {

  String[] values1 = new String[3];
  String[] values2 = new String[3];
  double[][] data = new double[2][144];
  int splitTag1 = 0;
  int splitTag2 = 0;
  String ip = request.getParameter("ip").trim();
  String url = request.getParameter("url").trim();
  String module = request.getParameter("module").trim();
  GetTime getTime = new GetTime();
  String[] TimeArray = getTime.getTime();
  
  String[] columnKeys = getTime.getShowTime();
  String title = "IP:" + ip + " url:" + url;
  String time = getTime.getNowTime();
  String costTime = "Cost Time(/second) ";
  String todayData = "todayData# ";
  String yesterdayData = "yesterdayData# ";
  String columnKeysData = "columnKeysData# ";
  String TimeArrayData = "TimeArrayData# ";
  String clientHost = request.getRemoteHost() + ".log";

  // save TimeArray into TimeArrayData
  for (int i = 0; i < TimeArray.length; i++) {
   if (i == 0) {
    TimeArrayData += i + " | " + TimeArray[i];
   } else {
    TimeArrayData += " @ " + i + " | " + TimeArray[i];
   }

  }

  // save columnKeys into columnKeys
  for (int i = 0; i < columnKeys.length; i++) {
   if (i == 0) {
    columnKeysData += i + " | " + columnKeys[i];
   } else {
    columnKeysData += " @ " + i + " | " + columnKeys[i];
   }

  }

  // Inital data[][] = -1
  for (int k = 0; k < 2; k++) {
   for (int h = 0; h < 144; h++) {
    data[k][h] = -1;
   }
  }

  // set values
  values1[0] = ip;
  values2[0] = ip;
  values1[1] = url;
  values2[1] = url;
  values1[2] = "%" + TimeArray[0] + "%";
  values2[2] = "%" + TimeArray[1] + "%";

  List todayList = tomcatWlsDAO.findByPersonal(values1);
  List yesterdayList = tomcatWlsDAO.findByPersonal(values2);
  for (Iterator it = todayList.iterator(); it.hasNext();) {
   tomcatWlsLog = (TomcatWlsLog) it.next();
   int hour = Integer
     .parseInt(tomcatWlsLog.getTime().substring(8, 10));
   int minute = Integer.parseInt(tomcatWlsLog.getTime().substring(10));
   int index = hour * 6 + minute / 10;
   if (splitTag1 == 0) {
    todayData += index + " | " + String.valueOf(tomcatWlsLog.getNum());
    splitTag1++;
   } else {
    todayData += " @ " + index + " | " + tomcatWlsLog.getNum();
   }

  }

  for (Iterator it = yesterdayList.iterator(); it.hasNext();) {
   tomcatWlsLog = (TomcatWlsLog) it.next();
   int hour = Integer
     .parseInt(tomcatWlsLog.getTime().substring(8, 10));
   int minute = Integer.parseInt(tomcatWlsLog.getTime().substring(10));
   int index = hour * 6 + minute / 10;
   if (splitTag2 == 0) {
    yesterdayData += index + " | "
      + String.valueOf(tomcatWlsLog.getNum());
    splitTag2++;
   } else {
    yesterdayData += " @ " + index + " | "
      +tomcatWlsLog.getNum();
   }

  }

  HttpSession session = request.getSession();
  
  request.setAttribute("ip", ip);
  request.setAttribute("url", url);
  request.setAttribute("module", module);

  //save date into clientHost
  try {
   File f = new File(clientHost);
   System.out.println("path:" + f.getAbsolutePath());
   if (f.exists()) {
    System.out.println("File:" + clientHost + " is exist!");
    f.delete();
    System.out.println("File:" + clientHost + " was deleted!");
   } else {
    f.createNewFile();
   }
   BufferedWriter output = new BufferedWriter(new FileWriter(f));
   output.write("time# " + time + "\n");
   output.write("title# " + title + "\n");
   output.write("costTime# " + costTime + "\n");
   output.write(todayData + "\n");
   output.write(yesterdayData + "\n");
   output.write(TimeArrayData + "\n");
   output.write(columnKeysData + "\n");
   output.flush();
   output.close();
  } catch (IOException e) {
   e.printStackTrace();
  }

  return mapping.findForward("success");
 }

 

//从文件中读出数据

public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  try {
   String clientHost = request.getRemoteHost() + ".log";
   System.out.println(clientHost);
   ParaseClientHostLog pch = new ParaseClientHostLog(clientHost);
   double[][] data = pch.getData();
   String title = pch.getTitle();
   String costTime = pch.getCostTime();
   String[] TimeArray = pch.getTimeArray();
   String time = pch.getTime();
   String[] columnKeys = pch.getColumnKeys();
   
   for(int i=0;i<data.length;i++){
    for(int j=0;j<data[i].length;j++){
     System.out.print("data["+i+"]["+j+"]["+data[i][j]+"] ");
    }
      System.out.println();
   } 
   for(int i=0;i<TimeArray.length;i++){
     System.out.print("TimeArray["+i+"]["+TimeArray[i]+"] ");
   }
   System.out.println();
   for(int i=0;i<columnKeys.length;i++){
    System.out.print("columnKeys["+i+"]["+columnKeys[i]+"] ");
  } 
   System.out.println();
   System.out.println("title["+title+"]");
   System.out.println("time["+time+"]");
            System.out.println("costTime["+costTime+"]");
   
   
   ObjectOutputStream dbStream = new ObjectOutputStream(response
     .getOutputStream());
   dbStream.writeObject(data);
   dbStream.writeObject(title);
   dbStream.writeObject(costTime);
   dbStream.writeObject(TimeArray);
   dbStream.writeObject(time);
   dbStream.writeObject(columnKeys);
  } catch (Exception e) {
   e.printStackTrace();
  }

 }

 

 

 


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

public class ParaseClientHostLog {

 public double[][] data = new double[2][144];
 public String[] columnKeys = new String[144];
 public String[] TimeArray = new String[2];
 public static String todayData = null;
 public static String yesterdayData = null;
 public static String columnKeysData = null;
 public static String TimeArrayData = null;
 public static String title = null;
 public static String time = null;
 public static String costTime = null;
 public String file = null;

 public ParaseClientHostLog(String file) {
  this.file = file;
  try {
   File f = new File(file);
   if (f.exists()) {
    FileReader fileread = new FileReader(f);
    BufferedReader reader = new BufferedReader(fileread);
    String line = "";
    while ((line = reader.readLine()) != null) {
     line = line.trim();
     if (!line.equals("")) {
      if (line.indexOf("time#") >= 0) {
       time = line;
      }
      if (line.indexOf("title#") >= 0) {
       title = line;
      }
      if (line.indexOf("costTime#") >= 0) {
       costTime = line;
      }
      if (line.indexOf("todayData#") >= 0) {
       todayData = line;
      }
      if (line.indexOf("yesterdayData#") >= 0) {
       yesterdayData = line;
      }
      if (line.indexOf("TimeArrayData#") >= 0) {
       TimeArrayData = line;
      }
      if (line.indexOf("columnKeysData#") >= 0) {
       columnKeysData = line;
      }
     }
    }
    reader.close();
    fileread.close();
   } else {
    System.out.println("文件不存在:" + file);
   }
   // f.delete(); // 删除文件
  } catch (IOException e) {
   e.printStackTrace();
  }

 }

 public String getTitle() {
  return splitStr(title);
 }

 public String getTime() {
  return splitStr(time);
 }

 public String getCostTime() {
  return splitStr(costTime);
 }

 public String splitStr(String str) {
  str = str.trim();
  int tag = str.indexOf("#") + 1;
  return str.substring(tag).trim();
 }

 public String[] splitArray(String str) {
  String str1 = splitStr(str);
  str1 = str1.trim();
  String[] info = new String[144];
  String[] temp = str1.split("@");
  for (int i = 0; i < temp.length; i++) {
   int tag = temp[i].indexOf("|");
   int index = Integer.parseInt(temp[i].substring(0, tag).trim());
   info[index] = temp[i].substring(tag + 1).trim();
  }
  return info;

 }
 

 public double[][] getData() {
  String[] info1 = splitArray(todayData);
  String[] info2 = splitArray(yesterdayData);
  for (int i = 0; i < info1.length; i++) {
   if(info1[i]!= null){
    data[0][i] = Double.parseDouble(info1[i]);
   }else{
    data[0][i] = -1;
   }
  }
  System.out.println();
  for (int i = 0; i < info2.length; i++) { 
   if(info2[i]!= null){
    data[1][i] = Double.parseDouble(info2[i]);
   }else{
    data[1][i] = -1;
   }
  }
  return data;
 }

 public String[] getColumnKeys() {
  columnKeys = splitArray(columnKeysData);
  return columnKeys;
 }

 public String[] getTimeArray() {
  
  String str1 = splitStr(TimeArrayData);
  str1 = str1.trim();
  String[] temp = str1.split("@");
  for (int i = 0; i < temp.length; i++) {
   int tag = temp[i].indexOf("|");
   int index = Integer.parseInt(temp[i].substring(0, tag).trim());
   TimeArray[index] = temp[i].substring(tag + 1).trim();
  }
  return TimeArray;
 }

}

 

 

以下为applet客户端的文件:

package applet;

import org.jfree.chart.JFreeChart;

public class AppletContro {
 
 double[][] dataOld = new double[2][144];
 double[][] data = new double[2][12];
 static String[] TimeArray = null;
 static String title = null;
 static String costTime = null;
 static String time = null;
 static String[] columnKeys = new String[12];
 String[] columnKeysOld = new String[144];
 LineShapeImply lineShape = new LineShapeImply();
 public static int tag = 0;
 
 public AppletContro(double[][] data,String[] TimeArray,String[] columnKeys,String title,String time,String costTime){
  this.dataOld = data;
  AppletContro.TimeArray = TimeArray;
  this.columnKeysOld = columnKeys;
  AppletContro.title = title;
  AppletContro.time= time;
  AppletContro.costTime = costTime;
 }
 
  public JFreeChart getShape(){
   Control();
   return lineShape.getPicture(data,TimeArray, columnKeys, title, time, costTime);
  }
 
  public void Control(){
   if((columnKeysOld[tag+12]!= null)&&(tag+12<144)){
    System.out.println("tag ="+tag);
    for(int j=tag;j<tag+12;j++){
     data[0][j-tag] = dataOld[0][j];
     data[1][j-tag] = dataOld[1][j];
     columnKeys[j-tag] = columnKeysOld[j];
     //System.out.println("data[0]["+(j-tag)+"]="+data[0][j-tag] + " data[1]["+(j-tag)+"]="+data[1][j-tag]+" columnKeys["+(j-tag)+"]="+columnKeys[j-tag] );
    }
    tag++;
   }else{
   System.out.println("It is finised...");
   }
  }

}

 

 

package applet;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.Timer;

public class LineShapeApplet extends JApplet implements Serializable, Runnable {

 double[][] data = null;
 String title = null;
 String costTime = null;
 String[] TimeArray = null;
 String time = null;
 String[] columnKeys = null;
 private AppletContro appletContro;
 private ChartPanel chartPanel = null;
 private static JFreeChart jfreechart = null;

 
 Thread clockthread = null; //设置一个线程

 public void start() //线程开始的类
 {
  if (clockthread == null) //如果线程为空,则
  {
   clockthread = new Thread(this); //开始新的线程
   clockthread.start(); //开始
  }
 }

 public void stop() //终止线程
 {
  clockthread.stop(); //终止线程,使它
  clockthread = null; //为空
 }

 public void run() //运行线程
 {
  while (true) //一个死循环,条件永远都是真的。
  {
   repaint(); //重新绘制界面
   try {
    Thread.sleep(2000);
    jfreechart = appletContro.getShape();
    repaint(); 
   } //让线程沉睡5000毫秒,也就是5秒钟
   catch (InterruptedException e) {
   } //捕获异常(也就是错误)
  }
 }

 public void init() {
  try {

   System.out.println("applet init()");
   Container container = getContentPane();
   container.setLayout(new BorderLayout());

   URL servletURL = null; // The URL to the servlet.
   URLConnection servletConnection = null; // The connection to the servlet.
   ObjectInputStream dbStream = null; // The stream from the servlet.
   servletURL = new URL("http://localhost:8080/xxxxxx/servlet/LineShapeData");
   servletConnection = servletURL.openConnection();
   dbStream = new ObjectInputStream(servletURL.openStream());
   data = (double[][]) dbStream.readObject();

   title = (String) dbStream.readObject();
   costTime = (String) dbStream.readObject();
   TimeArray = (String[]) dbStream.readObject();
   time = (String) dbStream.readObject();
   columnKeys = (String[]) dbStream.readObject();

   appletContro = new AppletContro(data, TimeArray, columnKeys, title,
     time, costTime);
   jfreechart = appletContro.getShape();
   chartPanel = new ChartPanel(jfreechart);

   container.add(chartPanel);
   //setPreferredSize(new Dimension(1000, 800));
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public void paint(Graphics g) {
  if (jfreechart != null) {
            jfreechart.draw((Graphics2D) g, getBounds());    
        }
 }

 public void update(Graphics g) {
  paint(g);
 }

}

 

 

package applet;

import java.awt.Color;
import java.awt.Font;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;

public class LineShapeImply {

 public JFreeChart getPicture(double[][] data, String[] rowKeys,
   String[] columnKeys, String title, String time, String costTime) {

  CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
  return createTimeXYChar(title, time, costTime, dataset);

 }

 // 折线图数据集
 public CategoryDataset getBarData(double[][] data, String[] rowKeys,
   String[] columnKeys) {
  return DatasetUtilities
    .createCategoryDataset(rowKeys, columnKeys, data);

 }

 
 public JFreeChart createTimeXYChar(String chartTitle, String x, String y,
   CategoryDataset xyDataset) {
  JFreeChart chart = ChartFactory.createLineChart(chartTitle, x, y,
    xyDataset, PlotOrientation.VERTICAL, true,true, true);

  chart.setTextAntiAlias(false);
  chart.setBackgroundPaint(Color.WHITE);
  // 设置图标题的字体重新设置title
  //Font font = new Font("宋体", Font.BOLD, 25);
  TextTitle title = new TextTitle(chartTitle);
  //title.setFont(font);
  chart.setTitle(title);
  // 设置面板字体
  //Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
  chart.setBackgroundPaint(Color.WHITE);
  CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
  // x轴 // 分类轴网格是否可见
  categoryplot.setDomainGridlinesVisible(true);
  // y轴 //数据轴网格是否可见
  categoryplot.setRangeGridlinesVisible(true);
  categoryplot.setRangeGridlinePaint(Color.WHITE);// 虚线色彩
  categoryplot.setDomainGridlinePaint(Color.WHITE);// 虚线色彩
  categoryplot.setBackgroundPaint(Color.lightGray);

  // 设置轴和面板之间的距离
  // categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
  CategoryAxis domainAxis = categoryplot.getDomainAxis();
  //domainAxis.setLabelFont(labelFont);// 轴标题
  //domainAxis.setTickLabelFont(labelFont);// 轴数值
  domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的
  // Lable
  // 45度倾斜
  // 设置距离图片左端距离
  domainAxis.setLowerMargin(0.0);
  // 设置距离图片右端距离
  domainAxis.setUpperMargin(0.0);

  NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
  numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
  numberaxis.setAutoRangeIncludesZero(true);

  // 获得renderer 注意这里是下嗍造型到lineandshaperenderer!!
  LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot
    .getRenderer();

  lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见
  lineandshaperenderer.setBaseLinesVisible(true); // series 点(即数据点)间有连线可见

  return chart;
 }
}

 

分享到:
评论

相关推荐

    applet_+_JfreeChart_实现曲线走势图

    5. 将图表嵌入Applet:创建一个Applet类,覆盖paintComponent()方法,在其中调用ChartPanel对象的drawChart()方法,将JFreeChart对象绘制到Applet的画布上。 6. HTML嵌入:在HTML文件中,使用&lt;applet&gt;标签指定...

    Applet版JFreeChart演示

    "Applet版JFreeChart演示"是一个很好的学习资源,它展示了如何利用JFreeChart库在Java Applet中生成和显示动态图表。虽然现代Web开发倾向于使用更现代的技术,如JavaScript和WebGL,但理解这种传统的Java图形编程...

    applet+jfreechat

    标题中的“applet+jfreechart”表明我们正在讨论一个结合了Java小应用程序(Applet)与JFreeChart库的项目。JFreeChart是一款强大的Java图形库,它允许开发者创建各种图表,如饼图、柱状图、线图、散点图等。在Web...

    java报表jfreechart开发

    ### Java报表JFreeChart开发知识点总结 #### 1. 简介 ##### 1.1 什么是JFreeChart JFreeChart是一款免费且开源的Java图表库,它提供了多种类型的图表绘制功能,如饼图、条形图、折线图等。JFreeChart适用于各种Java...

    JFreeChart实时折线图

    JFreeChart是一款强大的Java图表库,它允许开发者创建多种类型的图表,包括折线图、柱状图、饼图、散点图等。在本场景中,我们关注的是如何使用JFreeChart来实现实时更新的折线图。下面将详细介绍这个过程。 首先,...

    精通Java+Web动态图表编程

    书中详细解析了Java Applet的绘图基础,包括绘制文本、线段、矩形、椭圆、圆弧、多边形以及图像的载入与显示等基本图形元素的绘制方法。通过丰富的实例,如垂直柱状图、饼图、单据生成等,展示了如何结合HTML参数...

    JFreeChart文档、jar包、教程

    JFreeChart是一款强大的Java图表库,它为开发者提供了一种简单的方法来创建各种类型的图表,如饼图、柱状图、线图、散点图、甘特图等,适用于数据分析、报表生成以及可视化应用的开发。这个压缩包包含了JFreeChart的...

    JfreeChart(ppt)

    1. 使用Applet:通过Java的图形支持在浏览器中显示图表,但这种方法对客户端的要求较高,随着现代浏览器逐渐弃用对Java的支持,这种方式已不适用于互联网环境。 2. 服务器端生成图表图片:在Web服务器端利用...

    JFreeChart在JSP开发中的应用心得.pdf

    作为基于Java语言的图表开发工具,JFreeChart不仅可以在Servlet、JSP、Applet或Java Application环境中使用,还可以通过JDBC动态显示任何数据库中的数据,并结合Itext输出为PDF文件。 JFreeChart的核心类主要包括:...

    jfreechart 1.0.11

    3. **兼容性广泛**:Jfreechart可以与Swing、JavaFX、Applet和Web应用无缝集成,同时支持AWT和Swing组件,适应不同的开发环境。 4. **性能优秀**:JFreeChart在处理大量数据时表现出良好的性能,尤其适合大数据可视...

    jfreechart 1.0.10 demo 源代码

    JFreeChart是一个纯Java编写的图表库,它允许开发者在Java应用程序、Applet、JavaServer Pages (JSP) 或Servlets中生成高质量的图表。该项目始于2000年,经过多次迭代和改进,已经成为Java图表生成领域的经典之作。 ...

    JFreeChartApplet演示示例源码(eclipse部署可用)

    将 JFreeChart 与 Applet 结合使用,可以在网页上展示动态和交互式的图表,这对于数据分析和可视化非常有用。 **二、JFreeChartApplet 的使用** 1. **创建图表对象**:在 JFreeChartApplet 中,首先需要创建图表...

    JFreeChart Web例子

    2. **使用Java Applet**:虽然Java Applet现在已经不常用,但过去常常被用来在浏览器中直接绘制和交互JFreeChart。 3. **利用JavaScript库(如GWT或Vaadin)**:在客户端使用Java-to-JavaScript编译器,将...

    jfreechart图表分析.doc

    由于其丰富的功能和灵活性,JFreeChart 广泛应用于 Web 应用程序中,特别是在需要展示动态数据的场景下。 #### 二、JFreeChart 的优势 1. **开放源代码**:JFreeChart 是一个开源项目,开发者可以自由地使用和修改...

    JfreeChart

    3. **兼容性好**:支持 Java Swing 和 JavaFX,以及 Web 应用中的 Servlet 和 Applet。 4. **易用性**:API 设计简洁,便于理解和使用。 5. **国际化**:支持多语言显示,方便全球化的应用程序。 6. **良好的性能**...

    利用JFreeChart工具生成统计图的几种方法.doc

    JFreeChart 的优势在于其跨平台的特性,可以在各种 Java 应用中使用,包括应用程序、Applet、Servlet 和 JSP。 JFreeChart 还与 JFreeReport 和 JCommon 配合使用,提供完整的报表解决方案。JFreeReport 是一个报表...

    使用 JFreeChart来创建基于web的图表

    1. **使用Applet**:利用Java Applet来显示图表,但这种方法受限于客户端环境,并且随着现代浏览器逐渐停止对Java Applet的支持,其适用范围越来越有限。 2. **在服务器端生成图表图片**:这种方式更为灵活,兼容...

Global site tag (gtag.js) - Google Analytics