Pie charts documentation

Share RGraph:   

 

Example

<script>
    var data = [564,155,499,611,322];

    var pie = new RGraph.Pie({
        id: 'cvs',
        data: data,
        options: {
            labels: ['Abc', 'Def', 'Ghi', 'Jkl', 'Mno'],
            linewidth: 2,
            strokestyle: 'white'
        }
    }).draw();
</script>

The example file is here.

 

Properties

You can use these properties to control how the pie chart looks. You can set them by using the Set() method. Eg:

myPie.set('name', 'value');

 

Chart configuration

centerx
If required, you can position the Pie chart using this instead of the gutters.
Default: null

centery
If required, you can position the Pie chart using this instead of the gutters.
Default: null

radius
If required, you can size the Pie chart using this instead of the gutters.
Default: null

 

Margins

gutterLeft
The left gutter of the chart, (the gutter is where the labels and title are)).
Default: 25

gutterRight
The right gutter of the chart, (the gutter is where the labels and title are).
Default: 25

gutterTop
The top gutter of the chart, (the gutter is where the labels and title are).
Default: 25

gutterBottom
The bottom gutter of the chart, (the gutter is where the labels and title are).
Default: 25

 

Colors

colors
Colors to be used for the pie segments.
Default: ['rgb(255,0,0)', '#ddd', 'rgb(0,255,0)', 'rgb(0,0,255)', 'rgb(255,255,0)', 'rgb(0,255,255)', 'red', 'pink', 'black', 'white']

strokestyle
The color of the seperating lines. This can also be an array of different colors - eg pie.set('strokestyle', ['red', 'blue', 'green'])
Default: #999

 

Labels and text

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>

Default: true

textAccessibleOverflow
This can be visible or hidden and it controls whether the text is clipped to the edges of the canvas. It defaults to be visible and means you can set small gutters if you wish.
Default: visible

textAccessiblePointerevents
This controls whether the DOM text responds to mouse based events or not (it sets the pointer-events CSS property to none).
Default: true

textSize
The size of the text.
Default: 10

textFont
The font used to render the text.
Default: Arial

textColor
The color of the labels.
Default: black

labels
An array of labels to be used for the chart.
Default: An empty array

labelsColors
An array of colors to be used for the labels.
Default: null

labelsSticks
Stipulates that sticks for the labels are shown. This can also be an array of stick lengths - which may be useful if you're experiencing the labels clashing.
Default: false

labelsSticksColors
Sets the color of the label sticks.
Default: #aaa

labelsSticksUsecolors
If true this will set the sticks to use the same colors as the pie chart itself
Default: false

labelsSticksLength
Sets the length of the label sticks.
Default: 7

labelsSticksLinewidth
Sets the linewidth of the label sticks.
Default: 1

labelsSticksList
A new way of organising the labels - this should help avoid label text overlaps.
Default: false

labelsIngraph
This shows in-graph labels in each segment
Default: null

labelsIngraphBounding
Whether the in-graph labels are surrounded by a bounding box
Default: true

labelsIngraphBoundingFill
The fill color of the bounding square
Default: white

labelsIngraphUnitsPre
This string is PREPENDED to the label
Default: (empty string)

labelsIngraphUnitsPost
This string is APPENDED to th label
Default: (empty string)

labelsIngraphRadius
If specified this is the radius that the labels are positioned at. If its a number between 0 and 2 it is MULTIPLIED with the radius to get the position. If its above 2 though its used as the EXACT radius
Default: null

labelsIngraphColor
The color of the labels.
Default: null

labelsIngraphFont
The font to use for the ingraph labels
Default: Same as text.font

labelsIngraphSize
The size of the ingraph labels
Default: Same as text.size

labelsIngraphSpecific
Instead of the values you use this option to specify the text that is shown
Default: null

labelsCenter
If you have a donut chart this can show a label in the center
Default: null

labelsCenterSize
The size of the center label
Default: 26

labelsCenterFont
The font used to render the label
Default: Arial

labelsCenterColor
The color of the center label
Default: black

labelsCenterItalic
Whether the center label is italic
Default: false

labelsCenterBold
Whether the center label is bold
Default: false

labelsCenterUnitsPre
This string is PREPENDED to the label
Default: none

labelsCenterUnitsPost
This string is APPENDED to the label
Default: none

 

Titles

title
The title of the pie chart.
Default: none

