Skip to content

3.10 Draw Pie Chart

OgreTransporter edited this page Mar 2, 2020 · 1 revision

The DrawChart method is an example of defining a chart and drawing it to the PDF document.

// Draw chart
private void DrawChart()
{
    // save graphics state
    Contents.SaveGraphicsState();

    // create chart
    Chart PieChart = PdfChart.CreateChart(Document, 1.8, 1.5, 300.0);

    // create PdfChart object with Chart object
    PdfChart PiePdfChart = new PdfChart(Document, PieChart);
    PiePdfChart.SaveAs = SaveImageAs.IndexedImage;

    // make sure we have good quality image
    PieChart.AntiAliasing = AntiAliasingStyles.None; //.All;

    // set colors
    PieChart.BackColor = Color.FromArgb(220, 220, 255);
    PieChart.Palette = ChartColorPalette.BrightPastel;

    // default font
    Font DefaultFont = PiePdfChart.CreateFont("Verdana", FontStyle.Regular, 0.05, FontSizeUnit.UserUnit);
    Font TitleFont = PiePdfChart.CreateFont("Verdana", FontStyle.Bold, 0.07, FontSizeUnit.UserUnit);

    // title (font size is 0.25 inches)
    Title Title1 = new Title("Pie Chart Example", Docking.Top, TitleFont, Color.Purple);
    PieChart.Titles.Add(Title1);

    // legend
    Legend Legend1 = new Legend();
    PieChart.Legends.Add(Legend1);
    Legend1.BackColor = Color.FromArgb(230, 230, 255);
    Legend1.Docking = Docking.Bottom;
    Legend1.Font = DefaultFont;

    // chart area
    ChartArea ChartArea1 = new ChartArea();
    PieChart.ChartAreas.Add(ChartArea1);

    // chart area background color
    ChartArea1.BackColor = Color.FromArgb(255, 200, 255);

    // series 1
    Series Series1 = new Series();
    PieChart.Series.Add(Series1);
    Series1.ChartType = SeriesChartType.Pie;
    Series1.Font = DefaultFont;
    Series1.IsValueShownAsLabel = true;
    Series1.LabelFormat = "{0} %";

    // series values
    Series1.Points.Add(22.0);
    Series1.Points[0].LegendText = "Apple";
    Series1.Points.Add(27.0);
    Series1.Points[1].LegendText = "Banana";
    Series1.Points.Add(33.0);
    Series1.Points[2].LegendText = "Orange";
    Series1.Points.Add(18.0);
    Series1.Points[3].LegendText = "Grape";

    Contents.DrawChart(PiePdfChart, 5.6, 5.0);

    // restore graphics state
    Contents.RestoreGraphicsState();
    return;
}
Clone this wiki locally