#111 Rendering pie charts with IsPenWidthScaled = true

open
nobody
5
2015-02-13
2009-03-19
No

When rendering a pie chart and setting the IsPenWidthScaled to true on the graph pane. Artifacts appear in the center of the pie due to the line join of the pen defaulting to miter. The following code illustrates this problem:

        GraphPane myPane = new GraphPane();
        myPane.IsPenWidthScaled = true;

        // Set the pane title
        myPane.Title.Text = "2004 ZedGraph Sales by Region\n ($M)";

        // Enter some data values
        double[] values = { 15, 15, 40, 20 };
        double[] values2 = { 250, 50, 400, 50 };
        System.Drawing.Color[] colors = { System.Drawing.Color.Red, System.Drawing.Color.Blue, System.Drawing.Color.Green, System.Drawing.Color.Yellow };
        double[] displacement = { .0, .0, .0, .0 };
        string[] labels = { "Europe", "Pac Rim", "South America", "Africa" };

        // Fill the pane and axis background with solid color
        myPane.Fill = new Fill(System.Drawing.Color.Cornsilk);
        myPane.Chart.Fill = new Fill(System.Drawing.Color.Cornsilk);
        myPane.Legend.Position = LegendPos.Right;

        // Create some pie slices
        PieItem segment1 = myPane.AddPieSlice(20, System.Drawing.Color.Navy, .20, "North");
        PieItem segment2 = myPane.AddPieSlice(40, System.Drawing.Color.Salmon, 0, "South");
        PieItem segment3 = myPane.AddPieSlice(30, System.Drawing.Color.Yellow, .0, "East");
        PieItem segment4 = myPane.AddPieSlice(10.21, System.Drawing.Color.LimeGreen, 0, "West");
        PieItem segment5 = myPane.AddPieSlice(10.5, System.Drawing.Color.Aquamarine, .3, "Canada");

        // Add some more slices as an array
        PieItem[] slices = new PieItem[values2.Length];
        slices = myPane.AddPieSlices(values2, labels);

        // Modify the slice label types                        
        slices[0].LabelType = PieLabelType.Name_Value;
        slices[1].LabelType = PieLabelType.Name_Value_Percent;
        slices[2].LabelType = PieLabelType.Name_Value;
        slices[3].LabelType = PieLabelType.Name_Value;
        slices[1].Displacement = .2;
        segment1.LabelType = PieLabelType.Name_Percent;
        segment2.LabelType = PieLabelType.Name_Value;
        segment3.LabelType = PieLabelType.Percent;
        segment4.LabelType = PieLabelType.Value;
        segment5.LabelType = PieLabelType.Name_Value;
        segment2.LabelDetail.FontSpec.FontColor = System.Drawing.Color.Red;

        // Sum up the values                                                                                    
        CurveList curves = myPane.CurveList;
        double total = 0;
        for (int x = 0; x < curves.Count; x++)
            total += ((PieItem)curves[x]).Value;

        // Add a text item to highlight total sales
        TextObj text = new TextObj("Total 2004 Sales - " + "$" + total.ToString() + "M", 0.85F, 0.80F, CoordType.PaneFraction);
        text.Location.AlignH = AlignH.Center;
        text.Location.AlignV = AlignV.Bottom;
        text.FontSpec.Border.IsVisible = false;
        text.FontSpec.Fill = new Fill(System.Drawing.Color.White, System.Drawing.Color.PowderBlue, 45F);
        text.FontSpec.StringAlignment = StringAlignment.Center;
        myPane.GraphObjList.Add(text);

        // Add a colored background behind the pie
        BoxObj box = new BoxObj(0, 0, 1, 1, System.Drawing.Color.Empty, System.Drawing.Color.PeachPuff);
        box.Location.CoordinateFrame = CoordType.ChartFraction;
        box.Border.IsVisible = false;
        box.Location.AlignH = AlignH.Left;
        box.Location.AlignV = AlignV.Top;
        box.ZOrder = ZOrder.E_BehindCurves;
        myPane.GraphObjList.Add(box);

        int wdMM=100,hgMM=100;
    int dpi=600;
        Bitmap bmp = new Bitmap((int)((wdMM / 25.4) * dpi), (int)((hgMM / 25.4) * dpi), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        bmp.SetResolution(dpi,dpi);
        Graphics gmdc=Graphics.FromImage(bmp);
        gmdc.Clear(System.Drawing.Color.White);
        myPane.Rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);

        myPane.Draw(gmdc);

        bmp.Save("pie1.bmp");

Setting the line join to rounded in the draw method of PieItem removes these artifacts.

borderPen.LineJoin = LineJoin.Round;

Discussion


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks