DotNet.Highcharts:未按正确日期绘制成本

时间:2016-03-04 17:41:38

标签: c# sql asp.net datatable dotnethighcharts

我正在创建一个使用两个表中数据的DotNet.Highcharts图表:支出和收入。我正在使用SQL语句为每个语句创建SELECT ConsumerID, CountOfOrdersTwelve FROM ( SELECT ConsumerID, TIMESTAMP_TO_SEC(TIMESTAMP(TransactionDate)) AS ts, COUNT(ts) OVER (PARTITION BY ConsumerID ORDER BY ts RANGE BETWEEN CURRENT ROW AND 365*24*3600 FOLLOWING) AS CountOfOrdersTwelve, ROW_NUMBER() OVER(PARTITION BY ConsumerID ORDER BY ts) AS pos FROM [ordertable.orders] ) WHERE pos = 1 ORDER BY ConsumerID 。第一个(对于Incomings)称为DataTable包含DtIncCost。名为IncDate的第二个(支出)包含Dt2ExpCost

我可以ExpDate针对IncCostIncDate针对ExpCost进行展示。当我尝试连接ExpDateIncDate(最终)时会出现问题,因为ExpDateIncCost的成本与(最终)中的日期位置相关联,而不是与之相关的实际日期。

以下是我用来创建每个DataTable的调用:

ExpCost

以下是从数据表中调用其中每一项的代码

SqlDataAdapter Adp = new SqlDataAdapter("select CONVERT(DATETIME, IncDate, 103) AS IncDate, SUM(IncCost) AS IncCost from Incomings GROUP BY CONVERT(DATETIME, IncDate, 103) ORDER BY CONVERT(DATETIME, IncDate, 103)", con);

SqlDataAdapter Adp2 = new SqlDataAdapter("select CONVERT(DATETIME, ExpDate, 103) AS ExpDate, SUM(ExpCost) AS ExpCost from Expenditures GROUP BY CONVERT(DATETIME, ExpDate, 103) ORDER BY CONVERT(DATETIME, ExpDate, 103)", con);

这是我用来创建我的高级图的代码:

var dateArr = Dt.AsEnumerable().Select(r => r.Field<DateTime>("IncDate")).ToArray();
var objectArr = Dt.AsEnumerable().Select(r => r.Field<object>("IncCost")).ToArray();

var dateArr2 = Dt2.AsEnumerable().Select(r => r.Field<DateTime>("ExpDate")).ToArray();
var objectArr2 = Dt2.AsEnumerable().Select(r => r.Field<object>("ExpCost")).ToArray();   

var res = dateArr.Concat(dateArr2).ToArray();
var final = res.Select(d => d.ToString(@"dd\/MM\/yyyy")).ToArray();

Plot 1

正如您所看到的,系列正在创建,但它们是根据数组中的数字绘制的,而不是与应链接的日期相对应。

我不确定我对解决方案有多近或多远,但感谢任何帮助。 这是我的incomings数据表

Here is the data for my incomings datatable Dt

这是我的支出数据

Here is the data for my expenditures datatable (Dt2)

真的不知道该如何去做。非常感谢任何信息

1 个答案:

答案 0 :(得分:4)

您的Y值与X值完全断开。为了达到您的目的,您需要重新构建数据,如下所示:

        DateTime dt = new DateTime(2016, 2, 27);

        Highcharts chart = new Highcharts("graph")
           .SetTitle(new Title { Text = "Incoming Stats" })
           .SetXAxis(new XAxis { Type = AxisTypes.Datetime })
           .SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Amount Incoming" } })
           .SetSeries(new[]
             {
              new Series { Name = "inc", Data = new Data(new object[,] { 
                  {dt , 23 }, 
                  {dt.AddDays(1) , 223 }, 
                  {dt.AddDays(2) , 51 }, 
                  {dt.AddDays(11) , 200 }, }) },
              new Series { Name = "exp", Data = new Data(new object[,] { 
                  {dt.AddDays(5) , 100 }, 
                  {dt.AddDays(6) , 23 }, 
                  {dt.AddDays(11) , 23 }, 
                  {dt.AddDays(19) , 35 }, 
                  {dt.AddDays(35) , 288 }, }) }
             });

enter image description here

修改 如何动态地做到:

        object[,] data1 = new object[Dt.Rows.Count, 2];
        for (int i = 0; i < Dt.Rows.Count; i++)
        {
            data1[i, 0] = Dt.Rows[i]["IncDate"];
            data1[i, 1] = Dt.Rows[i]["IncCost"];
        }

        object[,] data2 = new object[Dt2.Rows.Count, 2];
        for (int i = 0; i < Dt2.Rows.Count; i++)
        {
            data2[i, 0] = Dt2.Rows[i]["ExpDate"];
            data2[i, 1] = Dt2.Rows[i]["ExpCost"];
        }

        Highcharts chart = new Highcharts("graph")
           .SetTitle(new Title { Text = "Incoming Stats" })
           .SetXAxis(new XAxis { Type = AxisTypes.Datetime })
           .SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Amount Incoming" } })
           .SetSeries(new[]
             {
                    new Series { Name = "inc", Data = new Data(data1) },
                    new Series { Name = "exp", Data = new Data(data2) }
             });

enter image description here

从数据库中检索的数据:

enter image description here

enter image description here