Gal Ratner
Gal Ratner is a Techie who lives and works in Los Angeles CA and Austin TX. Follow galratner on Twitter Google
Display a sales chart with ASP.NET Chart control and Linq to SQL

The Chart control is an optional download for .NET 3.5. It features a complete chart including bars, stocks, pie, 3D and many more chart types.
You can get the download links to the control from Scott Gu’s blog here:
I was playing around with it yesterday and made a little chart to display yearly sales by month.
First I started by creating the control on my aspx page

<asp:Chart ID="SalesChart" Width="400px" runat="server">
            <asp:Legend Name="Default"></asp:Legend>
            <asp:Series Name="Series1" LegendText="Sales"></asp:Series>
            <asp:ChartArea Name="ChartArea1"></asp:ChartArea>

I wanted to display a month and the number of sales in that month so I prepared an enum with month names

public enum Month


        January = 1,

        February = 2,

        March = 3,

        April = 4,

        May = 5,

        June = 6,

        July = 7,

        August = 8,

        September = 9,

        October = 10,

        November = 11,

        December = 12



And then connected a query grouping the sales to the chart

public void BindSalesChart(int year)


        using (CartDataClassesDataContext context = new CartDataClassesDataContext())


            var sales = from o in context.Orders

                        where o.DatePlaced.Value.Year == year

                        group o by o.DatePlaced.Value.Month into g

                        select new {Month = g, Orders = g.Count()};


            foreach (var sale in sales)

                SalesChart.Series["Series1"].Points.AddXY(Enum.Parse(typeof(Month), sale.Month.FirstOrDefault().DatePlaced.Value.Month.ToString()).ToString(), (double)sale.Orders);



The result was a chart control displaying my sales for the year.

Posted 2 Aug 2009 12:33 AM by Gal Ratner
Filed under:
Attachment: Chart-Control.jpg

Powered by Community Server (Non-Commercial Edition), by Telligent Systems