Page tree
Skip to end of metadata
Go to start of metadata

将“ 图表”字段添加到报表中时,第一步是将图表绑定到数据源。

假设您的报表有两个数据源,“Employees”和“Products”。您要创建两个图表,一个显示“Employees”数据源的全名和年龄,另一个显示“Products”数据源的CategoryName和UnitsInStock字段的总计。

实现此场景的步骤如下:

  1. 在报表中创建两个数据源(“Employees”,“Products”)。
  2. 在员工数据源中定义两个计算的字段(“FullName”,“Age”)。
  3. 在“产品”数据源中定义两个计算字段(“ CategoryName”,“ Sum(UnitsInStock)”)。
  4. 创建两个图表字段,分别绑定到“雇员”和“产品”数据源。

    CS
     private C1FlexReport CreateChartSampleReport()
            {
               
               var report = new C1FlexReport { ReportName = "ChartSample" };
                // add data source "Employees"
                var dsEmployees = new DataSource
                {
                    Name = "Employees",
                    ConnectionString =
                        @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\Reports\C1Nwind.mdb;Persist Security Info=False",
                    RecordSource = "Select * from Employees",
                };
                report.DataSources.Add(dsEmployees);
                // add calculated field "FullName".
                var calcFullName = new CalculatedField("FullName", typeof(string), "=LastName & \" \" & FirstName");
                dsEmployees.CalculatedFields.Add(calcFullName);
               
                // add calculated field "Age".
                var calcAge = new CalculatedField("Age", typeof(int), "=Year(Now())-Year(BirthDate) + 1");
                dsEmployees.CalculatedFields.Add(calcAge);
                // add data source "Products"
                var dsProducts = new DataSource
                {
                    Name = "Products",
                    ConnectionString =
                        @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\Reports\C1Nwind.mdb;Persist Security Info=False",
                    RecordSource =
                        "Select Products.CategoryID as CategoryID, Categories.CategoryName as CategoryName, Products.UnitsInStock as UnitsInStock from Products inner join Categories on Products.CategoryID = Categories.CategoryID"
                };
                report.DataSources.Add(dsProducts);
                report.Sections.Header.Visible = true;
                // add ChartField using Employees data source.
                var sectionEmployees = report.Sections.Header.SubSections.Add();
                sectionEmployees.Name = "ChartWithEmployees";
                sectionEmployees.Height = 5200;
                sectionEmployees.Visible = true;
                sectionEmployees.Fields.Add(CreateChartForEmployees());
                // add ChartField using Products data source.
                var sectionProducts = report.Sections.Header.SubSections.Add();
                sectionProducts.Name = "ChartWithProducts";
                sectionProducts.Height = 5200;
                sectionProducts.Visible = true;
                sectionProducts.Fields.Add(CreateChartForProducts());
               
                return report;          
            }
         
            private ChartField CreateChartForEmployees()
            {
                var chart = CreateChartField("Chart1", "Employees");
                chart.Header.Text = "Employees Age";
                chart.ChartArea2D.Inverted = true;
                chart.ChartArea2D.AxisX.OnTop = true;
                var group = chart.ChartGroups2D.Group0;
                group.ChartType = Chart2DType.Bar;
                var data = group.ChartData;
                data.IsForEachRecord = true; // show value of each record in data source
                data.CategoryGroups.AddNewGroup("=FullName"); // group by FullName
                var seriesTemplate = data.SeriesValues.AddNewSeries();
                seriesTemplate.DataValues.AddNewValue("=Age"); // show Age in AxisY
                return chart;
            }
            private ChartField CreateChartForProducts()
            {
                var chart = CreateChartField("Chart2", "Products");
                chart.Header.Text = "Sum of UnitsInStock by Category";
                chart.ChartArea2D.Inverted = true;
                chart.ChartArea2D.AxisX.OnTop = true;
                var group = chart.ChartGroups2D.Group0;
                group.ChartType = Chart2DType.Bar;
                var data = group.ChartData;
                var categoryGroup = data.CategoryGroups.AddNewGroup("=CategoryID"); // group by each CategoryID
                categoryGroup.LabelExpression = "=CategoryName"; // show the CategoryName in AxisX.
                var seriesTemplate = data.SeriesValues.AddNewSeries();
                seriesTemplate.DataValues.AddNewValue("=Sum(UnitsInStock)"); // show sum of UnitsInStock in AxisY.
                return chart;
            }
            private ChartField CreateChartField(string name, string datasource)
            {
                var chart = new ChartField
                {
                    Name = name,
                    Width = 7500,
                    Height = 5000,
                    Top = 100,
                    Left = 100,
                    DataSource = datasource,
                };
                chart.Border.Color = Color.Black;
                chart.Border.Width = 15;
                chart.Border.Style = DashStyle.Solid;
                chart.Border.CornerRadius = new CornerRadius(200d);
                chart.ChartArea2D.AxisY.AutoMin = false;
                return chart;         
            }
    private void button1_Click(object sender, EventArgs e)
            {
                CreateChartSampleReport();
                c1FlexViewer1.DocumentSource = report;
            }


    具有多个数据源的图表

 

  • No labels