使用MSChart,如何绕过条形图的边缘?

时间:2011-11-30 19:58:17

标签: graph mschart

我正在使用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 :(得分:0)

你能提供你正在使用的基本图表吗?我可以继续把它靠近但是我相信你会使用

    chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;

答案 1 :(得分:0)

我能想到做这样的事情的唯一方法是使用 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();
            }
        }