Publish a statistical curve on a Web page

Source: Internet
Author: User
Tags count implement inheritance new features versions
Statistics | Page One, INTRODUCTION
Make a very personalized homepage on the bustling Internet and deliver exciting information in a timely manner
In order to attract people of all colors and occupations from all over the world to watch the "foot" so that they can linger and be generous
Creators, is the dream of Internet surfers in their hearts. Tables, columns, provided using the HTML scripting language reasonably
table, font and paragraph formatting, multimedia support, etc., can make the content of the Web page clear, structured, readable
Strong, these already have many literature elaboration, here no longer repeat.

We know that in a variety of information, statistical information can be used more and more convincing information
, and the most intuitive way to express statistical data is to draw curves from these data. Literature [1] in considerable length
Describes how to write a statistical curve-drawing work with OLE server capabilities in the Windows operating system
, and the theme of this article is: How to use Java support for drawing, to write a curve based on statistical data
Line applet (applet) Plot, embedding Plot compiled bytecode file plot.class into H
TML script, which enables you to publish a statistical curve on a Web page.

II. Overview of Java programming

1.Java Language Specification

Java inherits the basic syntax of C + +, discarding some outdated features such as precompiled header files, macros
, conditional compilation, structure of data structure, enumeration body and federation, easy to lead to security problems of pointers to
and obscure multiple inheritance, for reference to the interface of Objective C and the ADA package (package), as well as the inner
The basic features required for Internet networking: Structural neutrality (any chip, any operating system can be shipped
The same version of Java programs), security (to avoid virus infection and prevent illegal access), multithreading, and networking
Communications and so on. In fact, the above features that are discarded by Java, such as pointers and multiple inheritance of classes, are too complex
and is cursed by C + + programmers; new features such as structural neutrality, security, multithreading, and network communication
And so on, is a C + + programmer very want to implement and very difficult to achieve the characteristics.

2.JDK

Windows programmers must know that Windows SDK (software development tools) is used to
Now, developers doing the JA va also know that writing Java applets uses JDK (Java development tools).
However, unlike the SDK, which uses a process-oriented approach such as architecture and functions, it provides different Windows programming interfaces,
The JDK provides Java programming interfaces in object-oriented ways, such as packages, interfaces, and classes. In this sense, JD
K is similar to MFC for Microsoft Visual C + + or Bor land C + +: It is a class library, a
An application framework, a seamless integration of solutions.

The first JDK to be published should be JDK 1.0.3α, which is the 1995 Sun Company's Worldwide ja
VA Program Design Grand Prix with HotJava 1.0.3α issued. In this release, the common package implementations are compared
Few, only Java.lang, J Ava.util and Java.io, while other packages on user interface and network communication
are provided in the HotJava. The release of version 1.0, which is provided to various Java platform development companies,
, the package is all independent out, a total of eight: Java.applet, ja va.awt, Java.awt.image, java.
Awt.peer, Java.io, Java.lang, java.net, and Java. Util and a Sun.tools
. Debug. The program in this article is written based on the 1.0 version. Due to 1.0.3α and 1.0 two versions of package arrangement differences
is so large that a program written in the previous version may be compiled in a later version of the compilation tool.
To make changes. At the end of August 1996, Sun was released on the internet in version 1.1, version 1.1 on the 1.0 base
Slightly improved on the first, two versions remain fully compatible.

Iii. design objectives and procedural concepts

Now let's go back to the subject of this article. First, consider writing a Java applet, whose interface and work
Can be described in this way:

The statistic title and statistic data are provided by the Param annotation of HTML, the number of statistic data is not limited;
The X and Y axes with scale are drawn according to the value of the data;
The coordinate value of the point is marked while connecting each data point with a line;
Provides options for drawing three sets of curves.

In response to the above requirements, consider constructing two classes to complete: curve class is used to implement the drawing of statistical curves
Aspects Plo T class is born in the applet, it is the program's small program class, by using curve class to complete the unified
The drawing of the gauge curve. Given the length, the following are the main points of implementation and their procedures. Readers who need all the source code
, please contact with the author.

1.Curve class

The curve structure is mainly based on the following considerations.

(1) Base class. Curve derives from the base class object of all Java classes, you can declare it without explicitly declaring it.


Class Curve {
......
}

