如何传递或更多系列列表来设置系列

时间:2012-06-16 19:13:57

标签: asp.net-mvc-3 highcharts

您好我正在尝试使用dotnet highcharts实现组合高图。所以我有一个柱形图+饼图。 我为列表绘制了List allSeries = new List(),并为饼图列出了pieSeries = new List()。

我不知道如何将这两个系列传递给.SetSeries(),它接受SetSeries(系列丛书); 或SetSeries(Series [] seriesArray);

      public ActionResult Profit()
    {
        DBContext.Current.Open();
        List<RoomType> result = new List<RoomType>();
        result = RoomType.Selectcount();
        List<Series> allSeries = new List<Series>();
        List<Series> pieSeries = new List<Series>();
        List<DotNet.Highcharts.Options.Point> puncte = new List<DotNet.Highcharts.Options.Point>();
        string[] categories = new[] { "Ianuarie", "Februarie", "Martie", "Aprilie", "Mai", "Iunie", "Iulie", "August", "Septembrie", "Octombrie", "Noiembrie", "Decembrie" };
        object[] pointnum = new object[12];
        foreach (var j in result)
        {

            for (int i = 0; i < pointnum.Length; i++)
            {
                pointnum[i] = Roomtypereservations.RoomTypeByDate(j.RoomType_ID, i + 1).FirstOrDefault().NumRezervari;
            }
            allSeries.Add(new Series
            {
                Type=ChartTypes.Column,
                Name = j.Room_Type,
                //Data = new Data(myData)
                Data = new Data(pointnum.ToArray())

            });
            pieSeries.Add(new Series
            {
                Type = ChartTypes.Pie,
                Name = "Total rooms",
                Data = new Data(puncte.ToArray())
            });
            puncte.Add(new DotNet.Highcharts.Options.Point
            {

                Name = j.Room_Type,
                Y=13
                //Data = new Data(myData)


            }); 
        }

        Highcharts chart = new Highcharts("chart")
            .SetTitle(new Title { Text = "Combination chart" })
            .SetTooltip(new Tooltip { Formatter = "function() { return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %'; }" })
            .SetXAxis(new XAxis { Categories =categories} )
            .SetTooltip(new Tooltip { Formatter = "TooltipFormatter" })
            .AddJavascripFunction("TooltipFormatter",
                                  @"var s;
                if (this.point.name) { // the pie chart
                   s = ''+
                      this.point.name +': '+ this.y +' fruits';
                } else {
                   s = ''+
                      this.x  +': '+ this.y;
                }
                return s;")
            .SetLabels(new Labels
            {
                Items = new[]
                                   {
                                       new LabelsItems
                                       {
                                           Html = "Total fruit consumption",
                                           Style = "left: '40px', top: '8px', color: 'black'"
                                       }
                                   }
            })
            .SetPlotOptions(new PlotOptions
            {
                Pie = new PlotOptionsPie
                {
                    Center = new[] { "100", "80" },
                    Size = "100",
                    ShowInLegend = false,
                    DataLabels = new PlotOptionsPieDataLabels { Enabled = false }
                }
            })
            .SetSeries(allSeries.Select(s => new Series { Type = s.Type, Name = s.Name, Data = s.Data }).ToArray());


        return View(chart);

当我只使用我的样本中的一个系列时  它的工作:  .SetSeries(allSeries.Select(s =&gt; new Series {Type = s.Type,Name = s.Name,Data = s.Data})。ToArray());

我怎样才能将pieSeries和所有系列都放到.SetSeries?

1 个答案:

答案 0 :(得分:3)

您不需要allSeries和pieSeries。我会摆脱pieSeries。您可以根据需要为allSeries List分配尽可能多的系列,它们可以是任何类型。因此,将您的pieSeries.Add更改为以下内容:

allSeries.Add(new Series
{
   Type = ChartTypes.Pie,
   Name = "Total rooms",
   Data = new Data(puncte.ToArray())
})

然后,以下声明将起作用,并且您所需的所有系列都会出现在图表中:

.SetSeries(allSeries.Select(s => new Series { Type = s.Type, Name = s.Name, Data = s.Data }).ToArray());