locked
How to Create Pie chart with Database? RRS feed

  • Question

  • User-524221330 posted

    Hello All,

    Here is my code, please help me with this,

    @{
        
        var db = Database.Open("patientvalueservices");
        var sales = "SELECT categoryTitle from Categories";
        
         foreach (var item in sales) {
                
                var salesone = new Dictionary<string, int>()
            {
     
                { item.title, 10 }
     
            };
     
             }
     
        
        
        var myChart = new Chart(width: 1000, height: 800)
            .AddTitle("SALES")
            .AddSeries(
                chartType: "Pie",
                name: "ProductSales",
                xValue: salesone.Keys,
                yValues: salesone.Values)
            .Write();
    }

    Thanks,

    Vikram

    Tuesday, March 5, 2013 6:49 AM

Answers

  • User-1731511703 posted

    I was able to get a Pie chart created from a database.  I recommend going throught this link and creating a SmallBakery.sdf database so you can see how it works:

    http://www.asp.net/web-pages/tutorials/data/5-working-with-data

    Once I had the database built, I was able to get a Pie chart to generate based on the Database information using information from the following link:

    http://www.asp.net/web-pages/tutorials/data/7-displaying-data-in-a-chart

    Here's my code for reference:

    @{
        var db = Database.Open("SmallBakery");
        var data = db.Query("SELECT Name, Price FROM Product");
        var myChart = new Chart(width: 600, height: 400)
            .AddTitle("Product Sales")
            .AddSeries(chartType: "Pie",
            xValue: data, xField: "Name",
            yValues: data, yFields: "Price")
            .Write();
     }
    

    Once you get it working from the example, you can see how it all comes together and work on modifying it to meet your needs.

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Tuesday, March 5, 2013 10:58 AM

All replies

  • User-1731511703 posted

    Did you take a look at this post?  It seems to have a lot of the information that you would need.

    http://www.asp.net/web-pages/tutorials/data/7-displaying-data-in-a-chart

    Tuesday, March 5, 2013 6:57 AM
  • User-524221330 posted

    Yes, I have seen the code. For the pie chart, they reteriving not from database directly. Can you help me with the code.

    Tuesday, March 5, 2013 7:05 AM
  • User-1731511703 posted

    Sure, I'll take a look at the code and see what I can come up with.

    Tuesday, March 5, 2013 7:37 AM
  • User-1731511703 posted

    I was able to get a Pie chart created from a database.  I recommend going throught this link and creating a SmallBakery.sdf database so you can see how it works:

    http://www.asp.net/web-pages/tutorials/data/5-working-with-data

    Once I had the database built, I was able to get a Pie chart to generate based on the Database information using information from the following link:

    http://www.asp.net/web-pages/tutorials/data/7-displaying-data-in-a-chart

    Here's my code for reference:

    @{
        var db = Database.Open("SmallBakery");
        var data = db.Query("SELECT Name, Price FROM Product");
        var myChart = new Chart(width: 600, height: 400)
            .AddTitle("Product Sales")
            .AddSeries(chartType: "Pie",
            xValue: data, xField: "Name",
            yValues: data, yFields: "Price")
            .Write();
     }
    

    Once you get it working from the example, you can see how it all comes together and work on modifying it to meet your needs.

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Tuesday, March 5, 2013 10:58 AM
  • User-1731511703 posted

    Were you able to get this to work with the code and info that I provided above?

    Saturday, March 9, 2013 12:39 PM