Scatter chart is a graph in which the values of two variables are plotted along two axes. Chart.Scatter - is an addon for the Chart.js library.
First we need to include the Chart.js (or Chart.Core.js) and Chart.Scatter.js libraries on the page.
<script src="Chart.js"></script> <script src="Chart.Scatter.js"></script>
Alternatively you can use an AMD loader.
// Using requirejs require(['Chart', 'path/to/Chart.Scatter'], function(Chart){ ... });
Please note, that Chart.Scatter addon requires to Chart.js module named "Chart". If Chart.js module has another name in your application (not "Chart"), you can adjust module name mapping.
// Using requirejs requirejs.config({ ... map: { '*': { 'Chart': 'myAppChartjsName' } } });
You can also grab Chart.Scatter.js using bower:
bower install Chart.Scatter.js --save
... or npm:
npm i chart.js-scatter
To create a chart, we need to instantiate the Chart class. To do this, we need to pass in the 2d context of where we want to draw the chart. Here's an example.
<canvas id="myChart" width="400" height="400"></canvas> // Get the context of the canvas element we want to select var ctx = document.getElementById("myChart").getContext("2d");
With the Chart class set up, we can go on to create Scatter chart.
new Chart(ctx).Scatter(data, options);
We call a method named Scatter. We pass in the data for that chart type, and the options for that chart as parameters. Chart.js will merge the global defaults with chart type specific defaults, then merge any options passed in as a second argument after data.
These are the customisation options specific to Scatter charts. These options are merged with the global chart configuration options, and form the options of the chart.
// SUPPORTED GLOBAL OPTIONS // Boolean - If we should show the scale at all showScale: true, // String - Colour of the scale line scaleLineColor: "rgba(0,0,0,.1)", // Number - Pixel width of the scale line scaleLineWidth: 1, // Boolean - Whether to show labels on the scale scaleShowLabels: true, // Interpolated JS string - can access value scaleLabel: "<%=value%>", // Interpolated JS string - can access value scaleArgLabel: "<%=value%>", // String - Message for empty data emptyDataMessage: "chart has no data", // GRID LINES // Boolean - Whether grid lines are shown across the chart scaleShowGridLines: true, // Number - Width of the grid lines scaleGridLineWidth: 1, // String - Colour of the grid lines scaleGridLineColor: "rgba(0,0,0,.05)", // Boolean - Whether to show horizontal lines (except X axis) scaleShowHorizontalLines: true, // Boolean - Whether to show vertical lines (except Y axis) scaleShowVerticalLines: true, // HORIZONTAL SCALE RANGE // Boolean - If we want to override with a hard coded x scale xScaleOverride: false, // ** Required if scaleOverride is true ** // Number - The number of steps in a hard coded x scale xScaleSteps: null, // Number - The value jump in the hard coded x scale xScaleStepWidth: null, // Number - The x scale starting value xScaleStartValue: null, // VERTICAL SCALE RANGE // Boolean - If we want to override with a hard coded y scale scaleOverride: false, // ** Required if scaleOverride is true ** // Number - The number of steps in a hard coded y scale scaleSteps: null, // Number - The value jump in the hard coded y scale scaleStepWidth: null, // Number - The y scale starting value scaleStartValue: null, // DATE SCALE // String - scale type: "number" or "date" scaleType: "number", // Boolean - Whether to use UTC dates instead local useUtc: true, // String - short date format (used for scale labels) scaleDateFormat: "mmm d", // String - short time format (used for scale labels) scaleTimeFormat: "h:MM", // String - full date format (used for point labels) scaleDateTimeFormat: "mmm d, yyyy, hh:MM", // LINES // Boolean - Whether to show a stroke for datasets datasetStroke: true, // Number - Pixel width of dataset stroke datasetStrokeWidth: 2, // String - Color of dataset stroke datasetStrokeColor: '#007ACC', // String - Color of dataset stroke datasetPointStrokeColor: 'white', // Boolean - Whether the line is curved between points bezierCurve: true, // Number - Tension of the bezier curve between points bezierCurveTension: 0.4, // POINTS // Boolean - Whether to show a dot for each point pointDot: true, // Number - Pixel width of point dot stroke pointDotStrokeWidth: 1, // Number - Radius of each point dot in pixels pointDotRadius: 4, // Number - amount extra to add to the radius to cater for hit detection outside the drawn point pointHitDetectionRadius: 4, // TEMPLATES // Interpolated JS string - can access point fields: // argLabel, valueLabel, arg, value, datasetLabel, size tooltipTemplate: "<%if (datasetLabel){%><%=datasetLabel%>: <%}%><%=argLabel%>; <%=valueLabel%>" // Interpolated JS string - can access point fields: // argLabel, valueLabel, arg, value, datasetLabel, size multiTooltipTemplate: "<%=argLabel%>; <%=valueLabel%>", // Interpolated JS string - can access all chart fields legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><%for(var i=0;i<datasets.length;i++){%><li><span class=\"<%=name.toLowerCase()%>-legend-marker\" style=\"background-color:<%=datasets[i].strokeColor%>\"></span><%=datasets[i].label%></li><%}%></ul>"
Any point can have x
, y
, r
fields.
The fields named "x" and "y" are the coordinates of the point. Optional field "r" is a multiplier
for the point radius (pointDotRadius
). The default multiplier is 1.
var data = [ { label: 'My First dataset', strokeColor: '#F16220', pointColor: '#F16220', pointStrokeColor: '#fff', data: [ { x: 19, y: 65 }, { x: 27, y: 59 }, { x: 28, y: 69 }, { x: 40, y: 81 }, { x: 48, y: 56 } ] }, { label: 'My Second dataset', strokeColor: '#007ACC', pointColor: '#007ACC', pointStrokeColor: '#fff', data: [ { x: 19, y: 75, r: 4 }, { x: 27, y: 69, r: 7 }, { x: 28, y: 70, r: 5 }, { x: 40, y: 31, r: 3 }, { x: 48, y: 76, r: 6 }, { x: 52, y: 23, r: 3 }, { x: 24, y: 32, r: 4 } ] } ];
You can change the chart data, using datasets
collection of the chart.
Use the methods of it elements: addPoint
, setPointData
, removePoint
.
// Add a new point { x: 5, y: 7 } myChart.datasets[0].addPoint(5, 7); // Add a new point { x: 5, y: 7, r: 4 } (with radius) myChart.datasets[0].addPoint(5, 7, 4); // Set point data { x: 11, y: 4 } at index 3 myChart.datasets[0].setPointData(3, 11, 4); // Set point data { x: 11, y: 4, r: 2 } at index 3 (with radius) myChart.datasets[0].setPointData(3, 11, 4, 2); // Remove the point at index 0 myChart.datasets[0].removePoint(0);
After all changes call the update
method of the chart for render your chart with new data.
myChart.update();
Chart.Scatter uses the date format library by Steven Levithan.
Mask | Description |
---|---|
d |
Day of the month as digits; no leading zero for single-digit days. |
dd |
Day of the month as digits; leading zero for single-digit days. |
ddd |
Day of the week as a three-letter abbreviation. |
dddd |
Day of the week as its full name. |
m |
Month as digits; no leading zero for single-digit months. |
mm |
Month as digits; leading zero for single-digit months. |
mmm |
Month as a three-letter abbreviation. |
mmmm |
Month as its full name. |
yy |
Year as last two digits; leading zero for years less than 10. |
yyyy |
Year represented by four digits. |
h |
Hours; no leading zero for single-digit hours (12-hour clock). |
hh |
Hours; leading zero for single-digit hours (12-hour clock). |
H |
Hours; no leading zero for single-digit hours (24-hour clock). |
HH |
Hours; leading zero for single-digit hours (24-hour clock). |
M |
Minutes; no leading zero for single-digit minutes. Uppercase M unlike CF timeFormat 's m to avoid conflict with months. |
MM |
Minutes; leading zero for single-digit minutes. Uppercase MM unlike CF timeFormat 's mm to avoid conflict with months. |
s |
Seconds; no leading zero for single-digit seconds. |
ss |
Seconds; leading zero for single-digit seconds. |
l or L |
Milliseconds. l gives 3 digits. L gives 2 digits. |
'…' or "…" |
Literal character sequence. Surrounding quotes are removed. |