Handling JpGraph exceptions

JpGraph is one of the most powerful charting libraries for popular PHP language. This outstanding library contains massive amount of useful features for plotting and charting plain numbers thus making complex data visually more understandable. Implementing JpGraph in your PHP project is extremely easy and straightforward. One can get started with only a couple of lines of code. The interface is simple and self-explaining, which substantially helps the coder’s work. Data values are passed in an array and the other settings, such as data labels and colors use the same notation.

Custom JpGraph error message

Data is not always available and even JpGraph fails to show an empty dataset. In such cases the sturdy object model throws a catchable exception to handle charting with missing data. By default JpGraph plots everything as an image. This is true with error messages as well, which can be shown instead of the graph. The receiving web page does not break, but shows the error message in place of the expected graph image. The default error reporting language is English, but anyone can localize the messages into their own language. Translating all the messages may take some time, but there is an easier way to achieve almost the same result.

try { graph->Stroke(); } catch (Exception $e) { JpGraphError::SetTitle(’Plotting error’); $e = new JpGraphException(’Cannot plot graph without values.’); echo $e->Stroke(); }

In this code I first try to plot the graph with the Stroke() method. If this fails, an exception is thrown. I then catch the exception using the native PHP Exception class, so no matter what the error actually is. The next step is to set the title to the error message. I use the static SetTitle() method for this purpose. The actual error message is constructed as a new JpGraphException object, which extends the basic PHP Exception class. I pass the message as an argument. Finally, I plot the contents of the JpGraphException object with its Stroke() method producing an image as shown.

Julkaistu torstaina 8.9.2011 klo 16:57 avainsanalla ohjelmointi.

Edellinen
Suomalaisen uusi ilme
Seuraava
Vispipuuro