itextsharp表图表下面的图例

时间:2017-01-10 16:25:30

标签: c# pdf itext

有人可以解释我如何使用itextsharp库创建这样的图表。

legend table is attached to graph, with years for each bar

正如您在此处所看到的,此图表附有表格,其底部带有图例。每个酒吧都附有一年。我想创造类似的东西。

legend table is not attached to graph: Chart I created

第二张照片。这就是我到目前为止所拥有的。我没有附加到每个栏的年份,我的传说不在顶部图表中的每个栏下面。如果有人可以指导我如何在顶部创建相同的图形,我将不胜感激。提前谢谢!

I added each year to each bar: I have this now

如何将这些标签水平显示,并将这些图例放在表格中,如图1中的那样。

        // Chart Centers By Year
        var chartCentersByYear = new Chart
        {
            Width = 1000,
            Height = 450,
            RenderType = RenderType.ImageTag,
            AntiAliasing = AntiAliasingStyles.Graphics,
            TextAntiAliasingQuality = TextAntiAliasingQuality.High


        };

        chartCentersByYear.Titles.Add("Centers By Year");
        chartCentersByYear.Titles[0].Font = new Font("Arial", 16f);
        chartCentersByYear.Titles[0].Alignment = System.Drawing.ContentAlignment.TopLeft;

        chartCentersByYear.ChartAreas.Add("");
        chartCentersByYear.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
        chartCentersByYear.ChartAreas[0].AxisY.MajorGrid.Enabled = false;


        chartCentersByYear.Series.Add("Count");
        chartCentersByYear.Series.Add("Cases");
        chartCentersByYear.Series[0].ChartType = SeriesChartType.Column; //Pie
        chartCentersByYear.Series[1].ChartType = SeriesChartType.StepLine; //StepLine
        chartCentersByYear.Series[1].BorderDashStyle = ChartDashStyle.DashDot;
        chartCentersByYear.Series[1].BorderWidth = 3;

        chartCentersByYear.Series[0].Color = Color.Brown;

        chartCentersByYear.Legends.Add("1");
        chartCentersByYear.Legends.Add("2");

        chartCentersByYear.Legends[0].HeaderSeparator = LegendSeparatorStyle.Line;
        chartCentersByYear.Legends[0].HeaderSeparatorColor = Color.Black;
        chartCentersByYear.Legends[0].ItemColumnSeparator = LegendSeparatorStyle.Line;
        chartCentersByYear.Legends[0].ItemColumnSeparatorColor = Color.Black;

        chartCentersByYear.Legends[1].HeaderSeparator = LegendSeparatorStyle.Line;
        chartCentersByYear.Legends[1].HeaderSeparatorColor = Color.Black;
        chartCentersByYear.Legends[1].ItemColumnSeparator = LegendSeparatorStyle.Line;
        chartCentersByYear.Legends[1].ItemColumnSeparatorColor = Color.Black;

        //For the Legend  
        LegendCellColumn firstColumn = new LegendCellColumn();
        firstColumn.ColumnType = LegendCellColumnType.SeriesSymbol;
        firstColumn.HeaderBackColor = Color.WhiteSmoke;
        chartCentersByYear.Legends[0].CellColumns.Add(firstColumn);
        chartCentersByYear.Legends[1].CellColumns.Add(firstColumn);

        LegendCellColumn secondColumn = new LegendCellColumn();
        secondColumn.ColumnType = LegendCellColumnType.Text;
        secondColumn.Text = "#LEGENDTEXT";
        secondColumn.HeaderBackColor = Color.WhiteSmoke;

        LegendItem newItemCount = new LegendItem();
        newItemCount.Cells.Add(LegendCellType.Text, "Count", System.Drawing.ContentAlignment.MiddleCenter);
        newItemCount.BorderWidth = 1;
        newItemCount.BorderDashStyle = ChartDashStyle.Solid;

        LegendItem newItemCases = new LegendItem();
        newItemCases.Cells.Add(LegendCellType.Text, "Cases", System.Drawing.ContentAlignment.MiddleCenter);
        newItemCases.BorderWidth = 1;
        newItemCases.BorderDashStyle = ChartDashStyle.Solid;

        // Getting data from a stored procedure  
        var totalCentersByYearResult = new Repository().GetTotalCentersByYear();

        foreach (IGD_spInternationalReportCenterWithTots1_Result item in totalCentersByYearResult)
        {
            // For Series
            chartCentersByYear.Series[0].Points.AddXY(item.YearEcmo, item.Count);
            chartCentersByYear.Series[1].Points.AddY(item.Cases);

            // For Legend
            newItemCount.Cells.Add(LegendCellType.Text, item.Count.ToString(), System.Drawing.ContentAlignment.MiddleCenter);
            newItemCases.Cells.Add(LegendCellType.Text, item.Cases.ToString(), System.Drawing.ContentAlignment.MiddleCenter);

        }

        chartCentersByYear.Legends[0].CustomItems.Add(newItemCount);
        chartCentersByYear.Legends[0].CustomItems.Add(newItemCases);

        chartCentersByYear.Legends[0].Docking = Docking.Bottom;
        chartCentersByYear.Legends[1].Docking = Docking.Bottom; //Top

        chartCentersByYear.Series[0].YAxisType = AxisType.Primary;
        chartCentersByYear.Series[1].YAxisType = AxisType.Secondary;

        //For two coordinate systems
        chartCentersByYear.ChartAreas[0].AxisY2.LineColor = Color.Transparent;
        chartCentersByYear.ChartAreas[0].AxisY2.MajorGrid.Enabled = false;
        chartCentersByYear.ChartAreas[0].AxisY2.Enabled = AxisEnabled.True;
        chartCentersByYear.ChartAreas[0].AxisY2.IsStartedFromZero = chartCentersByYear.ChartAreas[0].AxisY.IsStartedFromZero;            

        using (var chartimage = new MemoryStream())
        {
            chartCentersByYear.SaveImage(chartimage, ChartImageFormat.Png);
            Byte[] newChart = chartimage.GetBuffer(); //return chartimage.GetBuffer();

            var image = Image.GetInstance(newChart); //Image.GetInstance(Chart());
            image.ScalePercent(50f);
            image.SetAbsolutePosition(document.LeftMargin + 40, document.BottomMargin + 100);

            document.Add(image);
        }

0 个答案:

没有答案