2

嗨,我正在尝试使用 dotnet highcharts 实现组合的 highchart。所以我有一个柱形图+饼图。我为柱形图制作了 List allSeries = new List(),为饼图制作了 List pieSeries = new List()。

我不知道如何将这两个系列传递给 .SetSeries() ,它接受 SetSeries(Series series); 或 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 => new Series { Type = s.Type, Name = s.Name, Data = s.Data }).ToArray( ));

我如何将 pieSeries 和所有系列都传递给 .SetSeries?

4

1 回答 1

3

您不需要 allSeries 和 pieSeries。我会摆脱 pieSeries。您可以根据需要将任意数量的系列分配给您的 allSeries 列表,它们可以是任何类型。因此,更改您的 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());
于 2012-06-16T21:38:23.353 回答