Exemplo n.º 1
0
        protected void BuildLegend(ParametersBase parameters)
        {
            LegendTitle = parameters.LegendTitle;
            IColorSelector color = parameters.Color;

            if (String.IsNullOrEmpty(LegendTitle))
            {
                LegendTitle = "Color";
            }
            if (LegendTitle.Length > 15)
            {
                LegendTitle = LegendTitle.Substring(0, 13) + "...";
            }

            float         maxWidth    = 0f;
            List <string> legendTexts = new List <string>();

            if (color is ColorGradient)
            {
                ColorGradient gradient = color as ColorGradient;
                legendTexts = gradient.Stops.Select(s => parameters.LegendTextFormater.Format(s.Value)).ToList();
            }
            else if (color is ColorPalette)
            {
                ColorPalette palette = color as ColorPalette;
                legendTexts = palette.Colors.Keys.Select(s => parameters.LegendTextFormater.Format(s)).ToList();
            }

            var sizes = legendTexts.Select(t => DefaultFont.RenderText(t));

            maxWidth         = sizes.Max(s => s.Width);
            LegendTextHeight = sizes.Max(s => s.Height);

            //Legend title
            var titleSize = DefaultFontBold.RenderText(LegendTitle);

            LegendTitleWidth  = titleSize.Width;
            LegendTitleHeight = titleSize.Height;

            if (parameters.LegendPosition == Position.Right || parameters.LegendPosition == Position.Left)
            {
                maxWidth = Math.Max(maxWidth, (float)(LegendTitleWidth - LegendThickness - SmallMargin));
            }

            switch (parameters.LegendPosition)
            {
            case Position.Left:
            case Position.Right:
                PlotArea        = PlotArea.WithWidth(PlotArea.Width - Margin - LegendThickness - SmallMargin - maxWidth);
                LegendTitleArea = new Rect(
                    PlotArea.Left + PlotArea.Width + Margin,
                    PlotArea.Top,
                    LegendTitleWidth,
                    LegendTitleHeight);
                LegendArea = new Rect(
                    PlotArea.Left + PlotArea.Width + Margin,
                    PlotArea.Top + LegendTitleHeight + SmallMargin,
                    LegendThickness,
                    Math.Max(PlotArea.Height - LegendTitleHeight - SmallMargin, 0));
                break;

            case Position.Top:
            case Position.Bottom:
                PlotArea = PlotArea.WithHeight(PlotArea.Height - Margin - LegendTitleHeight - SmallMargin - LegendThickness);
                if (color is ColorGradient)
                {
                    PlotArea = PlotArea.WithHeight(PlotArea.Height - SmallMargin - LegendTextHeight);
                }

                LegendTitleArea = new Rect(
                    Math.Max(PlotArea.Left + (PlotArea.Width - LegendTitleWidth) / 2, 0),
                    PlotArea.Top + PlotArea.Height + Margin,
                    LegendTitleWidth,
                    LegendTitleHeight);
                LegendArea = new Rect(
                    PlotArea.Left,
                    PlotArea.Top + PlotArea.Height + Margin + LegendTitleHeight + SmallMargin,
                    PlotArea.Width,
                    LegendThickness);
                break;

            default:
                break;
            }
        }