A 3D Line chart demonstration
Written by Richard Heyes, RGraph author, on 13th June 2021A new demonstration of a 3D Line chart. You can find this demo file in the demos folder of the next release of RGraph.
Here's a novel new demonstration of a chart: A 3D Line chart with multiple datasets. This is not a
natively supported type of chart though the code to create it, whilst big, is not complicated
and it's fully annotated so you should find it easy to follow.
The next release will feature it as part of the demos that come with RGraph which are located
in the demos/ folder and it will be called: demos/line-chart-3d.html.
Here's an image showing it and you can see the demo in its full glory by following the link
below to a codepen of it.
<script>
//
// Margins for the rear of the chart
//
marginLeft = 90;
marginRight = 40;
marginTop = 20;
marginBottom = 105;
marginInner = 5;
//
// Colors for the lines
//
colors = [
'rgba(255,0,0,0.75)',
'rgba(0,0,255,0.75)',
'rgba(0,128,0,0.75)'
];
filledColors = [
'rgba(255,0,0,0.5)',
'rgba(100,100,255,0.65)',
'rgba(0,200,0,0.75)'
];
//
// The line data
//
data = [
[8,9,7,8,10,10,13,12,9,8,9,11,8,11,13,12,11,13,10,8,9,10,8,12,11,9,10,9,8,11],
[8,7,6,5,6,7,5,6,8,8,8,9,5,6,8,7,5,6,6,8,7,5,6,8,7,8,6,9,9,7],
[5,6,8,3,6,5,5,4,5,6,6,7,6,5,4,6,7,5,4,5,6,5,5,5,6,8,7,8,6,5]
];
//
// X axis labels
//
xaxisLabels = [
'','','','Week 1','','','',
'','','','Week 2','','','',
'','','','Week 3','','','',
'','','','Week 4','','','',
'',''
];
// The title
title = 'Amount of sales last month';
// Maximum value for the Y scale
yaxisScaleMax = 15;
// How much to transform by to get the 3D look
transformX = -25;
transformY = 15;
// Set the initial transformation on the canvas in order
// to provide the 3D look
context = document.getElementById('cvs').getContext('2d');
context.setTransform(1,0.1,0,1,0,0);
//
// Draw the "chart" that provides the
// background grid. The chart is not
// displayed - it only shows the
// background grid
//
new RGraph.Line({
id: 'cvs',
data: [],
options: {
// The background grid is only enabled on this chart
//backgroundGridDashed: false,
//backgroundGridColor: '#333',
// Set the margins based on the values that are defined above
marginBottom: marginBottom,
marginTop: marginTop,
marginLeft: marginLeft,
marginRight: marginRight,
marginInner: marginInner,
yaxisScaleMax: yaxisScaleMax,
yaxisPosition: 'right',
yaxisScaleUnitsPost: 'Kg',
textSize: 10,
title: title,
titleSize: 16,
titleOffsety: 5
}
}).draw();
//
// Draw the first chart after adjusting
// the transformation
//
context.transform(1,0,0,1,transformX,transformY);
new RGraph.Line({
id: 'cvs',
data: data[0],
options: {
colors: [colors[0]],
// The background grid is only enabled on this chart - the
// bar chart at the back
backgroundGrid: false,
marginBottom: marginBottom,
marginTop: marginTop,
marginLeft: marginLeft,
marginRight: marginRight,
marginInner: marginInner,
filled: true,
filledColors: [filledColors[0]],
yaxisScaleMax: yaxisScaleMax,
yaxisPosition: 'right',
yaxisScale: false,
linewidth: 5
}
}).draw();
//
// Draw the second line chart after adjusting
// the transformation
//
context.transform(1,0,0,1,transformX,transformY);
new RGraph.Line({
id: 'cvs',
data: data[1],
options: {
colors: [colors[1]],
backgroundGrid: false,
marginBottom: marginBottom,
marginTop: marginTop,
marginLeft: marginLeft,
marginRight: marginRight,
marginInner: marginInner,
filled: true,
filledColors: [filledColors[1]],
yaxisScaleMax: yaxisScaleMax,
yaxisScale: false,
linewidth: 5
}
}).draw();
//
// Draw the third line chart after adjusting
// the transformation
//
context.transform(1,0,0,1,transformX,transformY);
new RGraph.Line({
id: 'cvs',
data: data[2],
options: {
colors: [colors[2]],
backgroundGrid: false,
marginBottom: marginBottom,
marginTop: marginTop,
marginLeft: marginLeft,
marginRight: marginRight,
marginInner: marginInner,
filled: true,
filledColors: [filledColors[2]],
yaxisScaleMax: yaxisScaleMax,
yaxisScale: false,
// The X axis labels are shown on this chart
textSize: 10,
linewidth: 5,
xaxisLabels: xaxisLabels
}
}).draw();
</script>