Barchart creation in PLPDF


As we mentioned in our previous post, today we will talk about our plpdf3_chart package. There are some simple procedures with which you can create charts into your PDF files. Now, we will discuss two types of charts, the Column chart and the Row chart.

Initialization

Let’s start with the initialization. First you have to initialize for the basic plpdf, and after creating a new page and setting the PrintFont, you can start to create your chart. First, you have to initialize that too.

plpdf.Init;
plpdf.NewPage();
plpdf.SetPrintFont(
p_family => 'Arial',
p_style => null,
p_size => 8
);
plpdf3_chart.Init(
p_type => plpdf3_chart.c_bar
);

In the Init procedure you have to set the type of the chart. In this article we will talk about ’c_bar’ only, and in the next one we will discuss the ’c_line’ (line chart) and the ’c_pie’ (pie chart). After you initialized the chart, you have to decide whether you want to do a column or a row chart.

plpdf3_chart.setBarProperties(
p_direction => plpdf3_chart.c_column
--p_direction => plpdf3_chart.c_row
);

After setting the basic properties of the chart, you can start adding Categories. All of them.
plpdf3_chart.AddCategory(
p_textValue => 'Category 1'
);
plpdf3_chart.AddCategory(
p_textValue => 'Category 2'
);
plpdf3_chart.AddCategory(
p_textValue => 'Category 3'
);

Data

And when you are done with this, you have to fill the chart with data, which are contained in a DataSet. For each group of values, you have to create a DataSet with a name and color, then fill it with values, but not more than the number of categories you have. You also have to index the data you want to add by the number of the category you want to add it to.

plpdf3_chart.addDataSet(
p_name => 'Series 1', -- Name of the Series
p_color => plpdf_const.Red -- You can select the color of the columns(/rows)
);
--plpdf3_chart.setDataSetLabel( -- Data Label position on the bar
-- p_position => plpdf3_chart.c_outEnd);
plpdf3_chart.AddValue(
p_value => '6', -- Value of the data
p_idx => 1 -- Index in the Series
);
plpdf3_chart.AddValue('2,5',2);
plpdf3_chart.AddValue('3,5',3);
plpdf3_chart.addDataSet('Series 2', plpdf_const.Green);
-- plpdf3_chart.setDataSetLabel(plpdf3_chart.c_b);
plpdf3_chart.AddValue('2,4',1);
plpdf3_chart.AddValue('4,4',2);
plpdf3_chart.AddValue('1,8',3);
plpdf3_chart.addDataSet('Series 3', plpdf_Const.purple);
-- plpdf3_chart.setDataSetLabel(plpdf3_chart.c_l);
plpdf3_chart.AddValue('2',1);
plpdf3_chart.AddValue('2',2);
plpdf3_chart.AddValue('3',3);
plpdf3_chart.addDataSet('Random Data', plpdf_Const.Medium_spring_green);
-- plpdf3_chart.setDataSetLabel(plpdf3_chart.c_r);
plpdf3_chart.AddValue('2,8',1);
plpdf3_chart.AddValue('10,1',2);
plpdf3_chart.AddValue('4,1',3);

You can modify the position of the Data value labels on the chart by each or you can set it for all dataset. For setting one by one, see the commented sections of the previous code. To set all at once, you can use this:
plpdf3_chart.setEntireChartDataLabels(p_position => 't');
Now we are done with data, and we have to costumize our two Axis.

Horizontal axis (X)

- Set the title of the axis and it’s properties

plpdf3_chart.setXAxisTitle(
p_title => 'Horizontal Axis',
p_angle => 270,
p_italic => true,
p_font => 'Helvetica',
p_size => 10,
p_text_color => plpdf_const.Dark_magenta
);
- Set the line type and color, and if you want to show the axis and it’s labels.
plpdf3_chart.setXAxisLine(
p_lineType => plpdf3_chart.c_sng,
p_color => plpdf_const.Gray,
p_showAxis => true
);
- Set the properties of the Labels
plpdf3_chart.setXLabels( -- Sets the prooperties of the Labels on the Horizontal Axis
p_align => plpdf3_chart.c_b, -- Alignment
p_offset => 0, -- Distance from the axis
p_fontname => plpdf_const.Times_Roman,
p_fontsize => 9,
p_color => plpdf_const.Purple
);
- Set the vertical Major gridlines if you want them to be shown
plpdf3_chart.setXMajorGridLine(
p_lineType => plpdf3_chart.c_sng,
p_color => plpdf_const.Black
);
- Set the TickMarks and their position
plpdf3_chart.setXTickMarks(
p_major => plpdf3_chart.c_out,
p_minor => plpdf3_chart.c_in,
p_labelPosition => plpdf3_chart.c_low
);

