4

I have two series in a chart in c#. I just want to get the area between these two series. These series are connect with each other.

I have attached the chart image.

enter image description here

Here there are two series. For example Series1 and Series2. If series2 cross above the series1 then that area will not be consider. Only where series2 is below series1 this area only have to calculate.

rakib
  • 197
  • 8
  • I don't know if there is a built in way to do this but its pretty simple to calculate the area for both series then take their difference. – Axis Jun 16 '16 at 04:00
  • How to I calculate the area of a single series? – rakib Jun 16 '16 at 04:03
  • Each line segment is a trapezoid(or a rectangle + a triangle). Add each line segment area up and you have the total area of the series. – Axis Jun 16 '16 at 04:28
  • You can color the area, [see here](http://stackoverflow.com/questions/37465005/how-can-i-fill-the-areas-between-two-series-of-splines-or-lines/37465012#37465012), but calculating its area it is a matter of math. – TaW Jun 16 '16 at 04:32

2 Answers2

3
static void Main(string[] args)
{
    List<Point> Points1 = new List<Point>();
    //Points1.Add(); assign your series points

    List<Point> Points2 = new List<Point>();
    //Points2.Add(); assign your series points

    Series Series1 = new Series(Points1);
    Series Series2 = new Series(Points2);

    var betweenArea = Math.Abs(Series1.Area() - Series2.Area());
}

public class Series
{

    List<Point> Points { get; set; }

    public Series(List<Point> points)
    {
        Points = points;
    }

    public double Area()
    {
        double Area = 0;
        var points = Points.OrderBy(P => P.X).ToList();
        for (int i = 0; i < points.Count - 1; i++)
        {
            Point Point1;
            Point Point2;
            if (points[i].Y < points[i + 1].Y)
            {
                Point1 = points[i];
                Point2 = points[i + 1];
            }
            else
             {
                 Point1 = points[i + 1];
                 Point2 = points[i];
             }

             Area += Point1.Y * (Math.Abs(Point1.X - Point2.X));

            Area += ((Math.Abs(Point1.Y - Point2.Y)) * (Math.Abs(Point1.X - Point2.X)))/2;
        }

        return Area;
    }
}

public class Point
{
    public double X { get; set; }
    public double Y { get; set; }
}
Kahbazi
  • 14,331
  • 3
  • 45
  • 76
  • Ah, much simpler this way! – TaW Jun 16 '16 at 04:47
  • I am trying this code. But I get an error message "Value cannot be null" in this line "var points = Points.OrderBy(P => P.X).ToList();" public partial class Form1 : Form { public Form1() { InitializeComponent(); } Series Series1 = new Series(); Series Series2 = new Series(); private void button1_Click(object sender, EventArgs e) { var betweenArea = Math.Abs(Series1.Area() - Series2.Area()); } private void button2_Click(object sender, EventArgs e) { – rakib Jun 16 '16 at 05:36
  • chart1.Series["Series1"].ChartType = SeriesChartType.Line; chart1.Series["Series1"].MarkerStyle = MarkerStyle.Circle; chart1.Series["Series1"].Color = Color.Blue; chart1.Series["Series2"].ChartType = SeriesChartType.Line; chart1.Series["Series2"].MarkerStyle = MarkerStyle.Circle; chart1.Series["Series2"].Color = Color.Red; – rakib Jun 16 '16 at 05:40
  • chart1.Series["Series1"].Points.AddXY(3, 4); chart1.Series["Series1"].Points.AddXY(5, 4); chart1.Series["Series2"].Points.AddXY(3, 4); chart1.Series["Series2"].Points.AddXY(3, 2); chart1.Series["Series2"].Points.AddXY(5, 2); chart1.Series["Series2"].Points.AddXY(5, 4); } } – rakib Jun 16 '16 at 05:41
  • This is a console application or Windows form application? @Arvin My application is windows form application. – rakib Jun 16 '16 at 05:54
  • 1
    @rakib I wrote this in console application but mainly this is CODE, you can copy paste it wherever you want – Kahbazi Jun 16 '16 at 05:56
  • When I am trying to add points "Points1.Add(3,4);" It shows error "Add an object to the end of the List" – rakib Jun 16 '16 at 06:17
  • @rakib you have to do it like this `Points1.Add(new Point(){X=3,Y=4});` – Kahbazi Jun 16 '16 at 06:36
  • @rak: Please do not add large pieces of code in comments! Instead do edit your question! – TaW Jun 16 '16 at 07:23
2

You can color the area, see here, but calculating its area it is a matter of math.

Here is how I would do it:

  • first for each point in each series add/insert the point directly above/ below it.
  • Now you have a series of polygons with parallel vertical edges.
  • The area of each can be split in three parts: the center rectangle and the top and bottom triangle
  • width and height are knonw, so the areas are w*h for the rectangles and w*h/2 for the triangles.
  • the corresponding points are found by interpolating the previous and next points in the corresponding series.

Just a number of simple steps. The harder part is to keep track of the start and stop points and the empty and special cases..

  • The normal case is green/magenta below.
  • If a side if zero there is no problem
  • If a point from series1 is higher or lower than the next on in series2 you need to add two more points and do the calcualting over a tilted polygon. See the orange/cyan lines below!

enter image description here

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111