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

    Horizontal tetragons are used to display information in Bar Charts. There are three different types:

    Normal
    Normal Bar chartS create tetragons based on the data. They use DateTimeYAxis or IndexedYAxis as the YAxis based on the series and NumericXAxis as the XAxis.
                             Figure 1 - Normal Bar 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);
            
        // Get the default plot area from the chart
        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);
        
    AutoGradient myAutogradient1 = new AutoGradient(180.0f, CmykColor.getRed(), CmykColor.getIndianRed())
    AutoGradient myAutogradient2 = new AutoGradient(180.0f, CmykColor.getGreen(), CmykColor.getYellowGreen())
    AutoGradient myAutogradient3 = new AutoGradient(180.0f, CmykColor.getBlue(), CmykColor.getLightBlue()) // Create a indexed bar series and add values to it IndexedBarSeries barSeries1 = new IndexedBarSeries("Website A", myAutogradient1); barSeries1.getValues().add(new float[] { 5, 7, 9, 6 }); IndexedBarSeries barSeries2 = new IndexedBarSeries("Website B", myAutogradient2); barSeries2.getValues().add(new float[] { 4, 2, 5, 8 }); IndexedBarSeries barSeries3 = new IndexedBarSeries("Website C", myAutogradient3); barSeries3.getValues().add(new float[] { 2, 4, 6, 9 }); // Add indexed bar series to the plot area plotArea.getSeries().add(barSeries1); plotArea.getSeries().add(barSeries2); plotArea.getSeries().add(barSeries3); // Create a title and add it to the yaxis Title lTitle = new Title("Visitors (in millions)"); barSeries1.getXAxis().getTitles().add(lTitle); // Adding AxisLabels to the XAxis barSeries1.getYAxis().getLabels().add(new IndexedYAxisLabel("Q1", 0)); barSeries1.getYAxis().getLabels().add(new IndexedYAxisLabel("Q2", 1)); barSeries1.getYAxis().getLabels().add(new IndexedYAxisLabel("Q3", 2)); barSeries1.getYAxis().getLabels().add(new IndexedYAxisLabel("Q4", 3)); // Add the chart to the page page.getElements().add(chart); // Save the PDF document.draw("[PhysicalPath]/MyDocument.pdf");
    Stacked
    Stacked charts display related data, one on top of the other. They use DateTimeYAxis or IndexedYAxis as the YAxis based on the series and NumericXAxis as the XAxis.
                           Figure 2 - Stacked Bar 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);
         AutoGradient myAutogradient1 = new AutoGradient(180.0f, CmykColor.getRed(), CmykColor.getIndianRed()) 
    AutoGradient myAutogradient2 = new AutoGradient(180.0f, CmykColor.getGreen(), CmykColor.getYellowGreen())
    AutoGradient myAutogradient3 = new AutoGradient(180.0f, CmykColor.getBlue(), CmykColor.getLightBlue()) // Create a indexed stacked bar series elements and add values to it IndexedStackedBarSeriesElement seriesElement1 = new IndexedStackedBarSeriesElement("Website A", myAutogradient1); seriesElement1.getValues().add(new float[] { 5, 7, 9, 6 }); IndexedStackedBarSeriesElement seriesElement2 = new IndexedStackedBarSeriesElement("Website B", myAutogradient2); seriesElement2.getValues().add(new float[] { 4, 2, 5, 8 }); IndexedStackedBarSeriesElement seriesElement3 = new IndexedStackedBarSeriesElement("Website C", myAutogradient3); seriesElement3.getValues().add(new float[] { 2, 4, 6, 9 }); // Create a Indexed Stacked Bar Series IndexedStackedBarSeries barSeries = new IndexedStackedBarSeries(); // Add indexed stacked bar series elements to the Indexed Stacked Bar Series barSeries.add(seriesElement1); barSeries.add(seriesElement2); barSeries.add(seriesElement3); //Add series to the plot area plotArea.getSeries().add(barSeries); // Create a title and add it to the xaxis Title lTitle = new Title("Visitors (in millions)"); barSeries.getXAxis().getTitles().add(lTitle); //Adding AxisLabels to the yaxis barSeries.getYAxis().getLabels().add(new IndexedYAxisLabel("Q1", 0)); barSeries.getYAxis().getLabels().add(new IndexedYAxisLabel("Q2", 1)); barSeries.getYAxis().getLabels().add(new IndexedYAxisLabel("Q3", 2)); barSeries.getYAxis().getLabels().add(new IndexedYAxisLabel("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 DateTimeYAxis or IndexedYAxis as the YAxis based on the series and PercentageXAxis as the XAxis.
                          Figure 3 - Stacked 100% Bar 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);
         AutoGradient myAutogradient1 = new AutoGradient(180.0f, CmykColor.getRed(), CmykColor.getIndianRed()) 
    AutoGradient myAutogradient2 = new AutoGradient(180.0f, CmykColor.getGreen(), CmykColor.getYellowGreen())
    AutoGradient myAutogradient3 = new AutoGradient(180.0f, CmykColor.getBlue(), CmykColor.getLightBlue()) // Create a indexed 100% bar series elements and add values to it Indexed100PercentStackedBarSeriesElement seriesElement1 = new Indexed100PercentStackedBarSeriesElement("Website A", myAutogradient1); seriesElement1.getValues().add(new float[] { 5, 7, 9, 6 }); Indexed100PercentStackedBarSeriesElement seriesElement2 = new Indexed100PercentStackedBarSeriesElement("Website B", myAutogradient2); seriesElement2.getValues().add(new float[] { 4, 2, 5, 8 }); Indexed100PercentStackedBarSeriesElement seriesElement3 = new Indexed100PercentStackedBarSeriesElement("Website C", myAutogradient3); seriesElement3.getValues().add(new float[] { 2, 4, 6, 9 }); // Create a Indexed 100% Stacked Bar Series Indexed100PercentStackedBarSeries barSeries = new Indexed100PercentStackedBarSeries(); // Add SeriesElements to the Indexed 100% Stacked Bar Series barSeries.add(seriesElement1); barSeries.add(seriesElement2); barSeries.add(seriesElement3); // Add series to the plot area plotArea.getSeries().add(barSeries); // Create a title and add it to the xaxis Title lTitle = new Title("Visitors (in millions)"); barSeries.getXAxis().getTitles().add(lTitle); //Adding AxisLabels to the yaxis barSeries.getYAxis().getLabels().add(new IndexedYAxisLabel("Q1", 0)); barSeries.getYAxis().getLabels().add(new IndexedYAxisLabel("Q2", 1)); barSeries.getYAxis().getLabels().add(new IndexedYAxisLabel("Q3", 2)); barSeries.getYAxis().getLabels().add(new IndexedYAxisLabel("Q4", 3)); //Add the chart to the page page.getElements().add(chart); // Save the PDF document.draw("[PhysicalPath]/MyDocument.pdf");