2014-08-28 63 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