highcharts日期型X轴示例

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

HTML:

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


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title></title>
	<script src="../JS/jquery-1.7.min.js" type="text/javascript"></script>
	<script src="../JS/highcharts.js" type="text/javascript"></script>
</head>
<body>
	<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto">
	</div>
</body>
</html>

前端脚本:

<script type="text/javascript">
	$(function () {
		var chart;
		$(document).ready(function () {
			chart = new Highcharts.Chart({
				chart: {
					renderTo: 'container',
					type: 'line',
					marginRight: 130,
					marginBottom: 25
				},
				title: {
					text: '员工工资曲线图',
                    style:{
					    fontSize: '20px'
                    },
					x: -20 //center
				},
				subtitle: {
					text: 'Source: WorldClimate.com',
					x: -20
				},
				xAxis: {
					type: 'datetime',
					labels: {
						step: 4, 
						formatter: function () {
							return Highcharts.dateFormat('%Y-%m-%d', this.value);
						}
					}
				},
				yAxis: {
					title: {
						text: '工资(¥)'
					},
					plotLines: [{
						value: 0,
						width: 1,
						color: '#808080'
					}]
				},
				tooltip: {
					formatter: function () {
						return '<b>' + this.series.name + '</b><br/>' +
						Highcharts.dateFormat('%Y-%m-%d', this.x) + ': ¥' + this.y.toFixed(2);
					}
				},
				legend: {
					layout: 'vertical',
					align: 'right',
					verticalAlign: 'top',
					x: -10,
					y: 100,
					borderWidth: 0
				},
				plotOptions: {
					series: {
						cursor: 'pointer',
						dataLabels: {
							enabled: true,
							formatter: function() {
								return '¥' + this.y.toFixed(2);
							}
						},
						point: {
							events: {
								click: function() {
									alert ('Category: '+ Highcharts.dateFormat('%Y-%m-%d', this.x) +', value: '+ this.y + ",id:" + this.id);
								}
							}
						}
					}
				},
				series: [<%=dataPoints%> ]
			});
		});

	});
</script>

后台代码示例:

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();
        }
    }
}



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hello World,

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值