The Text object
The drawing api
text object makes it easy to add text to your charts and
being a drawing api
object - you can also easily
add event listeners or tooltips to it too. The chart to the right shows
an example chart where the title is drawn using the
drawing api
text object.
Usage example
<script> line = new RGraph.Line({ id: 'cvs', data: [4,9,1,3,2,6,5], options: { spline: true, backgroundGridVlines:false, backgroundGridBorder:false, xaxis: false, yaxis: false, xaxisLabels: ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'], marginInner: 5, tickmarksStyle: 'endcircle', tickmarksSize: 10, colors: ['black'], textSize: 16 } }).draw(); x = line.canvas.width / 2; y = line.get('marginTop') + 5; text = new RGraph.Drawing.Text({ id: 'cvs', x: x, y: y, text: 'An example of the \r\ndrawing API text object', options: { valign: 'top', halign: 'center', textSize: 18, tooltips: ['The drawing API text object'], tooltipsHighlight: false, tooltipsEvent: 'mousemove', shadow: true, shadowColor: 'red', shadowOffsetx: 0, shadowOffsety: 0, shadowBlur: 5 } }).on('beforedraw', function (obj) { RGraph.setShadow(obj,'#aaa',2,2,5); }).on('draw', function (obj) { RGraph.noShadow(obj); }).draw(); </script>
Properties
- Configuration properties
- Color properties
- Bounding box properties
- Interactive features properties
- Link properties
- Shadow properties
- Other text properties
- Miscellaneous properties
Configuration properties
textSize
The size of the text (in points).
Default: 10
textFont
The font that the text is rendered in.
Default: Arial
textBold
Whether the text is bold or not.
Default: false
textItalic
Whether the text is italic or not.
Default: false
angle
The angle that the text is shown at. If angles other than 0/90/180/270 are used then neither events nor tooltips will function.
Default: 0
marker
Whether a marker is shown that illustrates the coordinates given as the position of the text.
Default: false
halign
The horizontal alignment of the text. This can be
Default: left
valign
The vertical alignment of the text. This can be
Default: bottom
The size of the text (in points).
Default: 10
textFont
The font that the text is rendered in.
Default: Arial
textBold
Whether the text is bold or not.
Default: false
textItalic
Whether the text is italic or not.
Default: false
angle
The angle that the text is shown at. If angles other than 0/90/180/270 are used then neither events nor tooltips will function.
Default: 0
marker
Whether a marker is shown that illustrates the coordinates given as the position of the text.
Default: false
halign
The horizontal alignment of the text. This can be
left
, center
or right
.Default: left
valign
The vertical alignment of the text. This can be
bottom
, center
or top
.Default: bottom
Color properties
Property | Description | Default |
---|---|---|
colors | An array of colors for the text. Even though it's an array only the first color is used. | ['black'] |
colors
An array of colors for the text. Even though it's an array only the first color is used.
Default: ['black']
An array of colors for the text. Even though it's an array only the first color is used.
Default: ['black']
Bounding box properties
bounding
Whether the text is shown with a bounding box or not.
Default: false
boundingFill
The color used as the fill of the bounding box.
Default: rgba(255,255,255,0.7)
boundingStroke
The color used as the stroke of the bounding box.
Default: #777
boundingShadow
Whether the bounding box has a shadow or not.
Default: false
boundingShadowColor
The color of the shadow.
Default: #ccc
boundingShadowOffsetx
The X offset of the shadow.
Default: 3
boundingShadowOffsety
The Y offset of the shadow.
Default: 3
boundingShadowBlur
The severity of the shadow blurring effect.
Default: 3
Whether the text is shown with a bounding box or not.
Default: false
boundingFill
The color used as the fill of the bounding box.
Default: rgba(255,255,255,0.7)
boundingStroke
The color used as the stroke of the bounding box.
Default: #777
boundingShadow
Whether the bounding box has a shadow or not.
Default: false
boundingShadowColor
The color of the shadow.
Default: #ccc
boundingShadowOffsetx
The X offset of the shadow.
Default: 3
boundingShadowOffsety
The Y offset of the shadow.
Default: 3
boundingShadowBlur
The severity of the shadow blurring effect.
Default: 3
Interactive features properties
Property | Description | Default |
---|---|---|
tooltips | The tooltip for the circle. Even though you can only have one - this should still be an array: obj.set('tooltips', ['The tooltip']); | null |
tooltipsEvent | This can be click or mousemove and controls what event is used to trigger the tooltip. | click |
tooltipsHighlight | This stipulates whether the circle will be highlighted when the tooltip is shown. | true |
tooltipsCssClass | This is the name of the css class the chart uses. | RGraph_tooltip |
tooltipsNohideonclear | Not an option that you'll need particularly often if at all. Setting this to true means that when you call the RGraph.clear api function the tooltip DOES NOT get hidden. | false |
tooltipsFormattedPoint | When using formatted tooltip strings this is used as the point when using the %{value_formatted} option. | . |
tooltipsFormattedThousand | When using formatted tooltip strings this is used as the thousand separator when using the %{value_formatted} option. | , |
tooltipsFormattedDecimals | When using formatted tooltip strings this specifies the number of decimals when using the %{value_formatted} option. | 0 |
tooltipsFormattedUnitsPre | When using formatted tooltip strings these units are prepended to the number when using the %{value_formatted} option. | (an empty string) |
tooltipsFormattedUnitsPost | When using formatted tooltip strings these units are appended to the number when using the %{value_formatted} option. | (an empty string) |
tooltipsFormattedListType | With this property you can switch between an unordered list (the default) and an ordered list. Possible values are ul and ol . | ul |
tooltipsFormattedListItems | This should be a two-dimensional array of the list items that are to be shown for all of the tooltips. An example of this property is:tooltipsFormattedListItems: [ ['Bill','Jerry','Berty'], // First tooltip ['Gill','Carrie','Lucy'], // Second tooltip ['Pob','Nobby','Hilda'] // Third tooltip ]You can use css to style this list - for example:.RGraph_tooltip ul#rgraph_formatted_tooltips_list li { text-align: left; color: yellow; } | null |
tooltipsFormattedTableHeaders | When showing a table in the tooltips this can be an array of headers for the table. These are added to the tooltip using th tags. | null |
tooltipsFormattedTableData | This is the data that is added to the table. This is a 3-dimensional array so it's easy to make a mistake. See the example, copy the code from it and then modify it suit. You'll create fewer bugs this way. | null) |
tooltipsPointer | By default, the tooltips have a small triangular pointer that points to the shape that was clicked on. You can turn this off with this property. | true |
tooltipsPointerCss | If you want any css values applied to the tooltips pointer (a css border, for example) then specify an object containing those values to this property. For example: tooltips: { borderLeft: 'gray 2px solid', borderBottom: 'gray 2px solid' } | null |
tooltipsPointerOffsetx | This allows you to adjust the vertical position of the tooltips pointer. | 0 |
tooltipsPointerOffsety | This allows you to adjust the vertical position of the tooltips pointer (for example, if you add a border you may need to move it down slightly). | 0 |
tooltipsPositionStatic | The new default (as of August 2020) is for tooltips to be positioned statically and not be dependent on the mouse position. If you don't want this for whatever reason, you can disable it with this setting. When you set it to false tooltips are positioned next to the mouse pointer. | true |
tooltipsCss | If you want to specify some css that gets applied to all of the tooltips, but don't want to use the RGraph.tooltips.style object (which gets applied to all of the tooltips on the page for every chart) you can use this property to give some per-object css for the tooltips. These are css styles that get applied to all of the tooltips for the specific object only. It should look like this:tooltipsCss: { fontFamily: 'Verdana', fontSize: '20pt' } | null |
tooltipsOffsetx | This property allows you to shift the tooltips left or right. | 0 |
tooltipsOffsety | This property allows you to shift the tooltips up or down. | 0 |
tooltips
The tooltip for the circle. Even though you can only have one - this should still be an array:
Default: null
tooltipsEvent
This can be
Default: click
tooltipsHighlight
This stipulates whether the circle will be highlighted when the tooltip is shown.
Default: true
tooltipsCssClass
This is the name of the
Default: RGraph_tooltip
tooltipsNohideonclear
Not an option that you'll need particularly often if at all. Setting this to true means that when you call the
Default: false
tooltipsFormattedPoint
When using formatted tooltip strings this is used as the point when using the
Default: .
tooltipsFormattedThousand
When using formatted tooltip strings this is used as the thousand separator when using the
Default: ,
tooltipsFormattedDecimals
When using formatted tooltip strings this specifies the number of decimals when using the
Default: 0
tooltipsFormattedUnitsPre
When using formatted tooltip strings these units are prepended to the number when using the
Default: (an empty string)
tooltipsFormattedUnitsPost
When using formatted tooltip strings these units are appended to the number when using the
Default: (an empty string)
tooltipsFormattedListType
With this property you can switch between an unordered list (the default) and an ordered list. Possible values are
Default: ul
tooltipsFormattedListItems
This should be a two-dimensional array of the list items that are to be shown for all of the tooltips. An example of this property is:
Default: null
tooltipsFormattedTableHeaders
When showing a table in the tooltips this can be an array of headers for the table. These are added to the tooltip using
Default: null
tooltipsFormattedTableData
This is the data that is added to the table. This is a 3-dimensional array so it's easy to make a mistake. See the example, copy the code from it and then modify it suit. You'll create fewer bugs this way.
Default: null)
tooltipsPointer
By default, the tooltips have a small triangular pointer that points to the shape that was clicked on. You can turn this off with this property.
Default: true
tooltipsPointerCss
If you want any
Default: null
tooltipsPointerOffsetx
This allows you to adjust the vertical position of the tooltips pointer.
Default: 0
tooltipsPointerOffsety
This allows you to adjust the vertical position of the tooltips pointer (for example, if you add a border you may need to move it down slightly).
Default: 0
tooltipsPositionStatic
The new default (as of August 2020) is for tooltips to be positioned statically and not be dependent on the mouse position. If you don't want this for whatever reason, you can disable it with this setting. When you set it to
Default: true
tooltipsCss
If you want to specify some
Default: null
tooltipsOffsetx
This property allows you to shift the tooltips left or right.
Default: 0
tooltipsOffsety
This property allows you to shift the tooltips up or down.
Default: 0
The tooltip for the circle. Even though you can only have one - this should still be an array:
obj.set('tooltips', ['The tooltip']);
Default: null
tooltipsEvent
This can be
click
or mousemove
and controls what event is used to trigger the tooltip.Default: click
tooltipsHighlight
This stipulates whether the circle will be highlighted when the tooltip is shown.
Default: true
tooltipsCssClass
This is the name of the
css
class the chart uses.Default: RGraph_tooltip
tooltipsNohideonclear
Not an option that you'll need particularly often if at all. Setting this to true means that when you call the
RGraph.clear
api
function the tooltip DOES NOT get hidden.Default: false
tooltipsFormattedPoint
When using formatted tooltip strings this is used as the point when using the
%{value_formatted}
option.Default: .
tooltipsFormattedThousand
When using formatted tooltip strings this is used as the thousand separator when using the
%{value_formatted}
option.Default: ,
tooltipsFormattedDecimals
When using formatted tooltip strings this specifies the number of decimals when using the
%{value_formatted}
option.Default: 0
tooltipsFormattedUnitsPre
When using formatted tooltip strings these units are prepended to the number when using the
%{value_formatted}
option.Default: (an empty string)
tooltipsFormattedUnitsPost
When using formatted tooltip strings these units are appended to the number when using the
%{value_formatted}
option.Default: (an empty string)
tooltipsFormattedListType
With this property you can switch between an unordered list (the default) and an ordered list. Possible values are
ul
and ol
.Default: ul
tooltipsFormattedListItems
This should be a two-dimensional array of the list items that are to be shown for all of the tooltips. An example of this property is:
tooltipsFormattedListItems: [ ['Bill','Jerry','Berty'], // First tooltip ['Gill','Carrie','Lucy'], // Second tooltip ['Pob','Nobby','Hilda'] // Third tooltip ]You can use
css
to style this list - for example:.RGraph_tooltip ul#rgraph_formatted_tooltips_list li { text-align: left; color: yellow; }
Default: null
tooltipsFormattedTableHeaders
When showing a table in the tooltips this can be an array of headers for the table. These are added to the tooltip using
th
tags.Default: null
tooltipsFormattedTableData
This is the data that is added to the table. This is a 3-dimensional array so it's easy to make a mistake. See the example, copy the code from it and then modify it suit. You'll create fewer bugs this way.
Default: null)
tooltipsPointer
By default, the tooltips have a small triangular pointer that points to the shape that was clicked on. You can turn this off with this property.
Default: true
tooltipsPointerCss
If you want any
css
values applied to the tooltips pointer (a css
border, for example) then specify an object containing those values to this property. For example: tooltips: { borderLeft: 'gray 2px solid', borderBottom: 'gray 2px solid' }
Default: null
tooltipsPointerOffsetx
This allows you to adjust the vertical position of the tooltips pointer.
Default: 0
tooltipsPointerOffsety
This allows you to adjust the vertical position of the tooltips pointer (for example, if you add a border you may need to move it down slightly).
Default: 0
tooltipsPositionStatic
The new default (as of August 2020) is for tooltips to be positioned statically and not be dependent on the mouse position. If you don't want this for whatever reason, you can disable it with this setting. When you set it to
false
tooltips are positioned next to the mouse pointer.Default: true
tooltipsCss
If you want to specify some
css
that gets applied to all of the tooltips, but don't want to use the RGraph.tooltips.style
object (which gets applied to all of the tooltips on the page for every chart) you can use this property to give some per-object css
for the tooltips. These are css
styles that get applied to all of the tooltips for the specific object only. It should look like this:tooltipsCss: { fontFamily: 'Verdana', fontSize: '20pt' }
Default: null
tooltipsOffsetx
This property allows you to shift the tooltips left or right.
Default: 0
tooltipsOffsety
This property allows you to shift the tooltips up or down.
Default: 0
Link properties
link
If you want the text object to be a link you can specify a URL here. Remember to include the
Default: null
linkTarget
This is used as the target window for the link (the name of the window to show the link in). By default this will open in the same window (_self) - but if you want it to open in a new window you can use
Default: _self
linkOptions
Options that will be passed to the
Default: nothing
If you want the text object to be a link you can specify a URL here. Remember to include the
RGraph.common.dynamic.js
file as well or it won't function. Remember that you can also use links in tooltips - that are html
div
tags at their core - so no special syntax is needed.Default: null
linkTarget
This is used as the target window for the link (the name of the window to show the link in). By default this will open in the same window (_self) - but if you want it to open in a new window you can use
_blank
.Default: _self
linkOptions
Options that will be passed to the
window.open
call. These are the same as the third argument to window.open
- eg width=400,height=400
.Default: nothing
Shadow properties
shadow
Whether a drop shadow is applied.
Default: false
shadowColor
The color of the shadow.
Default: #ccc
shadowOffsetx
The X offset of the shadow.
Default: 2
shadowOffsety
The Y offset of the shadow.
Default: 2
shadowBlur
The severity of the shadow blurring effect.
Default: 3
Whether a drop shadow is applied.
Default: false
shadowColor
The color of the shadow.
Default: #ccc
shadowOffsetx
The X offset of the shadow.
Default: 2
shadowOffsety
The Y offset of the shadow.
Default: 2
shadowBlur
The severity of the shadow blurring effect.
Default: 3
Other text properties
Property | Description | Default |
---|---|---|
textAccessible | A new feature in 2016 that allows you to use dom text in place of canvas text. It makes for much higher quality text that you can also select if desired (for copy/paste operations). It won't fit all situations and you can read more about the DOM text feature here. A good way to control borders/margins/padding etc is not to set them on the canvas but to wrap the canvas in a div and set them on that like this:
<div style="margin-left: 50px; display: inline-block"> <canvas id="cvs" width="650" height="250"></canvas> </div> | false |
text | This allows you to add custom text to your chart if you want to. There's a dedicated page that describes this option here. | null |
textAccessible
A new feature in 2016 that allows you to use
Default: false
text
This allows you to add custom text to your chart if you want to. There's a dedicated page that describes this option here.
Default: null
A new feature in 2016 that allows you to use
dom
text in place of canvas
text. It makes for much higher quality text that you can also select if desired (for copy/paste operations). It won't fit all situations and you can read more about the DOM text feature here. A good way to control borders/margins/padding etc is not to set them on the canvas
but to wrap the canvas
in a div
and set them on that like this:
<div style="margin-left: 50px; display: inline-block"> <canvas id="cvs" width="650" height="250"></canvas> </div>
Default: false
text
This allows you to add custom text to your chart if you want to. There's a dedicated page that describes this option here.
Default: null
Miscellaneous properties
highlightStyle
By default this is
Default: null
highlightStroke
This is the color that the circle is highlighted (the stroke) in when the tooltip is shown.
Default: #ccc
highlightFill
This is the color that the circle is highlighted in (the fill) when the tooltip is shown.
Default: rgba(255,255,255,0.7)
By default this is
null
but you can set it to a function if you wish so that function is called to do the chart highlighting. It's passed the shape object as an argument.Default: null
highlightStroke
This is the color that the circle is highlighted (the stroke) in when the tooltip is shown.
Default: #ccc
highlightFill
This is the color that the circle is highlighted in (the fill) when the tooltip is shown.
Default: rgba(255,255,255,0.7)
Methods
obj.get(name)
An accessor that you can use to retrieve the values of properties.
obj.set(name, value)
An accessor that you can use to set the values of properties.
obj.getShape(event)
This method makes it easy to get hold of the Text object when it has been clicked on or hovered over. It returns an object which has the following indexes available:
object |
The chart object. |
x |
The X coordinate of the text. |
y |
The Y coordinate of the text. |
width |
The width of the text. |
height |
The height of the text. |
dataset |
As there's only ever one element this is always zero. |
index |
As there's only ever one element this is always zero. |
sequentialIndex |
As there's only ever one element this is always zero. |
tooltip |
If a tooltip is associated with the Text object this will be it.id:
strings are expanded for you (where the tooltip text is retrieved from the html
tag with the matching ID).
|
<script>
text.canvas.onclick = function (e)
{
RGraph.redraw();
var canvas = e.target,
obj = canvas.__object__,
shape = obj.getShape(e);
if (shape) {
// Highlight the main body of the Text object.
obj.path(
'lw 10 b r % % % % s black f rgba(255,0,0,0.25)',
shape.x, shape.y, shape.width, shape.height
);
}
}
</script>
obj.on(event, function)
This method can be used to set an event listener on an object.
It operates similarly to the jquery
on
function.
The first argument is the event that you wish to attach to and the second
is the handler function. For example:
obj.on('draw', function (obj)
{
// Put your event code here
});
The function is useful if you use method chaining when creating your charts:
obj = new RGraph.Drawing.Text({ id: 'cvs', x: 100, y: 100, text: 'Some sample text!', options: { } }).on('draw', function (obj) { // Put your draw event code here }).on('click', function (e, shape) { // Handle the click event }).draw();
obj.exec(function)
The exec function is documented here.