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