Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2945
Different colours in Category chart
posted

If I have the following data (dt):

id label value
1 one 123
2 two 234
3 three 345

and the following code:

var yAxis = new NumericYAxis();
var xAxis = new CategoryXAxis();
xAxis.DataSource = dt;
xAxis.Label = "label";

var series = new ColumnSeries();
series.DataSource = dt;
series.ValueMemberPath = "value";            
series.XAxis = xAxis;
series.YAxis = yAxis;            

var chart = this.ultraDataChart2;            
chart.Axes.Add(xAxis);
chart.Axes.Add(yAxis);
chart.Series.Add(series);

This draws the chart I want.  However, all three columns are the same colour, presumably because they are the same series. 

How can I get each column in a different colour?  Do I have to create a series per row?  If so, how would that look?

  • 34810
    Verified Answer
    Offline posted

    Hello Campbell,

    You are correct that the columns are drawing in the same color because they are all part of the same series. In order to prevent this, I have a couple of recommendations for you:

    1. You can use the AssigningCategoryStyle event on the ColumnSeries to control the color of each column. You need to opt-in to this event, because with some series types that may have many data items, this can cause a hit to performance. This is done by setting the ColumnSeries. IsCustomCategoryStyleAllowed property to true.

    In the event, the event arguments can allow you to set the Fill and Outline of the column. You can get the underlying data items from the GetItems method and pass in the StartIndex and EndIndex parameters. Note though, this will not affect the legend or tooltip badges if you are using them. Those care only about the actual series properties, in which case I would recommend that you see recommendation 2.

    2. Use multiple series. For this, you would need a different value member path per series. So, rather than having the structure that you provided, you could do something like the following:

    ID     Label     value   value2
    1       one       123      null
    2       two       null      234

    This would allow you to create 2 series. One for “value” and one for “value2,” where the first category  (ID 1) would have a column “value” but not “value 2” and vice versa for the second category (ID 2).

    I hope this helps. Please let me know if you have any other questions or concerns on this matter.