DotNet.Highcharts: Cost not plotted against the correct date

前端 未结 1 1526
无人及你
无人及你 2020-12-19 15:45

I\'m creating a DotNet.Highcharts chart that is using data from two tables: Expenditures and Incomings. I am using an SQL statement to create a DataTable for ea

相关标签:
1条回答
  • 2020-12-19 16:14

    Your Y values are completely disconnected from X values. In order to achieve what you want, you need to restructure your data as below:

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

    EDIT: How to do it dynamically:

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

    Data retrieved from database:

    0 讨论(0)
提交回复
热议问题