Profile Picture

XY Scatter Line graph painting/coloring both sides of the line

Posted By Bruce Guthrie 14 Years Ago

XY Scatter Line graph painting/coloring both sides of the line

Author
Message
Bruce Guthrie
Posted 14 Years Ago
View Quick Profile
Junior Member

Junior Member (14 reputation)Junior Member (14 reputation)Junior Member (14 reputation)Junior Member (14 reputation)Junior Member (14 reputation)Junior Member (14 reputation)Junior Member (14 reputation)Junior Member (14 reputation)Junior Member (14 reputation)

Group: Forum Members
Last Active: 6 Years Ago
Posts: 14, Visits: 2

i am try to color both sides of a simple XY line graph. below is sample data. i would like the left side of the line to be colored green for safe and the right side to be colored red for unsafe. i can partially acheive this by using and area chart or hi-lo chart but cannot acheive fully what i want and that is to color the chart on both sides of the line from both axis right up to the line. also another problem is my x-values do not run in numerical order. how can i easily achieve my coloring and also some interactivity so when the user mouses over the green or red area a interactive tip pops up to tell the user, safe or unsafe area?

xy
600
56.3425.6
28.9258.6
23.4491.6
21.61124.6
19.78157.6
18.87190.6
17.95223.6
17.04256.6
17.04289.6
16.13322.6
16.13355.6
15.21388.6
15.21421.6
14.75454.6
14.3487.6
14.3518.9
14.3518.9
14.3551.9
13.84584.9
13.84617.9
14.3650.9
14.3683.9
14.75716.9
15.21749.9
15.21782.9
15.21815.9
15.21848.9
15.21863.8
15.21896.8
15.21929.8
15.21962.8
15.21995.8
15.211017
15.211017
15.211050
15.211083
15.211102
15.211102
15.211135
15.211168
15.211195
15.211195
15.211200
 

 



Attachments
nevron graph for color.bmp (39 views, 1.00 MB)
bob milanov
Posted 14 Years Ago
View Quick Profile
Supreme Being

Supreme Being (152 reputation)Supreme Being (152 reputation)Supreme Being (152 reputation)Supreme Being (152 reputation)Supreme Being (152 reputation)Supreme Being (152 reputation)Supreme Being (152 reputation)Supreme Being (152 reputation)Supreme Being (152 reputation)

Group: Forum Members
Last Active: 6 Months Ago
Posts: 153, Visits: 11

Hi Bruce,

You'll have to split lines that cross the left to right boundary and assign colors per individual line segment. The following code snippet shows how to do that:

 public class NColoredLine
 {
  #region Constructors

  public NColoredLine(float middle, NLineSeries lineSeries, Color leftColor, Color rightColor)
  {
   m_Middle = middle;
   m_MiddleRay = NLineF.FromTwoPoints(new NPointF(middle, -100000.0f), new NPointF(middle, 100000.0f));
   m_LineSeries = lineSeries;
   m_LeftColor = leftColor;
   m_RightColor = rightColor;
  }

  #endregion

  public void AddLineSegment(NPointF a, NPointF b)
  {
   NLineF line = NLineF.FromTwoPoints(a, b);
   int key = m_LineSeries.Values.Count;

   if (a.X <= m_Middle && b.X <= m_Middle)
   {
    // left
    m_LineSeries.Values.Add(a.Y);
    m_LineSeries.XValues.Add(a.X);

    m_LineSeries.Values.Add(b.Y);
    m_LineSeries.XValues.Add(b.X);

    m_LineSeries.BorderStyles[key++] = new NStrokeStyle(m_LeftColor);
    m_LineSeries.BorderStyles[key++] = new NStrokeStyle(m_LeftColor);
   }
   else if (a.X >= m_Middle && b.X >= m_Middle)
   {
    // right
    m_LineSeries.Values.Add(a.Y);
    m_LineSeries.XValues.Add(a.X);

    m_LineSeries.Values.Add(b.Y);
    m_LineSeries.XValues.Add(b.X);

    m_LineSeries.BorderStyles[key++] = new NStrokeStyle(m_RightColor);
    m_LineSeries.BorderStyles[key++] = new NStrokeStyle(m_RightColor);
   }
   else
   {
    // intersect
    NPointF split;
    line.IntersectWith(m_MiddleRay, out split);

    // left
    m_LineSeries.Values.Add(a.Y);
    m_LineSeries.XValues.Add(a.X);

    m_LineSeries.Values.Add(split.Y);
    m_LineSeries.XValues.Add(split.X);

    m_LineSeries.BorderStyles[key++] = new NStrokeStyle(m_LeftColor);
    m_LineSeries.BorderStyles[key++] = new NStrokeStyle(m_LeftColor);

    // right
    m_LineSeries.Values.Add(split.Y);
    m_LineSeries.XValues.Add(split.X);

    m_LineSeries.Values.Add(b.Y);
    m_LineSeries.XValues.Add(b.X);

    m_LineSeries.BorderStyles[key++] = new NStrokeStyle(m_RightColor);
    m_LineSeries.BorderStyles[key++] = new NStrokeStyle(m_RightColor);

   }


   
  }

  #region Fields

  float m_Middle;
  NLineSeries m_LineSeries;
  Color m_LeftColor;
  Color m_RightColor;
  NLineF m_MiddleRay;

  #endregion
 }
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }

  private void Form1_Load(object sender, EventArgs e)
  {
   NChart chart = nChartControl1.Charts[0];

   NLineSeries line = new NLineSeries();
   line.DataLabelStyle.Visible = false;
   chart.Series.Add(line);

   NColoredLine colorLine = new NColoredLine(0, line, Color.Green, Color.Red);

   line.UseXValues = true;
   
   float yVal= -40;
   float xVal = -40;

   float prevYVal;
   float prevXVal;

   for (int i = 0; i < 10; i++)
   {
    prevYVal = yVal;
    prevXVal = xVal;

    xVal += 10;
    yVal += 10;

    colorLine.AddLineSegment(new NPointF(prevXVal, prevYVal), new NPointF(xVal, yVal));
   }

   for (int i = 0; i < 10; i++)
   {
    prevYVal = yVal;
    prevXVal = xVal;

    xVal -= 10;
    yVal += 10;

    colorLine.AddLineSegment(new NPointF(prevXVal, prevYVal), new NPointF(xVal, yVal));
   }
  }
 }

Hope this helps - let me know if you meet any problems.

Best regards,
Bob





Similar Topics


Reading This Topic