Getting started with RGraph
Written by Richard Heyes, RGraph author, on 15th November 2026- Introduction
- 1. A basic chart
- 2. Adding labels and configuring the text
- 3. Changing the colors and the margins
- 4. Sorting the data
- 5. Adding tooltips to your chart
- 6. Integrating with the Google Sheets connector
- 7. Integrating with the CSV connector
- 8. Integrating with the HTML table connector
- 9. Integrating with the AJAX functions
- 10. Making the charts adjustable
- 11. Supporting different screen sizes with the responsive option
- Conclusion
Introduction
RGraph is a very versatile charting library that supports lots of different types of charts and has lots of different options for those charts.
Because of this, it might be a little overwhelming to you at first. It really isn't though! There's simply a lot of options and features available to you because it supports lots of things! Getting yourself up-and-running with a chart (for example a Bar chart) is actually very simple.
In this guide we'll look at getting examples of the canvas and SVG Bar chart up-and-running and showing data. We'll also look at integrating the chart with the CSV connector, the Google Sheets connector, the HTML table connector and the AJAX functions that allow you to pull in data from external sources. Then we'll have a look at using adjusting to make your charts interactive and finally the responsive option that allows you to accommodate different screen sizes.
The Library files
The RGraph library files are the same for all the examples and can be
included once
at the top of the page regardless of how many charts that you
create on the page. So here they are - ideally you would include
these in the <head> section of your page though as long as
they're included before the chart is created you could very well
include them in the body.
<!-- Canvas charts libraries --> <script src="RGraph.common.core.js"></script> <script src="RGraph.common.key.js"></script> <script src="RGraph.common.dynamic.js"></script> <script src="RGraph.common.tooltips.js"></script> <script src="RGraph.common.csv.js"></script> <script src="RGraph.common.sheets.js"></script> <script src="RGraph.common.table.js"></script> <script src="RGraph.bar.js"></script> <!-- SVG charts libraries --> <script src="RGraph.svg.common.core.js"></script> <script src="RGraph.svg.common.key.js"></script> <script src="RGraph.svg.common.tooltips.js"></script> <script src="RGraph.svg.common.csv.js"></script> <script src="RGraph.svg.common.sheets.js"></script> <script src="RGraph.svg.common.table.js"></script> <script src="RGraph.svg.common.ajax.js"></script> <script src="RGraph.svg.bar.js"></script>
1. A basic chart
So lets begin with a basic Bar chart. There's nothing to this chart - it just uses the default values for the margins and colors and, as you can see it looks quite reasonable. One minor difference between the canvas and SVG charts is the corner rounding - this is supported by the canvas charts but not by the SVG charts.
CANVAS
<div style="text-align: center">
<canvas id="cvs1" width="750" height="350">[No canvas support]</canvas>
</div>
<script>
new RGraph.Bar({
id: 'cvs1',
data: [4,8,6,5,3,4,8],
options: {
}
}).draw();
</script>
SVG
<div style="text-align: center">
<div id="cc1" style="width: 750px; height: 350px"></div>
</div>
<script>
new RGraph.SVG.Bar({
id: 'cc1',
data: [4,8,6,5,3,4,8],
options: {
}
}).draw();
</script>
2. Adding labels and configuring the text
The first thing that you'll most likely want to do is add some labels to your chart so that your viewers can make sense of it and understand what's being represented by the bars. The labels can be given in a few formats. You can either specify an array of strings (as is done below) in which case each string is the whole label.
Or alternatively you can give a single string that contains formatting macros. These are cut down versions of the macros that are available to tooltips but are still very useful and allow you to dynamically build up your labels from whatever source your labels are coming from (for example, you could have a function that returns the label and do whatever you need to do inside that function).
Properties used in this example:
-
xaxisLabels
This property allows you to specify labels for the chart that appear under the bars. You can use a limited set of formatting macros in your labels, meaning that you could easily specify a function that returns the correct label for a point and add your own logic to determine the actual text to use.
-
textColor
The color of the text on the chart. There are various properties that determine the color of the various bits of text (eg X axis labels, Y axis labels, title, subtitle, above labels etc) but they all fall back to this property if they're not specified.
-
textSize
Similar to the above property, there are various text size properties that are available to you but they all fall back to this one.
-
title
This is the main title for the chart. There are various title configuration properties that go along with it as well as various subtitle properties too.
CANVAS
<div style="text-align: center">
<canvas id="cvs2" width="750" height="350">[No canvas support]</canvas>
</div>
<script>
new RGraph.Bar({
id: 'cvs2',
data: [4,8,6,5,3,4,8],
options: {
xaxisLabels: ['Alf','Bert','Chip','Dug','Elgar','Flo','Gary'],
textColor: 'black',
textSize: 16,
title: 'Friday race results'
}
}).draw();
</script>
SVG
<div style="text-align: center">
<div id="cc2" style="width: 750px; height: 350px"></div>
</div>
<script>
new RGraph.SVG.Bar({
id: 'cc2',
data: [4,8,6,5,3,4,8],
options: {
xaxisLabels: ['Alf','Bert','Chip','Dug','Elgar','Flo','Gary'],
textColor: 'black',
textSize: 16,
title: 'Friday race results'
}
}).draw();
</script>
3. Changing the colors and the margins
Probably one of the first things you'll want to do is change the colors of your charts. On the Bar chart (with most types of chart in fact) this can be done with the colors property. By default, the colors on the Bar chart are used in a "per group/stack" style but you can change that (as done here) with the colorsSequential property. Then, the colors will be used in the order that you specify them in.
Changing the margins of the chart allows you to give more space around the sides of the chart - where the title, the scale and the labels sit. If you've increased the size of the text then you might need to also increase these margin sizes or the text will be cut off by the edges of the canvas or SVG area. Note: In the examples below a border has been added to the canvas and div (the SVG container) tags to better illustrate the increased margin sizes.
Properties used in this example:
-
colorsSequential
For the Bar chart (along with other types of chart too) the default way of using the colors is "per dataset" (or group). This means that the colors are used on a per group (or per stack for stacked charts) basis. The colorsSequential property changes that, however, and forces the Bar chart to just use the colors sequentially - one after the other. You can see the effect that this has by looking at the charts below.
-
marginLeft
This is the laft margin of the chart, that the Y axis labels sit in. If your scale is large or your text size is large you might need to increase this. The default value for margins on most charts is 35.
-
marginBottom
This is the bottom margin of the chart, that the X axis labels sit in. If your text size is large or your labels contain carriage returns you might need to increase this. The default value for margins on most charts is 35.
CANVAS
<div style="text-align: center">
<canvas id="cvs3" width="750" height="350" style="border: 1px solid #aaa">[No canvas support]</canvas>
</div>
<script>
new RGraph.Bar({
id: 'cvs3',
data: [4,8,6,5,3,4,8],
options: {
xaxisLabels: ['Alf','Bert','Chip','Dug','Elgar','Flo','Gary'],
textColor: 'black',
textSize: 16,
title: 'Friday race results',
colorsSequential: true,
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50
}
}).draw();
</script>
SVG
<div style="text-align: center">
<div id="cc3" style="border: 1px solid #aaa; width: 750px; height: 350px"></div>
</div>
<script>
new RGraph.SVG.Bar({
id: 'cc3',
data: [4,8,6,5,3,4,8],
options: {
xaxisLabels: ['Alf','Bert','Chip','Dug','Elgar','Flo','Gary'],
textColor: 'black',
textSize: 16,
title: 'Friday race results',
colorsSequential: true,
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50
}
}).draw();
</script>
4. Sorting the data
Now we come on to an interesting topic - sorting the data that's displayed on the chart. Unfortunately, this isn't as easy as simply calling the sort() function on the data array, the labels array and the tooltips array - which could be done quite easily if there weren't actually any labels or tooltips. However, when you do that you end up losing the association between value and label (and tooltip) (ie they end up being in different orders so the labels and/or tooltips would be out-of-place).
So for that reason we'll first have to change how the data, labels and tooltips are initially stored.
It's going to be changed to an array of objects - one object for each datapoint. Each of those objects will hold the datapoint, the label and the tooltip (which we'll use in the next step). Here's the new data array and the code that extracts it into suitable arrays/objects for passing to the Bar chart object:
// An array of objects that contain the data for the chart. Each // object contains the datapoint, the label and the tooltip. The // tooltips could easily use a template string - and we'll come // on to that later. data = [ {value: 4, label: 'Alf', tooltip: 'Result: 4%'}, {value: 8, label: 'Bert', tooltip: 'Result: 8%'}, {value: 6, label: 'Chip', tooltip: 'Result: 6%'}, {value: 5, label: 'Dug', tooltip: 'Result: 5%'}, {value: 3, label: 'Elgar',tooltip: 'Result: 3%'}, {value: 4, label: 'Flo', tooltip: 'Result: 4%'}, {value: 8, label: 'Gary', tooltip: 'Result: 8%'} ]; // Create some arrays that will hold the information that we'll pass // to the chart. values = []; labels = []; tooltips = []; // Loop through the data array and separate it into the individual arrays: for (var i=0; i<data.length; ++i) { values.push(data[i].value); labels.push(data[i].label); tooltips.push(data[i].tooltip); }
Now we have the data separated out into the individual arrays that will be passed to the Bar chart. However, the whole point of this step is to sort the data before it's displayed.
So before it's separated out into the arrays, we can sort the data using a custom sort function that we'll use in conjuction with the built-in JavaScript sort() function like this:
// An array of objects that contain the data for the chart. Each // object contains the datapoint, the label and the tooltip. The // tooltips could easily use a single template string though - and // we'll come on to that later. data = [ {value: 4, label: 'Alf', tooltip: 'Result: 4%'}, {value: 8, label: 'Bert', tooltip: 'Result: 8%'}, {value: 6, label: 'Chip', tooltip: 'Result: 6%'}, {value: 5, label: 'Dug', tooltip: 'Result: 5%'}, {value: 3, label: 'Elgar',tooltip: 'Result: 3%'}, {value: 4, label: 'Flo', tooltip: 'Result: 4%'}, {value: 8, label: 'Gary', tooltip: 'Result: 8%'} ]; // Sort the data using a custom sort function and the JavaScript // sort() function. data.sort(function (a, b) { return a.value - b.value; }); // Create some arrays that will hold the information that we'll pass // to the chart. values = []; labels = []; tooltips = []; // Loop through the data array and separate it into the individual arrays: for (var i=0; i<data.length; ++i) { values.push(data[i].value); labels.push(data[i].label); tooltips.push(data[i].tooltip); }
CANVAS
<div style="text-align: center">
<canvas id="cvs4" width="750" height="350">[No canvas support]</canvas>
</div>
<script>
data = [
{value: 4, label: 'Alf', tooltip: 'Result: 4%'},
{value: 8, label: 'Bert', tooltip: 'Result: 8%'},
{value: 6, label: 'Chip', tooltip: 'Result: 6%'},
{value: 5, label: 'Dug', tooltip: 'Result: 5%'},
{value: 3, label: 'Elgar',tooltip: 'Result: 3%'},
{value: 4, label: 'Flo', tooltip: 'Result: 4%'},
{value: 8, label: 'Gary', tooltip: 'Result: 8%'}
];
data.sort(function (a, b)
{
return b.value - a.value;
});
values = [];
labels = [];
tooltips = [];
for (var i=0; i<data.length; ++i) {
values.push(data[i].value);
labels.push(data[i].label);
tooltips.push(data[i].tooltip);
}
new RGraph.Bar({
id: 'cvs4',
data: values,
options: {
xaxisLabels: labels,
textColor: 'black',
textSize: 16,
title: 'Friday race results',
colorsSequential: true,
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50
}
}).draw();
</script>
SVG
<div style="text-align: center">
<div id="cc4" style="width: 750px; height: 350px"></div>
</div>
<script>
data = [
{value: 4, label: 'Alf', tooltip: 'Result: 4%'},
{value: 8, label: 'Bert', tooltip: 'Result: 8%'},
{value: 6, label: 'Chip', tooltip: 'Result: 6%'},
{value: 5, label: 'Dug', tooltip: 'Result: 5%'},
{value: 3, label: 'Elgar',tooltip: 'Result: 3%'},
{value: 4, label: 'Flo', tooltip: 'Result: 4%'},
{value: 8, label: 'Gary', tooltip: 'Result: 8%'}
];
data.sort(function (a, b)
{
return b.value - a.value;
});
values = [];
labels = [];
tooltips = [];
for (var i=0; i<data.length; ++i) {
values.push(data[i].value);
labels.push(data[i].label);
tooltips.push(data[i].tooltip);
}
new RGraph.SVG.Bar({
id: 'cc4',
data: values,
options: {
xaxisLabels: labels,
textColor: 'black',
textSize: 16,
title: 'Friday race results',
colorsSequential: true,
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50
}
}).draw();
</script>
5. Adding tooltips to your chart
Now we come on to one of, if not the, most useful and most important features of the Bar chart (of any of the supporting charts in fact) - and that's tooltips.
Tooltips are, at the most basic level, <div> tags that are shown next to a bar (or point, or segment etc) when you click on it (could also be a hover - depends on the type of chart). They show anything that can sit inside a <div> - so text, images, video etc. They can even show more charts and that's mentioned in the documentation here.
They have the ability to transform a pretty average chart into one that really conveys a lot of information so they certainly shouldn't be overlooked.
So how do you specify tooltips? At the most basic level they can just be given in your chart configuration as an array of strings like this:
tooltips: ['Fred','Burt','Lucy','Peter','Jim','Hoolio','Dave']
Nice and simple - just a typical array of strings. Not terribly dynamic, but if your information is coming from a database then this may suffice.
You can also specify a function instead of an array of strings which is called when the tooltip is about to be shown and it can return the relevant text to show in the tooltip. The function is given the relevant index of the tooltip (which starts at zero). You can either give a single function or an array of different functions - one for each tooltip. Specifying a single function looks like this:
tooltips: function (index)
{
console.log(index);
}
Note that the function may actually be called multiple times when a tooltip is shown - so don't rely on it just being called once.
Another way of defining tooltips - and the easiest and most powerful in my opinion - is to use a single string which contains formatting macros. These macros are expanded to the relevant information when the tooltip is shown. This makes it easy to have unique, dynamic tooltips that are an easily managed, single string in the source code. These might look like this in the code:
tooltips: '<b>Results for %{property:xaxisLabels[%{dataset}]}: %{value}%</b>'
And when the tooltip is shown the text might look like this:
Results for Richard: 5%
There are a number of different macros that are available to you and you can see a reference of them all on the tooltips documentation page.
Finally, should you want to handle everything yourself and create and show your own tooltip - or maybe do something else entirely - then you do that with the tooltipsOverride property. This allows you to specify a function which is called when a tooltip is going to be shown. This is just a regular JavaScript function so it can do whatever you want it to. It would look like this:
tooltipsOverride: function (obj, text, x, y, idx)
{
alert('In the tooltip override function');
}
Properties used in this example:
-
tooltips
Allows you to specify a set of popup tooltips for each entry on the chart. Tooltips may contain a multitude of HTML including text, images and video etc. As a result they can really add a lot of information and usefulness to your chart.
CANVAS
<div style="text-align: center">
<canvas id="cvs5" width="750" height="350">[No canvas support]</canvas>
</div>
<script>
new RGraph.Bar({
id: 'cvs5',
data: [4,8,6,5,3,4,8],
options: {
xaxisLabels: ['Alf','Bert','Chip','Dug','Elgar','Flo','Gary'],
textColor: 'black',
textSize: 16,
title: 'Friday race results',
colorsSequential: true,
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50,
tooltips: '<b>Results for %{property:xaxisLabels[%{dataset}]}: %{value}%</b>'
}
}).draw();
</script>
SVG
<div style="text-align: center">
<div id="cc5" style="width: 750px; height: 350px"></div>
</div>
<script>
new RGraph.SVG.Bar({
id: 'cc5',
data: [4,8,6,5,3,4,8],
options: {
xaxisLabels: ['Alf','Bert','Chip','Dug','Elgar','Flo','Gary'],
textColor: 'black',
textSize: 16,
title: 'Friday race results',
colorsSequential: true,
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50,
tooltips: '<b>Results for %{property:xaxisLabels[%{dataset}]}: %{value}%</b>'
}
}).draw();
</script>
The full documentation page for the tooltips, which you can use whether you're using canvas or SVG charts, can be found here: the tooltips documentation page.
6. Integrating with the Google Sheets connector
Google Sheets is an extremely capable online spreadsheet application that's part of the Google Apps online offerings from Google. Not wanting to be a Google fanboy, but Google Apps could provide you with a way to improve your collaboration between your companies employees and also your customers.
Google Sheets provides a JSON-based feed of your spreadsheet which you can fetch with an AJAX request and then use the data to build charts with. Thereby providing a real-time view of data that's held in a Google Sheets spreadsheet.
This might be a bit messy normally - but enter the RGraph Google Sheets connector! This is a file that you include into your HTML page which then allows you to very easily request the data from Google Sheets and display it in your page - in this case building a chart with it.
There is a bit of setup required - the relevant section in the documentation is here. These are the steps that are necessary:
- Include the RGraph Google Sheets library.
- Create a spreadsheet in the Google Sheets application.
- Get an OAuth authentication key.
- Publish your spreadsheet to the web.
- Allow access to the spreadsheet.
- Get the data from your spreadsheet with the RGraph Sheets connector API.
If that seems like a lot of steps just take a look at the link to the Google Sheets documentation page above where it takes you through each step.
Fetching the data from your spreadsheet is then done with the get() method and you can fetch a single cell, a column of cells, a row of cells or a whole matrix of cells (a 2D array). This is detailed in the Fetching data from your Google Sheet section. Remember that you might have to convert your values to numbers (with the parseInt() or parseFloat() functions) before they can be used with RGraph.
There are demos of pages that use the Google Sheets connector in the download archive and these are set up to use a public spreadsheet so they'll run straight away.
CANVAS
<div style="text-align: center">
<canvas id="cvs6" width="750" height="350">[No canvas support]</canvas>
</div>
<script>
new RGraph.Sheets(
'AIzaSyBPofvjcDhOdet_U2Tr4-rSLItAgaCsMCM',
'1ncvARBgXaDjzuca9i7Jyep6JTv9kms-bbIzyAxbaT0E',
'Bar chart',
function (sheet)
{
var data = sheet.get('B2:B13');
var labels = sheet.get('A2:A13');
for (var i=0; i<labels.length; i+=2) {
labels[i] = "\n" + labels[i];
}
new RGraph.Bar({
id: 'cvs6',
data: data,
options: {
xaxisLabels: labels,
textColor: 'black',
textSize: 16,
title: 'Friday race results',
colorsSequential: true,
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50
}
}).draw();
});
</script>
SVG
<div style="text-align: center">
<div id="cc6" style="width: 750px; height: 350px"></div>
</div>
<script>
new RGraph.SVG.Sheets(
'AIzaSyBPofvjcDhOdet_U2Tr4-rSLItAgaCsMCM',
'1ncvARBgXaDjzuca9i7Jyep6JTv9kms-bbIzyAxbaT0E',
'Bar chart',
function (sheet)
{
var data = sheet.get('B2:B13');
var labels = sheet.get('A2:A13');
for (var i=0; i<labels.length; i+=2) {
labels[i] = "\n" + labels[i];
}
new RGraph.SVG.Bar({
id: 'cc6',
data: data,
options: {
xaxisLabels: labels,
textColor: 'black',
textSize: 16,
title: 'Friday race results',
colorsSequential: true,
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50
}
}).draw();
});
</script>
7. Integrating with the CSV connector
CSV files are a very easy (albeit basic) way of storing your data. Perhaps you have legacy systems that export their data to a CSV format and you can't change that.
The CSV import utility allows you to easily read a CSV file, extract the data from it that you need and then use that data to build your charts with.
Or what you could also do is use it as a way to get your data from an external script. Have that script output the data to CSV format and then read the data with the CSV reader and produce your chart(s).
Like the Google Sheets tool there's a small set of functions that are available to you that allow you to fetch data from the CSV file and these are:
- row(index[, start = 0[, length = null]]) - Allows you to fetch a row of data. The optional start and length arguments allow you to control how much data you fetch from the file.
- col(index[, start = 0[, length = null]]) - Allows you to fetch a column of data. Like the row() function the optional start and length arguments give you control over how much and what data you fetch from the CSV file.
That's pretty much all there is to fetching data from a CSV - but one thing that's also worthy of mention is the ability of the CSV reader to read a <div> tag as well as a standalone CSV file.
This means that you can use the CSV reader utility to read CSV data that's embedded in your page - in a hidden <div> tag - and use that data to build charts with. This might be another avenue of integration with server-side systems that you could explore as a way of getting data into your charts.
You can read the full CSV import utility documentation here. This documentation applies to both the canvas and the SVG CSV import utilities.
CANVAS
<div style="text-align: center">
<canvas id="cvs7" width="750" height="350">[No canvas support]</canvas>
</div>
<script>
RGraph.CSV('/sample.csv', function (csv)
{
var data = csv.row(0,1,-1);
var labels = csv.column(0);
new RGraph.Bar({
id: 'cvs7',
data: data,
options: {
xaxisLabels: labels,
textColor: 'black',
textSize: 16,
title: 'Friday race results',
colorsSequential: true,
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50
}
}).draw();
});
</script>
SVG
<div style="text-align: center">
<div id="cc7" style="width: 750px; height: 350px"></div>
</div>
<script>
RGraph.SVG.CSV('/sample.csv', function (csv)
{
var data = csv.row(0,1,-1);
var labels = csv.column(0);
new RGraph.SVG.Bar({
id: 'cc7',
data: data,
options: {
xaxisLabels: labels,
textColor: 'black',
textSize: 16,
title: 'Friday race results',
colorsSequential: true,
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50
}
}).draw();
});
</script>
8. Integrating with the HTML table connector
The HTML table connector that's bundled with RGraph is a very useful way to get data from the page which is formatted as part of a table. Maybe the data is already in the page - in a table - and you're adding a chart to the page - then this is a very easy way to get data from the table without adding any more server-side logic.
It reads the table by reading the DOM and provides a small API that you can then use to get hold of the data. You can then use the data to build your chart as with any other method of making charts. The API methods are:
Functions:- row(index[, start = 0[, length = null]]) - Allows you to fetch a row of data. The optional start and length arguments allow you to control how much data you fetch from the file.
- column(index[, start = 0[, length = null]]) - Allows you to fetch a column of data. Like the row() function the optional start and length arguments allow you control over how much and what data you fetch from the CSV file.
- numcols - Reports the number of columns in the table.
- numrows - Reports the number of rows in the table.
And you can read more about the API on the HTML table documentation page. This page serves both the canvas and SVG versions of the import utility.
You can use the table connector without RGraph if you wish - meaning that you can use it in your own code, without RGraph, should you wish to.
You can also supply the table as a string, instead of the id of the table, if that's easier for you. Read more about that here.
| Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday | |
|---|---|---|---|---|---|---|---|
| Number of Racers | 29 | 35 | 43 | 3 | 46 | 12 | 25 |
| Average result | 4 | 6 | 8 | 6 | 9 | 4 | 7 |
CANVAS
<div style="text-align: center">
<canvas id="cvs8" width="750" height="350">[No canvas support]</canvas>
</div>
<script>
new RGraph.HTMLTable('racing_results', function (table)
{
var labels = table.row(0, 1),
racers = table.row(1, 1),
average = table.row(2, 1);
// Combine the racers and average arrays into the
// format RGraph expects for a grouped Bar chart.
var data = [];
for (var i=0; i<racers.length; ++i) {
data.push([
racers[i],
average[i]
]);
}
for (var i=1; i<labels.length; i+=2) {
labels[i] = "\n" + labels[i];
}
new RGraph.Bar({
id: 'cvs8',
data: data,
options: {
key: ['Number of racers','Average result'],
keyPosition: 'margin',
keyLabelsSize: 12,
keyLabelsOffsety: -1,
xaxisLabels: labels,
textColor: 'black',
textSize: 16,
title: 'Friday race results',
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50,
colors: ['orange','gray']
}
}).draw();
});
</script>
SVG
<div style="text-align: center">
<div id="cc8" style="width: 750px; height: 350px"></div>
</div>
<script>
new RGraph.SVG.HTMLTable('racing_results', function (table)
{
var labels = table.row(0, 1),
racers = table.row(1, 1),
average = table.row(2, 1);
// Combine the racers and average arrays into the
// format RGraph expects for a grouped Bar chart.
var data = [];
for (var i=0; i<racers.length; ++i) {
data.push([
racers[i],
average[i]
]);
}
for (var i=1; i<labels.length; i+=2) {
labels[i] = "\n" + labels[i];
}
new RGraph.SVG.Bar({
id: 'cc8',
data: data,
options: {
key: ['Number of racers','Average result'],
keyPosition: 'margin',
keyLabelsSize: 12,
keyLabelsOffsety: -1,
xaxisLabels: labels,
textColor: 'black',
textSize: 16,
title: 'Friday race results',
marginLeft: 50,
marginRight: 50,
marginBottom: 75,
marginTop: 50,
colors: ['orange','gray']
}
}).draw();
});
</script>
9. Integrating with the AJAX functions
The RGraph AJAX functions allow you to fetch data from other pages on your website (due to browser restrictions, cross-domain AJAX is not permitted by default).
These pages could be just scripts that return a comma-separated list of values therefore allowing you to have a server-side script that outputs just the data and your frontend page, that your users load, separate.
There are various functions available to you that help you to fetch specific types of data (they do the necessary string/number/array conversions for you) and they're all documented on the canvas AJAX documentation page and the SVG AJAX documentation page.
AJAX functions:
| RGraph.AJAX(url, callback) RGraph.SVG.AJAX(url, callback) |
The base AJAX function. This returns the raw response without doing any parsing or filtering. |
| RGraph.AJAX.post(url, data, callback) RGraph.SVG.AJAX.post(url, data, callback) |
Sends an HTTP POST request to the server instead of a regular GET request. Using a POST request allows you to easily send data TO the server. |
| RGraph.AJAX.number(url, callback) RGraph.SVG.AJAX.number(url, callback) |
Fetches an AJAX resource and then automatically converts it to a JavaScript number type. |
| RGraph.AJAX.string(url, callback) RGraph.SVG.AJAX.string(url, callback) |
Fetches an AJAX resource and then automatically converts it to a JavaScript string type. |
| RGraph.AJAX.json(url, callback) RGraph.SVG.AJAX.json(url, callback) |
Fetches an AJAX resource and then automatically converts it to JavaScript JSON. JSON is an easy way to return multiple bits of data. |
| RGraph.AJAX.csv(url, callback[, separator = ","]) RGraph.SVG.AJAX.csv(url, callback[, separator = ","[, lineend = "\r?\n"]]) |
Fetches an AJAX resource and then automatically treats it as CSV data and converts it to an array. CSV is an easy way to return an entire dataset. |
CANVAS
<div style="text-align: center">
<canvas id="cvs9" width="750" height="350">[No canvas support]</canvas>
</div>
<script>
RGraph.AJAX.string('/sample.csv', function (csv)
{
// Get the first line of data and use that
csv = csv.split(/\n/);
csv = csv[0];
csv = csv.split(/,/);
csv = csv.slice(1);
var data = csv;
new RGraph.Bar({
id: 'cvs9',
data: data,
options: {
xaxisLabels: ['Fred','Lucy','Pob','Jane','Kevin','Jimmy','Gary'],
textColor: 'black',
textSize: 16,
title: 'Friday race results',
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50,
colors: ['orange','gray']
}
}).draw();
});
</script>
SVG
<div style="text-align: center">
<div id="cc9" style="width: 750px; height: 350px"></div>
</div>
<script>
RGraph.SVG.AJAX.string('/sample.csv', function (csv)
{
// Get the first line of data and use that
csv = csv.split(/\n/);
csv = csv[0];
csv = csv.split(/,/);
csv = csv.slice(1);
var data = csv;
new RGraph.SVG.Bar({
id: 'cvs9',
data: data,
options: {
xaxisLabels: ['Fred','Lucy','Pob','Jane','Kevin','Jimmy','Gary'],
textColor: 'black',
textSize: 16,
title: 'Friday race results',
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50,
colors: ['orange','gray']
}
}).draw();
});
</script>
10. Making the charts adjustable
RGraph enables you to make interactive dashboards that contain dynamic gauges, progress bars, meters and charts. And all you have to do is include an extra file (for canvas charts - no extra file needed with SVG charts) and add an extra parameter to your charts configuration!
There are custom events that are associated with adjusting and these allow you to integrate adjusting closely with your own processes. For example, you could use a bank of Gauges to control your physical devices - maybe even have some Gauges or a Bar chart that control a nuclear reactor (if you do do that I'd really love to know about it...!).
To add the control to your chart, you would need to use the events in conjunction with the AJAX functions to send information back to your server, at which point your server-based programs can take that data and do whatever they need to with it.
Various charts are adjustable in RGraph and you can see a list of the various canvas types on the Adjustable charts documentation page. The list of adjustable SVG charts is:
- Activity
- Bar
- Gauge
- Horseshoe
- Segmented
- Vertical progress bar (Bar chart)
CANVAS
<div style="text-align: center">
<canvas id="cvs10" width="750" height="350">[No canvas support]</canvas>
</div>
<script>
new RGraph.Bar({
id: 'cvs10',
data: [4,8,6,5,3,4,8],
options: {
xaxisLabels: ['Alf','Bert','Chip','Dug','Elgar','Flo','Gary'],
textColor: 'black',
textSize: 16,
title: 'Friday race results',
colorsSequential: true,
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50,
adjustable: true,
events: {
adjustend: function (obj)
{
alert('Updated data: ' + obj.data);
}
}
}
}).draw();
</script>
SVG
<div style="text-align: center">
<div id="cc10" style="width: 750px; height: 350px"></div>
</div>
<script>
new RGraph.SVG.Bar({
id: 'cc10',
data: [4,8,6,5,3,4,8],
options: {
xaxisLabels: ['Alf','Bert','Chip','Dug','Elgar','Flo','Gary'],
textColor: 'black',
textSize: 16,
title: 'Friday race results',
colorsSequential: true,
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50,
adjustable: true,
events: {
adjustend: function (obj)
{
alert('Updated data: ' + obj.data);
}
}
}
}).draw();
</script>
11. Supporting different screen sizes with the responsive option
The final item that we're going to cover in this getting started guide is responsiveness. This means configuring your chart so that it accommodates multiple screen sizes.
This could be as simple as changing the size of the canvas (or the SVG) when the size of the screen changes. However, if you do this then you'll probably also want to change various configuration options - such as changing the X-axis labels on a Bar chart to be angled instead of horizontal so that they take up less horizontal space.
Currently, the only condition that can be made is the width of the screen - so the correct configuration to use for the chart is chosen based on the width and just the width.
This, however, covers most needs. Previously, the window.onresize event was used to perform the checks, however, this was later changed to be dynamically installed media queries that are installed using JavaScript.
You can do various things with each media query that you setup (you can have multiple rules, each based on a particular screen width) such as change the width and height of the canvas (or SVG) tag, set or change whatever properties you want to on the chart, set CSS properties on the canvas (or SVG) tag, set or change CSS properties on the container of the canvas (or SVG) tag and run a JavaScript callback function if required.
As well as the two examples below you can find examples of responsive charts in the demos folder of the download archive.
CANVAS
<div style="text-align: center">
<canvas id="cvs11" width="750" height="350">[No canvas support]</canvas>
</div>
<script>
new RGraph.Bar({
id: 'cvs11',
data: [4,8,6,5,3,4,8],
options: {
xaxisLabels: ['Alf','Bert','Chip','Dug','Elgar','Flo','Gary'],
textColor: 'black',
textSize: 16,
title: 'Friday race results',
colorsSequential: true,
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50,
responsive: [
{maxWidth: null, width: 750, height: 300, options: {textSize: 14, marginInner: 25, colorsSequential: true, corners: 'round'}},
{maxWidth: 900, width: 400, height: 200, options: {textSize: 10, marginInner: 5, colorsSequential: false, corners: 'square'}}
]
}
}).draw();
</script>
SVG
<div style="text-align: center">
<div id="cc11" style="width: 750px; height: 350px"></div>
</div>
<script>
new RGraph.SVG.Bar({
id: 'cc11',
data: [4,8,6,5,3,4,8],
options: {
xaxisLabels: ['Alf','Bert','Chip','Dug','Elgar','Flo','Gary'],
textColor: 'black',
textSize: 16,
title: 'Friday race results',
colorsSequential: true,
marginLeft: 50,
marginRight: 50,
marginBottom: 50,
marginTop: 50,
responsive: [
{maxWidth: null, width: 750, height: 300, options: {textSize: 14, marginInner: 25, colorsSequential: true}},
{maxWidth: 900, width: 400, height: 200, options: {textSize: 10, marginInner: 5, colorsSequential: false}}
]
}
}).draw();
</script>
Conclusion
RGraph is very powerful! This was just a short introduction to, what I think, are some of the more useful features that RGraph supports. A bigger list of features for the canvas charts is here and you can find the SVG documentation here.
There are, of course, plenty of other chart types that you can play with including all the favourites - like Line charts and Pie charts and also great components like dynamic Tree structures and the datagrid component.
So have fun exploring RGraph and take time to go through the demos that are included in the download archive. Not all of those demos are intended as capability demonstrations - some lean more towards being test pages - but these are obvious and all of the demos will function as soon as you load them into your browser with no configuration necessary.
Enjoy! Download RGraph