Using Web Service to render asp.net chart
Using Javscript webservices, it’s easy to render Chart image.
1. Create a webservice to get chart image
[WebMethod(EnableSession=true)]
public string SupplierPLCategory(string type, string charttype, int from, int to)
{
var sessionUser = HttpContext.Current.Session["User"];
if (sessionUser != null)
{
Data.DTO.User user = sessionUser as Data.DTO.User;
int scenarioid = Convert.ToInt32(HttpContext.Current.Session["ScenarioID"]);
int countryid = Convert.ToInt32(HttpContext.Current.Session["CountryID"]);
List<Filter> filters = new List<Filter>();
filters.Add(new Filter() { Column = EnumModel.WidgetVariable.From, Value = from });
filters.Add(new Filter() { Column = EnumModel.WidgetVariable.To, Value = to });
filters.Add(new Filter() { Column = EnumModel.WidgetVariable.ChartType, Value = charttype });
filters.Add(new Filter() { Column = EnumModel.WidgetVariable.Display, Value = type });
filters.Add(new Filter() { Column = EnumModel.WidgetVariable.Country, Value = countryid });
filters.Add(new Filter() { Column = EnumModel.WidgetVariable.Scenario, Value = scenarioid });
filters.Add(new Filter() { Column = EnumModel.WidgetVariable.UserID, Value = user.ID });
return ViewManager.RenderView(“~/Controls/Reports/SupplierPLCategoryData.ascx”, filters);
}
return “error”;
}
Here viewmanager is a simple class which create a object of system.web.ui.page at run time and adds the controls
to it to execute and get the resultant HTML.
public class ViewManager
{
public static string RenderView(string path)
{
return RenderView(path, null);
}
public static string RenderView(string path, object data)
{
Page pageHolder = new Page();
UserControl viewControl = (UserControl)pageHolder.LoadControl(path);
if (data != null)
{
Type viewControlType = viewControl.GetType();
FieldInfo field = viewControlType.GetField(“ObjectData”);
if (field != null)
{
field.SetValue(viewControl, data);
}
else
{
throw new Exception(“View file: ” + path + ” does not have a public Data property”);
}
}
pageHolder.Controls.Add(viewControl);
StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute(pageHolder, output, false);
return output.ToString();
}
}
2. Create a user control to bind chart
public object ObjectData;
public void Fill()
{
if (ObjectData != null)
{
int scenarioId = 0;
string type=string.Empty ; string chartType=string.Empty ; int from=0; int to=0;
int userId = 0;
int countryId = 0;
List<Filter> filters = ObjectData as List<Filter>;
countryId = Convert.ToInt32(filters.Where(c => c.Column == EnumModel.WidgetVariable.Country).SingleOrDefault().Value);
scenarioId = Convert.ToInt32(filters.Where(c => c.Column == EnumModel.WidgetVariable.Scenario).SingleOrDefault().Value);
userId = Convert.ToInt32(filters.Where(c => c.Column == EnumModel.WidgetVariable.UserID).SingleOrDefault().Value);
type =filters.Where(c => c.Column == EnumModel.WidgetVariable.Display).SingleOrDefault().Value.ToString();
chartType= filters.Where(c => c.Column == EnumModel.WidgetVariable.ChartType).SingleOrDefault().Value.ToString();
from =Convert.ToInt32( filters.Where(c => c.Column == EnumModel.WidgetVariable.From).SingleOrDefault().Value.ToString());
to = Convert.ToInt32(filters.Where(c => c.Column == EnumModel.WidgetVariable.To).SingleOrDefault().Value.ToString());
// fill chart here
}
}
In control’s HTML, just drag a chart control and create the required series and set the properties according to your requirements
3. Calling code is written in Asp.net Ajax but you can use Jquery if you would like to
function GetSupplierPLCategory(type, charttype, from, to) {
retailInsight.ProfitModel.Web.UI.Widgets.SupplierPLCategory(type,charttype, from ,to,
function (result) {
$get(“contents_pl_category”).innerHTML = result;
});
}