Ejemplo n.º 1
0
        private void DrawChart()
        {
            if (plotArea == null)
            {
                return;
            }
            if (Series == null || Series.Count() == 0)
            {
                return;
            }
            if (Series.FirstOrDefault().Count() == 0)
            {
                return;
            }
            plotArea.Children.Clear();

            ChartPlotInfo plotInfo = CalculatePlotInfo();

            plotArea.Width  = plotInfo.Width + plotInfo.LeftMarginWidth + plotInfo.RightMarginWidth;
            plotArea.Height = plotInfo.MaxHeight + plotInfo.FooterHeight + plotInfo.HeaderHeight;

            DrawLegend();
            DrawBackground(plotInfo);
            DrawGrid(plotInfo);
            foreach (var seria in Series)
            {
                DrawSeria(seria, plotInfo, BrushGenerator.GetBrush());
            }
        }
Ejemplo n.º 2
0
        private void DrawBezierSegment(Point p1, Point p2, Point p3, ChartPlotInfo info, Brush brush)
        {
            //var line = new BezierSegment();
            //line.Point1 = GeometricHelper.PointToChart(p1, info);
            //line.Point2 = GeometricHelper.PointToChart(p2, info);
            //line.Point3 = GeometricHelper.PointToChart(p3, info);
            //line.Stroke = brush;
            //line.StrokeThickness = 2;

            //RenderElement(line);
        }
Ejemplo n.º 3
0
        private void DrawBackground(ChartPlotInfo plotInfo)
        {
            var chartBack = new Rectangle();

            chartBack.Width  = plotInfo.MaxWidth;
            chartBack.Height = plotInfo.MaxHeight;
            Canvas.SetLeft(chartBack, plotInfo.LeftMarginWidth);
            Canvas.SetTop(chartBack, plotInfo.HeaderHeight);
            chartBack.Fill = ChartBackground;

            plotArea.Children.Add(chartBack);
        }
Ejemplo n.º 4
0
        private ChartPlotInfo CalculatePlotInfo()
        {
            ChartPlotInfo plotInfo = new ChartPlotInfo();

            DateTime minDate  = Series.FirstOrDefault().Min(element => element.Argument);
            DateTime maxDate  = Series.FirstOrDefault().Max(element => element.Argument);
            double   maxValue = Series.FirstOrDefault().Max(element => element.Value);

            foreach (var seria in Series.Skip(1))
            {
                var minX = seria.Min(el => el.Argument);
                if (minX < minDate)
                {
                    minDate = minX;
                }
                var maxX = seria.Max(el => el.Argument);
                if (maxX > maxDate)
                {
                    maxDate = maxX;
                }
                var maxY = seria.Max(el => el.Value);
                if (maxY > maxValue)
                {
                    maxValue = maxY;
                }
            }

            plotInfo.MaxX = maxDate;
            plotInfo.MaxY = maxValue;
            plotInfo.MinX = minDate;
            plotInfo.MinY = 0;

            plotInfo.XStep = new TimeSpan((int)Math.Ceiling(plotInfo.DaysWidth / MAX_DESIRED_X_VALUES), 0, 0, 0);

            var valuesPower        = Math.Log10(plotInfo.MaxY * 10);
            var valuesPowerRounded = Math.Ceiling(valuesPower);

            plotInfo.YStep = 100 / Math.Pow(10, valuesPowerRounded) / 2;
            plotInfo.MaxY  = Math.Ceiling(plotInfo.MaxY / plotInfo.YStep) * plotInfo.YStep;


            plotInfo.FooterHeight     = 50;
            plotInfo.HeaderHeight     = 50;
            plotInfo.LeftMarginWidth  = 40;
            plotInfo.RightMarginWidth = 20;

            return(plotInfo);
        }
Ejemplo n.º 5
0
        private void DrawGrid(ChartPlotInfo info)
        {
            var frameBrush = Brushes.SteelBlue;
            var gridBrush  = Brushes.LightSteelBlue;

            //Grid
            for (DateTime x = info.MinX; x < info.MaxX; x += info.XStep)
            {
                if (x > info.MinX)
                {
                    DrawLine(x, 0, x, info.PhysHeight, info, gridBrush, true);
                }
                var labelModel = new LabelModel(info)
                {
                    Text  = string.Format("{0:dd MMM yyyy}", x),
                    XPhys = x,
                    YPix  = info.MaxHeight + ARGUMENT_LABEL_VERTICAL_SHIFT + info.HeaderHeight
                };

                DrawLabel(labelModel, info);
            }

            for (var y = info.MinY; y <= info.MaxY; y += info.YStep)
            {
                DrawLine(0, y, info.Width, y, info, gridBrush, true);

                var labelModel = new LabelModel(info)
                {
                    Text  = string.Format("{0:F1}", y),
                    XPix  = VALUE_LABEL_HORIZONTAL_SHIFT,
                    YPhys = y
                };
                DrawLabel(labelModel, info);
            }

            //Frame
            //left
            DrawLine(0, 0, 0, info.MaxY, info, frameBrush);
            //right
            DrawLine(info.Width, 0, info.Width, info.MaxY, info, frameBrush);
            //bottom
            DrawLine(0, info.MaxY, info.Width, info.MaxY, info, frameBrush);
            //top
            DrawLine(0, 0, info.Width, 0, info, frameBrush);
        }
Ejemplo n.º 6
0
        private void DrawLine(double x1, double y1, double x2, double y2, ChartPlotInfo info, Brush brush, bool isDashed = false)
        {
            var line = new Line();

            line.X1              = GeometricHelper.XCoordToChart(x1, info);
            line.X2              = GeometricHelper.XCoordToChart(x2, info);
            line.Y1              = GeometricHelper.YCoordToChart(y1, info);
            line.Y2              = GeometricHelper.YCoordToChart(y2, info);
            line.Stroke          = brush;
            line.StrokeThickness = 2;
            if (isDashed)
            {
                line.StrokeThickness  = 1;
                line.StrokeDashOffset = 5;
            }

            RenderElement(line);
        }
Ejemplo n.º 7
0
        private void DrawCircle(DateTime dateTime, double p, ChartPlotInfo info, Brush brush)
        {
            var circle = new Ellipse();

            circle.ToolTip = string.Format("Дата: {0:yyyy MMM dd}, значення: {1}", dateTime, p);

            circle.Stroke = brush;
            circle.Fill   = Brushes.White;

            circle.Width           = 10;
            circle.Height          = 10;
            circle.StrokeThickness = 2;
            var x = GeometricHelper.DateToChart(dateTime, info) - circle.Width / 2;
            var y = GeometricHelper.YCoordToChart(p, info) - circle.Height / 2;

            Canvas.SetTop(circle, y);
            Canvas.SetLeft(circle, x);

            RenderElement(circle);
        }
Ejemplo n.º 8
0
        private void DrawSeria(IEnumerable <SeriesDateBasedElement> seria, ChartPlotInfo info, Brush brush)
        {
            var timeOrderedSeria = seria.OrderBy(x => x.Argument).ToList();
            var prevItem         = timeOrderedSeria.First();

            foreach (var item in timeOrderedSeria.Skip(1))
            {
                DrawLine(
                    prevItem.Argument,
                    prevItem.Value,
                    item.Argument,
                    item.Value,
                    info,
                    brush);
                prevItem = item;
            }
            foreach (var item in timeOrderedSeria)
            {
                DrawCircle(item.Argument, item.Value, info, brush);
            }
        }
Ejemplo n.º 9
0
        private void DrawLine(DateTime x1, double y1, DateTime x2, double y2, ChartPlotInfo info, Brush brush, bool isDashed = false)
        {
            var line = new Line();

            line.X1              = GeometricHelper.DateToChart(x1, info);
            line.X2              = GeometricHelper.DateToChart(x2, info);
            line.Y1              = GeometricHelper.YCoordToChart(y1, info);
            line.Y2              = GeometricHelper.YCoordToChart(y2, info);
            line.Stroke          = brush;
            line.StrokeThickness = 2;
            if (isDashed)
            {
                DoubleCollection dashes = new DoubleCollection(2);
                dashes.Add(5);
                dashes.Add(5);
                line.StrokeThickness = 1;
                line.StrokeDashArray = dashes;
            }

            RenderElement(line);
        }
Ejemplo n.º 10
0
        private void DrawLabel(LabelModel labelModel, ChartPlotInfo info, LabelAlign align = LabelAlign.Left)
        {
            switch (align)
            {
            case LabelAlign.Left:
                break;

            case LabelAlign.Center:
                labelModel.XPix -= labelModel.Text.Length * 4;
                break;

            case LabelAlign.Right:
                break;

            default:
                break;
            }

            var textBlock = new TextBlock();

            textBlock.Text = labelModel.Text;

            RenderElement(textBlock, labelModel.XPix, labelModel.YPix);
        }
Ejemplo n.º 11
0
 public LabelModel(ChartPlotInfo info)
 {
     this.info = info;
 }