(2) Drawing environment. According to Windows and other Windows operating system programming experience, to the display, printer, etc.
The output device drawing is implemented through the drawing environment. Because Java is intended for all platforms, including Windows
, UNIX, and Macintosh, their mapping environment, such as displays, is very different. So, Java like
How do you implement a drawing? Originally, Java in the java.awt package through the graphics class to provide a variety of drawing
Abstract class encapsulation of the device environment for the device. Using window s programming to contrast, the concept represented by graphics
is the device environment of Windows GDI (Device context), which is the TDC class of CDC class or Owl in MFC
。 With such a comparison, the graphics is better understood. That is, all drawn lines, rectangles,
Ellipses, polygons, setting fonts, drawing text, and so on, call the corresponding method in the Graphics class.
The following code passes the device Environment object G of the applet class plot to curve through the curve constructor to be
Other drawing methods are used:

Public Curve (Graphics g)
{
MYGC = g;
......
}

(3) Rendering algorithm. The drawing algorithm is to draw the statistical curve realization method, including the drawing title, coordinates
Axes, data lines, coordinates of data points, and their inverse process: clear these drawings (because when drawing other
Curve, you have to be sure to refresh all the content that was drawn earlier. Given the length, the following only gives the side of the display data
method to realize ShowData.

Public synchronized void ShowData (Vector v)
{
float maxy=0;
Mygc.setcolor (color.red);
int Xpoint;
int ypoint;
Determines the maximum value in a vector.
for (int i = 0; i < v.size (); i++)
{
float temp = ((float) v.elementat (i)). Floatvalue ();
if (Temp > Maxy)
{
Maxy = temp;
}
}
Determines the x-coordinate of the first data point.
Xpoint = Xorigin + (XSPACING/2);
int oldx = 0;
int oldy = 0;
Draw a small circle at the data point.
for int j = 0; J < V.size (); j + +)
{
Ypoint = yorigin (int) (AXISH/MAXY) *
((Float) V.elementat (j)). Floatvalue ());
Mygc.fillrect (Xpoint, Ypoint, 3, 3);
Lines are linked between data points.
if ((oldx!= 0) && (oldy!= 0))
{
Mygc.drawline (oldx-xspacing, Oldy, Xpoint, Ypoint)
;
}
Mygc.setcolor (Color.Blue);
Writes out the data value on the edge of the data point.
String coordstring =
Float.tostring (((Float) V.elementat (j)). Floatvalue (
));
Mygc.drawstring (coordstring, xpoint+5, yPoint-4);
Mygc.setcolor (color.red);
Xpoint + = xspacing;
OLDX = Xpoint;
Oldy = Ypoint;
}
}

2.Plot class

The plot structure is mainly based on the following considerations.

(1) derived from applets

A Java applet has and only one class is derived from the applet, as if in MFC, there must be and only
There is a class that derives from CWinApp. In addition, unlike MFC programs, whether or not the inclusion is derived from the APPL
The source program file (. java) of the ET class is named for the bytecode file (. Class) generated for use by the Web page.
is not the same as the derived class, but is not related to the source file name, which can cause confusion. For example, if a package
The file containing the plot class is Plottest.java, and the compiled bytecode file name is Plot.class, not
It's plottest.class. Finally, plot must be declared public, because an editing unit must have
And only one class is declared public.

public class Plot extends Applet
{
......
}

(2) User interface

Currently, Java provides the interfaces and classes required for the applet's user interface in java.awt (42 classes in total)
and 2 interfaces, which implement the various user interfaces we see in windows, such as menus, dialogs
boxes, and various controls, such as buttons, list boxes, check boxes, radio boxes, edit boxes, combo boxes, and so on. Given
This program requires the selection of a set of 3 sets of data to draw, you need to select a single box control. The Radio box is in the Java
. AWT is implemented jointly with CheckBox and CheckboxGroup two classes: When a checkbox is used alone, it is
check box, and adding it to the CheckboxGroup becomes a radio box.

public void Init ()
{
......
CBG = new CheckboxGroup ();
Radio button.
CB1 = new Checkbox ("Data 1", CBG, false);
CB2 = new Checkbox ("Data 2", CBG, false);
CB3 = new Checkbox ("Data 3", CBG, false);
}

(3) Layout manager

In Java, in order to make a small program in a variety of operating systems of the user interface has a consistent appearance, mining
Use the Layout manager (Layout Manager) to manage the relative position of the user interface. Java in JAVA.A
WT Package includes flow Layout Manager (FlowLayout), Boundary layout Manager (BorderLayout), card
Slice layout Manager (cardlayout), Grid layout Manager (GridLayout), and grid package layout manager
(gridbaglayout) 5 layout managers. If you want the 3 radio box buttons to be placed underneath the drawing graphic
By line, how do I write code? You can do this: first set the plot applet for the border layout
, and then create a panel object Cbpanel for the 3 radio box buttons to organize 3 controls and set up the CBP
Anel is the grid layout, and finally adds Cbpanel to the plot class and sets its direction to South (South).

