2014-08-28 199 views
1

我需要以C#格式创建维恩图表。我一直在尝试使用Graphics.DrawEllipse和FillElipse。但我不确定我如何填写常见部分。在C#win中创建维恩图表格

我要做到这一点,

enter image description here

这里是我的这段代码,

private void panelControlVennDiagram_Paint(object sender, PaintEventArgs e) 
{ 
    Brush brushLeft = new SolidBrush(Color.Blue); 
    Brush brushRight = new SolidBrush(Color.LightPink); 
    Brush brushCommon = new SolidBrush(Color.Purple); 

    Pen pen = new Pen(brushLeft, 10); 

    Rectangle leftVenn = new Rectangle(20, 50,100,100); 
    Rectangle rightVenn = new Rectangle(90, 50, 100, 100); 
    Rectangle commonVenn = new Rectangle(100, 120, 100, 100); 

    Font stringFont = new Font("Times New Roman", 9); 

    e.Graphics.DrawString("Left:" + leftValue, stringFont, brushLeft, 10,70); 
    e.Graphics.DrawString("Right:" + rightValue, stringFont, brushRight, 90,70); 
    e.Graphics.DrawString("Common:" + commonValue,stringFont, brushCommon, 100,70); 

    // Fill ellipse on screen. 
    e.Graphics.FillEllipse(brushLeft, leftVenn); 
    e.Graphics.FillEllipse(brushRight, rightVenn); 

    e.Graphics.DrawEllipse(Pens.White, leftVenn); 
    e.Graphics.DrawEllipse(Pens.White, rightVenn); 

}

我绘制两个椭圆,需要有不同的颜色为通用部分。我无法使用任何图书馆。请帮忙。

+0

边注:请处理所有的刷子和笔,或者把它们变成领域,而不是局部变量重新使用它们。 – 2014-08-28 06:31:40

回答

2

您可以添加System.Drawing.Drawing2D;命名空间使用GraphicPath。创建一个图形路径并获取相交区域。

试试这个代码:(我曾评论DrawString用于测试目的)

private void panelControlVennDiagram_Paint(object sender, PaintEventArgs e) 
{ 
    Rectangle leftVenn = new Rectangle(20, 50, 100, 100); 
    Rectangle rightVenn = new Rectangle(90, 50, 100, 100);   
    Region region1 = new Region(); 

    //Font stringFont = new Font("Times New Roman", 9); 
    //e.Graphics.DrawString("Left:" , stringFont, brushLeft, 10, 70); 
    //e.Graphics.DrawString("Right:" , stringFont, brushRight, 90, 70); 
    //e.Graphics.DrawString("Common:", stringFont, brushCommon, 100, 70); 

    // Fill ellipse on screen. 
    using(Brush brushLeft = new SolidBrush(Color.Blue)) 
    {  
     e.Graphics.FillEllipse(brushLeft, leftVenn); 
     e.Graphics.DrawEllipse(Pens.White, leftVenn); 
    } 

    using(Brush brushRight = new SolidBrush(Color.LightPink)) 
    { 
     e.Graphics.FillEllipse(brushRight, rightVenn);   
     e.Graphics.DrawEllipse(Pens.White, rightVenn); 
    } 

    using (GraphicsPath circle_path = new GraphicsPath()) 
    { 
     circle_path.AddEllipse(leftVenn); 
     region1.Intersect(circle_path); 
    } 

    using (GraphicsPath circle_path = new GraphicsPath()) 
    { 
     circle_path.AddEllipse(rightVenn); 
     region1.Intersect(circle_path); 
    } 

    using(Brush brushCommon = new SolidBrush(Color.Purple)) 
    { 
     e.Graphics.FillRegion(brushCommon, region1); 
    } 

} 

输出

enter image description here

+0

This works good ...还有一个与此相关的问题,我也想处理鼠标点击左/右和公共图部分。如何保存鼠标位置? – user1821499 2014-08-28 22:46:29

3

你可以使用一个半透明的颜色,这样的颜色重叠部分是两个圆圈的实际复合颜色

Brush brushLeft = new SolidBrush(Color.FromArgb(50, Color.Blue)); 
Brush brushRight = new SolidBrush(Color.FromArgb(50, Color.Red)); 

enter image description here

+0

有人可以请建议如何处理这些区域的每个鼠标点击...左/右/共同? – user1821499 2014-09-02 18:57:09