Line chart is the basic chart type, which is designed to show series of data points, and display particular trends within a period of time. It can give users a directly visual impack against the plain data list.

In information portals and business, users want to be able to show their data to chart in PDF document. With the help of XsPDF SDK, you can easily add and insert line chart shapes to PDF file.

In this article, we will explain how to draw and render line chart control to PDF using C#.


void AddLineChartToPDF()
{
    // Create a new PDF document.
    PdfDocument document = new PdfDocument();

    // Create a page.
    PdfPage page = document.AddPage();  

    // Generate a line chart
    Chart chart = LineChart();

    // Create a chart frame, set chart location and size
    ChartFrame chartFrame = new ChartFrame();
    chartFrame.Location = new XPoint(30, 30);
    chartFrame.Size = new XSize(500, 600);
    chartFrame.Add(chart);

    // Draw chart into page
    XGraphics g = XGraphics.FromPdfPage(page);
    chartFrame.Draw(g);

    // Save and show the document           
    document.Save("Chart.pdf");
    Process.Start("Chart.pdf");
}

Chart LineChart()
{
    // Set chart type to line
    Chart chart = new Chart(ChartType.Line);

    // Create first series with name and data
    Series series = chart.SeriesCollection.AddSeries();
    series.Name = "Series 1";
    series.Add(new double[] { 1, 12, -6, 15, 11 });

    // Create second series with name and data
    series = chart.SeriesCollection.AddSeries();
    series.Name = "Series 2";
    series.Add(new double[] { 22, 4, 12, 8, 12 });

    // Create third series with name and data
    series = chart.SeriesCollection.AddSeries();
    series.Name = "Series 3";
    series.Add(new double[] { 12, 17, -3, 18, 1 });

    // Set X axes
    chart.XAxis.MajorTickMark = TickMarkType.Outside;
    chart.XAxis.Title.Caption = "X-Axis";
    chart.XAxis.HasMajorGridlines = true;

    // Set Y axes
    chart.YAxis.MajorTickMark = TickMarkType.Outside;
    chart.YAxis.Title.Caption = "Y-Axis";
    chart.YAxis.HasMajorGridlines = true;

    // Set plot area (chart diagram)
    chart.PlotArea.LineFormat.Color = XColors.DarkGray;
    chart.PlotArea.LineFormat.Width = 1;
    chart.PlotArea.LineFormat.Visible = true;
                       
    // Set legend
    chart.Legend.Docking = DockingType.Bottom;
    chart.Legend.LineFormat.Visible = true;

    XSeries xseries = chart.XValues.AddXSeries();
    xseries.Add("A", "B", "C", "D", "E", "F");

    return chart;
}

More PDF tutorial