highcharts不能正常工作

时间:2012-06-04 19:44:31

标签: asp.net-mvc highcharts

我想制作一个像这样的高图http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/line-labels/我有一个存储过程,它计算每个月保留的所有房型(1月:双2,三重4 ...) 我需要在Roomtypereservations.RoomTypeByDate(i)中将I参数传送给我的存储过程;当我是1时我获得了1月份的数据,我是2月份的数据......

  public ActionResult Gresit()
    {
   DBContext.Current.Open();

  List<Series> allSeries = new List<Series>();
  List<Roomtypereservations> rezervari = new List<Roomtypereservations>();
  for (int i =1; i < 13; i++)
  {
      rezervari = Roomtypereservations.RoomTypeByDate(i);
      var results = new object[4];

      foreach (var a in rezervari)
      {
          results = (new object[] { a.NumRezervari });

          allSeries.Add(new Series
      {
          Name = a.Room_Type,

    //Data = new Data(myData)
          Data = new Data(results.ToArray())

      });
      }
  };


      Highcharts charts = new Highcharts("chart")
          .InitChart(new Chart { DefaultSeriesType = ChartTypes.Spline, })
          .SetTitle(new Title { Text = "Gradul De Ocupare pe luni" })
          .SetXAxis(new XAxis { Categories = Reservation.Categories })
          .SetYAxis(new YAxis
          {
              Title = new YAxisTitle { Text = "Numarul de Camere" },
              Labels = new YAxisLabels { Step = 5, Formatter = "function() { return this.value}" }

          })
          .SetTooltip(new Tooltip
          {
              Crosshairs = new Crosshairs(true),
              Shared = true
          })
          .SetPlotOptions(new PlotOptions
          {
              Spline = new PlotOptionsSpline
              {
                  Marker = new PlotOptionsSeriesMarker
                  {
                      Radius = 4,
                      LineColor = ColorTranslator.FromHtml("#666666"),
                      LineWidth = 1
                  }
              }
          })
          .SetSeries(allSeries.Select(s => new Series { Name = s.Name, Data = s.Data }).ToArray());

      return View(charts);

    }

但是我做错了,因为我明白了:我的所有数据都显示在1月份。我究竟做错了什么? enter image description here

1 个答案:

答案 0 :(得分:4)

这是因为您正在循环并为每个数据点创建一个系列。您需要从foreach结构中获取allSeries.Add语句。尝试使用以下修改结构:

for (int i =1; i < 13; i++)
{
  rezervari = Roomtypereservations.RoomTypeByDate(i);
  var results = new List<object>();

  foreach (var a in rezervari)
  {
    results.Add(a.NumRezervari);
  }

  allSeries.Add(new Series
  {
    Name = a.Room_Type,
    Data = new Data(results.ToArray())
  });
}

从外观上看,你创造了12种不同的系列。