chart.js-scatter
Version:
Scatter chart plugin for Chart.js
628 lines (544 loc) • 18.7 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Scatter chart</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" />
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<style>
body {
padding: 42px 0;
}
.jumbotron {
background-color: #4DB7EC;
color: white ;
}
.jumbotron h1 {
margin-top: 0;
}
.jumbotron a {
color: #e6f5fc;
}
.btn-jumbotron {
background-color: transparent;
border-color: white;
color: white ;
margin-bottom: 4px;
}
.btn-jumbotron:hover {
border-color: transparent;
background-color: white;
color: #4DB7EC ;
}
</style>
</head>
<body>
<div class="container">
<div class="jumbotron">
<div class="row">
<div class="col-md-6">
<h1>Chart.Scatter
</h1>
<p class="lead">
Scatter chart is a graph in which the values of two variables are plotted along two axes. Chart.Scatter - is an addon for the
<a href="http://www.chartjs.org">Chart.js</a> library.
</p>
<div>
<a class="btn btn-lg btn-jumbotron" href="https://github.com/dima117/Chart.Scatter/archive/master.zip">Download</a>
<a class="btn btn-lg btn-jumbotron" href="https://github.com/dima117/Chart.Scatter">Source on GitHub</a>
</div>
</div>
<div class="col-md-6 hidden-sm hidden-xs">
<canvas id="header-canvas" width="420" height="180"></canvas>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<h2>Creating a chart</h2>
<p>
First we need to include the Chart.js (or Chart.Core.js) and Chart.Scatter.js
libraries on the page.
</p>
<pre>
<script src="Chart.js"></script>
<script src="Chart.Scatter.js"></script>
</pre>
<p>
Alternatively you can use an AMD loader.
</p>
<pre>
// Using requirejs
require(['Chart', 'path/to/Chart.Scatter'], function(Chart){
...
});
</pre>
<p>
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.
</p>
<pre>
// Using requirejs
requirejs.config({
...
map: {
'*': { 'Chart': 'myAppChartjsName' }
}
});
</pre>
<p>
You can also grab Chart.Scatter.js using bower:
<pre>
bower install Chart.Scatter.js --save
</pre>
</p>
<p>
... or npm:
<pre>
npm i chart.js-scatter
</pre>
</p>
<p>
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.
</p>
<pre>
<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");
</pre>
<p>
With the Chart class set up, we can go on to create Scatter chart.
</p>
<pre>
new Chart(ctx).Scatter(data, options);
</pre>
<p>
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.
</p>
</div>
<div class="col-md-6">
<h3>Number scale</h3>
<div>
<canvas id="myChart" width="430" height="220"></canvas>
</div>
<h3>Date scale</h3>
<div>
<canvas id="myChartWithDates" width="430" height="220"></canvas>
</div>
<h3>Custom point radius</h3>
<div>
<canvas id="myBubbleChart" width="430" height="220"></canvas>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<h2>Chart options</h2>
<p>
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.
</p>
<pre>
<strong>// SUPPORTED GLOBAL OPTIONS</strong>
// Boolean - If we should show the scale at all
showScale: <strong>true</strong>,
// String - Colour of the scale line
scaleLineColor: <strong>"rgba(0,0,0,.1)"</strong>,
// Number - Pixel width of the scale line
scaleLineWidth: <strong>1</strong>,
// Boolean - Whether to show labels on the scale
scaleShowLabels: <strong>true</strong>,
// Interpolated JS string - can access value
scaleLabel: <strong>"<%=value%>"</strong>,
// Interpolated JS string - can access value
scaleArgLabel: <strong>"<%=value%>"</strong>,
// String - Message for empty data
emptyDataMessage: <strong>"chart has no data"</strong>,
<strong>// GRID LINES</strong>
// Boolean - Whether grid lines are shown across the chart
scaleShowGridLines: <strong>true</strong>,
// Number - Width of the grid lines
scaleGridLineWidth: <strong>1</strong>,
// String - Colour of the grid lines
scaleGridLineColor: <strong>"rgba(0,0,0,.05)"</strong>,
// Boolean - Whether to show horizontal lines (except X axis)
scaleShowHorizontalLines: <strong>true</strong>,
// Boolean - Whether to show vertical lines (except Y axis)
scaleShowVerticalLines: <strong>true</strong>,
<strong>// HORIZONTAL SCALE RANGE</strong>
// Boolean - If we want to override with a hard coded x scale
xScaleOverride: <strong>false</strong>,
// ** Required if scaleOverride is true **
// Number - The number of steps in a hard coded x scale
xScaleSteps: <strong>null</strong>,
// Number - The value jump in the hard coded x scale
xScaleStepWidth: <strong>null</strong>,
// Number - The x scale starting value
xScaleStartValue: <strong>null</strong>,
<strong>// VERTICAL SCALE RANGE</strong>
// Boolean - If we want to override with a hard coded y scale
scaleOverride: <strong>false</strong>,
// ** Required if scaleOverride is true **
// Number - The number of steps in a hard coded y scale
scaleSteps: <strong>null</strong>,
// Number - The value jump in the hard coded y scale
scaleStepWidth: <strong>null</strong>,
// Number - The y scale starting value
scaleStartValue: <strong>null</strong>,
<strong>// DATE SCALE</strong>
// String - scale type: "number" or "date"
scaleType: <strong>"number"</strong>,
// Boolean - Whether to use UTC dates instead local
useUtc: <strong>true</strong>,
// String - short date format (used for scale labels)
scaleDateFormat: <strong>"mmm d"</strong>,
// String - short time format (used for scale labels)
scaleTimeFormat: <strong>"h:MM"</strong>,
// String - full date format (used for point labels)
scaleDateTimeFormat: <strong>"mmm d, yyyy, hh:MM"</strong>,
<strong>// LINES</strong>
// Boolean - Whether to show a stroke for datasets
datasetStroke: <strong>true</strong>,
// Number - Pixel width of dataset stroke
datasetStrokeWidth: <strong>2</strong>,
// String - Color of dataset stroke
datasetStrokeColor: <strong>'#007ACC'</strong>,
// String - Color of dataset stroke
datasetPointStrokeColor: <strong>'white'</strong>,
// Boolean - Whether the line is curved between points
bezierCurve: <strong>true</strong>,
// Number - Tension of the bezier curve between points
bezierCurveTension: <strong>0.4</strong>,
<strong>// POINTS</strong>
// Boolean - Whether to show a dot for each point
pointDot: <strong>true</strong>,
// Number - Pixel width of point dot stroke
pointDotStrokeWidth: <strong>1</strong>,
// Number - Radius of each point dot in pixels
pointDotRadius: <strong>4</strong>,
// Number - amount extra to add to the radius to cater for hit detection outside the drawn point
pointHitDetectionRadius: <strong>4</strong>,
<strong>// TEMPLATES</strong>
// Interpolated JS string - can access point fields:
// argLabel, valueLabel, arg, value, datasetLabel, size
tooltipTemplate: <strong>"<%if (datasetLabel){%><%=datasetLabel%>: <%}%><%=argLabel%>; <%=valueLabel%>"</strong>
// Interpolated JS string - can access point fields:
// argLabel, valueLabel, arg, value, datasetLabel, size
multiTooltipTemplate: <strong>"<%=argLabel%>; <%=valueLabel%>",</strong>
// Interpolated JS string - can access all chart fields
legendTemplate: <strong>"<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>"</strong>
</pre>
</div>
<div class="col-md-6">
<h2>Data structure</h2>
<p>
Any point can have <code>x</code>, <code>y</code>, <code>r</code> fields.
The fields named "x" and "y" are the coordinates of the point. Optional field "r" is a multiplier
for the point radius (<code>pointDotRadius</code>). The default multiplier is 1.
</p>
<pre>
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 }
]
}
];
</pre>
<h2>Dataset API</h2>
<p>
You can change the chart data, using <code>datasets</code> collection of the chart.
Use the methods of it elements: <code>addPoint</code>, <code>setPointData</code>, <code>removePoint</code>.
</p>
<pre>
<strong>// Add a new point { x: 5, y: 7 }</strong>
myChart.datasets[0].addPoint(5, 7);
<strong>// Add a new point { x: 5, y: 7, r: 4 } (with radius)</strong>
myChart.datasets[0].addPoint(5, 7, 4);
<strong>// Set point data { x: 11, y: 4 } at index 3</strong>
myChart.datasets[0].setPointData(3, 11, 4);
<strong>// Set point data { x: 11, y: 4, r: 2 } at index 3 (with radius)</strong>
myChart.datasets[0].setPointData(3, 11, 4, 2);
<strong>// Remove the point at index 0</strong>
myChart.datasets[0].removePoint(0);
</pre>
<p>
After all changes call the <code>update</code> method of the chart for render your chart with new data.
</p>
<pre>
myChart.update();
</pre>
<h2>Date Format mask</h2>
<p>
Chart.Scatter uses the
<a href="http://blog.stevenlevithan.com/archives/date-time-format" target="_blank">date format library
</a>
by Steven Levithan.
</p>
<table class="table">
<thead>
<tr>
<th>Mask</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>d</code></td>
<td>Day of the month as digits; no leading zero for single-digit days.</td>
</tr>
<tr>
<td><code>dd</code></td>
<td>Day of the month as digits; leading zero for single-digit days.</td>
</tr>
<tr>
<td><code>ddd</code></td>
<td>Day of the week as a three-letter abbreviation.</td>
</tr>
<tr>
<td><code>dddd</code></td>
<td>Day of the week as its full name.</td>
</tr>
<tr>
<td><code>m</code></td>
<td>Month as digits; no leading zero for single-digit months.</td>
</tr>
<tr>
<td><code>mm</code></td>
<td>Month as digits; leading zero for single-digit months.</td>
</tr>
<tr>
<td><code>mmm</code></td>
<td>Month as a three-letter abbreviation.</td>
</tr>
<tr>
<td><code>mmmm</code></td>
<td>Month as its full name.</td>
</tr>
<tr>
<td><code>yy</code></td>
<td>Year as last two digits; leading zero for years less than 10.</td>
</tr>
<tr>
<td><code>yyyy</code></td>
<td>Year represented by four digits.</td>
</tr>
<tr>
<td><code>h</code></td>
<td>Hours; no leading zero for single-digit hours (12-hour clock).</td>
</tr>
<tr>
<td><code>hh</code></td>
<td>Hours; leading zero for single-digit hours (12-hour clock).</td>
</tr>
<tr>
<td><code>H</code></td>
<td>Hours; no leading zero for single-digit hours (24-hour clock).</td>
</tr>
<tr>
<td><code>HH</code></td>
<td>Hours; leading zero for single-digit hours (24-hour clock).</td>
</tr>
<tr>
<td><code>M</code></td>
<td>Minutes; no leading zero for single-digit minutes.<br>
<span>Uppercase M unlike CF <code>timeFormat</code>'s m to avoid conflict with months.</span></td>
</tr>
<tr>
<td><code>MM</code></td>
<td>Minutes; leading zero for single-digit minutes.<br>
<span>Uppercase MM unlike CF <code>timeFormat</code>'s mm to avoid conflict with months.</span></td>
</tr>
<tr>
<td><code>s</code></td>
<td>Seconds; no leading zero for single-digit seconds.</td>
</tr>
<tr>
<td><code>ss</code></td>
<td>Seconds; leading zero for single-digit seconds.</td>
</tr>
<tr>
<td><code>l</code> <em>or</em> <code>L</code></td>
<td>Milliseconds. <code>l</code> gives 3 digits. <code>L</code> gives 2 digits.</td>
</tr>
<tr>
<td><code>'…'</code> <em>or</em> <code>"…"</code></td>
<td>Literal character sequence. Surrounding quotes are removed.<br>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="Chart.Core.min.js"></script>
<script src="Chart.Scatter.min.js"></script>
<script>
Chart.defaults.global.responsive = true;
Chart.defaults.global.animation = false;
$(function () {
//#region header
var data = [{ x: 0, y: 0 }];
for (var x = 1; x < 10; x++) {
var sq = Math.sqrt(x);
data.push({ x: x, y: sq });
data.unshift({ x: x, y: -sq });
}
var dataset = {
label: 'y = sqrt(x)',
strokeColor: 'white',
pointStrokeColor: '#4db7ec',
data: data
}
var ctx = document.getElementById("header-canvas").getContext("2d");
new Chart(ctx).Scatter([dataset], {
responsive: true,
scaleGridLineColor: "rgba(255,255,255,.3)",
scaleLineColor: "rgba(255,255,255,.8)"
});
//#endregion
var data2 = [
{
label: 'complex-1',
strokeColor: '#F16220',
data: [
{ x: 19, y: 65 }, { x: 27, y: 59 }, { x: 28, y: 69 }, { x: 40, y: 81 },
{ x: 48, y: 56 }, { x: 86, y: 55 }, { x: 90, y: 40 }, { x: 95, y: 60 }
]
},
{
label: 'complex-2',
strokeColor: '#007ACC',
data: [
{ x: 19, y: 75 }, { x: 27, y: 69 }, { x: 28, y: 70 }, { x: 40, y: 31 },
{ x: 48, y: 76 }, { x: 52, y: 23 }, { x: 24, y: 32 },
{ x: 86, y: 45 }, { x: 90, y: 50 }
]
}
];
var ctx2 = document.getElementById("myChart").getContext("2d");
var myLineChart = new Chart(ctx2).Scatter(data2, {
bezierCurve: true,
showTooltips: true,
scaleShowHorizontalLines: true,
scaleShowLabels: true,
scaleLabel: "<%=value%>°C",
scaleArgLabel: "<%=value%>mmm",
scaleBeginAtZero: true
});
var data3 = [
{
label: 'temperature',
strokeColor: '#A31515',
data: [
{
x: new Date('2011-04-11T11:45:00'),
y: 25
},
{
x: new Date('2011-04-11T12:51:00'),
y: 28
},
{
x: new Date('2011-04-11T14:10:00'),
y: 22
},
{
x: new Date('2011-04-11T15:15:00'),
y: 18
},
{
x: new Date('2011-04-11T17:00:00'),
y: 25
},
{
x: new Date('2011-04-11T21:00:00'),
y: 24
},
{
x: new Date('2011-04-12T13:00:00'),
y: 24
}
]
}];
var ctx3 = document.getElementById("myChartWithDates").getContext("2d");
var myDateLineChart = new Chart(ctx3).Scatter(data3, {
bezierCurve: true,
showTooltips: true,
scaleShowHorizontalLines: true,
scaleShowLabels: true,
scaleType: "date",
scaleLabel: "<%=value%>°C"
});
var data4 = [
{
label: 'Bubble chart example',
strokeColor: 'rgba(77, 180, 73, 0.3)',
data: [
{ x: 1, y: 10, r: 7 }, { x: 2, y: 12, r: 5 }, { x: 3, y: 14, r: 10 }, { x: 4, y: 18, r: 6 },
{ x: 5, y: 26, r: 9 }, { x: 6, y: 42, r: 4 }, { x: 7, y: 60, r: 8 }
]
}
];
var ctx4 = document.getElementById("myBubbleChart").getContext("2d");
var myBubbleChart = new Chart(ctx4).Scatter(data4, {
bezierCurve: true,
showTooltips: true,
scaleShowHorizontalLines: true,
scaleShowLabels: true,
scaleBeginAtZero: true,
datasetStroke: false
});
});
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-12512709-9', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>