About
RGraph is a JavaScript charts library based on HTML5 SVG and canvas. RGraph is mature (over 15 years old) and has a wealth of features making it an ideal choice to show charts on your website.

More »

 

License
RGraph can be used for free under the GPL or if that doesn't suit your situation there's an inexpensive (£99) commercial license available.

More »

 

Download
Get the latest version of RGraph (version 6.17) from the download page. There's also older versions available, minified files and links to cdnjs.com hosted libraries.

More »

The drawing API X-axis object API reference

The x-axis object allows you to have extra X axes in your chart. Formerly this was an independent function but it's now been converted to a drawing api object. This means that it's easier to use and has added interactivity options - such as mousemove and click events and tooltips.

Usage example

View example on CodePen
<script>
   bar = new RGraph.Bar({
        id: 'cvs',
        data: [4,8,6,5,6,9,9,4],
        options: {
            xaxis: false,
            yaxis: false,
            textSize: 14,
            marginBottom: 45,
            colors: ['gradient(white:red)'],
            shadow: false,
            backgroundGridVlines: false,
            backgroundGridBorder: false
        }
    }).draw();
    
    new RGraph.Drawing.XAxis({
        id: 'cvs',
        y: bar.canvas.height - bar.marginBottom,
        options: {
            tooltips: ['The X-axis represents each>br /<person teaching at the college.'],
            xaxisLabels: ['Rich','Alex','Johnny','Kev','Pete','Luis','John','Barry'],
            xaxisTickmarksCount: 8,
            colors: ['#666']
        }
    }).draw();
</script>

Properties

Color properties

Name: colors
Description: 
An array of colors. Even though it's an array only the first color is used.
Default: ['black']

Margin properties

Name: marginLeft
Description: 
The left margin of the axis. This can be the same or different to the chart.
Default: 35
Name: marginRight
Description: 
The right margin of the axis. This can be the same or different to the chart.
Default: 35
Name: marginTop
Description: 
The top margin of the axis.
Default: 35
Name: marginBottom
Description: 
The bottom margin of the axis.
Default: 35
Name: marginInner
Description: 
The (inner) horizontal margin of the axis. Useful for matching up labels/points of the Line chart.
Default:  0

X-axis properties

