I am using the Charts component in Windows Forms.
I create a straight line
using
chart1.Series["Grenzwert"].Points.Add(new DataPoint(0, y));
chart1.Series["Grenzwert"].Points.Add(new DataPoint(maxwidth, y));
Also I plot a a series of points connected by a line, let's call it curve
.
How do I show everything over straight line
and under curve
filled?
Column fills the whole area, not just above straight line
.
Example:
This is late and not really short but imo it is the best way to color areas in a chart.
The
Lines
and also theSpline
charttypes can be very precisely colored by coding thePaint
event with the right data. The necessary pixel values can be obtained by the axis functionValueToPixelPosition
. See here for another example!The following code is a little longer because we need to add certain points at the start and end of both the chart and each colored area. Other than that it is very straight forward: Create
GraphicsPaths
by adding the pixel coordinates withAddLines
and fill theGraphicsPaths
in thePaint
event.For testing and for fun I have added a movable
HorizontalLineAnnotation
, so I can see how the areas vary when I drag it up and down..:The
Paint
event is rather simple; it refers to aHorizontalLineAnnotation hl
:The code to get the paths is obviously way too long for comfort..:
It uses two helper functions:
The
HorizontalLineAnnotation
is set up like this:I have an idea that use
SeriesChartType.Range
as follow.As in the below drawing to judge whether the straight line and a line between (x0,y0) and (x1,y1) intersect, case 1 is
(y0 < y && y1 > y)
and case 2 is(y0 > y && y1 < y)
. In case 1 and case 2, they intersect each other. In case 3 and case 4, they don't intersect each other.You can do this as follows.