Hello,
I am trying to parse datetime parameter as date but I am not getting desired output, since the output is only showing year instead of the whole date.
This is my current javascript code:
function drawVisualization(dataValues, chartTitle, columnNames, categoryCaption) {
if (dataValues.length < 1)
return;
var data = new google.visualization.DataTable();
data.addColumn('string', columnNames.split(',')[0]);
data.addColumn('number', columnNames.split(',')[1]);
data.addColumn('string', columnNames.split(',')[2]);
data.addColumn('datetime', columnNames.split(',')[3]);
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 pie = new google.visualization.ChartWrapper({
'chartType': 'LineChart',
'containerId': 'PieChartContainer',
'options': {
'width': 950,
'height': 450,
'legend': 'right',
'title': chartTitle,
'chartArea': { 'left': 100, 'top': 100, 'right': 0, 'bottom': 100 },
'pieSliceText': 'label',
'tooltip': { 'text': 'percentage' }
},
'view': { 'columns': [3, 1] }
});
new google.visualization.Dashboard(document.getElementById('PieChartExample')).bind([categoryPicker, stringFilter], [pie]).draw(data);
}
}
From the code behind, I am passing the datetime from the server as part of web method:
protected void Page_Load(object sender, EventArgs e)
{
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()),
"Text Example",
"Text Example",
"type,"));
}
[WebMethod]
public static List<test> 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 5 Name, [Decimal price],Cover, UploadDate from [dbo].[database]";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
dt = ds.Tables[0];
List<test> dataList = new List<test>();
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 test(cat, val, typ, dat));
}
return dataList;
}
Updated date code:
function drawVisualization(dataValues, chartTitle, columnNames, categoryCaption) {
if (dataValues.length < 1)
return;
var data = new google.visualization.DataTable();
data.addColumn('string', columnNames.split(',')[0]);
data.addColumn('number', columnNames.split(',')[1]);
data.addColumn('string', columnNames.split(',')[2]);
data.addColumn('datetime', columnNames.split(',')[3]);
for (var i = 0; i < dataValues.length; i++) {
var dateString = dataValues[i].Date.substr(6);
var currentTime = new Date(parseInt(dateString));
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var date = day + "/" + month + "/" + year;
alert(date);
data.addRow([dataValues[i].ColumnName, dataValues[i].Value, dataValues[i].Type, date]);
}
Any help would be very much appreciated. Many thanks in advance.