Canvas Gantt chart API reference
Summary
Documentation about the Gantt chart including information on all the options available to you. The Gantt chart is commonly used to show scheduling information regarding timetables for people or events. There's a demo that shows an interactive Gantt chart.
- Example
- Properties
- Methods
- Adding events to your Gantt charts
- Adding vertical and horizontal bars to your Gantt chart
- Effects
Example
<script> // Create the Gantt chart. Note the (October 2012) change in how the events data is specified. // You now give the data as an argument to the constructor. var gantt = new RGraph.Gantt({ id: 'cvs', data: [ [31, 28, null, 'Richard', null, null, 'rgba(0,0,0,0)'], [31, 28, null, 'Fred', null, null, 'rgba(0,0,0,0)'], [59, 14, null, 'Barney', null, null, 'rgba(0,0,0,0)'], [59, 21, null, 'Gloria', null, null, 'rgba(0,0,0,0)'], [59, 31, null, 'Paul', null, null, 'rgba(0,0,0,0)'], [80, 21, null, 'Harry', null, null, 'rgba(0,0,0,0)'], [94, 7, null, 'Shane', null, null, 'rgba(0,0,0,0)'], [120, 14, null, 'Barry', null, null, 'rgba(0,0,0,0)'], [130, 14, null, 'Cynthia', null, null, 'rgba(0,0,0,0)'], [211, 61, null, 'Graham', null, null, 'rgba(0,0,0,0)'], [334, 31, null, 'Paul', null, null, 'rgba(0,0,0,0)'] ], options: { xmax: 365, gutterleft: 75, labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], title: 'Holiday schedule for Xyz Ltd', defaultcolor: 'rgba(255,0,0,1)', tooltips: [ "<b>Richard</b><br />Richard is going on holiday.<br />He'll be away for 28 days.", "<b>Fred</b><br />Fred is also going away for 28 days", "<b>Barney</b><br />Barney is off work for two weeks", "<b>Gloria</b><br />Gloria is off for 3 weeks", "<b>Paul</b><br /> Away for 31 days", "<b>Harry</b><br />Away for three weeks", "<b>Shane</b><br />Away for one week", "<b>Barry</b><br />Away for two weeks", "<b>Cynthia</b><br />Away for two weeks", "<b>Graham</b><br />Away for August and September", "<b>Paul</b><br />Away for 31 days" ], vbars: [ [0, 31, 'rgba(192,255,192,0.5)'], [59, 31, 'rgba(192,255,192,0.5)'], [120, 31, 'rgba(192,255,192,0.5)'], [181, 31, 'rgba(192,255,192,0.5)'], [243, 30, 'rgba(192,255,192,0.5)'], [304, 30, 'rgba(192,255,192,0.5)'] ] } }).draw(); </script>
Properties
You can use these properties to control how the chart apears. You can set them by including them in the options section of the configuration as above.
- Chart configuration
- Background
- Margins
- Labels and text
- Titles
- Interactive features
- Zoom
- Events
- Miscellaneous
Chart configuration
Background
Margins
Labels and text
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"> | 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. | visible |
textAccessiblePointerevents | This controls whether the DOM text responds to mouse based events or not (it sets the pointer-events CSS property to none). | true |
textSize | The size of the text (in points). | 10 |
textFont | The font used to render the text. | Arial |
textColor | The color of the labels. | black |
labels | An array of the labels which are applied to the chart. | [] (An empty array) |
labelsColor | The color of the labels | null (same as textColor) |
labelsPercent | This allows you to turn off the percent indicator if you want to. | true |
labelsAlign | This determines whether the horizontal labels are drawn at the top (underneath the title) or at the bottom (in the bottom gutter). | top |
labelsInbar | An array of labels that are drawn inside the Gantt chart bars. These labels can also be drawn on the right of these bars. | null |
labelsInbarAlign | This controls whether the labels are aligned left, center or right when the labels are drawn inside the bars. | left |
labelsInbarBgcolor | The background color for the text. By default there's no background to the text. | null |
labelsInbarColor | The color of the labels. | black |
labelsInbarFont | The font used to draw the labels. | Arial |
labelsInbarSize | The size of the text labels. | 10 |
labelsInbarAbove | If you prefer, the labels can be drawn "above" the bars (actually to the right but for API compatibility it's called "above"). | false |
Titles
Interactive features
Property | Description | Default |
---|---|---|
tooltips | An array of tooltips that are shown when the bars are clicked. They can contain HTML. | (An empty array) |
tooltipsEvent | This is the event that triggers the tooltips. It can be either onclick or onmousemove. | onclick |
tooltipsEffect | The effect used for showing tooltips. Can be either fade or none. | fade |
tooltipsCssClass | This is the name of the CSS class the chart uses. | 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 | null |
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. | false |
contextmenu | An array of context menu items. Cannot be used in conjunction with tooltips. | (An empty array) |
annotatable | Whether annotations are enabled for the chart (ie you can draw on the chart interactively. | false |
annotateColor | If you do not allow the use of the palette, then this will be the only colour allowed for annotations. | black |
annotateLinewidth | This is the line width of the annotations. | 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). | 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. | null |
resizableMinwidth | This allows you to set a minimum width that the chart can be resized to. | null |
resizableMinheight | This allows you to set a minimum height that the chart can be resized to. | null |
resizableMaxwidth | This allows you to set a maximum width that the chart can be resized to. | null |
resizableMaxheight | This allows you to set a maximum height that the chart can be resized to. | null |
adjustable | Defaulting to false, this determines whether your bar chart will be adjustable. | false |
adjustableOnly | This should be an array of values that determine whether a Bar is adjustable or not. A truthy value for when it is, a falsey value for when its not. | null |
Zoom
Events
Property | Description | Default |
---|---|---|
eventsClick | If you want to add your own onclick function you can do so by assigning it to this property. See here for details. | 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. | 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. | null |
eventsMouseout | If you want to add your own onmouseout function you can do so by assigning it to this property. See here for details. | null |
miscellaneous
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)
This method returns the details of a focused bar (ie clicked or mouseover'ed. It returns:
- The chart object
- The X coordinate
- The Y coordinate
- The width of the bar
- The height of the bar
- The index of the bar (starting from 0)
The shape also includes textual indexes like this: shape['object'] And they are:
- object
- x
- y
- width
- height
- tooltip
- index
obj.getXCoord(value)
This method can be used to get an appropriate X coordinate for a value when you're doing custom drawing on the chart. It returns null if the value is out of range.
obj.getValue(mixed)
This method can be used to get the value when the chart has been clicked on. The argument is the event object. You can also use this method outside of an event listener by giving the function an array as the argument containing the X/Y coordinates.
obj.canvas.onclick = function (e)
{
var obj = e.target.__object__;
var value = obj.getValue(e);
// ...
}
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 exec option and method
The exec function is documented here.
Adding events (the information represented on the chart) to your Gantt chart
To add events (the information represented on the chart), you now specify them as an argument to the constructor - NOT the events property.
myGantt = new RGraph.Gantt({ id: 'cvs', data: [ [31, 28, 75, 'Richard'], [12, 28, 67, 'Fred'], [59, 14, 0, 'Barney'], [59, 21, 5, 'Gloria'], [46, 31, 94, 'Paul'], [80, 21, 46, 'Harry'], [94, 17, 84, 'Shane'], [34, 14, 32, 'Barry'], [64, 14, 28, 'Cynthia', 'red', 'yellow'], [13, 61, 74, 'Graham'], [84, 31, 16, 'Paul'] ], options: { } );
The first value is the (zero indexed) start number relative to your xmax. So if you have set your xmax to 62, (to represent two months), and this value is 31, the start will be the start of the second month. In this case you could give two labels - ['July', 'August'].
The second value is the duration. So using the previous example, if you set this to 7, you would have an event lasting a week.
The third value is the "percentage complete" indicator. This is shown to the right of the event, and is optional - you can specify null if you wish.
The fourth value is the label that is used on the left hand side.
The optional fifth value stipulates the background colour that the bar is. Default is white.
The optional sixth value is the foreground color of the bar. Defaulting to #0c0.
The optional seventh value is the border color, defaulting to black
And the optional eigth value is the numerical linewidth of the border. The default is 1.
Adding vertical and horizontal bars to your Gantt chart
You can add decorative vertical or horizontal bars like so:
... vbars: [ [0, 31, 'rgba(192,255,192,0.5)'], [59, 31, 'rgba(192,255,192,0.5)'], [120, 31, 'rgba(192,255,192,0.5)'], [181, 31, 'rgba(192,255,192,0.5)'], [243, 30, 'rgba(192,255,192,0.5)'], [304, 30, 'rgba(192,255,192,0.5)'], ], hbars: [,'red',,,,'green'], ...
You can use these for decorative purposes, or to mark events/limits. For the vertical bars the first value is the start index, as above. The second value is the unit length, again as above. The third value is the color of the vertical bar.
And for the horizontal bars you just specify the color of the bar - they automatically extend across the entire gant chart area. If you don't specify a color (as above) then there won't be a bar for that entry.
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.Gantt({ id: 'cvs', data: [[0,30],[30,60],[60,90]], options: { gutterLeft: 35, max: 90 } }).grow({frames: 60}, myCallback) </script>