如何使用 GDI+当线条与区域重叠时更改线条的颜色?
我正在使用 .NET GDI+ 在图表上绘制波浪线。 (想想股票交易) 如果线条高于 90% 或低于 10%,我希望它改变颜色。
关于如何改变颜色有什么建议吗?
我的两个想法是:- 1. 创建 0%-10% 的矩形90%-100% &以某种方式使用它们是颜色剪切/变换区域。这可能吗?如果可以的话怎么办。 2. 使用画笔,但这些似乎更像是渐变和渐变。不是精确地按某个值进行确定的颜色切换。
其中任何一个可行吗?有更好的办法吗?
I'm using .NET GDI+ to draw a wavy line on a chart. (think sharetrading)
I want it to change color if the line goes above 90% or below 10%.
Any tips on how to get the color to change?
My two ideas are:-
1. Create rectangles from 0%-10% & 90%-100% & somehow use them are a color clipping/transform region. is that possible if so how.
2. Use a Brush but these seem to be more of a gradient & not a definate color switch precicely at a value.
Are either of these viable? Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这两种方法似乎都是可行的。
要执行第一种方法,请定义三个
Region
或
矩形
图形中三个范围的对象,然后创建三个Pen
对象,每个对象都有不同的颜色。调用Graphics.SetClip
方法用于第一个区域,并使用第一支笔绘制整个曲线。当前剪切区域之外的任何内容都不会显示,因此您不必担心自己找出交点。然后将剪切区域设置为第二个区域,并使用第二支笔再次绘制整个曲线。重复使用第三个区域和笔。对于第二种方法,请创建一个
位图
具有绘图区域的完整高度,任意宽度。用您想要的颜色区域绘制整个位图。定义一个纹理画笔并用它来创建你的笔。然后一次画出整个路径。 MSDN 有一个示例。Both methods seem viable.
To do your first method, define three
Region
orRectangle
objects for the three ranges in your graph, and then make threePen
objects, each with a different color. Call theGraphics.SetClip
method for the first region, and draw your entire curve using the first pen. Anything outside the current clipping region won't show up, so you don't have to worry about figuring out the intersection points yourself. Then set the clipping region to the second region and draw the entire curve again using the second pen. Repeat using the third region and pen.For your second method, create a
Bitmap
with the full height of your drawing area, with any width. Paint the entire bitmap with the color regions you want. Define a textured brush and use it to create you pen. Then draw the entire path at once. MSDN has an example.谢谢罗布,我非常感谢你的回复。
在测试它的同时。我找到了一个更简单的替代方案来满足我的需要。我希望您也觉得这很有用。
混合对象可让您创建从开始到结束 X% 的位置数组。您还可以在该点创建一个颜色混合百分比的匹配数组,例如:0= 所有一种颜色 & 0= 所有颜色。 1=所有其他。
然后我创建了一个与我的图表高度完全相同的画笔。
然后,我将 Brush 的 Blend 属性设置为 Blend 对象。并使用画笔创建了一支笔。
这让我可以在任何地方绘制一条线一次,当它通过我的混合过渡点的高度时,它会神奇地改变颜色。
Thanks Rob, I really appreciated your reply.
While testing it out. I found an alternate that was even simpler for what I needed. I hope you find this useful too.
The Blend Object is lets you create an array of Positions X% of the way from start to finish. You also create a matching array of Percent mix of the colors at that point eg: 0= all one colour & 1= all the other.
I then created a Brush that was exactly the same height as my chart.
I then set the Blend property of the Brush to my Blend object. And Created a Pen using the Brush.
This let me draw the line anywhere once, as it passed the height of my Blend transition points it magically changed colour.