2009-11-06 139 views
1

我使用.NET GDI +在圖表上繪製波浪線。 (想想sharetrading) 我希望它改變顏色,如果線路高於90%或低於10%。如何使用GDI +在重疊區域時更改線條的顏色?

關於如何讓顏色改變的任何提示?

我的兩個想法: - 1. 0%-10%& 90%-100%&創建矩形以某種方式使用它們是彩色剪輯/轉換區。如果是這樣的話,那是可能的。 2.使用畫筆,但它們看起來更像是一個漸變色&不是一個確定的顏色切換值。

這些是可行的嗎?有沒有更好的辦法?

回答

3

這兩種方法似乎都是可行的。

要做你的第一個方法,在圖中爲三個範圍定義三個RegionRectangle對象,然後製作三個Pen對象,每個對象具有不同的顏色。調用第一個區域的Graphics.SetClip方法,並使用第一支筆畫出整個曲線。當前剪切區域外的任何內容都不會顯示出來,因此您不必擔心自己計算出交點。然後將裁剪區域設置爲第二個區域,並使用第二支筆再次繪製整條曲線。重複使用第三個區域和筆。

對於第二種方法,創建一個Bitmap,其繪圖區域的高度爲任意寬度。使用所需的顏色區域繪製整個位圖。定義一個textured brush並用它來創建你的筆。然後一次繪製整個路徑。 MSDN has an example.

0

感謝羅布,我非常感謝您的回覆。 進行測試時。我找到了一個替代品,這對我所需要的更簡單。我希望你也覺得這很有用。

Blend Object允許您從頭到尾創建一個位置數組的X%。您還可以在該點創建顏色百分比組合的匹配數組,例如:0 =全部顏色& 1 =所有其他顏色。 然後我創建了一個與我的圖表高度完全一樣的畫筆。 然後,我將Brush的Blend屬性設置爲我的Blend對象。並使用畫筆創建了一支筆。

這讓我畫了一條線,因爲它通過了我的混合過渡點的高度,它奇蹟般地改變了顏色。

if (enableThresholdColors) { // Color the extreme values a different color 

    int Threshold = (thresholdValue < 50 ? 100 - thresholdValue : thresholdValue); 
    float UpperThreshold = ((float) Threshold)/100f; 
    float LowerThreshold = ((float) 100 - Threshold)/100f; 

    LinearGradientBrush br = new LinearGradientBrush(new Rectangle(20, bounds.Top, 30, bounds.Height), Plots[0].Pen.Color, colorThreshold, 90); 
    Blend bl = new Blend(); 
    // --- this colors the Extreme values the same color ---  
    bl.Factors = new float[] {1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f}; 
    // --- this colors the Extreme values the opposite color & transitions the line --- 
    //  bl.Factors = new float[] {1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f}; 

    bl.Positions = new float[]{0, LowerThreshold, LowerThreshold, UpperThreshold, UpperThreshold, 1.0f}; 
    br.Blend = bl; 
    // --- for testing - show where the threshold is. --- 
    // graphics.FillRectangle(br, new Rectangle(50, bounds.Top, 400, bounds.Height)); 

//--------------------------------------------------------------------------------------- 

    Pen stocPen = new Pen(br, Plots[0].Pen.Width); 
    stocPen.DashStyle = Plots[0].Pen.DashStyle; 
    graphics.DrawPath(stocPen, path); 
    stocPen.Dispose(); 
    br.Dispose(); 

} else { // Color the entire line all the same color 
    graphics.DrawPath(Plots[0].Pen, path); 
} 
相關問題