Using XML to implement universal web report printing (5)

xiaoxiao2021-03-06  107

Program expansion:

So how do you print some special forms of charts, which already mentioned above, which uses this solution that can be very convenient to define the labels you need, in theory, the special chart of any style can be printed. Therefore, this article intends to detail the specific process of adding your own defined label expansion printing format.

First assume that our customers are basically satisfied after watching the printed effect, but there is still a little short, if you need to print some charts? For example, a line diagram, a K-line diagram, a pie chart, a histogram, and the like. With our existing tags, we must first expand our label library to make it more expressive. Here, I will only plan to let our print control learn to draw simple line charts, I hope the reader can give an anti-three to create other various printing effects.

The most basic line diagram is composed of a line connected by the X coordinate axis, Y coordinate axis, and a series of points, so I defined the following tags:

1. Linechart: Like the Table, the Text Tag, for the pattern root label.

Property: None

2. Coordinate: coordinates.

Property: None

3. XCoordinate: X-axis coordinate line

Attributes:

# x: starting point X coordinate value

# y: The starting point Y coordinate value

# Length: Length value

# stroke: thick

# color: Color

# arrow: Is there an arrow?

4. Ycoordinate: Y-axis coordinate line

Attribute: with XCoordinate.

5. Scale: Turning

Tag content: text displayed on the scale

Attributes:

# Length: Distance Point Length Value

# hT: Tariff Highness

# width: Turning Width

# color: Color

# fontsize: Font size

6. Chart: chart root

Property: None

7. Lines: line segment

Attribute value:

# stroke: thick

# color: Color

8. Point: Point

Attribute value:

# x: x coordinate value

# y: y coordinate value

# RADIUS: radius

# color: Color

The structure is shown below:

Below is an XML line diagram example of the label just defined:

100 200 300 400 500 600 700

100 200 300

"400" y = "600" RADIUS = "5" color = "black" /> Complete the definition of the label, the next step is to modify our program, let him "read" these labels.

First, we will add a new class of LineChart to the project, just like the Table, Text class, which is also inherited from the Printelement class, which also overloads the Draw virtual method. code show as below:

using System; using System.Xml; using System.Drawing; using System.Drawing.Drawing2D; namespace RemotePrint {public class LineChart: PrintElement {private XmlNode chart; public LineChart (XmlNode Chart) {chart = Chart;} public override bool Draw ( Graphics g) {Drawcoordinate (G, Chart ["COORDINATE"]); // Painting Scarner Drawchart (G, Chart ["Chart"]); Return False;} Private Void Drawcoordinate (Graphics G, XMLNode Coo) {Drawxcoor G, COO ["XCoordinate"]); // Draw X Coordinate Drawyc OR (G, COO ["Ycoordinate"]); // Draw Y Coordinate} Private Void Drawxcoor (Graphics G, XMLNode Xcoo) {Int x = Int.Parse (Xcoo.attributes ["x"]. innertext); int y = int.parse (xcoo.attributes ["y"]. innerText); intlene = int.parse (Xcoo.attributes ["length"]. innertext) Bool arrow = BOOL.PARSE (Xcoo.attributes ["arrow"]. innerText); int stroke = int.parse (xcoo.attributes ["stroke"]. innerText); color color = color.fromname (Xcoo.attributes "color"]. innerText); Pen Pen = new pen (color, (float) stroke); if (arrow) // is an arrow {adjustablerowcap arrow = new adjustableArrowcap ((float) (Stroke * 1.5 1.5), Float) (stroke * 1.5 2), true); pen.customedcap = arrow;} g.drawline (Pen, x, y, x length, y); // Painting Split File: // Draw Foreach (XMLNode Scale in xcoo.childnodes) {int LEN = int.parse (Scale.attributes ["Length"]. InnerText; int.com = int.parse (scale.attributes ["height"]. Innertext); int width = int. PARSE (Scale.attributes ["Width"]. InnerText); int FontSIZE = INT.PARS (Scale.attributes ["fontsize"]. InnerText); Color CLR = Color.FromName (Scale.attributes ["color"]. InnerText ); string name = scale.innertext; Pen P = new Pen (CLR, (float) width; g.drawline (p, x

LEN, Y, X LEN, Y - Height; Font Font = New Font ("Arial", (FLOAT) FONTSIZE); G.DrawString (Name, Font, New Solidbrush (CLR), (FLOAT) (X LEN - 10), (FLOAT) (Y 10));}}}} private void drawycoor (graphics g, xmlnode ycoo) {int x = int.parse (Ycoo.attributes ["x"]. InnerText); int y = int = int .PARSE (YCOO.ATTRIBUTES ["Y"]. InnerText); int length = int.parse (Ycoo.attributes ["length"]. InnerText); Bool Arrow = bool.parse (Ycoo.attributes ["arrow"]. InnerText); int stroke = int.parse (Ycoke "]. InnerText); color color = color.fromname (Ycoo.attributes [" color "]. InnerText; pen pen = new pen (color, Float); if (arrow) // Is there an arrow {adjustableArrowcap arrow = new adjustableArrowcap (FLOAT) (stroke * 1.5 3), (stroo * 1.5 3), true); pen.customedcap = Arrow;} g.drawline (Pen, X, Y, X, Y Length); // Painting Split File: // Draw Foreach (XMLNode Scale in Ycoo.childNodes) {Int Len = Int.Parse (Scale.attributes ["Length"]. innerText); int Height = int.parse (scale.attributes ["height"]. innerText); int width = int.parse (Scale.attribut ES ["Width"]. InnerText); int FontSIZE = INT.PARS (Scale.attributes ["Fontsize"]. InnerText); Color CLR = Color.FromName (Scale.attributes ["Color"]. innerText; String Name = Scale.innertext; Pen P = New Pen (CLR, (FLOAT) Width; g.drawline (P, X, Y LEN, X Height, Y LEN); Font Font = New Font ("Arial", (float); stringformat sf = new stringFormat (); sf.alignment = stringalignment.far; Rectanglef Rect = new Rectanglef (x - 100), (float) (Y LEN - 25), 90F, 50f); sf.LineAlignment = stringalignment.center;

G. DrawString (Name, Font, New Sofidbrush (CLR), RECT, SF);}}} Private Void Drawchart (Graphics G, XMLNode Chart) {Drawlines (g, lines);} } private void DrawLines (Graphics g, XmlNode lines) {int Stroke = int.Parse (lines.Attributes [ "stroke"] InnerText.); Point [] points = new Point [lines.ChildNodes.Count]; Color linecolor = Color .Fromname (lines.attributes ["color"]. Innertext); for (int i = 0; i

Switch (Element.name) {CASE "text": printelement = new text (element); Break; case "table": printelement = new table (element); Break; case "linechart": // Added linechartprintelement = new Linechart (Element); Break; default: printelement = new printelement (); Break;}

Replace the Table tag in the original XML file and its child tags to the next paragraph, then compile the program, the effect is as follows:

Now, our print control can print the drawline map, because we use the Abstract Factory design mode, divide the resolution of the prints and formats of the report so that this program has a very convenient expansion capability, if you need a new new The formal chart, then you need to define the label, write a parsing class, and add a CASE to this class in PASER to get it, the code inside the PrintControl does not need to be rewritten.

转载请注明原文地址:https://www.9cbs.com/read-122907.html

New Post(0)