public void Init ()
{
......
Set plot as the border layout.
SetLayout (New BorderLayout ());
Request Panel Object Cbpanel for 3 controls.
Cbpanel = new Panel ();
Set up the Grid layout manager and arrange it in 1x3 mode.
Cbpanel.setlayout (New GridLayout (1, 3));
Apply 1 panels for each button.
Cb1panel = new Panel ();
Cb1panel.add (CB1);
Cb2panel = new Panel ();
Cb2panel.add (CB2);
Cb3panel = new Panel ();
Cb3panel.add (CB3);
Add to the Unified 1 panel Cbpanel.
Cbpanel.add (Cb1panel);
Cbpanel.add (Cb2panel);
Cbpanel.add (Cb3panel);
Add Cbpanel to plot in the direction: graphics in the north, buttons in the south.
Add ("South", Cbpanel);
}

(4) Reading HTML parameters

Because this program uses statistical data and statistical headings to be stored in HTML documents as parameters for small programs
, so you need to read these parameters from HTML before you start drawing the graphic, before you start to show the caption and draw
Statistical curve. Among them, reads the statistic title and the statistic data realizes separately.

① statistics title, which can be in the form of:
<param value= "1-desc" value= "Monthly Visitor Statistics" >
1-desc represents the statistical title of the first set of data, and "Monthly visitor statistics" is the title content.

public string Readstringdata (string s)
{
String tempstring = null;
Integer param;
Boolean datapresent = true;
int i = 0;
Try
{
TempString = GetParameter (s + "-desc");
catch (Exception e)
{
System.out.println (e);
}
return tempstring;
}

② statistical data, the format can be:
<param value= "1-2" value= ">"
1-2 represents the second data point of the first set of data, and 14 represents the value of that data point.

Public Vector ReadData (String s)
{
Vector tempvector = new vector ();
Float param;
String tempdata = null;
Boolean datapresent = true;
int i = 0;
while (datapresent)
{
try {
TempData = GetParameter (S + "-" + (i+1));
}
catch (Exception e)
{
System.out.println (e);
}
if (TempData = null)
{
Datapresent = false;
} else {
param = float.valueof (tempdata);
Tempvector.addelement (param);
i + 1;
}
}
return tempvector;
}

(5) message loop

According to Windows programming experience, there is a message loop with the user interface to organize the message handling function handle to the user
Interface to respond to messages sent by the Unfortunately, there is no message map like MFC in JDK 1.0
Things like tables organize message loops. However, Java is developing rapidly, and in the near future there will be companies that will
It comes true! Now, we need to organize it ourselves: The Applet class has an action method in which it can pass
A if...then statement to respond to a different message.

Public boolean action (Event E, Object o)
{
Vector actionvector = new vector ();
String actionstring = new string ();
if (e.target instanceof Checkbox)
{
Message sent by data 1
if (cbg.getcurrent () = = CB1)
{
Actionvector = ReadData ("1");
actionstring = Readstringdata ("1");
System.out.println ("1");
}
The message sent by data 2
if (cbg.getcurrent () = = CB2)
{
Actionvector = ReadData ("2");
actionstring = Readstringdata ("2");
}
The message sent by data 3
if (cbg.getcurrent () = = CB3)
{
Actionvector = ReadData ("3");
actionstring = Readstringdata ("3");
}
int count = Actionvector.size ();
C.clearhashmarks ();
C.clearscreen ();
C.drawaxes (Curvewidth, curveheight);
C.makehashmarks (count);
C.showdata (Actionvector);
C.maketitle (actionstring);
Repaint ();
return true;
}
return false;
}

3.HTML Documentation and program presentation

The following is an example of an HTML instance that is used to observe the small program described above, and the results are shown in the drawings.

<applet code= "plot.class" width = height = 200>
<param name= "1-desc" value= "Monthly Visitor Statistics" >
<param name= "1-1" value= "ten" >
<param name= "1-2" value= "4" >
<param name= "1-3" value= ">"
<param name= "1-4" value= ">"
<param name= "1-5" value= "9" >
<param name= "1-6" value= "7" >
<param name= "1-7" value= ">"
<param name= "2-desc" value= "annual Visitor Statistics" >
<param name= "2-1" value= ">"
<param name= "2-2" value= ">"
<param name= "3-desc" value= "one-time purchase tax statistics" >
<param name= "3-1" value= "10000" >
<param name= "3-2" value= "130000" >
<param name= "3-3" value= "16100" >
<param name= "3-4" value= "14000" >
</applet></center></body>
<address>
Li Zhenwen, Zhenwen@Websoft.com
</address>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.