titleFont
The font that the title is rendered in. If not specified the text.font setting is used (usually Arial)
Default: null

titleSize
The size of the title. If not specified the size is usually 2pt bigger than the text.size setting.
Default: null

titleBold
Whather the title is bold or not.
Default: true

titleItalic
Whether the title is italic or not.
Default: false

titleBackground
The background color (if any) for the title.
Default: null

titleColor
The color of the title.
Default: black

titleX
To give the exact X coordinate for the title - use this
Default: null

titleY
To give the exact Y coordinate for the title - use this
Default: null

 

Shadow

shadow
Whether a drop shadow is applied.
Default: true

shadowColor
The color of the shadow.
Default: #aaaa

shadowOffsetx
The X offset of the shadow.
Default: 0

shadowOffsety
The Y offset of the shadow.
Default: 0

shadowBlur
The severity of the shadow blurring effect.
Default: 15

 

Interactive features

tooltips
A numerically indexed array of tooltips that are shown when a bar is clicked. These can contain HTML.
Default: []

tooltipsEvent
This is the event that triggers the tooltips. It can be either onclick or onmousemove.
Default: onclick

tooltipsEffect
The effect used for showing tooltips. Can be either fade or none.
Default: fade

tooltipsCssClass
This is the name of the CSS class the chart uses.
Default: RGraph_tooltip

tooltipsOverride
If you wish to handle showing tooltips yourself, this should be a function object which does just that. There's more information on the tooltips documentation page
Default: null

tooltipsCoordsPage
If set to true the tooltips will be positioned using the .pageX and .pageY coordinates. If you use canvas tags that are offset from the .pageX and .pageY coordinates - this can help with tooltip positioning.
Default: false

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 tooltip DO NOT get hidden.
Default: false

contextmenu
An array of context menu items. More information on context menus is here.
Default: [] (An empty array)

