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

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.

Related

Chartjs Polar Area Chart - Data labels shown don't rotate with startAngle

I am setting the startAngle on my polar chart so the first category starts on the -180 degrees.
But I can't figure out why the data point labels don't correspondingly adjust and rotate. As a result, they show up incorrectly against the wrong data. As you can see in the image, Question 6 still appears where Question 1 should be. The tooltips on each section do appear correctly.
Note: When I use the datalabels plugin, it does show the label correctly. But I am not using it because it does not wrap the labels and also it cuts off the labels if they become too big. In addition I had responsiveness problems with it.
So please suggest a solution without that plugin if possible.
Thank you very much for helping me out.
const mydata = {
labels: [
'Question1',
'Question2',
'Question3',
'Question4',
'Question5',
'Question6'
],
datasets: [{
label: 'Data labels don't move',
data: [5, 8, 6, 6, 7, 8],
backgroundColor: [
'rgb(255, 99, 132)',
'rgb(75, 192, 192)',
'rgb(255, 205, 86)',
'rgb(201, 203, 207)',
'rgb(255, 99, 132)',
'rgb(201, 203, 207)'
]
}]
};
var myChart = new Chart('mychartId'),
{
type: "polarArea",
data: mydata,
options:
{
responsive: true,
cutoutPercentage: 20,
startAngle: -1 * Math.PI,
legend: {
display: false
},
layout: {
padding: 20
},
scale: {
ticks: {
max: 10,
beginAtZero: true,
min: 1,
stepSize: 1,
display: false
},
angleLines: {
display: false
},
pointLabels: {
display: true
}
}
}
}
sample image showing the problem
Update to V3, there they do turn with the chart if you adjust the startAngle
var options = {
type: 'polarArea',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [2, 9, 3, 5, 2, 3],
borderWidth: 1
}]
},
options: {
scales: {
r: {
startAngle: 180,
ticks: {
display: false
},
pointLabels: {
display: true
}
}
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.6.0/chart.js"></script>
</body>
V3 has some breaking changes over V2, for all of them you can read the migration guide

ChartJS 3+ x-axis only showing full moment object instead of just month

I'm trying to show only the month and year in the x-axis of my chart by using momentjs, but it is only putting what appears to be the full moment date in the x-axis.
I have been looking at many examples of people doing this but none of them seem to work in the latest version of chartjs.
I am aware of an "adapter" needed for use with momentjs which I have included but have no way to know if it is working.
Here is a jsfiddle of the code I am using, help would be greatly appreciated. https://jsfiddle.net/7z20jg68/
I also have error telling me Invalid scale configuration for scale: x which is confusing because my x axis seems to be in the correct order..Thanks!
Your X axis scale was indeed invalid, you declared it as an array which is V2 syntax, in V3 all the scales are their own object where the key of the object is the scaleID. So removing the array brackets around the x axis scale object will resolve the issue.
Also your legend and tooltip config where wrong and in the wrong place, also V2 syntax.
For all changes please read the migration guide
const gainedChart = document.getElementById('gained-chart').getContext('2d');
const gain = [74.699997, 76.580002, 81.349998, 83.000000, 85.879997, 83.120003, 77.989998, 81.279999];
const dates = ["Jan 2, 20", "Feb 3, 20", "Mar 4, 20", "Apr 5, 20", "May 6, 20", "Jun 9, 20", "Nov 10, 20", "Dec 11, 20"];
let gainData = {
datasets: [{
label: "Gain (%)",
data: gain,
borderColor: 'rgba(255, 66, 66, 1)',
backgroundColor: 'rgba(255, 66, 66, 1)',
pointRadius: 1.2,
pointBackgroundColor: 'rgba(255, 66, 66, 0.8)',
pointHoverRadius: 6,
pointHitRadius: 20,
pointBorderWidth: 2,
}]
};
let gainConfig = {
plugins: {
legend: {
display: true,
position: 'top',
labels: {
boxWidth: 80,
color: 'black'
},
},
tooltip: {
callbacks: {
label: function(tooltipItem, data) {
return parseInt(tooltipItem.parsed.y)
}
}
},
},
scales: {
x: {
type: 'time',
time: {
minUnit: 'month'
}
},
y: {
suggestedMax: 45,
ticks: {
stepSize: 5,
//max: 100
},
},
},
maintainAspectRatio: false,
responsive: true,
};
let lineGainChart = new Chart(gainedChart, {
type: 'line',
data: gainData,
options: gainConfig,
plugins: [{
beforeInit: function(lineGainChart) {
for (let c = 0; c < dates.length; c++) {
let myMoment = moment(dates[c], 'MMM D, YYYY');
lineGainChart.data.labels.push(myMoment);
}
}
}]
});
<canvas class="graph-canvas" id="gained-chart" width="400" height="400"></canvas>
<script src="https://cdn.jsdelivr.net/npm/moment#2.29.1/moment.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-moment#^1"></script>

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 scale ticks with scatter plot

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>

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.