开发者

ASP.NET Chart for Grades Distribution

I'd like to show an ASP.NET Chart control that shows a distribution of grades.

It should show how many students got which grade, ranging from A to F.

How do I make the chart include the grades A, B, C, D, F开发者_StackOverflow中文版 in the cases where no students got, say, a D letter grade? For example, I'd like the D value to to present on the X-axis, but with a student count of zero.

The data is being pulled from a database and I'm using an ObjectDataSource to bind to the chart control.

EDIT: The structure of the data is that there's a Students table and each stundent has a FirstName, LastName, and Grade.

EDIT: For the rest of the site I'm using Entity Framework with LINQ (as opposed to writing raw SQL), so, in a perfect world, the solution wouldn't involved raw SQL.


Is it possible to change your query includes all grades then fill in the details for each grade? total hack - but to show you the idea as I don't know your schema

select * from 
(
select 'a' Grade
union
select 'b' Grade
union
select 'c' Grade
union
select 'd' Grade
union
select 'f' Grade
) as grades g
outer join yourothertable t on g.Grade = t.Grade

Edit: After reading your edit realizing you don't want this to be a sql solution, then the choice is to take your objects and do a linq join on them to objects containing the grade.

see: http://www.hookedonlinq.com/OuterJoinSample.ashx use something like public class Grade { public string LetterGrade; } populate a list of them, and do an outer join to your results from your query. Your new linq results collection is what you now databind.


Without know how your data is structured it's a bit difficult to give a detailed answer. Here's a way to do it using SQL Common Table Expressions (CTEs). It will organize the data so that you can easily bind it to a chart control.

This will produce a table of data as follows:

| Grade | TotalGrades |
|-------|-------------| 
|   A   |      2      |
|   B   |      1      |
|   C   |      2      |
|   D   |      0      |
|   E   |      0      |
|   F   |      1      |

And here's the code:

;With Grades (Grade) as
(
    SELECT 'A' Grade
    UNION
    SELECT 'B' Grade
    UNION
    SELECT 'C' Grade
    UNION
    SELECT 'D' Grade
    UNION
    SELECT 'E' Grade
    UNION
    SELECT 'F' Grade
),
GradeResults (Name,Grade) as
(
    SELECT 'John','A'
    UNION
    SELECT 'Sally','B'
    UNION
    SELECT 'Dave','C'
    UNION
    SELECT 'Charlie','C'
    UNION
    SELECT 'Lisa','F'
    UNION
    SELECT 'Russ','A'
)
SELECT a.Grade, COUNT(b.Grade) as TotalGrades FROM Grades a LEFT OUTER JOIN GradeResults b on a.Grade = b.Grade
GROUP BY a.Grade
ORDER BY a.Grade;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