highcharts日期型X轴示例

highcharts处理日期型X轴比较麻烦,用以下方法可以实现:

HTML:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Highcharts_Default" %>





	
	
	


	

前端脚本:


后台代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Highcharts_Default : System.Web.UI.Page
{
    public string dataPoints="";
    protected void Page_Load(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dgmlOAConnectionString"].ConnectionString))
        {
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select max(money) as money,id, name,date from("
                + " SELECT  sum(e.amount) as money,w.id, w.name,d.date,e.empCode"
                + " FROM ProductpSchedule_Tasks t"
                + "     INNER JOIN ProductpSchedule_Main m ON t.pid = m.id"
                + "     INNER JOIN ProductpSchedule_ReportData d ON t.id = d.pid"
                + "     INNER JOIN workcenter w ON w.code = d.workcenterCode"
                + "     INNER JOIN department dept ON w.pid = dept.id"
                + "     Inner join dbo.ProductpSchedule_ReportData_Emps e on e.pid=d.id"
                + " WHERE d.date BETWEEN @date1 AND @date2 ";
            cmd.CommandText += " group by w.id, w.name,d.date,e.empCode) t"
                + " group by id,name,date having(max(money))>0 order by id,date";
            cmd.Parameters.Add("date1", "2012-4-1");
            cmd.Parameters.Add("date2", "2012-9-1");
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            string ser = "";
            string data = "";
            TimeSpan ticks =new TimeSpan(new DateTime(1970, 1, 1).Ticks);
            while (dr.Read())
            {
                if (ser != dr["name"].ToString())
                {
                    if (ser != "")
                    {
                        if (dataPoints != "")
                        {
                            dataPoints += ",";
                        }
                        dataPoints += "{name:'" + ser + "',data:[" + data + "]}";
                    }
                    ser = dr["name"].ToString();
                    data = "{x:" + new TimeSpan(DateTime.Parse(dr["date"].ToString()).Ticks).Subtract(ticks).Duration().TotalMilliseconds.ToString() + ",y:" + dr["money"].ToString() + ",id:" + dr["id"].ToString() + "}";
               }
                else
                {
                    if (data != "")
                    {
                        data += ",";
                    }
                    data += "{x:" + new TimeSpan(DateTime.Parse(dr["date"].ToString()).Ticks).Subtract(ticks).Duration().TotalMilliseconds.ToString() + ",y:" + dr["money"].ToString() + ",id:" + dr["id"].ToString() + "}";
                }
            }
            if (dataPoints != "")
            {
                dataPoints += ",";
            }
            dataPoints += "{name:'" + ser + "',data:[" + data + "]}";
            //Response.Write(dataPoints);
            dr.Close();
            cmd.Dispose();
        }
    }
}



你可能感兴趣的:(Javascript资料,ASP.NET,date,function,join,string,xhtml,html)