ExtGraph - Graph Component and Framework | ||||
|
updated 12.11.2004 Project is hosted by |
How to create new dynamic SVG objectExtGraph is based on SVG and XPascal.Even though both technologies are
feature-rich and powerful, they can be very simple to explain. SVG is
Scalable Vector Graphics defined by W3C consortium. XPascal is scripting
language using Pascal syntax and implementing large subset of Delphi objects,
libraries and components. If you already know SVG and Delphi, you know most of what is necessary in order to create dynamic graphs. If not there are many good tutorials available on the web. Learning SVGIf you want to understand SVG more thoroughly, you can look at W3C SVG reference. A good tutorial can be found at w3schools svg pages. Learning Object PascalBehind SVG graphics is Object Pascal scripting language. Some good sources are at Pascal language basics or About.com. There are also many other sources as well as books on both SVG and Pascal/Delphi. What we want to achieve?We want to draw an analog clock and make it display the current time. We will start with simple SVG object and display it on the graph canvas. Then we will add some other SVG objects to create clock shape. Once satisfied with the clock's graphical design, we will write pascal code that will animate the SVG picture. Creating clockStep 1 - download XGraph demo applicationFirst you will need XGraph application, only windows version is available
so far. Step 2 - create SVG objectWe will make it simple. Because svg is xml and xml is text, create text document with an .svg extension, for example clock.svg. You can use any standard plain text editor like Notepad.exe. Fill it with following content and save it.
This code will draw beige circle with red line in "twelve"
position. Go to main menu and select menu item Objects-SVG, select svg
file, you have just created and place it somewhere on canvas. Now you
should see something like this: Now let's enhance our clock a little. Add second line that will be shorter and just for fun will be thicker and in a slightly different color. We will also add 'id tags' to lines, so that later we can tell which line is short and which long. The modified svg file will now contain (changes are marked with red):
Do not forget to save your changes, clear graph and re-open changed svg.
Now you should see: Step 3 - Adding logic.We have a static svg picture and canvas that displays the vector graphics based on xml code. To make clock dynamic, we have to have read current time, and change coordinates of lines accordingly. We will use XPascal, especially its xml (remember? svg is xml), time and trigonometric routines. Let's start simple again. Now, we will programmatically rotate long line to a new position, say to quarter to three. XGraph executes the XPascal script as a part of the redraw process, so we will add some Pascal code into our clock.svg file. Code is stored in script element comment, similar to what Javascript does. <svg viewBox="0 0 400 400"> After you will re-open clock.svg you should see something like this.
Tip: You can edit XPascal code in integrated
XML editor, even with syntax hi-lighting: If everything works let's take one final step. Enhance the code so that it shows current time. Code: <svg viewBox="0 0 400 400"> Final picture will show current time: Tips for homework
Send us your homework, if it will be good, we would like to publish your design and code here. CommentsYour comments are welcomed. Feel free to send them to my email: Pavel Vrecion. |
|
©2004 Extgraph developers | About Related projects Case studies Docs |