如何使用FastReport.Net 2019.4報(bào)表腳本創(chuàng)建矢量圖形
在本文的第一部分,我們回顧了FastReport.Net 2019.4在矢量圖形方面的創(chuàng)新。現(xiàn)在可以通過貝塞爾曲線構(gòu)造折線和多邊形。在本文中,我們將考慮使用報(bào)表腳本創(chuàng)建曲線的可能性。
目前,有兩種方法可以從代碼創(chuàng)建曲線:使用PolyLineObject對(duì)象或從SVG加載多邊形。
如您所知,報(bào)表腳本可適用于任何報(bào)表對(duì)象,因此我們可以使用PolyLineObject設(shè)置點(diǎn)并將它們連接起來以創(chuàng)建形狀。在顯示對(duì)象之前運(yùn)行代碼,這意味著您需要為Polygon對(duì)象創(chuàng)建BeforePrint事件處理程序。讓我們看一個(gè)真實(shí)的例子。
將Polygon對(duì)象添加到報(bào)表頁(yè)面。為了不創(chuàng)建形狀的關(guān)鍵點(diǎn),只需按Esc。在對(duì)象屬性檢查器中,選擇事件“Events”。創(chuàng)建BeforePrint事件處理程序:
讓我們?cè)谔幚沓绦蛑芯帉懸韵麓a:
private void Polygon4_BeforePrint(object sender, EventArgs e) { // Create a star shape int spikes = 5; //Number of spikes PolyLineObject obj = sender as PolyLineObject; PolyLineObject.PolyPointCollection points = obj.Points; points.Clear(); const float outerRadius = 70; //External radius const float innerRadius = 30; //Internal radius float rot = (float)(Math.PI / 2 * 3); // Tilt angle const float cx = 0; const float cy = 0; float x = cx; float y = cy; float step = (float)(Math.PI / spikes); // Vertex creation step obj.Width = 100; obj.Height = 100; obj.CenterX = 50; obj.CenterY = 50; points.Add(new PolyLineObject.PolyPoint(cx, cy - outerRadius)); //Добавляем точки for (int i = 0; i < spikes; i++) { // Coordinates of interior points x = cx + (float)Math.Cos(rot) * outerRadius; y = cy + (float)Math.Sin(rot) * outerRadius; points.Add(new PolyLineObject.PolyPoint(x, y)); rot += step; // Next point // Coordinates of external points x = cx + (float)Math.Cos(rot) * innerRadius; y = cy + (float)Math.Sin(rot) * innerRadius; points.Add(new PolyLineObject.PolyPoint(x, y)); rot += step; } }
結(jié)果,我們得到了一個(gè)五角星:
在上面的示例中,我們計(jì)算了構(gòu)建形狀的坐標(biāo),但是如果您已經(jīng)有一個(gè)坐標(biāo)列表,那么只需將它們添加到點(diǎn)集合中:points.Add(new PolyLineObject.PolyPoint(x, y));
但是如果形狀比上面的例子復(fù)雜得多呢?創(chuàng)建它的代碼量太大。在這種情況下,您可以使用對(duì)象SVG。例如,它的路徑屬性。我們可以將svg圖像路徑的一組元素轉(zhuǎn)換為點(diǎn),并使用它們構(gòu)建多邊形。考慮貝塞爾曲線很重要。如何做到這一點(diǎn),您將在下面的代碼中看到:
private void PolyLine2_BeforePrint(object sender, EventArgs e) { PolyLineObject obj = sender as PolyLineObject; PolyLineObject.PolyPointCollection points = obj.Points; points.Clear (); // Svg builder along the way SvgPathBuilder builder = new SvgPathBuilder (); // Load the list of segments from the string SvgPathSegmentList list = builder.ConvertFromString ("M91.734 10.5L66.6384 59.025 10.5 66.8258l40.643 37.749-9.5696 53.327 50.2094-25.1932 50.234 25.1582-9.6124-53.321 40.6154-37.778-56.1505-7.76z") as SvgPathSegmentList; GraphicsPath _path = new GraphicsPath (); foreach (SvgPathSegment segment in list) { segment.AddToPath (_path); } PolyLineObject.PolyPoint point = null; for (int i = 0; i < _path.PointCount; i++) { PointF pnt = _path.PathPoints[i]; byte type = _path.PathTypes[i]; // If you have a curve, you have to consider three contractors. if (type == 3) { PointF pnt1 = _path.PathPoints[i]; PointF pnt2 = _path.PathPoints[i + 1]; PointF pnt3 = _path.PathPoints[i + 2]; i += 2; // Curvature to the right point.RightCurve = new PolyLineObject.PolyPoint (pnt1.X - point.X, pnt1.Y - point.Y); //Point point = new PolyLineObject.PolyPoint (pnt3.X, pnt3.Y); // Curvature to the left point.LeftCurve = new PolyLineObject.PolyPoint (pnt2.X - point.X, pnt2.Y - point.Y); } else { // Ordinary point point = new PolyLineObject.PolyPoint (pnt.X, pnt.Y); } // Add points points.Add (point); } obj.CenterX = 0; obj.CenterY = 0; obj.RecalculateBounds (); obj.Top = 0; obj.Left = 0; } }
這段代碼將向我們展示這種類型的五角星:
在SvgPathSegmentList類型的列表中,我們放置SVG文件的path標(biāo)記中的元素。然后,我們獲取點(diǎn)的坐標(biāo)并將它們添加到PolyLine對(duì)象。在此示例中,我們以直線段顯示對(duì)象,但您也可以使用曲線,例如:
...
SvgPathSegmentList list = builder.ConvertFromString ("m101.87775,57.26873c31.12829,-82.10042 153.08994,0 0,105.55768c-153.08994,-105.55768 -31.12829,-187.65809 0,-105.55768z") as SvgPathSegmentList;
...
結(jié)果,我們得到:
有必要考慮坐標(biāo)平面是Polyline或Polygon對(duì)象所在的波段。因此,在放置物體的波段中的哪個(gè)位置并不重要。它將根據(jù)指定的坐標(biāo)顯示。
使用我們考慮從腳本創(chuàng)建多邊形的第二種方法,您將能夠顯示先前創(chuàng)建的矢量圖。無需花費(fèi)大量時(shí)間手動(dòng)創(chuàng)建多邊形。
產(chǎn)品介紹 | 下載試用 | 優(yōu)惠活動(dòng) | 在線客服