Aligning Multiple Series with Categorical Values


Binding Categorical Data Overview


Binding chart to the data source with categorical values is pretty simple and usually requires just one line of code:

  






Chart1.Series[“Series1”].Points.DataBind(mySource, “ProductName”, “Sales”); 

It is important to understand that after DataBind method creates series DataPoint objects, their XValue property will always be set to zero and category is saved in the AxisLabel property. Position of each data point along the category axis is determined using the index of the object in Series.Points collection. Below is an example of what your chart may look like:


clip_image002


Alignment of Multiple Series


When you bind multiple series with categorical values you may run into some un-expected results based on how you prepare your data source. For example, let’s imagine that our two chart series are bound to this data:





















Series 1 Series 2
Product A = 320 Product B = 420
Product B = 580 Product E = 830
Product C = 980  
Product E = 100  
Product F = 220  

As you can notice, we have less categories for the second series and their order does not match categories from the first series. If we try binding chart to this data we will end up with the chart shown below. Chart uses index of the data point to position them along the axis and we end up with orange series points in the wrong position. I added data point labels so that it would easier to see the issue.


clip_image004


In order to resolve this we can modify our data source to contain same number of categories in the same order like this:





















Series 1 Series 2
Product A = 320 Product A = Null
Product B = 580 Product B = 420
Product C = 980 Product C = Null
Product E = 100 Product E = 830
Product F = 220 Product F = Null

Simpler approach is just to make a single call to Chart.AlignDataPointsByAxisLabel method which will automatically align the two series by inserting empty points and the end result will look like this:


clip_image006


Hope this helps!


Alex.

Comments (13)

  1. Bob C says:

    I still can not make this work.  It only works if all of the x-axis values are in the first series.  But what if the first series has, for example, only Products A, C, D and E?  When Series 2 has Product B it appears to bomb.  Also, I'm using the DataBindXY method.  The DataBind method doesn't work for me (it asks for a 4th

  2. arnold says:

    hi sir,

    pls, how i have to do with 4 hashtable for 4 series? (powershell)

    Mfg

    arnold

  3. charles says:

    Bob, for that you must have the datasource looking like this

    Series 1             Series 2

    Product A = 320 Product A = Null

    Product B = Null Product B = 420

    Product C = 980 Product C = Null

    Product D = 980 Product D = Null

    Product E = 100 Product E = 830

    Product F = Null Product F = Null

  4. Series 1 – Plot the Sales data

    Series 2 – Plot the sales data with details ( displayed only when the user clicks a check box to display details)

    Date            Sales    Sale Detail

    6/20/2006  320        0

    6/21/2006  320        1

    6/22/2006  320        0

    6/23/2006  320        0

    6/24/2006  320        1

    6/25/2006  320        0

    My data source has 0 or 1 in one of the columns and can i Skip or Plot a point based on the value in the columns

    if value is 0 – then Skip the point

    if value is 1 – the plot the point on the chart

  5. Manoj says:

    Hello,

    I am Using the win form,

    I exactly want you ve written here but without database or databinding,

    I directly assing the values.

    What i need is to allign multiple series, but i dont ve code

    Pls write an example code.

    Thank you.

  6. Silvana Siqueira says:

    Hey Alex!

    Could you post the source code?

    TYhanks!

  7. KK says:

    Not working.. can you post example pls, thanks

  8. MSCHART says:

    I am using four series, of which two are STRING lists and two are DOUBLE lists. On using align by axis label, the second pair of STRING & DOUBLES disappears.

    The x-values (strings) overlap, so I was expecting an overlapping graph. Instead only the first series of double values is plotted with respect to strings on x-axis. Any idea why this is happening? Thanks for your help.

  9. S.K.R.R says:

    I don't know how thanks… my problem is sloved (with this method Chart.AlignDataPointsByAxisLabel)

  10. you rock says:

    I just spent the last 5 hours trying to solve this…I had tried everything you can imagine. THANKS!

  11. Frustrated says:

    What would it take for you to actually post some working code?

  12. agradecido says:

    mechart.AlignDataPointsByAxisLabel();