« Previous       Home page demos       Next »

An SVG Line with a basic look and feel

Here is a simple Line that has three seperate datasets. The labels for the graph (on the X axis) is not a scale - it's just regular labels as you would normally give to a Line but they're numbers instead of words - that's all. The Line doesn't have the facility to show a scale across the X axis - for that you'll need to use the Scatter graph with the line option enabled.

As for the tickmarks, while at first glance it may appear that they're 'hollow' they're actually just using a fill colour that matches the background.

Other than this is a pretty basic dark-themed Line that uses the trace() effect.

This goes in the documents header:
<script src="RGraph.svg.common.core.js"></script>
<script src="RGraph.svg.line.js"></script>
Put this where you want the chart to show up:
<div id="cc" style="width:700px; height: 300px"></div>
This is the code that generates the chart:
<script>
    var line = new RGraph.SVG.Line({
        id: 'cc',
        data: [
            [9,9,0,9,0],
            [6,7,10,5,0],
            [9,4,0,3,4]
        ],
        options: {
            colors: ['#cc0','blue','red'],
            xaxis: false,
            yaxis: false,
            linewidth: 3,
            backgroundGridColor: '#999',
            xaxisLabels: ['5.0','6.0','7.0','8.0','9.0'],
            textColor: '#ccc',
            tickmarksStyle: 'circle',
            tickmarksFill: 'black',
            tickmarksLinewidth: 3
        }
    }).trace();
</script>

How to get transparent tickmarks using the canvas Line graph

[No canvas support] [No canvas support]

You may have noticed that on the graph above the center of the tickmarks are not transparent - they're just colored black in order to make them blend in with the rest of the graph.

Making them transparent is more complicated than you might think for these reasons:

The solution here is to use layers. "But wait! Canvas doesn't have layers!" you might cry. And yes - you would be correct - but they can be simulated by using - in this case - multiple canvas tags.

So there's two canvas tags being utilised here (positioned one in front of the other) - one on to which the background is drawn and a second on to which the lines are drawn. When the inside of the tickmarks are cleared to transparency then the canvas at the back shows through. And that is the canvas that has the background grid on it.

There's more code required than a regular chart (as you might imagine) - but as you can see it's not a lot more and no is it complicated. Feel free to copy and paste the code.


This goes in the documents header:
<script src="RGraph.common.core.js"></script>
<script src="RGraph.line.js"></script>
Put this where you want the chart to show up:
<div style="position: relative; background-color: black; width: 750px; height: 250px">
    <canvas id="cvs1" width="750" height="250" style="position: absolute; top: 0; left: 0">[No canvas support]</canvas>
    <canvas id="cvs2" width="750" height="250" style="position: absolute; top: 0; left: 0">[No canvas support]</canvas>
</div>
Th is is the code that generates the chart:
<script>
    line1 = new RGraph.Line({
        id: 'cvs1',
        data: [null,null,null,null,null],
        options: {
            axes: false,
            backgroundGridColor: '#999',
            yaxisLabels: false
        }
    }).draw();


    line2 = new RGraph.Line({
        id: 'cvs2',
        data: [
            [9,9,0,9,0],
            [6,7,10,5,0],
            [9,4,0,3,4]
        ],
        options: {
            colors: ['#cc0','blue','red'],
            axes: false,
            linewidth: 3,
            backgroundGrid: false,
            xaxisLabels: ['5.0','6.0','7.0','8.0','9.0'],
            textColor: '#ccc',
            tickmarksSize: 6,
            tickmarksStyle: function (obj, data, value, index, x, y, color, prevX, prevY)
            {
                // Draw the fill of the tickmark
                RGraph.path2(obj.context,
                    'gco destination-out b a % % % 0 6.29 false f red s % gco source-over',
                    x,
                    y,
                    obj.get('tickmarksSize') - 1,
                    color
                );

                // This bit draws the ourline of the tickmark
                RGraph.path2(obj.context,
                    'b lw 2 a % % % 0 6.29 false s %',
                    x,
                    y,
                    obj.get('tickmarksSize'),
                    color
                );
            },
            tickmarkSize: 6
        }
    }).draw();
</script>