Difference between revisions of "SVG Properties"

From Mea Wiki
Jump to navigation Jump to search
 
(56 intermediate revisions by 2 users not shown)
Line 1: Line 1:
An SVG presentation object can be used to draw svg shapes that scale with the dashboard size.
+
SVG presentation objects can be used for graphical drawing that contain e.g. shapes, text and images. SVG drawings scale with the dashboard size.
  
== SVG code ==
+
Security note: SVG presentation objects can embed [https://www.w3schools.com/js/default.asp JavaScript] code into the dashboards, which may cause information security issues, because the embedded code is executed when users open the dashboard. Thus the dashboard designer persons need to be trusted. Viewer users are not able to change dashboards and embed JavaScript code into dashboards.
 +
 
 +
== Defining SVG Code ==
 
Here is a simple SVG code, which draws a black square:
 
Here is a simple SVG code, which draws a black square:
 
<pre>
 
<pre>
 
<svg viewBox="0 0 1 1" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
 
<svg viewBox="0 0 1 1" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
   <g> 
+
   <rect x="0" y="0" width="1" height="1" />
    <rect x="0" y="0" width="1" height="1" />
 
  </g>
 
 
</svg>
 
</svg>
 
</pre>
 
</pre>
Following definitions are mandatory in the SVG code for QPR MobileDashboard:
+
In the SVG used in QPR UI, the following definitions are mandatory:
* Whole SVG code needs to reside inside a single svg tag, i.e. the code must start with '''<svg ...>''' and end with '''</svg>'''.
+
* Whole SVG code needs to reside inside a single SVG element, i.e. the code must start with '''<svg ...>''' and end with '''</svg>'''.
* SVG tag must have a '''viewBox''' attribute, which contains following four numbers separated by spaces: top left x coordinate, top left y coordinate, width, height. Note that SVG's coordinates are not pixels but SVG uses own coordinate system. Note also that the SVG can contain shapes that are outside the defined viewbox area. Usually it's practical to have the top left x and y coordinates set to zero and set the width and height so that there is a suitable granularity for the drawing.  
+
* SVG element must have a '''viewBox''' attribute, which contains following four numbers separated by spaces: top left x coordinate, top left y coordinate, width, height. Note that SVG's coordinates are not pixels but SVG uses an own coordinate system. Note also that the SVG can contain shapes that are outside the defined viewbox area. Usually it's practical to have the top left x and y coordinates set to zero and set the width and height so that there is a suitable granularity for the drawing.  
* SVG tag must have following namespace definitions: '''<nowiki>xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"</nowiki>'''. It may also include other namespace definitions if they are needed in the SVG.
+
* SVG element must have following namespace definitions: '''<nowiki>xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"</nowiki>'''. There may also be other namespace definitions if they are needed by SVG elements.
 +
 
 +
The SVG tag has an optional attribute '''preserveAspectRatio''', which determines how the SVG drawing is scaled when aspect ratios of the SVG's and presentation object's drawing area don't match. It has a default value of '''xMidYMid meet''', which is used when the preserveAspectRatio is not defined. When the default value is used, the aspect ratio is not changed and the SVG is drawn in the middle of the presentation object's area. The value may be '''none''', which means that the aspect ratio is allowed to change freely to fit the SVG to the presentation object's area (usually this is not used when the SVG contains text because the text will then become distorted). More information about preserveAspectRatio attribute: [https://developer.mozilla.org/en/docs/Web/SVG/Attribute/preserveAspectRatio Mozilla MDN].
 +
 
 +
The SVG tag may also contain '''width''' and '''height''' attributes but they are overwritten by QPR UI, so those attributes don't have any effect.
  
The SVG tag has an optional attribute '''preserveAspectRatio''', which has a default value '''xMidYMid meet'''. The parameter determines how the SVG drawing is scaled when aspect ratios of the SVG's and presentation object's drawing area don't match. The value may be '''none''', which means that the aspect ratio is freely allowed to change to fit the SVG to the presentation object area (usually this is not used when the SVG contains text because the text will then become distorted). More information about preserveAspectRatio attribute: [https://www.w3.org/TR/SVG/coords.html#PreserveAspectRatioAttribute preserveAspectRatio w3c documentation].
+
More information about SVG:
 +
* SVG tutorial: https://www.w3schools.com/graphics/svg_intro.asp
 +
* SVG examples: https://www.w3schools.com/graphics/svg_examples.asp
  
== Defining actions for the SVG ==
+
== SVG Actions ==
The SVG code can include SVG events (such as clicking the SVG), which will trigger QPR MobileDashboard actions. The events are embedded by defining them inside the SVG tag as follows:
+
In the SVG code it's possible to use html events and bind them to QPR UI actions. The events are for example mouse click and mouse hover, and actions can be used for example to change context variable values. The events are defined in the SVG code using '''action''' tags as follows:
 
<pre>
 
<pre>
<svg svgEventName=<#action name="ActionName">>
+
<tag eventAttributeName=<#action name="ActionName">>
 
</pre>
 
</pre>
 
Where:
 
Where:
* '''svgEventName''' can be for example '''click''', '''doubleclick''', '''mouseover''', '''mouseout''', '''mousedown''', '''mouseup''', '''mousemove'''. List of all available SVG events: [https://www.w3.org/TR/SVG/interact.html#SVGEvents SVG Event]
+
* '''tag''' can be any tag that can be used in the SVG code, such as '''svg''', '''g''', '''rect''', '''circle''' or '''text'''.
* '''ActionName''' is defined in the "Define SVG actions" text field similarly as with [[Data_Grid_Properties#Defining_Popup_Menu_Actions|Data Grid popup menu actions]].
+
* '''eventAttributeName''' can be for example '''onclick''', '''ondoubleclick''', '''onmouseover''', '''onmouseout''', '''onmousedown''', '''onmouseup''', '''onmousemove'''. List of all available SVG events: [https://www.w3.org/TR/SVG/interact.html#SVGEvents SVG Event]
 +
* '''ActionName''' is defined in the "Define SVG actions" text field similarly as with [[Data_Grid_Properties#Data_Grid_Click_and_Popup_Menu_Events|Data Grid click and popup menu events]].
  
== Context Variables in SVG Code ==
+
== SVG Editors ==
It's possible to use context variables in the SVG code definition. To do this, write the context variable reference as "'''{#variablename}'''" (without the quotes). It's even possible to put the whole SVG code into a context variable, and just use the context variable reference in the SVG code.
+
Instead of editing SVG code as a text, it's possible use an external graphical editor to create SVG drawings. SVG editors:
 +
* '''Boxy SVG''': https://boxy-svg.com/ (free extension for Google Chrome)
 +
* '''Method Draw''': http://editor.method.ac/ (free online editor)
 +
* '''Inkscape''': https://inkscape.org/en/ (free desktop application for Windows/Mac/Linux)
 +
* '''Adobe Illustrator''': http://www.adobe.com/products/illustrator.html (commercial product for Windows/Mac)
 +
* '''Sketch''': https://www.sketchapp.com/ (commercial product for Mac)
  
=== SVG code example with hover effect ===
+
The SVG code is exported from the external editor and pasted to QPR UI. Check that the SVG code contains the '''viewBox''' attribute instead of '''width''' and '''height'''.
The '''SVG Code''' and '''SVG Action''' sections below result in an SVG image with the following properties:
 
* A circle inside a rectangle is drawn
 
* The SVG colors are changed when mouse is hovered over the SVG image. The "style" element contains the CSS which defines the rectangle and circle colors to be used when mouse is hovered over the SVG image.
 
* A bitmap image residing in the URL defined in the "image" context variable value is placed in the center of the circle.
 
* The SVG contains an embedded action named "gotodashboard" which is activated when the SVG image is clicked causing the dashboard to be changed to the dashboard the id of which is defined in the "targetdashboard" context variable.
 
'''SVG Code''':
 
<pre>
 
<svg onclick="<#action name="gotodashboard">" viewBox="0 0 205 205" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
 
<style>
 
    .myHoverGroup:hover rect
 
    \{
 
      opacity: 0.5;
 
      fill: #FFFFFF;
 
    \}
 
    .myHoverGroup:hover ellipse
 
    \{     
 
      stroke: #00CC00;
 
    \}
 
</style>
 
  <g> 
 
    <rect height="197" width="197" y="5" x="5" stroke-width="0" fill="#CBCBCB" />
 
    <rect height="197" width="197" y="0" x="0" stroke-width="0" fill="#CBCBCB" />
 
    <g class="myHoverGroup" cursor="pointer" transform="translate(0,0)">           
 
      <rect id="rectangle" height="197" width="197" y="0" x="0" stroke-width="1" stroke="#808080" fill="#FAFAFA" />     
 
      <ellipse ry="68" rx="68" cy="82" cx="100" stroke-width="5" stroke="#FFCCAA" fill-opacity="1" fill="#FFFFFF" />
 
      <image xlink:href="{#image}" height="105" width="105" y="28" x="47" />
 
      <text xml:space="preserve" font-size="8" text-anchor="middle" y="167" x="100" stroke-width="0" fill="#808080">Click and hover demo</text>
 
      <text xml:space="preserve" font-size="10" text-anchor="middle" y="183" x="100" stroke-width="0" fill="#808080">Click me!</text>
 
    </g>
 
  </g>
 
</svg>
 
</pre>
 
'''SVG Action''':
 
<pre>
 
[
 
{
 
  "name": "gotodashboard",
 
  "contextChanges": {
 
  "sys:dashboard": {
 
    "behavior": "FIXED",
 
    "value": "{#targetdashboard}"
 
  }
 
  }
 
}
 
]
 
</pre>
 
  
{{MDBTutorialAddSVG}}
+
Microsoft PowerPoint presentation can converted to SVG using Inkscape: Export the PowerPoint presentation as a PDF and import the PDF to Inkscape where it can be saved as SVG.
  
[[Category: QPR MobileDashboard]]
+
[[Category: QPR UI]]

Latest revision as of 14:52, 15 January 2020

SVG presentation objects can be used for graphical drawing that contain e.g. shapes, text and images. SVG drawings scale with the dashboard size.

Security note: SVG presentation objects can embed JavaScript code into the dashboards, which may cause information security issues, because the embedded code is executed when users open the dashboard. Thus the dashboard designer persons need to be trusted. Viewer users are not able to change dashboards and embed JavaScript code into dashboards.

Defining SVG Code

Here is a simple SVG code, which draws a black square:

<svg viewBox="0 0 1 1" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <rect x="0" y="0" width="1" height="1" />
</svg>

In the SVG used in QPR UI, the following definitions are mandatory:

  • Whole SVG code needs to reside inside a single SVG element, i.e. the code must start with <svg ...> and end with </svg>.
  • SVG element must have a viewBox attribute, which contains following four numbers separated by spaces: top left x coordinate, top left y coordinate, width, height. Note that SVG's coordinates are not pixels but SVG uses an own coordinate system. Note also that the SVG can contain shapes that are outside the defined viewbox area. Usually it's practical to have the top left x and y coordinates set to zero and set the width and height so that there is a suitable granularity for the drawing.
  • SVG element must have following namespace definitions: xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink". There may also be other namespace definitions if they are needed by SVG elements.

The SVG tag has an optional attribute preserveAspectRatio, which determines how the SVG drawing is scaled when aspect ratios of the SVG's and presentation object's drawing area don't match. It has a default value of xMidYMid meet, which is used when the preserveAspectRatio is not defined. When the default value is used, the aspect ratio is not changed and the SVG is drawn in the middle of the presentation object's area. The value may be none, which means that the aspect ratio is allowed to change freely to fit the SVG to the presentation object's area (usually this is not used when the SVG contains text because the text will then become distorted). More information about preserveAspectRatio attribute: Mozilla MDN.

The SVG tag may also contain width and height attributes but they are overwritten by QPR UI, so those attributes don't have any effect.

More information about SVG:

SVG Actions

In the SVG code it's possible to use html events and bind them to QPR UI actions. The events are for example mouse click and mouse hover, and actions can be used for example to change context variable values. The events are defined in the SVG code using action tags as follows:

<tag eventAttributeName=<#action name="ActionName">>

Where:

  • tag can be any tag that can be used in the SVG code, such as svg, g, rect, circle or text.
  • eventAttributeName can be for example onclick, ondoubleclick, onmouseover, onmouseout, onmousedown, onmouseup, onmousemove. List of all available SVG events: SVG Event
  • ActionName is defined in the "Define SVG actions" text field similarly as with Data Grid click and popup menu events.

SVG Editors

Instead of editing SVG code as a text, it's possible use an external graphical editor to create SVG drawings. SVG editors:

The SVG code is exported from the external editor and pasted to QPR UI. Check that the SVG code contains the viewBox attribute instead of width and height.

Microsoft PowerPoint presentation can converted to SVG using Inkscape: Export the PowerPoint presentation as a PDF and import the PDF to Inkscape where it can be saved as SVG.