Hi,
I would like the javascript to show data from getData method (webMethod), when the page loads but when filter the data (categoryPicker control), using webMethod getData2.
This is currently my c# page.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
ClientScript.RegisterStartupScript(this.GetType(), "TestInitPageScript",
string.Format("<script type=\"text/javascript\">google.load('visualization','1.0',{{'packages':['corechart','controls']}});google.setOnLoadCallback(function(){{drawVisualization({0},'{1}','{2}','{3}');}});</script>",
jss.Serialize(GetData()),
"Name Example",
"Name",
"Type Example",
"Type Example",
"Type,"));
}
else
{
JavaScriptSerializer jtt = new JavaScriptSerializer();
ClientScript.RegisterStartupScript(this.GetType(), "TestInitPageScript",
string.Format("<script type=\"text/javascript\">google.load('visualization','1.0',{{'packages':['corechart','controls']}});google.setOnLoadCallback(function(){{drawVisualization({0},'{1}','{2}','{3}');}});</script>",
jtt.Serialize(GetData2()),
"Name Example",
"Name",
"Type Example",
"Type Example",
"Type,"));
}
}
[WebMethod]
public static List<Data> GetData()
{
SqlConnection conn = new SqlConnection("#####");
DataSet ds = new DataSet();
DataTable dt = new DataTable();
conn.Open();
var yesterday = DateTime.Today.AddDays(-1);
string cmdstr = "select top 500 Name, [Decimal price],Cover, UploadDate from [dbo].[database] order by UploadDate desc";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
dt = ds.Tables[0];
List<Data> dataList = new List<Data>();
string cat = "";
float val = 0;
string typ = "";
DateTime dat = DateTime.Now;
foreach (DataRow dr in dt.Rows)
{
try
{
cat = dr[0].ToString();
val = Convert.ToInt32(dr[1]);
typ = dr[2].ToString();
dat = (DateTime)(dr[3]);
}
catch
{
}
dataList.Add(new Data(cat, val, typ, dat));
}
return dataList;
}
[WebMethod]
public static List<Data> GetData2()
{
SqlConnection conn = new SqlConnection("#####");
DataSet ds = new DataSet();
DataTable dt = new DataTable();
conn.Open();
var yesterday = DateTime.Today.AddDays(-1);
string cmdstr = "select Name, [Decimal price],Cover, UploadDate from [dbo].[database] order by UploadDate desc";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
dt = ds.Tables[0];
List<Data> dataList = new List<Data>();
string cat = "";
float val = 0;
string typ = "";
DateTime dat = DateTime.Now;
foreach (DataRow dr in dt.Rows)
{
try
{
cat = dr[0].ToString();
val = Convert.ToInt32(dr[1]);
typ = dr[2].ToString();
dat = (DateTime)(dr[3]);
}
catch
{
}
dataList.Add(new Data(cat, val, typ, dat));
}
return dataList;
}
I am little unsure, how do I go about updating on the javascript side. Could any one provide some guideline or process in what I need to do, please.
function drawVisualization(dataValues, chartTitle, columnNames, categoryCaption) {
if (dataValues.length < 1)
return;
var data = new google.visualization.DataTable();
data.addColumn('string', columnNames.split(',')[0], 'name');
data.addColumn('number', columnNames.split(',')[1], 'price');
data.addColumn('string', columnNames.split(',')[2], 'type');
data.addColumn('datetime', columnNames.split(',')[3], 'date');
for (var i = 0; i < dataValues.length; i++) {
var date = new Date(parseInt(dataValues[i].Date.substr(6), 10));
data.addRow([dataValues[i].ColumnName, dataValues[i].Value, dataValues[i].Type, date]);
}
var dateFormatter = new google.visualization.DateFormat({ pattern: 'dd MM yyyy' });
var line = new google.visualization.ChartWrapper({
'chartType': 'AreaChart',
'containerId': 'PieChartContainer',
'options': {
'width': 1200,
'height': 500,
'legend': 'none',
'hAxis': {
'format': "dd-MM-yyyy",
'hAxis.maxValue': 'viewWindow.max',
'maxValue': new Date(2014, 05, 30), 'minValue': new Date(2014, 04, 05),
'viewWindow': { 'max': new Date(2014, 05, 30) },
},
'chartArea': { 'left': 150, 'top': 100, 'right': 50, 'bottom': 100 },
'tooltip': { isHtml: true }
},
'view': {
'columns': [{
type: 'string',
label: data.getColumnLabel(3),
calc: function (dt, row) {
var date = dt.getValue(row, 3);
return dateFormatter.formatValue(date);
}
}, 1, {
type: 'string',
role: 'tooltip',
calc: function (dt, row) {
return 'Name: ' + dt.getValue(row, 0) + ', Price: ' + +dt.getValue(row, 1) + ', Date: ' + +dt.getFormattedValue(row, 3);
}
}]
}
});
var categoryPicker = new google.visualization.ControlWrapper({
'controlType': 'CategoryFilter',
'containerId': 'CategoryPickerContainer',
'options': {
'filterColumnLabel': columnNames.split(',')[3],
'filterColumnIndex': '3',
'ui': {
'labelStacking': 'horizontal',
'allowTyping': false,
'allowMultiple': false,
'caption': categoryCaption,
'label': 'Date',
}
}
});
new google.visualization.Dashboard(document.getElementById('PieChartExample')).bind([categoryPicker], [line]).draw(data);
table.draw(data, { showRowNumber: true });
}
Any help or guidance would be very much appreciated. Thanks in advance.
|