С помощью MSChart, как это сделать по краям гистограммы? - PullRequest
0 голосов
/ 30 ноября 2011

Я использую System.Drawing для создания следующего изображения, но я хочу переключиться на MSChart (System.Windows.Forms.DataVisualization.Charting).

Кто-нибудь смог создать закругленные края, которые вы видите в моем баре? Оба бара (синий и серый) имеют закругленные гладкие края.

Simple rounded edged graph created by drawing the bar chart manually

ОБНОВЛЕНИЕ: (Добавление метода, возвращающего мою диаграмму, которая нуждается в кривых)

    private MSChart.Chart createChart3(double dataPointYvalue, string chartName, MSChart.Axis yAxis, SysDraw.Size sizeChart)
{
    // Chart
    // --------------------------------
    MSChart.Chart chart6 = new MSChart.Chart();
    chart6.BorderlineWidth = 0;
    chart6.BorderSkin.BackColor = SysDraw.Color.Empty;
    //chart6.Name = chartName;
    //chart6.Size = new SysDraw.Size(720, 90);
    chart6.Width = 720;
    chart6.Height = 90;
    chart6.AntiAliasing = MSChart.AntiAliasingStyles.All;
    chart6.TextAntiAliasingQuality=MSChart.TextAntiAliasingQuality.High;
    chart6.IsSoftShadows = false;

    // ChartAreas collection
    // --------------------------------
    string chartAreaName = "Default";
    SysDraw.Font labelFont = new SysDraw.Font("Arial Narrow", 10,SysDraw.FontStyle.Regular);
    SysDraw.Font labelFontBold = new SysDraw.Font("Arial Narrow", 10,SysDraw.FontStyle.Bold);



    #region Chart Area
    MSChart.ChartArea chartArea6 = new MSChart.ChartArea();
    chartArea6.Area3DStyle.Enable3D = false;

    chartArea6.BackGradientStyle = MSChart.GradientStyle.TopBottom;
    chartArea6.BackColor = grayStart;
    chartArea6.BackSecondaryColor = grayEnd;
    chartArea6.BorderDashStyle=MSChart.ChartDashStyle.NotSet;
    chartArea6.Name = chartAreaName;

    // --   Axes under Area collection
    // --------------------------------
    chartArea6.AxisX.Enabled = MSChart.AxisEnabled.False;
    chartArea6.AxisX.IsMarginVisible = false;
    //chartArea6.AxisX.LabelStyle.Font = new SysDraw.Font("Trebuchet MS", 8.25F, SysDraw.FontStyle.Bold);
    chartArea6.AxisX.LabelStyle.Font = labelFont;
    chartArea6.AxisX.LabelStyle.Interval = 1D;
    chartArea6.AxisX.LineColor = SysDraw.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
    chartArea6.AxisX.MajorGrid.LineColor = SysDraw.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));




    chartArea6.AxisY.CustomLabels.Add(createLabelRegion(1,"Underweight",0D,18.49D,SysDraw.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))))));
    chartArea6.AxisY.CustomLabels.Add(createLabelRegion(1,"Normal",18.5D,24.9D,SysDraw.Color.Black));
    chartArea6.AxisY.CustomLabels.Add(createLabelRegion(1,"Overweight",25D,29.9D,SysDraw.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))))));
    chartArea6.AxisY.CustomLabels.Add(createLabelRegion(1,"Obese",30D,50D,SysDraw.Color.Red));
    chartArea6.AxisY.CustomLabels.Add(createLabelRegion(0,"18.5",15.5D,21.5D,SysDraw.Color.Black));
    chartArea6.AxisY.CustomLabels.Add(createLabelRegion(0,"25",23D,27D,SysDraw.Color.Black));
    chartArea6.AxisY.CustomLabels.Add(createLabelRegion(0,"30",28D,32D,SysDraw.Color.Black));

    chartArea6.AxisY.Enabled = MSChart.AxisEnabled.True;
    chartArea6.AxisY.Interval = 50D;
    chartArea6.AxisY.Maximum = 50D;
    chartArea6.AxisY.Minimum = 0D;
    chartArea6.AxisY.IntervalType = MSChart.DateTimeIntervalType.Number;
    //chartArea6.AxisY.LabelStyle.Font = new SysDraw.Font("Trebuchet MS", 8.25F, SysDraw.FontStyle.Bold);

    chartArea6.AxisY.LabelStyle.Font = labelFontBold;

    chartArea6.AxisY.LineColor = SysDraw.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
    chartArea6.AxisY.MajorGrid.LineColor = SysDraw.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
    chartArea6.AxisY.MajorTickMark.Enabled = false;

    chartArea6.AxisY2.Enabled = MSChart.AxisEnabled.True;
    chartArea6.AxisY2.Interval = 50D;
    chartArea6.AxisY2.Maximum = 50D;
    chartArea6.AxisY2.Minimum = 0D;
    chartArea6.AxisY2.MajorTickMark.Enabled = false;
    #endregion
    chart6.ChartAreas.Add(chartArea6);

    // Series Collection Editor
    // --------------------------------
    #region Series
    // --   Datapoints
    // --------------------------------
    MSChart.DataPoint dataPoint19 = new MSChart.DataPoint(0D,dataPointYvalue);

    MSChart.Series series7 = new MSChart.Series();
    series7.BackGradientStyle = MSChart.GradientStyle.TopBottom;
    series7.Color = blueStart;
    series7.BackSecondaryColor = blueEnd;
    series7.BorderColor = SysDraw.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
    series7.ChartArea = chartAreaName;
    series7.ChartType = MSChart.SeriesChartType.Bar;
    series7.Name = "PlotMemberValue";
    series7.Points.Add(dataPoint19);
    #endregion
    chart6.Series.Add(series7);

    // Legend
    // --------------------------------
    #region Legend
    MSChart.Legend legend6 = new MSChart.Legend();
    legend6.BackColor = SysDraw.Color.Transparent;
    legend6.Enabled = false;
    legend6.Font = new SysDraw.Font("Trebuchet MS", 8.25F, SysDraw.FontStyle.Bold);
    legend6.IsTextAutoFit = false;
    legend6.Name = "Default";
    #endregion
    //chart6.Legends.Add(legend6);

    // Title
    // --------------------------------

    // Annotations
    // --------------------------------
    chart6.Annotations.Add(valueAnnotation(dataPoint19));
    chart6.Annotations.Add(regionDividerLine(dataPoint19,chartAreaName,18.5D));
    chart6.Annotations.Add(regionDividerLine(dataPoint19,chartAreaName,25D));
    chart6.Annotations.Add(regionDividerLine(dataPoint19,chartAreaName,30D));

    return chart6;
}

Ответы [ 2 ]

0 голосов
/ 09 декабря 2011

Единственный способ сделать что-то подобное - использовать событие PostPaint для рисования поверх стандартных квадратных столбцов.Вы можете попытаться скрыть угол, например, нарисовав прозрачное растровое изображение.Хотя добиться идеального выравнивания по пикселям может быть довольно сложно.Приведенный ниже код демонстрирует только принцип и не позволяет решить, как правильно расположить растровое изображение.

private void chart1_PostPaint(object sender, System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs e)
    {
        // Painting series object
        if (e.ChartElement is System.Windows.Forms.DataVisualization.Charting.Series)
        {
            Series series = (Series)e.ChartElement;

            foreach (DataPoint pt in series.Points)
            {
                // Load bitmap from resource
                System.Drawing.Image bitmap = Properties.Resources.corners;

                // Set Red color as transparent
                ImageAttributes attrib = new ImageAttributes();
                attrib.SetColorKey(Color.Red, Color.Red, ColorAdjustType.Default);

                // Calculates marker position depending on the data point X and Y values
                RectangleF imagePosition = RectangleF.Empty;
                imagePosition.Y = (float)e.ChartGraphics.GetPositionFromAxis(
                    "Default", AxisName.X, pt.XValue);
                imagePosition.X = (float)e.ChartGraphics.GetPositionFromAxis(
                    "Default", AxisName.Y, pt.YValues[0]);

                imagePosition = e.ChartGraphics.GetAbsoluteRectangle(imagePosition);
                imagePosition.Width = bitmap.Width;
                imagePosition.Height = bitmap.Height;
                imagePosition.X -= bitmap.Width - 1;
                imagePosition.Y -= 1;

                // Draw image
                e.ChartGraphics.Graphics.DrawImage(bitmap,
                    Rectangle.Round(imagePosition),
                    0, 0, bitmap.Width, bitmap.Height,
                    GraphicsUnit.Pixel,
                    attrib);

                // Dispose image object
                bitmap.Dispose();
            }
        }
0 голосов
/ 08 декабря 2011

Можете ли вы предоставить основную диаграмму, которую вы используете, и я могу продолжать приближать ее, однако я полагаю, что вы будете использовать

    chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
...