Contents Previous Next

# 11 Miscellaneous features

## 11.1 Anti-aliasing in JpGraph

From version 1.2 JpGraph supports drawing of anti-aliased lines. There are a few caveats in order to use this which is discussed in this section.

Sidebar Note that anti-alising will not be used for either horizontal, vertical or 45 degree lines since they are by their nature are sampled at adequate rate.

### 11.1.1 Enabling anti-aliased lines

Anti-aliased lines are enabled by calling the method SetAntiAliasing() in the Image class in the script where you want to use anti-aliasing.

The anti-aliasing for lines works by "smoothing" out the edges on the line by using a progressive scale of colors interpolated between the background color and the line color.

Sidenote: The algorithm used for anti-aliasing of lines is quite simple. It would be possible to achieve even better result by doing some real 2D signal processing. However, doing real time 2D signal processing on a HTTP server would be madness so I deliberately kept it simple. To achieve best visual result always use a dark line color on a light background.

An example will show that this, quite simple algorithm, gives a reasonable good result. The figures below shows a radar plot with and without anti-aliasing.

Figure 1: Spiderplot without anti-aliasing [src] | Figure 2: Spiderplot with anti-aliasing [src]

One thing you need to keep in mind when deciding to use anti-aliasing is that it could have potentially a dramatic effect on the time it takes to generate the image. Line drawing with anti-aliasing turned on is roughly 8 times slower than the normal line drawing so treat this feature wisely.

Furthermore there are a couple of "gotchas" you should be aware of when using anti-aliasing.

1. Anti-aliased lines uses up more of the available color-palette. The exact number of colors used is dependent on the line-angle, a near horizontal or near vertical line uses more colors (number of lines with different angles uses more colors). Hence it might not be possible to use anti-aliasing with color-gradient fill since the number of available colors in the palette might not be enough. A normal palette can keep around 256 colors. This means that you are advised to use a truecolor image when using anti-aliasing.
2. Anti-aliasing does not work very well together with background images since it assumes a the same solid color on each side of the line. Doing a more advanced anti-aliasing algorithm would simple take to much processing power.
3. Anti-aliased lines will ignore the line width specified. They will always have a width of roughly 1.

## 11.2 Rotating the graphs

JpGraph provide the possibility for you to rotate the generated graph an arbitrary angle. This will only affect the actual graph (axis, axis titles, labels and so on) and not fixed elements on the graph like title or footer.

Rotation is probably most used to rotate a graph 90 degrees, for example a bar graph to get the effect of horizontal bars.

Performance note: Adding a rotation transformation will make the graph generation slightly slower since each point of the graph as to go through a transformation step before being stroked on to the image. JpGraph optimises this by using a pre-calculated transformation matrice and also optimises the special case 90 degrees.

By default the center of the rotation will be the center of the plot area, which may or may not coincide with the center of the entire image.

To control the rotation you use the two methods

- Graph::image::SetAngle(), Specify rotation angle in degrees.
- Graph::image::SetCenter(), Specify center of rotation in absolute image pixels

For example
`$graph ->image->SetAngle( 45);`

There is actually a third method that you could use, adding a translation to the graph after the rotation. Since this probably a very little used method we don't discuss it further but refer the reader to the class reference instead Graph:image::SetTranslation()

When you rotate an image you should be aware of that the individual labels on the axis are not rotated. The design decision behind this is
a) Bit mapped font can't be rotated
b) Maintain readability

Please remember that you may still rotate the labels by calling the Axis::SetLabelAngle() method.

Since the anchor point for labels is by default the optimum for graph at 0 degree you might want to adjust the anchor point and alignment for the labels on the axis to get a better visual appearance on you rotated graph. This is accomplished by the method Axis::SetLabelAlign() For a detailed discussion on how to do this please see the section on horizontal bar graphs, ( Working with bar plots )

The table below shows some examples on different kinds of rotation to give you an idea of how changing the angle and rotation center may be used to generate different effects. The top left graph is the original image. The point of rotation has been marked with a red-cross in each of the images.

Figure 3: Original image [src] | Figure 4: Rotated 45 degrees around center of plot area [src]
Figure 5: Rotated 90 degrees around center of plot area [src] | Figure 6: Rotated 45 degrees around center of the image [src]
Figure 7: Rotated 90 degrees around center of the image [src] | Figure 8: Rotated -30 degrees around the lower left point of the plot area [src]

As you can see from the images above if you rotate about any other point than the center of the plot area the plot can be placed outside the image after rotation.

Since the rotation, by design, only affects the plot area it is often most effective to use when the color of the margin is the same as the background color.

## 11.3 Adjusting brightness and contrast for images and backgrounds

It is often desirable to have a background image look a little bit "washed" out so it doesn't take the concentration away from the actual graph. There are basically two ways of accomplish this

1. Prepare the image with an external images editor to adjust the level of brightnes and contrasty to a desirable level
2. Use JpGraph:s built int adjustment for contrast, brightness and color saturation.

To adjust the background image call The levels for both brightness and constrast are real numbers in the range [-1, 1] You can choose to adjust for example just the background image or you might also choose to adjust the whole image. To change the background image just use the method Graph::AdjBackgroundImage() to specify a suitable value. Let's show some example on what we can do with this. The following example have been generated by using the small utility "adjimg.php" which you can find in the "utils/" directory.

Brightness=0, contrast=0, saturation = -1 (Original image) | Brightness=0, contrast=0, saturation = -1 (Black White image)
Brightness=0.3, contrast=-0.3, saturation=0 | Brightness=0.4, contrast=-0.7, saturation=0
Brightness=0.4, contrast=-0.7, saturation=-1 | Brightness=0, contrast=0, saturation=1

## 11.4 Timing the generation of graphs

During development and optimization it can be very handy to have the actual time it took to generate the image as a footnote. The following example shows the usage of this feature

Figure 9: Timing of a graph [src]

To enable this feature you can proceed in two ways.

1. You can either set the global define BRAND_TIMIING (in jpgraph.php) to true. This will add the timing string to all graphs generated.
2. .. or you can enable it for a specific graph by setting the global variable $gJpgBrandTiming as in
   `$gJpgBrandTiming=true ;`

in the beginning of the script.

If you like you might also change the way the timing is formatted by setting the string defined by BRAND_TIMING_FORMAT (in jpgraph.php). This string represents a standard printf() format string.

Sidenote: JpGraph contains a utility class called JpgTimer which you can use yourself should you need ms timing of part of your own code. The API is really simple. The class supports multiple running timers and you start a timer simply by calling the Push() method. This will start a new timer and put it on the top of the timer stack. To stop the timer, pop it from the stack and return the timing value simply call Pop().

Contents Previous Next