简体   繁体   English

如何修圆饼图切片的边缘?

[英]How can I round off the edges of pie-chart slices?

I've got C# code to draw a pie chart using the conventional code for each slice:我有 C# 代码来使用每个切片的常规代码绘制饼图:

gr.FillPie(brFill, rect, start_angle, sweep_angle);
gr.DrawPie(penOutline, rect, start_angle, sweep_angle);

This works fine: the trouble is it looks very boring.这很好用:问题是它看起来很无聊。 I'd like to get the effect as below;我想得到如下效果; that is, keeping it 2-D but rounding off the edges.也就是说,保持它是二维的,但将边缘四舍五入。 在此处输入图像描述

I presume I'd need to use a path gradient brush somehow, but none of the examples I've looked at have helped.我想我需要以某种方式使用路径渐变画笔,但我看过的例子都没有帮助。

Grateful for any help or tips.感谢任何帮助或提示。

Tony Reynolds托尼·雷诺兹

I posted this 11 days ago hoping that someone would do my job for me.我在 11 天前发布了这篇文章,希望有人能为我做我的工作。 I now realise I was wrong and am a reformed character.我现在意识到我错了,我是一个改过自新的角色。

I tried drawing shading and highlights using ControlPaint.Dark() & Light() but couldn't get it to look good.我尝试使用 ControlPaint.Dark() & Light() 绘制阴影和高光,但无法让它看起来不错。 Finally I decided that as most pie charts have a limited range of colours I should set up a series of complete discs with colouring and shading and clip sectors out as required.最后我决定,由于大多数饼图的颜色范围有限,我应该设置一系列完整的圆盘,其中包含着色和阴影,并根据需要裁剪扇区。

A few sample discs are below:下面是一些示例光盘:

在此处输入图像描述

I then set up an equivalent of the FillPie method as below.然后我设置了一个等效的 FillPie 方法,如下所示。

       private static void FillImagePie(Graphics gr, Bitmap bm, Rectangle rect, 
                        float startAngle, float sweepAngle)
    // Display a pie slice of a background image. 
    {

        // Create a clipping Region
        GraphicsPath pathClip = new GraphicsPath();
        pathClip.AddPie(rect, startAngle, sweepAngle);
        Region regionClip = new Region(pathClip);

        // Use region to clip image to the pie sector
        gr.Clip = regionClip;

        // Draw the image portion
        gr.DrawImage(bm, rect);

        // Clean up
        gr.ResetClip();
        regionClip.Dispose();
        pathClip.Dispose();

    }

This takes a disc bitmap, bm, sets up a pie-shaped clip region and draws the image.这需要一张光盘bitmap,bm,设置一个饼状的剪辑区域并绘制图像。

I then needed a control routine that can be called from the Paint overload of the form.然后我需要一个可以从表单的 Paint 重载中调用的控制例程。 In the special case of the pie chart only having one category, the whole disc is displayed.在饼图只有一个类别的特殊情况下,将显示整个圆盘。 Otherwise each sector is taken in turn.否则,每个扇区都被依次取走。 I found in practice that the outline looked ragged, so I first flood the sector with a black brush, then call FillImagePie, then draw a black outline.我在实践中发现轮廓看起来参差不齐,所以我先用黑色画笔填充扇区,然后调用 FillImagePie,然后绘制黑色轮廓。 A sample result is here:示例结果在这里:

在此处输入图像描述

I'm happy enough with this effect.我对这个效果很满意。 All comments and tweaks welcome, and if anyone else has a way to jazz up pie charts please share!欢迎所有评论和调整,如果其他人有办法使饼图更加生动,请分享!

Tony Reynolds托尼·雷诺兹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM