c# - Xamarin.Forms: How to have a Pie Chart using data from the Database? -


i'm creating xamarin.forms portable application wherein able display pie chart using oxyplot. chart has pre-defined items. want make items database in visual studio displayed on chart , not pre-defined items.

i have 2 projects in 1 solution. webformsproject , xamarin.forms (portable)

in webformsproject, created salescontroller used linq expression data in database need. created salesviewmodel there have declared properties have. tried test in web api if return value or not, , return value.

in portable project, have created sales model has same properties salesviewmodel in webformsproject. have salesvm.cs wherein tried access records webformsproject using salesservices , restclient.

i have tried using these codes didn't work. think reason behind this?

here codes:

webforms

1.) salesviewmodel.cs

using system; using system.collections.generic; using system.linq; using system.web;  namespace webformsdemo.viewmodel {     public class salesviewmodel     {         public int id { get; set; }         public int order_id { get; set; }         public int order_details_id { get; set; }         public int product_id { get; set; }         public string product_code { get; set; }         public string net_amount { get; set; }     } } 

2.) salescontroller.cs

using system; using system.collections.generic; using system.data; using system.data.entity; using system.data.entity.infrastructure; using system.linq; using system.net; using system.net.http; using system.web.http; using system.web.http.description; using webformsdemo; using webformsdemo.viewmodel;  namespace webformsdemo.controllers { public class salescontroller : apicontroller {     private ebmsentities db = new ebmsentities();      // get: api/sales     public iqueryable<salesviewmodel> getsalesviewmodels()     {         //return db.salesviewmodels;         var sales = order in db.orders                     join order_detail in db.order_details                     on order.order_id equals order_detail.order_id                     join prod in db.products                     on order_detail.product_id equals prod.product_id                     orderby order_detail.total_amt_per_item descending                     //group prod prod.product_code                     group order_detail prod.product_code od                     select new salesviewmodel                     {                          product_code = od.key,                         net_amount = od.sum(p => p.total_amt_per_item).tostring(),                      };          return sales;       }      // get: api/sales/5     [responsetype(typeof(salesviewmodel))]     public ihttpactionresult getsalesviewmodel(int id)     {         salesviewmodel salesviewmodel = db.salesviewmodels.find(id);         if (salesviewmodel == null)         {             return notfound();         }          return ok(salesviewmodel);     }      // put: api/sales/5     [responsetype(typeof(void))]     public ihttpactionresult putsalesviewmodel(int id, salesviewmodel salesviewmodel)     {         if (!modelstate.isvalid)         {             return badrequest(modelstate);         }          if (id != salesviewmodel.id)         {             return badrequest();         }          db.entry(salesviewmodel).state = entitystate.modified;          try         {             db.savechanges();         }         catch (dbupdateconcurrencyexception)         {             if (!salesviewmodelexists(id))             {                 return notfound();             }             else             {                 throw;             }         }          return statuscode(httpstatuscode.nocontent);     }      // post: api/sales     [responsetype(typeof(salesviewmodel))]     public ihttpactionresult postsalesviewmodel(salesviewmodel salesviewmodel)     {         if (!modelstate.isvalid)         {             return badrequest(modelstate);         }          db.salesviewmodels.add(salesviewmodel);         db.savechanges();          return createdatroute("defaultapi", new { id = salesviewmodel.id }, salesviewmodel);     }      // delete: api/sales/5     [responsetype(typeof(salesviewmodel))]     public ihttpactionresult deletesalesviewmodel(int id)     {         salesviewmodel salesviewmodel = db.salesviewmodels.find(id);         if (salesviewmodel == null)         {             return notfound();         }          db.salesviewmodels.remove(salesviewmodel);         db.savechanges();          return ok(salesviewmodel);     }      protected override void dispose(bool disposing)     {         if (disposing)         {             db.dispose();         }         base.dispose(disposing);     }      private bool salesviewmodelexists(int id)     {         return db.salesviewmodels.count(e => e.id == id) > 0;         }     } } 

.

xamarinformsportable

1.) sales.cs

using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks;  namespace xamarinformsdemo.models { public class sales {     public int id { get; set; }     public int order_id { get; set; }     public int order_details_id { get; set; }     public int product_id { get; set; }     public string product_code { get; set; }     public double net_amount { get; set; }     } } 

2.) salesvm.cs

using system; using system.collections.generic; using system.componentmodel; using system.linq; using system.text; using system.threading.tasks; using oxyplot; using oxyplot.series; using oxyplot.xamarin.forms; using xamarin.forms; using system.runtime.compilerservices;  using xamarinformsdemo.models; using system.collections.objectmodel; using xamarinformsdemo.services;  namespace xamarinformsdemo.viewmodels  {     public class salesvm     {     private list<sales> salesmodel { get; set; }       public list<sales> salesmodelvm     {                 {             return salesmodel;         }         set         {             salesmodel = value;             onpropertychanged();         }     }          public salesvm()     {          initializedataasync();       }      private async task initializedataasync()     {         var salesservices = new salesservices();         salesmodelvm = await salesservices.getsalesasync();      }       public event propertychangedeventhandler propertychanged;      protected virtual void onpropertychanged([callermembername] string propertyname = null)     {         var handler = propertychanged;         if (handler != null) handler(this, new propertychangedeventargs(propertyname));         }      } } 

3.) salesservices.cs

using plugin.restclient; using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using xamarinformsdemo.models; namespace xamarinformsdemo.services {     public class salesservices     {      public async task<list<sales>> getsalesasync()     {         restclient_sales<sales> restclient = new restclient_sales<sales>();          var saleslist = await restclient.getsalesasync();          return saleslist;          }      } } 

4.) restclient.cs

    public class restclient_sales<t>     {      private const string webserviceurl = "http://localhost:50857/api/sales/";      public async task<list<t>> getsalesasync()     {         var httpclient = new httpclient();          var json = await httpclient.getstringasync(webserviceurl);          var taskmodels = jsonconvert.deserializeobject<list<t>>(json);          return taskmodels;        }      } 


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -