简体   繁体   English

在ASP.NET C#中使用自定义对象列表创建多列系列asp:chart

[英]Create multi column series asp:chart with custom objects list in asp.net C#

I would like to create a main overview chart with asp:chart control, like this: 我想使用asp:chart控件创建一个主要的概览图,如下所示:

在此处输入图片说明

Instead of Andrew, etc. it would be fiscal years (2009, 2010, 2011, etc.) and the products would be the 4 types of costs. 代替安德鲁等人,它将是会计年度(2009、2010、2011等),并且产品将是4种成本类型。 But how do I do this with a list of custom objects that are contstructed like this: 但是,如何使用这样构造的自定义对象列表来做到这一点:

List< Cost_cost > listOfCosts List <Cost_cost> listOfCosts

  • Cost 成本

    • Type (can be one of four types) 类型(可以是四种类型之一)
    • Amount (float) 金额(浮动)
    • Fiscal Year 财政年度

Anybody got a link or tips on how to handle this? 有人知道如何处理此问题的链接或提示吗?

Found a solution, it's partialy hard coded, ie, the types of costs but since it needs to be uploaded by the day after tomorrow, it'll do. 找到了一个解决方案,它是部分编码的硬编码,即成本的类型,但是由于需要在后天上传,因此可以解决。

The following code of course is after I created Chart1, the mainlegend and area and some styling stuff: 下面的代码当然是在创建Chart1,mainlegend和area以及一些样式设置之后的:

//Set the amount of the four series to zero
float totalAmountHousing = 0, totalAmountPersonnel = 0, totalAmountServices = 0, totalAmountIT = 0;

//Create the four series per year
Series sr = new Series(); Series sr2 = new Series(); Series sr3 = new Series(); Series sr4 = new Series();

//Set the series to the same chart area
sr.ChartArea = "mainArea"; sr2.ChartArea = "mainArea"; sr3.ChartArea = "mainArea"; sr4.ChartArea = "mainArea";

//Set them to the same legend
sr.Legend = "mainLegend"; sr2.Legend = "mainLegend"; sr3.Legend = "mainLegend"; sr4.Legend = "mainLegend";

//Set the names of the 4 series
sr.Name = "Housing"; sr2.Name = "IT"; sr3.Name = "Services"; sr4.Name = "Personnel";

//Add the series to the chart
Chart1.Series.Add(sr); Chart1.Series.Add(sr2); Chart1.Series.Add(sr3); Chart1.Series.Add(sr4);

//Set drawing style to cylinder of the four costs
Chart1.Series["Housing"]["DrawingStyle"] = "Cylinder";
Chart1.Series["IT"]["DrawingStyle"] = "Cylinder";
Chart1.Series["Services"]["DrawingStyle"] = "Cylinder";
Chart1.Series["Personnel"]["DrawingStyle"] = "Cylinder";

for (int i = 0; i < listOfFiscalYears.Count; i++) {

    //generate some point for the chart
    for (int j = 0; j < listOfCosts.Count; j++) {
        if ((listOfCosts[j].Type).ToLower() == "housing"    && listOfCosts[j].Cost_fiscalYear.Year == int.Parse(listOfFiscalYears[i].ToString())) totalAmountHousing += (float)listOfCosts[j].Amount;
        if ((listOfCosts[j].Type).ToLower() == "it"         && listOfCosts[j].Cost_fiscalYear.Year == int.Parse(listOfFiscalYears[i].ToString())) totalAmountIT += (float)listOfCosts[j].Amount;
        if ((listOfCosts[j].Type).ToLower() == "services"   && listOfCosts[j].Cost_fiscalYear.Year == int.Parse(listOfFiscalYears[i].ToString())) totalAmountServices += (float)listOfCosts[j].Amount;
        if ((listOfCosts[j].Type).ToLower() == "personnel"  && listOfCosts[j].Cost_fiscalYear.Year == int.Parse(listOfFiscalYears[i].ToString())) totalAmountPersonnel += (float)listOfCosts[j].Amount;
    }

    Chart1.Series["Housing"].Points.Add(totalAmountHousing);
    Chart1.Series["IT"].Points.Add(totalAmountIT);
    Chart1.Series["Services"].Points.Add(totalAmountServices);
    Chart1.Series["Personnel"].Points.Add(totalAmountPersonnel);
    Chart1.ChartAreas["mainArea"].AxisX.Interval = 1;

    //Add custom label to the X axis
    Chart1.ChartAreas[0].AxisX.CustomLabels.Add(new CustomLabel(i, i + 2, (listOfFiscalYears[i].ToString()), 0, LabelMarkStyle.None));

    //Reset the total cost after they have been added for the year
    totalAmountHousing = 0; totalAmountPersonnel = 0; totalAmountServices = 0; totalAmountIT = 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM