登陆

在FastReport.Net 2019.4报表脚本中创立矢量图形

admin 2019-10-31 227人围观 ,发现0个评论

目前,有两种方法可以从代码创建曲线:使用PolyLineObject对象或从SVG加载多边形。

如您所知,报表脚本可适用于任何报表对象,因此我们可以使用PolyLineObject设置点并将它们连接起来以创建形状。在显示对象之前运行代码,这意味着您需要为Polygon对象创建BeforePrint事件处理程序。让我们看一个真实的例子。

将Polygon对象添加到报表页面。为了不创建形状的在FastReport.Net 2019.4报表脚本中创立矢量图形关键点,只需按Esc。在对象属性检查器中,选择事件“Events”。创建BeforePrint事件处理程序:



让我们在处理程序中编写以下代码:

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;
}
}

结果,我们得到了一个五角星:



在上面的示例中,我们计算了构建形状的坐标,但是如果您已经有一个坐标列表,那么只需将它们添加到点集体悟道合中:points.Add(new PolyLineObject.PolyPoint(x, y));

但是如果形状比上面的例子复杂得多呢?创建它的代码量太大。在这种情况下,您可以使用对象SVG。例如,它的路径属性。我们可以将svg图像路径的一组元素转换为点,并使用它们构建多边形。考虑贝塞尔曲线很重要。如何做到这一点,您将在下面的代码中看到:

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;在FastReport.Net 2019.4报表脚本中创立矢量图形
for (int i = 0; i < _path.PointCount; i++) {
PointF pnt = _path.PathPoints[i];
byte type = _path.PathTypes[i];
// If you have a curve, you在FastReport.Net 2019.4报表脚本中创立矢量图形 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标记中的元素。然后,我们获取点的坐标并将它们添加到PolyLine对象。在此示例中,我们以直线段显示对象,但您也可以使用曲线,例如:

...

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;

...

结果,我们得到:



有必要考虑坐标平面是Polyline或Polygon对象所在的波段。因此,在放置物体的波段中的哪个位置并不重要。它将根据指定的坐标显示。

使用我们考虑从脚本创建多边形的第二种方法,您将能够显示先前创建的矢量图。无需花费大量时间手动创建多边形。

点击下方“了解更多”,获取详情!

请关注微信公众号
微信二维码
不容错过
Powered By Z-BlogPHP