ChartJS scale ticks with scatter plot - chart.js

I am just starting out with ChartJS. I am exploring the new scatter plot in version 2.6 and would like to start the origin of an X-Y plot at 0:0. I would also like to specify the maximum value of X and Y.
Unfortunately, setting beginAtZero to true and setting a max value for each axis does not seem to work. Sample code below:
var linedata = {
type: 'scatter',
data: {
datasets: [{
label: 'Simple Line',
fill: false,
data: [
{x: 1, y: 2},
{x: 2, y: 4},
{x: 3, y: 6},
{x: 4, y: 8},
{x: 5, y: 10},
{x: 6, y: 12},
{x: 7, y: 14}
]
}
]
}
};
var chartOptions = {
responsive: true,
maintainAspectRatio: true,
scales: {
xAxes: [{
ticks: {
beginAtZero: true,
max: 8
},
type: 'linear',
position: 'bottom'
}],
yAxes: [{
ticks: {
beginAtZero: true,
max: 16
}
}]
}
};
var lineID = document.getElementById('linechart').getContext('2d');
var lineChart = new Chart(lineID, linedata, chartOptions);
This still ends up with a line (scatter plot) starting at X=1, Y=2, and ending with X=7, Y=14 (rather than 0:0 to 8:16).

You are configuring your chart incorrectly. Here is how it should be created ...
var linedata = {
datasets: [{
label: 'Simple Line',
fill: false,
data: [
{x: 1, y: 2},
{x: 2, y: 4},
{x: 3, y: 6},
{x: 4, y: 8},
{x: 5, y: 10},
{x: 6, y: 12},
{x: 7, y: 14}
]
}]
};
var chartOptions = {
responsive: true,
maintainAspectRatio: true,
scales: {
xAxes: [{
ticks: {
beginAtZero: true,
max: 8
},
type: 'linear',
position: 'bottom'
}],
yAxes: [{
ticks: {
beginAtZero: true,
max: 16
}
}]
}
};
var lineID = document.getElementById('linechart').getContext('2d');
var lineChart = new Chart(lineID, {
type: 'scatter',
data: linedata,
options: chartOptions
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="linechart"></canvas>

Related

Possible to create quadrant chart with ChartJS, with the "origin" centered at a single point?

I'm trying to use ChartJS to plot a quadrant chart for a set of values and I'm wondering if there is a way to center the origin of the quadrants at some dynamic point when rendering the chart, as well as divide the chart into distinct quadrants with labels. Here is what my scatter plot looks like right now:
Image of scatter plot with ChartJS.
The scatter chart was done with the following JavaScript:
var ctxScatter = document.getElementById('myChartScatter');
var scatterChart = new Chart(ctxScatter, {
type: 'scatter',
data: {
datasets: [{
label: 'Data Point',
backgroundColor: 'royalblue',
pointRadius: 5,
pointHoverRadius: 10,
data: [{x: 10.25, y: 89}, {x: 12.60, y: 69}, {x: 11.23, y: 78}, {x: 11.82, y: 71},
{x: 12.21, y: 85}, {x: 10.84, y: 75}, {x: 9.86, y: 86}, {x: 11.82, y: 62},
{x: 13.00, y: 79}, {x: 13.19, y: 74}, {x:12.02, y: 69}, {x: 14.76, y: 81},
{x: 13.39, y: 79}, {x: 9.66, y: 75}, {x:12.21, y: 78}, {x: 12.60, y: 77}]
}, {
label: '1st Quartile',
borderColor: 'black',
borderWidth: 1,
backgroundColor: 'seagreen',
pointRadius: 7,
pointHoverRadius: 14,
data: [{x: 10.15, y: 85}]
}, {
label: 'Average',
borderColor: 'black',
borderWidth: 1,
backgroundColor: 'orange',
pointRadius: 7,
pointHoverRadius: 14,
data: [{x: 11.97, y: 77}]
}]
},
options: {
responsive: true,
scales: {
xAxes: [{
type: 'linear',
position: 'bottom',
ticks: {
callback: function(value){return "$" + value}
},
scaleLabel: {
display: true,
labelString: "Cost"
}
}],
yAxes: [{
ticks: {
callback: function(value){return value + "%"}
}, scaleLabel: {
display: true,
labelString: "Satisfaction"
}
}]
}
}
});
The scatter chart works great, but I need the yellow point to be at the dead-center as a fixed origin point for the four quadrants, as well as draw a vertical and horizontal line through this origin point to divide the chart into labeled quadrants, as shown below from my plot in Excel:
Image of quadrant chart in Excel.
Is this possible?
I was needing this same, here I share the model I made. I hope it helps you.
https://jsfiddle.net/aledc/1m89wju0/2/
HTML CODE:
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="somatocarta" style="height:450px;width:500px;"></div>
JAVASCRIPT CODE:
var data = [
{"x":0,"y":0}, // en el Centro
{"x":-10,"y":30}, // cuadrante Superioro Alto Izquierdo
{"x":15,"y":35}, // cuadrante Superior Alto Derecho
{"x":45,"y":13}, // cuadrante Superior Medio Derecho
{"x":52,"y":-13}, // cuadrante Inferior Medio Derecho
{"x":25,"y":-33}, // cuadrante Inferior Bajo Derecho
{"x":-25,"y":-33}, // cuadrante Inferior Bajo Izquierdo
{"x":-42,"y":-14}, // cuadrante Inferior Medio Izquierdo
{"x":-42,"y":14}, // cuadrante Superior Medio Izquierdo
]
//alert(JSON.stringify(data));
Highcharts.chart('somatocarta', {
chart: {
plotBackgroundImage: 'https://raw.githubusercontent.com/aledc7/Laravel/master/resources/somatocarta.png',
renderTo: 'somatocarta',
defaultSeriesType:'scatter',
borderWidth:1,
borderColor:'#ccc',
marginLeft:90,
marginRight:50,
},
title:{
text:'Somatocarta'
},
legend:{
enabled:false
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y;
}
},
plotOptions: {
series: {
shadow:false,
}
},
xAxis:{
title:{
text:'X Axis Title'
},
min:-100,
max:100,
tickInterval:100,
tickLength:0,
minorTickLength:0,
gridLineWidth:1,
showLastLabel:true,
showFirstLabel:false,
lineColor:'#ccc',
lineWidth:1
},
yAxis:{
title:{
text:'Y Axis<br/>Title',
rotation:0,
margin:25,
},
min:-100,
max:100,
tickInterval:100,
tickLength:3,
minorTickLength:0,
lineColor:'#ccc',
lineWidth:1
},
series: [{
color:'#185aa9',
data: data
}]
});

ChartJs - Angle line and ticks 'on top'

I have a chart as shown below. I have two expectation, firstly as you can see the tick number (1,2,3,4,5) hidden behind the background color of dump datasets (I need that background colors, that's why I am using fake datasets). I need to show them like using z-index or something. I wrote tick numbers in red color for sample.
Secondly I need the show angle line on top also.
var test2 = [2, 2, 2, 2, 2];
var test3 = [3, 3, 3, 3, 3];
var test4 = [4, 4, 4, 4, 4];
var test5 = [5, 5, 5, 5, 5];
new Chart(document.getElementById("myChart"), {
type: 'radar',
data: {
labels: sectionDescriptions,
datasets: [
{
label: "2050",
fill: true,
borderColor: "rgba(0,0,0,1)",
borderWidth: "4",
pointRadius: 3,
pointBorderWidth: 3,
pointBackgroundColor: "cornflowerblue",
pointBorderColor: "rgba(0,0,200,0.6)",
pointHoverRadius: 10,
data: sectionPoints
},
{
radius: 0,
fill: true,
backgroundColor: "rgba(240,0,0,1)",
data: test2
},
{
radius: 0,
fill: true,
backgroundColor: "rgba(255,190,4,1)",
data: test3
},
{
radius: 0,
fill: true,
backgroundColor: "rgba(255,250,5,1)",
data: test4
},
{
radius: 0,
fill: true,
backgroundColor: "rgba(0,180,233,1)",
data: test5
}
]
},
options: {
title: {
display: true,
text: 'Chart'
},
scale: {
gridLine: {
color : "white"
},
ticks: {
beginAtZero: true,
min: 0,
max: 5,
stepSize: 1,
fontColor: 'black',
backDropColor: 'black'
},
pointLabels: {
fontSize: 13,
//fontStyle: 'bold'
}
},
legend: {
display: false
}
}
});

Chartjs: Custom text on y axis at certain level

Is there any way to add a custom text to a certain level of the y axis of a chart created with Chartjs?
Something like this:
I've checked thick configuration options, but couldn't find anything that would result this kind of annotation.
Here is an example of what you can do using annotation plugin and custom drawing on canvas:
var data_set = [{x: 1, y: 12}, {x: 2, y: 3}, {x: 3, y: 2}, {x: 4, y: 1}, {x: 5, y: 8}, {x: 6, y: 8}, {x: 7, y: 2}, {x: 8, y: 2}, {x: 9, y: 3}, {x: 10, y: 5}, {x: 11, y: 11}, {x: 12, y: 1}];
var labels = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
var lines = [], id = 0;
var linesOn = false;
var data = {
labels: labels,
datasets: [{
label: "My First dataset",
backgroundColor: "rgba(5,215,2,0.2)",
borderColor: "rgba(5,215,2,1)",
borderWidth: 2,
hoverBackgroundColor: "rgba(5,215,2,0.4)",
hoverBorderColor: "rgba(5,215,2,1)",
data: data_set,
}]
};
addLine(7);
var option = {
legend: false,
title: {
display: true,
},
annotation: {
drawTime: "afterDraw",
annotations: lines
},
scales: {
xAxes: [{
id: 'x-axis',
type: 'linear',
position: 'bottom',
ticks: {
max: 12,
min: 1,
stepSize: 1,
callback: function(value, index, values) {
return data.labels[index];
}
}
}],
yAxes: [{
id: 'y-axis',
type: 'linear',
}],
},
animation: {
duration: 1,
onComplete: function () {
var ctx = this.chart.ctx;
console.log("onComplete", this,Chart.defaults.global)
ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontSize, Chart.defaults.global.defaultFontStyle, Chart.defaults.global.defaultFontFamily);
ctx.textAlign = "center";
ctx.textBaseline = "bottom";
var line = this.chart.annotation.elements.line1,
x = line._model.x1-15,
y = line._model.y1+5;
ctx.fillStyle = line.options.label.fontColor;
ctx.fillText("Y",x,y);
}
}
};
var myLineChart = Chart.Line('myChart', {
data: data,
options: option
});
function addLine(value) {
id++;
var ln = {
id: "line" + id,
type: "line",
mode: "horizontal",
scaleID: "y-axis",
value: value,
borderWidth: 2,
borderColor: "red",
label: {
enabled: false,
fontColor: "red",
}
};
lines.push(ln);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/0.5.7/chartjs-plugin-annotation.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>
In the example I disabled annotation label showing and added a custom label on yAxis (see animation.onComplete() handler).

line chart with {x, y} point data displays only 2 values

https://codepen.io/shaz1234/pen/PEKjOV
The codepen has my code
new Chart(document.getElementById("chartjs-0"), {
"type":"line",
"data": {
"datasets": [
{
"label":"My First Dataset",
"data": [
{x: 0, y: 65},
{x: 4, y: 59},
{x: 100, y: 80},
{x: 110, y: 81},
{x: 125, y: 56}
],
"fill":false,
"borderColor":"rgb(75, 192, 192)",
"lineTension":0.1
}
]
},
"options":{
}
}
);
Very simple example but the chart displays only the first two points. I would have expected the chart to scale to the min and max provided x values. Do I misunderstand how point line charts are designed to work or do I have a bug?
Thanks in advance for looking.
To help those who are stuck on this in 2019 (Chart.js 2.0), please see below :
For those who prefer to dig into the details and form your own inferences, You can refer to the chartjs example at https://www.chartjs.org/samples/latest/scales/time/line-point-data.html (go and view its source).
For those who want a quick answer: In short you can maintain your existing code, but add the following options: (I re-append Shaz's code for completeness)
new Chart(document.getElementById("chartjs-0"), {
"type":"line",
"data": {
"datasets": [
{
"label":"My First Dataset",
"data": [
{x: 0, y: 65},
{x: 4, y: 59},
{x: 100, y: 80},
{x: 110, y: 81},
{x: 125, y: 56}
],
"fill":false,
"borderColor":"rgb(75, 192, 192)",
"lineTension":0.1
}
]
},
// this is the part that will make it work... see .. xAxes type:'linear'
"options":{
responsive: true,
title: {
// optional: your title here
},
scales: {
xAxes: [{
type: 'linear', // MANDATORY TO SHOW YOUR POINTS! (THIS IS THE IMPORTANT BIT)
display: true, // mandatory
scaleLabel: {
display: true, // mandatory
labelString: 'Your label' // optional
},
}],
yAxes: [{ // and your y axis customization as you see fit...
display: true,
scaleLabel: {
display: true,
labelString: 'Count'
}
}],
}
}
}
);
Try this, This link may be helpful to you http://www.chartjs.org/docs/latest/charts/scatter.html
var ctx = document.getElementById("myChart");
var scatterChart = new Chart(ctx, {
type: 'scatter',
data: {
datasets: [{
label: 'Scatter Dataset',
data: [{x: 0, y: 65},
{x: 4, y: 59},
{x: 100, y: 80},
{x: 110, y: 81},
{x: 125, y: 56}]
}]
},
options: {
scales: {
xAxes: [{
type: 'linear',
position: 'bottom'
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>
Edit:
Why We use Scatter chart instead of line Chart?
Line Chart is use when we want to plot data set on same difference,and data structure is one dimensional array, for example, data: [20, 10] then we use line chart.
But When we want plot data on different differences and data structure is 2 dimensional array then we use scatter chart.
for example,
data: [{
x: 10,
y: 20
}, {
x: 15,
y: 10
}]
Point objects ( {x:2, y:4}) can be interpreted by line chart and should be displayed correctly if you just specified a labels key before the datasets key:
data: {
labels:["a","b","c",...]
datasets: [{
label: 'my Dataset',
data: [{x: 0, y: 65},
{x: 4, y: 59},
{x: 100, y: 80},
{x: 110, y: 81},
{x: 125, y: 56}]
}]
},
note that the labels array and the data object inside the datasets should have the same number of elements.

how to set chart.js grid color for line chart

I want to change the grids color of my line chart using options field but I have no idea from where to begin.
I First tried to change the canvas backgroud colors using gradient but the results weren't good.
canvas{
background:linear-gradient(top, #ea1a07 0%, #f4b841 75%,#f2dd43 100%);
}
However, I didn't get what I want because as you see in the above image, not only the grids were colored but also the x values, y labels and the chart legend were colored too.
Picture of the result I'm getting with this css code
Example of what I want to get
my chart.js options are
options = {
scales: {
xAxes: [{
gridLines: {
color: 'rgba(171,171,171,1)',
lineWidth: 1
}
}],
yAxes: [{
ticks: {
beginAtZero: true,
max: 100,
min: 0,
stepSize: 10
},
gridLines: {
color: 'rgba(171,171,171,1)',
lineWidth: 0.5
}
}]
},
responsive: true
};
So, is there a way to set only the grid's background to 3 different colors (with gradient or not)?
NB: I'm using chart.js with angular 2 (ng2-charts)
The easiest way to do this is to use the chartjs-plugin-annotation plugin and configure 3 box annotations bound to your Y axis (each box would have a different color).
Here is an example below (and you can see it in action with this codepen).
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
label: 'Points',
data: [
{x: 0, y: 2},
{x: 1, y: 3},
{x: 2, y: 2},
{x: 1.02, y: 0.4},
{x: 0, y: -1}
],
backgroundColor: 'rgba(123, 83, 252, 0.8)',
borderColor: 'rgba(33, 232, 234, 1)',
borderWidth: 1,
fill: false,
}],
},
options: {
title: {
display: true,
text: 'Chart.js - Gridline Background',
},
scales: {
xAxes: [{
type: 'linear',
position: 'bottom',
ticks: {
min: -1,
max: 8,
stepSize: 1,
fixedStepSize: 1,
},
gridLines: {
color: 'rgba(171,171,171,1)',
lineWidth: 1
}
}],
yAxes: [{
afterUpdate: function(scaleInstance) {
console.dir(scaleInstance);
},
ticks: {
min: -2,
max: 4,
stepSize: 1,
fixedStepSize: 1,
},
gridLines: {
color: 'rgba(171,171,171,1)',
lineWidth: 0.5
}
}]
},
annotation: {
annotations: [{
type: 'box',
yScaleID: 'y-axis-0',
yMin: 1,
yMax: 4,
borderColor: 'rgba(255, 51, 51, 0.25)',
borderWidth: 2,
backgroundColor: 'rgba(255, 51, 51, 0.25)',
}, {
type: 'box',
yScaleID: 'y-axis-0',
yMin: -1,
yMax: 1,
borderColor: 'rgba(255, 255, 0, 0.25)',
borderWidth: 1,
backgroundColor: 'rgba(255, 255, 0, 0.25)',
}, {
type: 'box',
yScaleID: 'y-axis-0',
yMin: -2,
yMax: -1,
borderColor: 'rgba(0, 204, 0, 0.25)',
borderWidth: 1,
backgroundColor: 'rgba(0, 204, 0, 0.25)',
}],
}
}
});
It's important to note that the annotations are painted on top of the chart, so your annotation color needs to contain some transparency to see the stuff behind it.
There is no problem using this plugin with ng2-charts. Just add the source in a <script> in your app's html file and add the annotation property into your options object. Here is an Angular2 / ng2-charts example demonstrating how to use the annotations plugin.