Beispiel #1
0
        /// <summary>
        /// 生成Y轴标签
        /// </summary>
        /// <param name="label">标签值</param>
        /// <param name="rotate">旋转角度</param>
        /// <returns></returns>
        protected TextBlock CreateYLabel(Axis.RadarAxis axis)
        {
            var textBlock = new TextBlock();

            textBlock.Text          = axis.DisplayName ?? axis.BindName;
            textBlock.Foreground    = new SolidColorBrush(Colors.Black);
            textBlock.TextAlignment = TextAlignment.Center;
            var transformgroup = new TransformGroup();

            //旋转角度
            var rotatetransform = new RotateTransform();

            rotatetransform.Angle   = axis.Rotate <= 180 && axis.Rotate > 0 ? axis.Rotate - 90 : axis.Rotate + 90;
            rotatetransform.CenterX = 0.5;
            rotatetransform.CenterY = 0.5;
            transformgroup.Children.Add(rotatetransform);

            //平移位置
            var r   = (rotatetransform.Angle % 360) * (Math.PI / 180);
            var cos = -Math.Abs(Math.Cos(r));
            var sin = r > 0 && r < Math.PI ? -Math.Abs(Math.Sin(r)) : Math.Abs(Math.Sin(r));
            var translatetransform = new TranslateTransform();

            translatetransform.X = cos * (textBlock.ActualWidth / 2);
            translatetransform.Y = sin * (textBlock.ActualWidth / 2);
            transformgroup.Children.Add(translatetransform);

            var x = rotatetransform.Angle > axis.Rotate ? axis.EndPoint.X + axis.RotateCos * textBlock.ActualHeight : axis.EndPoint.X - 2; // -Math.Abs(axis.RotateCos) * textBlock.ActualWidth / 2;
            var y = rotatetransform.Angle > axis.Rotate ? axis.EndPoint.Y + axis.RotateSin * textBlock.ActualHeight : axis.EndPoint.Y + 2; // -Math.Abs(axis.RotateCos) * textBlock.ActualHeight / 2;

            textBlock.SetValue(Canvas.LeftProperty, x);
            textBlock.SetValue(Canvas.TopProperty, y);
            textBlock.RenderTransformOrigin = new Point(0.5, 0.5);
            textBlock.RenderTransform       = transformgroup;
            return(textBlock);
        }
Beispiel #2
0
        /// <summary>
        /// 以中间为点画线
        /// </summary>
        /// <param name="center"></param>
        private void DrawLine()
        {
            var mappings = ItemMappings.Where <Model.ItemMapping>(p => p.DataMember == Model.ItemMapping.EnumDataMember.Y).ToArray <Model.ItemMapping>();

            VerticalCount = mappings.Length;
            var ynames = mappings.Select <Model.ItemMapping, string>(p => p.MemberName).ToArray <string>();

            var angle = Angle;

            var center = Center;
            //横轴个数
            var hcount = this.HorizontalCount;


            //x轴每一小格的长度
            var xstep   = Radius / hcount;
            var yaxises = new Axis.RadarAxis[VerticalCount];

            var sources = DataContext as System.Collections.ICollection;
            var dataDic = Common.Helper.GetMaxandNinValue(sources, ynames);

            //从90度起开始画线条
            //画星状线
            for (var i = 0; i < yaxises.Length; i++)
            {
                var axis    = yaxises[i] = new Axis.RadarAxis();
                var mapping = mappings[i];

                var path = new Path();
                //path.StrokeDashArray.Add(4);
                axis.AxisShap    = path;
                axis.BindName    = mapping.MemberName;
                axis.DisplayName = mapping.DisplayName;

                var dic = dataDic[axis.BindName];
                if (dic[0].HasValue)
                {
                    axis.MaxValue = dic[1].Value;
                    axis.MinValue = Common.Helper.CheckMinValue(dic[0].Value);
                }
                else
                {
                    axis.ItemCount = sources.Count;//数值个数
                }

                axis.Length = Radius;
                axis.Rotate = i * angle;

                /////设定Y标签
                //if (this.YLabels != null && i < this.YLabels.Length)
                //{
                //    axis.Label = this.YLabels[i];
                //}

                //计算纵轴离中心的偏移量
                var offsety = axis.RotateSin * Radius;
                var offsetx = axis.RotateCos * Radius;

                axis.StartPoint = center;
                axis.EndPoint   = new Point(center.X + offsetx, center.Y + offsety);
                axis.AType      = Axis.AxisType.YRadar;
                axis.Stroke     = ForeColor;

                //如果画基线
                if (IsDrawBaseLine)
                {
                    var geo = new PathGeometry();
                    path.Data = geo;
                    var fig = new PathFigure();
                    geo.Figures.Add(fig);
                    fig.StartPoint = axis.StartPoint;
                    fig.Segments.Add(new LineSegment()
                    {
                        Point = axis.EndPoint
                    });

                    AddChild(axis.AxisShap);
                    //生成Y轴标签
                    var label = CreateYLabel(axis);
                    AddChild(label);
                }
                this.Axises.Add(axis);
            }

            //如果画基线
            if (IsDrawBaseLine)
            {
                //当线条少于5条时。无法画多边形。因为四条就是一个正方形。所以会默认用圆形。
                if (IsCircle == false && VerticalCount > 4)
                {
                    for (var i = 0; i < hcount; i++)
                    {
                        var xaxis = new Axis.RadarAxis()
                        {
                            AType = Axis.AxisType.XRadar, Stroke = ForeColor
                        };
                        var shap = new Polygon();
                        shap.Stroke = xaxis.Stroke;
                        //shap.StrokeDashArray.Add(4);
                        xaxis.AxisShap = shap;

                        this.Axises.Add(xaxis);
                        var step = xstep * (i + 1);

                        foreach (var yaxis in yaxises)
                        {
                            //纵坐标上的点偏移量
                            var xoffsetx = yaxis.RotateCos * step;
                            var xoffsety = yaxis.RotateSin * step;

                            var p = new Point(Center.X + xoffsetx, Center.Y + xoffsety);
                            shap.Points.Add(p);
                        }

                        AddChild(shap);
                    }
                }
                else
                {
                    for (var i = 0; i < hcount; i++)
                    {
                        var xaxis = new Axis.RadarAxis()
                        {
                            AType = Axis.AxisType.XRadar, Stroke = ForeColor
                        };
                        var path = new Path();
                        xaxis.AxisShap = path;
                        //path.StrokeDashArray.Add(4);
                        this.Axises.Add(xaxis);

                        var step = xstep * (i + 1);
                        var cirl = new EllipseGeometry();
                        path.Data    = cirl;
                        cirl.Center  = Center;
                        cirl.RadiusX = cirl.RadiusY = step;

                        AddChild(xaxis.AxisShap);
                    }
                }
            }
        }