annotatable
Whether annotations are enabled for the chart (ie you can draw on the chart interactively.
Default: false

annotateColor
If you do not allow the use of the palette, then this will be the only colour allowed for annotations.
Default: black

annotateLinewidth
This is the line width of the annotations.
Default: 1

resizable
Defaulting to false, this determines whether your chart will be resizable. Because of the numerous event handlers this has to install code on, This feature is unlikely to work with other dynamic features (the context menu is fine however).
Default: false

resizeHandleBackground
With this you can specify the background color for the resize handle. If you're adjusting the position of the handle then you may need this to make the handle stand out more.
Default: null

resizableMaxwidth
This allows you to set a maximum width that the chart can be resized to.
Default: null

resizableMaxheight
This allows you to set a maximum height that the chart can be resized to.
Default: null

 

Key

The key properties are documented on the key documentation page.

 

Zoom

zoomFactor
This is the factor that the chart will be zoomed by (bigger values means more zoom)
Default: 1.5

zoomFadeIn
Whether the zoomed canvas fades in or not. This also can be used to control the fade in for the zoom in thumbnail mode.
Default: true

zoomFadeOut
Whether the zoomed canvas fades out or not. This also can be used to control the fade in for the zoom in thumbnail mode.
Default: true

zoomHdir
The horizontal direction of the zoom. Possible values are: left, center, right
Default: right

zoomVdir
The vertical direction of the zoom. Possible values are: up, center, down
Default: down

zoomDelay
The delay (in milliseconds) between frames.
Default: 50

zoomFrames
The number of frames in the zoom animation.
Default: 10

zoomShadow
Whether or not the zoomed canvas has a shadow or not.
Default: true

 

Events

eventsClick
If you want to add your own onclick function you can do so by assigning it to this property. See here for details.
Default: null

eventsMousemove
If you want to add your own onmousemove function you can do so by assigning it to this property. See here for details.
Default: null

eventsMouseover
If you want to add your own onmouseover function you can do so by assigning it to this property. See here for details.
Default: null

 

Miscellaneous

linewidth
By setting this to roughly 5, and setting the strokestyle to the same colour as your background colour,you will get a segment separation effect.
Default: 1

variant
This can be either pie (the default), pie3d, donut or donut3d. Setting this to donut or donut3d is how you get a donut chart.
Default: pie

variantThreedDepth
This is the depth of the 3D Pie chart
Default: 20

variantDonutWidth
This allows you to set how wide the donut is. It can be useful when nesting donuts.
Default: null

highlightStyle
Can be either 2d, 3d, outline or a function and determines which style of segment highlighting is used in conjunction with tooltips. If its a function the function is called and no highlighting is done - ie you should do it all yourself.
Default: 2d

highlightStyleOutlineWidth
When outline highlighting for tooltips is used, this controls the width of the highlighting band.
Default: 10

highlightStyleTwodFill
When 2D highlighting for tooltips is used, this controls the color of the highlighting. The former name for this property was highlightStyleTwodColor.
Default: rgba(255,255,255,0.5)

highlightStyleTwodStroke
When 2D highlighting for tooltips is used, this controls the stroke color of the highlighting.
Default: rgba(255,255,255,0)

exploded
This can either be an array with the same number of elements as segments, or a single number, in which case every segment will be exploded that amount. If it's an array then only those segments will be exploded - eg [0,5,0,0]
Default: An empty array

centerpin
If you choose a linewidth greater than 1 or 2 then because of the way that HTML5 canvas renders lines you may end up with visible edges to them. So by using a centerpin you can hide these. This setting should be a number and is used as the radius of the centerpin.
Default: null

centerpinStroke
This is used as the stroke style of the centerpin.
Default: null

centerpinFill
This is used as the fill style of the centerpin.
Default: null

clearto
This is used in animations and effects as the default color to use when the canvas.
Default: null

 

Methods

 

obj.get(name)
An accessor that you can use to retrieve the value of properties.

 

obj.set(name, value)
An accessor that you can use to set the value of properties.

 

obj.getShape(event)

obj.getSegment() makes it easy to determine which segment of the Pie chart was clicked on. It provides:

The shape also includes textual indexes like this: shape['object'] And they are:

<script>
    RGraph.register(myGraph);
    
    myGraph.canvas.onclick = function (e)
    {
        RGraph.fixEventObject(e);
        RGraph.redraw();

        var ca  = e.target;
        var co  = ca.getContext('2d');
        var obj = ca.__object__;
        var segment = obj.getShape(e);
        
        if (segment) {
            co.fillStyle = 'rgba(255,255,255,0.5)';
            co.beginPath();            
                co.moveTo(segment[0], segment[1]);
                co.arc(segment[0], segment[1], segment[2], segment[3], segment[4], 0);
            co.stroke();
            co.fill();
            
            e.stopPropagation();
        }
    }
    
    window.onclick = function (e)
    {
        RGraph.redraw();
    }
</script>
Important Note

This method was formerly a common object method, called RGraph.getSegment(e), but has now (6th March 2011) been moved to be part of the Pie chart object.

 

 

obj.explodeSegment(index, size)

The explodeSegment() method allows you to programmatically trigger the exploding (ie the highlighting of) a particular segment. The Explode() method is used like so:

<script>
    pie = new RGraph.Pie({
        id: 'cvs',
        data: [8,9,4,6]
    }).draw();

    pie.explodeSegment(index, size);
</script>

The index argument is the zero-index segment to operate on (counting from the north axis). An the size is measured in pixels.

 

obj.getAngle(value)

This method can be used to get an appropriate angle for a value using the "scale" of the Pie chart. So if your Pie chart is showing values that go from 0-100 and your requested value is 50, this method will return an angle for the bottom of the Pie chart (ie half way around).

 

obj.on(event, func)

This method can be used to set an event listener on an object. It operates in a similar way to the jQuery .on() function - the first argument is the event you wish to attach to and the second is the handler function. For example:

.on('draw', function (obj)
{
    // Put event code here
});

The function is useful if you use method chaining when creating your charts:

var obj = new RGraph.Pie({
    id: 'cvs',
    data: [4,5,3,8,6,3]
}).on('draw', function (e, shape)
{
    // Put ondraw code here
})
.on('click', function (e, shape)
{
    // Handle click event
})
.draw()

 

The exec option and method

The exec function is documented here.

 

Animation Effects

These effects are available and can be used instead of the .draw() function. There are also generic effects available which you can see here: Generic effects and transitions
<script>
    /**
    * Optional callback function that's called when the effect is complete
    */
    function myCallback (obj)
    {
        // ...
    }

    var obj = new RGraph.Pie({
        id: 'cvs',
        data: [8,6,6,5,3,4,2],
        options: {
        }
    }).grow({frames: 60}, myCallback)
    // .implode({frames: 60}, myCallback)
    // .roundRobin({frames: 60}, myCallback)
</script>

Comments

Add a new comment...