Vertical axis (Y)

- Set the title of the axis and it’s properties
plpdf3_chart.setYAxisTitle(
p_title => 'Vertical Axis',
p_angle => 270,
p_italic => true,
p_font => 'Helvetica',
p_size => 10,
p_text_color => plpdf_const.Dark_magenta
);
- Set the line type and color, and if you want to show the axis and it’s labels.
plpdf3_chart.setYAxisLine(
p_lineType => plpdf3_chart.c_sng,
p_color => plpdf_const.Blue,
p_showAxis => true
);
- Set the properties of the Labels
plpdf3_chart.setYLabels(
p_align => plpdf3_chart.c_b,
p_offset => 0, -- Distance from the axis
p_fontname => plpdf_const.Times_Roman,
p_italic => true,
p_bold => true,
p_fontsize => 10,
p_underline => plpdf3.c_none,
p_color => plpdf_const.Redv );
- Set the Major and Minor gridlines if you want them to be shown
plpdf3_chart.setYMajorGridLine( -- Sets the major gridlines on the Vertical Axis
p_lineType => plpdf3_chart.c_sng,
p_color => plpdf_const.Black
);
plpdf3_chart.setYMinorGridLine( -- Sets the minor gridlines on the Vertical Axis
p_lineType => plpdf3_chart.c_sng,
p_color => plpdf_const.Gray
);
- Then set the TickMarks of the axis
plpdf3_chart.setYTickMarks(
plpdf3_chart.c_out,
plpdf3_chart.c_in,
plpdf3_chart.c_nextTo
);
The axis scaling and it’s values are calculated automatically, but you can set your own values. For example:
plpdf3_chart.setYAxisScaling(
p_min => 0, -- Minimum value
p_max => 12.0, -- Maximum value
p_orientation => plpdf3_chart.c_minMax
);
plpdf3_chart.setYUnitShift(
p_major => 2.0,
p_minor => 0.5
);
As the last property, you can set up a legend for your series’ with this procedure: (There are four positions for it, and these are ‘c_t’ (top), ‘c_b’ (bottom), ‘c_l’ (left) and ‘c_r’ (right)
plpdf3_chart.AddLegend( -- Creating a Legend to the chart
p_position => plpdf3_chart.c_t,-- Position of the Legend
p_fontsize => 14,
p_color => plpdf_const.Red,
p_bold => true
);
The only thing now you have to do is to draw the chart into your PDF document:
plpdf3_chart.DrawChart( -- Sets the dimensions of the chart
p_x => 20, -- X Position (Top left corner)
p_y => 20, -- Y Position (Top left corner)
p_width => 160, -- Width
p_Height => 90 -- Height
);
You insert your PDF into the table you want (in this case we will use our STORE_BLOB),
plpdf.SendDoc(
p_blob => l_blob
);
insert into STORE_BLOB (blob_file, created_date, filename) VALUES (l_blob,sysdate, 'column_chart.pdf');
commit;
With one simple step, you can easily transform it into a Row chart: Let’s change the p_direction parameter of the plpdf3_chart.setBarProperties procedure to plpdf3_chart.c_row. It is simple as that:
But let’s do some modifitacion on it.
- Try to give it a Title:
plpdf3_chart.setChartTitle(
p_title => 'Chart Title',
p_bold => true,
p_italic => true,
p_text_color => plpdf_const.Blue
);
- Now change the position of the legend to the bottom
plpdf3_chart.AddLegend(
p_position => plpdf3_chart.c_b,
p_fontsize => 14,
p_color => plpdf_const.Red,
p_bold => true
);
- And we don’t want to see the Y axis and it’s labels:
plpdf3_chart.setYAxisLine(
p_lineType => plpdf3_chart.c_sng,
p_color => plpdf_const.Blue,
p_showAxis => false
);