Align two series with different number of points

# Align two series with different number of points

• Thursday, July 19, 2012 2:49 PM

Hi Sipla,

I have two data series:

chart2.Series["1"].IsXValueIndexed = true;

chart2.Series["2"].IsXValueIndexed = true;

double[] xValues_2 = { 1, 2, 3, 4, 5, 6, 7, 8 };

double[] yValues_2 = { 2, 1, 1, 2, 3, 3, 3, 9 };

chart2.Series["1"].Points.DataBindXY(xValues_2, yValues_2);

First series has 8 points. IsXValueIndexed is true. Second series has 8 but should have 9 points and also IsXValueIndexed is true. Series “2” should have two points at X 5 (5,6; 5,8). I disabled it because it throws an error (The series currently have a different number of data points). Now the chart looks like this:

But I need to have it like this:

The problem is that the series “2” contains 9 points and that’s why the exception is thrown. I can not set IsXValueIndexed = false for the series “1” because later it will be used for financial data storage. Now I need to align these two series and leave IsXValueIndexed = true for the first one. How can I do that?

### All Replies

• Monday, July 30, 2012 7:47 AM

Is somebody reading my posts? it seems like i'm talking to a silence...

Sipla, where are you?!

• Tuesday, July 31, 2012 12:10 PM

Hi Boris,

the problem is of course the duplicate x-value. Why don't you try to filter out all duplicates for the Series "2" and stuff it into a Series "3"?

Regards,
Raik

• Wednesday, August 01, 2012 5:22 PM

Hi Raik,

thx for the answer. this is not a problem that there is two of YValues at the point 5 - this is how i need it to be done! i need to have more than one Y value at the same X point in the series and both series must be indexed.

what kind of result will you have after filtering? never done it before.

boris

• Friday, August 03, 2012 5:32 AM

Let me show you what I meant:

```//---- chart layout setup ----
Series s = chart2.Series.Add("1");
s.ChartType = SeriesChartType.Line;
s.Label = "#VALY";
s.MarkerColor = Color.Black;
s.MarkerStyle = MarkerStyle.Circle;
s.Color = Color.Green;

s.ChartType = SeriesChartType.Point;
s.MarkerStyle = MarkerStyle.Triangle;
s.MarkerColor = Color.Purple;
s.MarkerSize = 10;

chart2.Legends.Clear();
//-----------------------------

//---- databind ----
chart2.Series["1"].IsXValueIndexed = true;
chart2.Series["2"].IsXValueIndexed = true;

double[] xValues_2 = { 1, 2, 3, 4, 5, 6, 7, 8 };
double[] yValues_2 = { 2, 1, 1, 2, 3, 3, 3, 9 };

chart2.Series["1"].Points.DataBindXY(xValues_2, yValues_2);

double[] xValues_new = { 1, 2, 3, 4, 5, 5, 6, 7, 8 };
double[] yValues_new = { 1, 1, 1, 1, 6, 8, 1, 1, 1 };

//---- filter duplicates from xValues_new/yValues_new
Dictionary<double, double> duplicate_Values = new Dictionary<double, double>();
Dictionary<double, double> distinct_Values = new Dictionary<double, double>();

for(int i=0; i<xValues_new.Length; i++)
{
if (!distinct_Values.ContainsKey(xValues_new[i]))
{
}
else
{
}
}

//---- databind distinct values (the duplicate is removed from this list ----
chart2.Series["2"].Points.DataBindXY(distinct_Values.Keys, distinct_Values.Values);

//---- add a new series "3" and make it look like series "2"
s.ChartType = SeriesChartType.Point;
s.MarkerStyle = MarkerStyle.Triangle;
s.MarkerColor = Color.Purple;
s.MarkerSize = 10;

//---- loop through all datapoint from series "2" to get the xValues
//---- if the xValue exists in the duplicate_Values -> use x/yValues from duplicate_Values list
//---- else use the xValue from the other series and create a new empty datapoint
foreach (DataPoint p in chart2.Series["2"].Points)
{
if (!duplicate_Values.ContainsKey(p.XValue))
{
int pnr = chart2.Series["3"].Points.AddXY(p.XValue, 0);
chart2.Series["3"].Points[pnr].IsEmpty = true;
}
else
{
}
}```

What you get is this:

Remember, this is coded really fast. You might want to implement some error-checking etc.

Hope that gives you an idea.

Raik

• Monday, August 06, 2012 2:58 PM

Hi Raik,

thx a lot for the answer. i played with your code and it seems like you the series "3" will contain only one value. it's ok bit what if the total quantity on points belonged to one xValue is unknown? i work with stock data, these points represent market deals and nobody knows it's quantity untill they happen.

boris