This is a migrated thread and some comments may be shown as answers.

1 Bar Series with different colored bar

2 Answers 336 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Ravindra
Top achievements
Rank 1
Ravindra asked on 20 Feb 2015, 10:46 AM
Hi Team,

greetings from our end. the tool is performing absolutely awesome. but we got stuck up with critical requirement. before this let me tell you, we are strictly following the following url in our implementation.
http://www.telerik.com/help/reporting/graphhowtocreateprogrammaticallygraph.html


the requirement says:
we need to set standard colors (black, white, green, red, orange, gray, .. ) for each bar we have under one and only 1 series and the color for each bar is to be defined based on dynamically mapped ProductCategoryGroup field ProductCategory from the above url.

for eg:-
product category is Bikes we need to have orange. 
product category is automobiles we need to have grey.

Now, the data source is not always loaded with same product categories list. we make load bikes, automobiles, etc:- or bikes alone or automobiles or different combination list.




2 Answers, 1 is accepted

Sort by
0
Nasko
Telerik team
answered on 24 Feb 2015, 04:30 PM
Hello Ravindra,

The Graph item uses Color Palettes to set the series colors. For more information on the topic, please refer to the Formatting Series Colors section of the Formatting Graph Series help article.
To change the color palette programmatically you can use the following code:
var colors = new ColorPalette();
colors.AddColors(Color.Blue, Color.Red, Color.Green, Color.Gray, Color.LightBlue, Color.Pink, Color.Purple);
  
graph1.ColorPalette = colors;

Note that there is no option to set a distinct color per data item in the series from a color palette. However, you can set the colors of different data points depending on their data using Conditional Formatting.

Regards,
Nasko
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Theo
Top achievements
Rank 2
answered on 18 Jan 2019, 05:26 AM

Data

ProcessStatusName    TotalCount StatusOrder StatusColor
New Process                40              1                   Black
Awaiting Authorisation  0                2                   Chocolate
Authorised                    19              3                    DarkGreen
Edit After Authorisation 5               4                    MediumTurquoise
Archived                        0               5                    Orange
Period Review               0               6                    OrangeRed

 

Report datasource is of type ObjectDataSource which is set as a datatable

Report contains a graph that should show the different statuses as a bar graph, the bar series uses the ProcessStatusName as the datapoint and the TotalCount as the DatapointValue

in the report code behind

Private Sub Graph1_ItemDataBinding(sender As Object, e As EventArgs) Handles Graph1.ItemDataBinding
    Dim graphDefinition As Graph = DirectCast(sender, Processing.ReportItemBase).ItemDefinition
    Dim series As Telerik.Reporting.GraphSeriesCollection = graphDefinition.Series
    series(0).DataPointConditionalFormatting.Clear()
    series(0).DataPointLabelConditionalFormatting.Clear()
 
    Dim reportData As DataTable = DirectCast(DirectCast(DirectCast(sender, Processing.DataItem).DataSource, ObjectDataSource).DataSource, DataTable)
    If reportData.Rows.Count > 0 Then
        For Each row As DataRow In reportData.Rows
            Dim rule As New Drawing.FormattingRule()
            rule.Filters.Add("=Fields.ProcessStatusName", FilterOperator.Equal, row("ProcessStatusName"))
            rule.Style.BackgroundColor = Color.FromName(row("StatusColor"))
            If row("StatusColor") = "Black" Then
                rule.Style.Color = Color.White
                series(0).DataPointLabelConditionalFormatting.Add(rule)
            End If
            series(0).DataPointConditionalFormatting.Add(rule)
        Next
    End If
End Sub
Indranil
Top achievements
Rank 1
commented on 01 Jan 2024, 04:30 PM

Dim graphDefinition As Graph = DirectCast(sender, Processing.ReportItemBase).ItemDefinition

 

when written in c#  the ItemDefinition  is not recognized ,coiuld you please provide  the above code  for c# ?

Momchil
Telerik team
commented on 02 Jan 2024, 09:30 AM

Hello Indranil,

I see that you have a support ticket with the same question. You can check your ticket to see my reply but I will copy the answer here as well in case anyone else has the same question.

The following C# code snippet should give you the definition of the graph.

Telerik.Reporting.Graph graphDefinition = (Telerik.Reporting.Graph)((Telerik.Reporting.Processing.ReportItemBase)sender).ItemDefinition;

Or, if you prefer to use the as operator, the following snippet should work too.

Telerik.Reporting.Graph graphDefinition = (sender as Telerik.Reporting.Processing.ReportItemBase).ItemDefinition as Telerik.Reporting.Graph;

Note that editing report items through their definitions in report events has been discouraged since the R3 2016 release and I would recommend adding the conditional formatting rules through the designer if possible. See the Changes on Items in Report Events Are Not Applied KB article for more information.

I hope this helps.

Indranil
Top achievements
Rank 1
commented on 02 Jan 2024, 11:24 AM | edited

hi Momchil . thanks for your valuable  comments . But as you suggested to use conditional  formatting via rules creation , here for my need there are so many data points like 100 datapoints  for each the bar will appear with data point value . For each data point name the bar color will be different and distinct  and so if I need to set a rule that means in design time I have to set 100  rules which may lead a hard go , and that is why code level am looking some better approach.  The c# code you gave will work  with my latest telerik reporting dll version I took from progress??

 

For example  A to Z 26 datapoints  and for 26 datapoints 26 bar  will come in the graph in one series . If I need to add 26 rules where datapoint name = "A" then color gold

If datapoint name ="G" then color  green like wise 26 times that lead many rules creations . Now just imagine this datapoint count to 100 then it will very hard to create 100 rules manually in design  time !

Momchil
Telerik team
commented on 02 Jan 2024, 02:06 PM

Hi Indranil,

I agree that applying the rules programmatically is more efficient in such scenarios.

Generally, the code we discussed should work but Report Events are not intended to be used as a place to modify the report definition and I cannot guarantee that every scenario you encounter will work.

This is why I would suggest applying the changes in the constructor of the report (after the InitializeComponent method if present) instead of the data binding event of the graph. See Understanding Report Events.

Tags
General Discussions
Asked by
Ravindra
Top achievements
Rank 1
Answers by
Nasko
Telerik team
Theo
Top achievements
Rank 2
Share this question
or