Hi.
The OnAfterAdd code looks fine ... but the problem is the coloring for area section is *always* done from for range between two points in series y values chart list. In your case, area series is constructed from four sections:
x=[0,1], y=[0,250] (section 1)
x=[1,2], y=[250,150] (section 2)
x=[2,3], y=[150,250] (section 3)
x=[3,4], y=[250,100] (section 4)
Now, each of these sections can be colored only with single color which you define in series OnAfterAdd event, so four segments are colored to:
segment 1 = green (0<200)
segment 2 = red (250 > 200)
segment 3 = green (150 < 200)
segment 4 = red (250 > 200)
Moving the YOrigin to 200 does not change number of section so you're still left with four segments and the same colors (as your images show). Solutions:
1) Split segments so that "negative" value will be colored differently from "positive" value. In your case, you have to insert the points at every position where area line crosses y=200 horizontal line:
Code: Select all
Series1.AddXY(0,0,'');
// need a point at y=200, x = ... ? calculate it
// y-y1=k(x-x1) => (y-y1)/k + x1 = x
// k= (250-0)(1-0) = 250, y1 = 0, x1= 0
// (200-0)/250 + 0 = x => x = 200/250 = 0.8
Series1.AddXY(0.8,200); // << inserted
Series1.AddXY(1,250,'');
// need another point between 1 and 2 .. use the same approach
// k=-100, y1=250, x1= 1
// x =-(200-250)/100 + 1 = 0.5 + 1 = 1.5
Series1.AddXY(1.5,200); // << inserted
Series1.AddXY(2,150,'');
// one more between 2 and 3
// k=100, y1=150, x1= 2
// x =(200-150)/100 + 2 = 0.5 + 2 = 2.5
Series1.AddXY(2.5,200); // << inserted
Series1.AddXY(3,250,'');
// one more between 3 and 4
// k=-150, y1=250, x1= 3
// x =-(200-250)/150 + 3 = 0.3333 + 3 = 3.3333
Series1.AddXY(3.333,200); // << inserted
Series1.AddXY(4,100,'');
I hope this example clarifies how area series segments are constructed and colored.