Name: xaxis
Description: 
Set this to true if you don't want an x-axis.
Default: true
Name: xaxisPosition
Description: 
This determines where the x-axis is positioned. If you wish to show negative values then you should set this to center or you can now (Jul 2016) use an offset x-axis by leaving this property at the default bottom and using the yaxisScaleMax and yaxisScaleMin properties.
Default: bottom
Name: xaxisLinewidth
Description: 
The linewidth used for the x-axis and the x-axis tickmarks.
Default: 1
Name: xaxisColor
Description: 
The color of the x-axis (and its tickmarks.
Default: black
Name: xaxisTickmarks
Description: 
Whether the x-axis tickmarks are shown.
Default: true
Name: xaxisTickmarksLastLeft
Description: 
Whether the last tickmark on the left is displayed.
Default: null
Name: xaxisTickmarksLastRight
Description: 
Whether the last tickmark on the right is displayed.
Default: null
Name: xaxisTickmarksLength
Description: 
The length of the tickmarks.
Default: 3
Name: xaxisTickmarksCount
Description: 
The number of tickmarks that are shown on the x-axis.
Default: null
Name: xaxisLabels
Description: 
An array of the labels to be used on the chart.
Default: An empty array
Name: xaxisLabelsFont
Description: 
The font used to render the labels.
Default: null
Name: xaxisLabelsSize
Description: 
The size of the labels.
Default: null
Name: xaxisLabelsColor
Description: 
The color of the labels.
Default: null
Name: xaxisLabelsBold
Description: 
Whether the labels are bold or not.
Default: null
Name: xaxisLabelsItalic
Description: 
Whether the labels are italic or not.
Default: null
Name: xaxisLabelsOffsetx
Description: 
This allows you finer-grained control in the horizontal direction over the X label positioning if you need it.
Default:  0
Name: xaxisLabelsOffsety
Description: 
This allows you finer-grained control in the vertical direction over the X label positioning if you need it.
Default:  0
Name: xaxisLabelsHalign
Description: 
The horizontal alignment of the labels.
Default: center
Name: xaxisLabelsValign
Description: 
The vertical alignment of the labels.
Default: top
Name: xaxisLabelsAngle
Description: 
The angle of the x-axis labels. For example, you could set this to 45 to get angled labels.
Default: 0 (Horizontal)
Name: xaxisLabelsPosition
Description: 
This property controls how the labels are arranged - section for most chart types and edge for Line charts. You probably won't need to change this.
Default: section
Name: xaxisLabelsClass
Description: 
This property allows you to add your own css class to the x-axis labels which you can then use for styling purposes or to make retrieving the span tags easier (the dom elements). If you inspect the labels in your browser's javascript console (you will first need to enable the textAccessiblePointerevents property) you will be able to see the other css classes that are assigned to the labels.
Default: [none]
Name: xaxisTitle
Description: 
This allows you to specify a title for the x-axis.
Default: none
Name: xaxisTitleSize
Description: 
This allows you to specify a size for the x-axis title.
Default: null
Name: xaxisTitleFont
Description: 
This allows you to specify a font for the x-axis title.
Default: null
Name: xaxisTitleBold
Description: 
This controls whether the x-axis title is bold or not.
Default: null
Name: xaxisTitleItalic
Description: 
This controls whether the x-axis title is italic or not.
Default: null
Name: xaxisTitleColor
Description: 
This controls the color of the x-axis title.
Default: null
Name: xaxisTitleX
Description: 
By giving this you can specifically set the X position of the x-axis title.
Default: null
Name: xaxisTitleY
Description: 
By giving this you can specifically set the Y position of the x-axis title.
Default: null
Name: xaxisTitleOffsetx
Description: 
The X pixel offset that's applied to the title.
Default:  0
Name: xaxisTitleOffsety
Description: 
The Y pixel offset that's applied to the title.
Default:  0
Name: xaxisTitleHalign
Description: 
The horizontal alignment of the title.
Default: center
Name: xaxisTitleValign
Description: 
The vertical alignment of the title.
Default: top
Name: xaxisTitlePos
Description: 
This is a multiplier (ie a digit usually between 0 and 1) that gets multiplied with the margin to get the Y position of the title.
Default: null
Name: xaxisScale
Description: 
If true then an X scale is drawn instead of X labels.
Default: false
Name: xaxisScaleUnitsPre
Description: 
These are units that are prepended to the number.
Default: none
Name: xaxisScaleUnitsPost
Description: 
These are units that are appended to the number.
Default: none
Name: xaxisScaleDecimals
Description: 
The number of decimals used for the X scale. If not specified the scaleDecimals property is used.
Default: null
Name: xaxisScalePoint
Description: 
The character that's used as a decimal point.
Default: .
Name: xaxisScaleThousand
Description: 
The character that's used as the thousand separator.
Default: ,
Name: xaxisScaleFormatter
Description: 
This can be a function, which is passed the object and the number and should return a formatted number (formatted as you wish).
function myFormatter(obj, num)
{
    return num + 'F'; // An example of formatting
}
Default: null
Name: xaxisScaleLabelsCount
Description: 
This is a number representing how many X labels there should be when you're using the X scale.
Default: 10
Name: xaxisScaleMin
Description: 
The minimum x-axis value you wish to set.
Default:  0
Name: xaxisScaleMax
Description: 
The maximum x-axis value you wish to set. For example, if you're displaying products sold in a year, you might use 365.
Default: none - must be supplied if you want to show a scale

Y-axis properties

Name: yaxisPosition
Description: 
This can facilitate when you have a y-axis in the center (eg the Horizontal Bar chart).
Default: left

Other label properties

Name: textColor
Description: 
The color of the text. If not specified then it's the same as the colors option.
Default: null
Name: textFont
Description: 
The font used when rendering the text.
Default: Arial
Name: textSize
Description: 
The size used when rendering text.
Default: 10
Name: textBold
Description: 
Whether the text is bold or not.
Default: 10
Name: textItalic
Description: 
Whether the text is italic or not.
Default: 10
Name: textAccessible
Description: 
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
Name: text
Description: 
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

Interactive features properties

Name: tooltips
Description: 
This can be an array of tooltips (only the first element is used) that are shown when the axis is clicked on.
Default: null
Name: tooltipsEvent
Description: 
This is the event that triggers the tooltips. It can be either click or mousemove.
Default: onclick
Name: tooltipsEffect
Description: 
The effect used for showing tooltips. Can be either fade or none.
Default: fade
Name: tooltipsCssClass
Description: 
This is the name of the css class the chart uses.
Default: RGraph_tooltip
Name: tooltipsOverride
Description: 
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
Name: tooltipsNohideonclear
Description: 
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
Name: tooltipsFormattedPoint
Description: 
When using formatted tooltip strings this is used as the point when using the %{value_formatted} option.
Default: .
Name: tooltipsFormattedThousand
Description: 
When using formatted tooltip strings this is used as the thousand separator when using the %{value_formatted} option.
Default: ,
Name: tooltipsFormattedDecimals
Description: 
When using formatted tooltip strings this specifies the number of decimals when using the %{value_formatted} option.
Default:  0
Name: tooltipsFormattedUnitsPre
Description: 
When using formatted tooltip strings these units are prepended to the number when using the %{value_formatted} option.
Default: (an empty string)
Name: tooltipsFormattedUnitsPost
Description: 
When using formatted tooltip strings these units are appended to the number when using the %{value_formatted} option.
Default: (an empty string)
Name: tooltipsFormattedListType
Description: 
With this property you can switch between an unordered list (the default) and an ordered list. Possible values are ul and ol.
Default: ul
Name: tooltipsFormattedListItems
Description: 
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
Name: tooltipsFormattedTableHeaders
Description: 
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
Name: tooltipsFormattedTableData
Description: 
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)
Name: tooltipsPointer
Description: 
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
Name: tooltipsPointerCss
Description: 
If you want any css values applied to the tooltips pointer (for example, a css border) then specify an object containing those values to this property. For example:
tooltips: {
    borderLeft: 'gray 2px solid',
    borderBottom: 'gray 2px solid'
}
Default: null
Name: tooltipsPointerOffsetx
Description: 
This allows you to adjust the vertical position of the tooltips pointer.
Default:  0
Name: tooltipsPointerOffsety
Description: 
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
Name: tooltipsPositionStatic
Description: 
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
Name: tooltipsCss
Description: 
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
Name: tooltipsOffsetx
Description: 
This property allows you to shift the tooltips left or right.
Default:  0
Name: tooltipsOffsety
Description: 
This property allows you to shift the tooltips up or down.
Default:  0

Miscellaneous properties

Name: highlightStyle
Description: 
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
Name: linewidth
Description: 
The linewidth that's used to draw the axis.
Default: 1

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 x-axis 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 for the axis (usually equivalent to the left margin).
y The Y coordinate for the axis.
width The width of the axis (usually equivalent to the canvas width minus the left and right margins).
height The height of the x-axis (including the label area).
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 x-axis 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>
    xaxis.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 x-axis 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.XAxis({
    id: 'cvs',
    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.