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

LINQ to SQL Bar Chart

4 Answers 102 Views
Chart (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Karl Rohrbach
Top achievements
Rank 1
Karl Rohrbach asked on 18 Feb 2010, 10:18 PM
Hi All,

I have been trying to get this to work with little to no success.  I have had mixed failed results sometimes showing numerous (3 bars for each value returned) 

//Get Counts and Bind to Bar Chart 
var itemdata = from x in Data.Items  
join r in Data.SubItems on x.ItemID equals r.ItemID  
join d in Data.Documents on r.DocID equals d.DocID  
                                  where d.DocID == documentID  
                                  select new  
                                  {  
      MajorCount = getCount(x.ItemID, r.Lookup1.Value, "Major"),  
      MinorCount = getCount(x.ItemID, r.Lookup1.Value, "Minor"),  
      NonCount = getCount(x.ItemID, r.Lookup1.Value, "Non")  
                                  };  
 
//Bar Chart  
PerformanceChart.DataSource = itemdata;  
PerformanceChart.DataBind(); 

The linq query above returns a count for each item (MajorCount, MinorCount, NonCount).  In a nutshell, I want to show 3 bars (MajorCount, MinorCount, NonCount) vertically along the X Axis.  And numbers (1, 2, 3, 4, etc) on the Y Axis. 

I did not paste my bar chart in this post... because it is already a lost cause.  Any simple solutions that someone has would be greatly appreciated. 

4 Answers, 1 is accepted

Sort by
0
Ves
Telerik team
answered on 19 Feb 2010, 04:03 PM
Hello Karl,

RadChart will create a ChartSeries for each property in the underlying data objects and a ChartSeriesItem in each ChartSeries for each item in the datasource. That said, it would be expected to get 15 bars for 5 items in the datasource (3 bars for each item, each bar displaying the value for MajorCount, MinorCount or NonCount). You would get similar results with a gridview - a column for each property and a row for each item in the datasource.

So, in order to get 3 bars (one for MajorCount, one for MinorCount and one for NonCount - that would be 3 chart series with one bar for each series) you need to aggregate the results to a list of a single object with these 3 properties.

Regards,
Ves
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Karl Rohrbach
Top achievements
Rank 1
answered on 20 Feb 2010, 01:33 AM
Thanks for the response Ves.  Awesome! 

I have a single list now, but am still having trouble understanding binding the list to a bar chart.

Major
Minor
Major
NonRev

I want to display 3 bars.  Major, Minor, NonRev   My Y axis will be 1, 2, 3.  My X axis will be Major, Minor, Non Rev.   For some reason when I bind my list to the bar chart, I get something funky like 4 bars with 5,7,5,5.  

Any help will be greatly appreciated... or if someone knows of an example that they can point me to.  Please note that it is a single list.  

Thanks.  

Karl 


0
Karl Rohrbach
Top achievements
Rank 1
answered on 22 Feb 2010, 07:04 PM
I am getting closer.  I had to iterate through the list and programatically add the chartseries/series items (see below).   I have a question, however.  How do I get the XAxis legend to display?   I would like to have in my XAxis:  Major  Minor  Non Rev

Thanks in advance for any insight.  Regards,  Karl

 
foreach (var item in typeCount)  
                    {  
                        if (item.MyCount.Min() == "Major")  
                        {  
                            majorcount = majorcount + 1;  
                        }  
 
                        if (item.MyCount.Min() == "Minor")  
                        {  
                            minorcount = minorcount + 1;  
                        }  
 
                        if (item.MyCount.Min() == "Non Rev")  
                        {  
                            nonrevcount = nonrevcount + 1;  
                        }  
                    }  
 
                    ChartSeries series = new ChartSeries();  
                    series.Appearance.Shadow.Color = System.Drawing.Color.Transparent;  
                    series.Appearance.Shadow.Distance = 0;  
 
                    ChartSeriesItem seriesItemMajor = new ChartSeriesItem(majorcount);  
                    seriesItemMajor.Name = "Major";  
                    seriesItemMajor.Appearance.FillStyle.MainColor = System.Drawing.Color.DarkRed;  
                    seriesItemMajor.Appearance.FillStyle.SecondColor = System.Drawing.Color.DarkRed;  
                    seriesItemMajor.Appearance.Border.Visible = false;  
                    series.AddItem(seriesItemMajor);  
 
                    ChartSeriesItem seriesItemMinor = new ChartSeriesItem(minorcount);  
                    seriesItemMinor.Name = "Minor";  
                    seriesItemMinor.Appearance.FillStyle.MainColor = System.Drawing.ColorTranslator.FromHtml("#FF9900");  
                    seriesItemMinor.Appearance.FillStyle.SecondColor = System.Drawing.ColorTranslator.FromHtml("#FF9900");  
                    seriesItemMinor.Appearance.Border.Visible = false;  
                    series.AddItem(seriesItemMinor);  
 
                    ChartSeriesItem seriesItemNonRev = new ChartSeriesItem(nonrevcount);  
                    seriesItemNonRev.Name = "Non Rev";  
                    seriesItemNonRev.Appearance.FillStyle.MainColor = System.Drawing.ColorTranslator.FromHtml("#FFCC00");  
                    seriesItemNonRev.Appearance.FillStyle.SecondColor = System.Drawing.ColorTranslator.FromHtml("#FFCC00");  
                    seriesItemNonRev.Appearance.Border.Visible = false;  
                    series.AddItem(seriesItemNonRev);  
 
                    RevisionPerformanceChart.Appearance.Shadow.Color = System.Drawing.Color.Transparent;  
                    RevisionPerformanceChart.Appearance.Shadow.Distance = 0;  
                    RevisionPerformanceChart.Series.Add(series); 
0
Ves
Telerik team
answered on 24 Feb 2010, 12:26 PM
Hello Karl,

You can populate the X axis manually too:

RevisionPerformanceChart.PlotArea.XAxis.AutoScale = false;
RevisionPerformanceChart.PlotArea.XAxis.AddItem("Major Count");
RevisionPerformanceChart.PlotArea.XAxis.AddItem("Minor Count");
RevisionPerformanceChart.PlotArea.XAxis.AddItem("Non Count");


Regards,
Ves
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Chart (Obsolete)
Asked by
Karl Rohrbach
Top achievements
Rank 1
Answers by
Ves
Telerik team
Karl Rohrbach
Top achievements
Rank 1
Share this question
or