ceTe Software Help Library for Java August - 2020
DynamicPDF Generator for Java / Programming with Generator for Java / Charts / Chart Types / Line Chart
In This Topic
    Line Chart
    In This Topic

    Lines are used to display information in Line Charts. There are three different types.

    Normal
    Normal Line charts create lines based on the data. They use DateTimeXAxis or IndexedXAxis as the XAxis based on the series and NumericYAxis as the YAxis.
                                     Figure 1 - Normal Line Chart
    [Java]
         // Create a PDF Document
         Document document = new Document();
         // Create a Page and add it to the document
         Page page = new Page();
         document.getPages().add(page);
            
         // Create a chart
         Chart chart = new Chart(0, 0, 400, 230);
         // Create a plot area
         PlotArea plotArea = chart.getPrimaryPlotArea();
            
         // Create header titles and add it to the chart
         Title title1 = new Title("Website Visitors");
         Title title2 = new Title("Year - 2007");
         chart.getHeaderTitles().add(title1);
         chart.getHeaderTitles().add(title2);
            
         // Create a indexed line series and add values to it
         IndexedLineSeries lineSeries1 = new IndexedLineSeries("Website A");
         lineSeries1.getValues().add(new float[] { 5, 7, 9, 6 });
         IndexedLineSeries lineSeries2 = new IndexedLineSeries("Website B");
         lineSeries2.getValues().add(new float[] { 4, 2, 5, 8 });
         IndexedLineSeries lineSeries3 = new IndexedLineSeries("Website C");
         lineSeries3.getValues().add(new float[] { 2, 4, 6, 9 });
            
         // Add indexed line series to the plot area
         plotArea.getSeries().add(lineSeries1);
         plotArea.getSeries().add(lineSeries2);
         plotArea.getSeries().add(lineSeries3);
             
         // Create a title and add it to the yaxis
         Title lTitle = new Title("Visitors (in millions)");
         lineSeries1.getYAxis().getTitles().add(lTitle);
             
         //Adding AxisLabels to the XAxis
         lineSeries1.getXAxis().getLabels().add(new IndexedXAxisLabel("Q1", 0));
         lineSeries1.getXAxis().getLabels().add(new IndexedXAxisLabel("Q2", 1));
         lineSeries1.getXAxis().getLabels().add(new IndexedXAxisLabel("Q3", 2));
         lineSeries1.getXAxis().getLabels().add(new IndexedXAxisLabel("Q4", 3));
            
         // Add the chart to the page
         page.getElements().add(chart);
         // Save the PDF
         document.draw("[PhysicalPath]/MyDocument.pdf");
    
    Stacked
    Stacked charts show related data, one on top of the other. They use DateTimeXAxis or IndexedXAxis as the XAxis based on the series and NumericYAxis as the YAxis.
                                  Figure 2 - Stacked Line Chart
    [Java]
       // Create a PDF Document
       Document document = new Document();
       // Create a Page and add it to the document
       Page page = new Page();
       document.getPages().add(page);
            
       // Create a chart
       Chart chart = new Chart(0, 0, 400, 230);
       // Create a plot area
       PlotArea plotArea = chart.getPrimaryPlotArea();
            
       // Create header titles and add it to the chart
       Title title1 = new Title("Website Visitors");
       Title title2 = new Title("Year - 2007");
       chart.getHeaderTitles().add(title1);
       chart.getHeaderTitles().add(title2);
        
       // Create a indexed stacked line series elements and add values to it
       IndexedStackedLineSeriesElement seriesElement1 = new IndexedStackedLineSeriesElement("Website A");
       seriesElement1.getValues().add(new float[] { 5, 7, 9, 6 });
       IndexedStackedLineSeriesElement seriesElement2 = new IndexedStackedLineSeriesElement("Website B");
       seriesElement2.getValues().add(new float[] { 4, 2, 5, 8 });
       IndexedStackedLineSeriesElement seriesElement3 = new IndexedStackedLineSeriesElement("Website C");
       seriesElement3.getValues().add(new float[] { 2, 4, 6, 9 });
            
       // Create a Indexed Stacked Line Series
       IndexedStackedLineSeries lineSeries = new IndexedStackedLineSeries();
       // Add indexed stacked line series elements to the Indexed Stacked Line Series
       lineSeries.add(seriesElement1);
       lineSeries.add(seriesElement2);
       lineSeries.add(seriesElement3);
            
       // Add the series to the plot area
       plotArea.getSeries().add(lineSeries);
            
       // Create a title and add it to the yaxis
       Title lTitle = new Title("Visitors (in millions)");
       lineSeries.getYAxis().getTitles().add(lTitle);
             
       //Adding AxisLabels to the XAxis
       lineSeries.getXAxis().getLabels().add(new IndexedXAxisLabel("Q1", 0));
       lineSeries.getXAxis().getLabels().add(new IndexedXAxisLabel("Q2", 1));
       lineSeries.getXAxis().getLabels().add(new IndexedXAxisLabel("Q3", 2));
       lineSeries.getXAxis().getLabels().add(new IndexedXAxisLabel("Q4", 3));
            
       // Add the chart to the page
       page.getElements().add(chart);
       // Save the PDF
       document.draw("[PhysicalPath]/MyDocument.pdf");
    
    100% Stacked
    100% stacked charts are similar to stacked charts, but here the data is expressed as a percentage. They use DateTimeXAxis or IndexedXAxis as the XAxis based on the series and PercentageYAxis as the YAxis. 
                                  Figure 3 - Stacked 100% Line Chart
    [Java]
         // Create a PDF Document
         Document document = new Document();
         // Create a Page and add it to the document
         Page page = new Page();
         document.getPages().add(page);
            
         // Create a chart
         Chart chart = new Chart(0, 0, 400, 230);
         // Create a plot area
         PlotArea plotArea = chart.getPrimaryPlotArea();
            
         // Create header titles and add it to the chart
         Title title1 = new Title("Website Visitors");
         Title title2 = new Title("Year - 2007");
         chart.getHeaderTitles().add(title1);
         chart.getHeaderTitles().add(title2);
            
         // Create indexed 100% line series elements and add values to it
         Indexed100PercentStackedLineSeriesElement seriesElement1 = new Indexed100PercentStackedLineSeriesElement("Website A");
         seriesElement1.getValues().add(new float[] { 5, 7, 9, 6 });
         Indexed100PercentStackedLineSeriesElement seriesElement2 = new Indexed100PercentStackedLineSeriesElement("Website B");
         seriesElement2.getValues().add(new float[] { 4, 2, 5, 8 });
         Indexed100PercentStackedLineSeriesElement seriesElement3 = new Indexed100PercentStackedLineSeriesElement("Website C");
         seriesElement3.getValues().add(new float[] { 2, 4, 6, 9 });
            
         // Create a Indexed 100% Stacked Line Series
         Indexed100PercentStackedLineSeries lineSeries = new Indexed100PercentStackedLineSeries();
         // Add indexed 100% line series elements to the Indexed 100% Stacked Line Series
         lineSeries.add(seriesElement1);
         lineSeries.add(seriesElement2);
         lineSeries.add(seriesElement3);
         // Add the series to the plot area
         plotArea.getSeries().add(lineSeries);
            
         // Create a title and add it to the yaxis
         Title lTitle = new Title("Visitors (in millions)");
         lineSeries.getYAxis().getTitles().add(lTitle);
            
         //Adding AxisLabels to the XAxis
         lineSeries.getXAxis().getLabels().add(new IndexedXAxisLabel("Q1", 0));
         lineSeries.getXAxis().getLabels().add(new IndexedXAxisLabel("Q2", 1));
         lineSeries.getXAxis().getLabels().add(new IndexedXAxisLabel("Q3", 2));
         lineSeries.getXAxis().getLabels().add(new IndexedXAxisLabel("Q4", 3));
            
         // Add the chart to the page
         page.getElements().add(chart);
         // Save the PDF
         document.draw("[PhysicalPath]/MyDocument.pdf");
    
    See Also