I have implemented Charts JS library in one of my web apps and am having an issue. Right now i need to add multiple y values to one single x-axis point. however did not come to solution.
var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, config);
var config = {
type: 'line',
data: {
labels: [['01', '11' ,'2018'],
['01', '11' ,'2018'],
['01', '11' ,'2018'],
['01','11','2018'], '02','02','02','02','02','05','05','05','05','06','06','06','06','07','07','07','07','07','07','07','07','08','08','08','08','08','08','08','08','09','09','09','09',['20','02','2019'],['20','02','2019'],'21','21','21','21',['06','03'],['06','03'] ,'','','','' ,
],
datasets: [{
label: "My First dataset",
data: [35.48 ,35.50 ,35.5 ,35.49 ,35.49 ,35.50 ,35.50 ,35.49 ,35.45 ,35.38 ,35.42 ,35.4 ,35.49 ,35.50 ,35.49 ,35.49 ,35.49 ,35.49 ,35.50 ,35.50 ,35.48 ,35.47 ,35.48 ,35.47 ,35.49 ,35.50 ,35.50 ,35.49 ,35.48 ,35.47 ,35.48 ,35.47 ,35.50 ,35.49 ,35.49 ,35.50 ,37 ,37 ,38 ,39 ,40 ,51 ,28 ,29 ],
}]
},
options: {
scales: {
xAxes:[{
ticks: {
autoSkip: false,
}
}],
},
}
};
var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, config);
<canvas id="myChart"></canvas>
You can add multiple datasets which will plot a separate line on the graph.
Here is your example.
function newDate() {
return moment().add(days, 'd');
}
var config = {
type: 'line',
data: {
labels: [
["01", "11", "2018"],
['01', '11', '2018'],
['01', '11', '2018'],
['01', '11', '2018'], , '02', '02', '02', '02', '05', '05', '05', '05', '06', '06', '06', '06', '07', '07', '07', '07', '07', '07', '07', '07', '08', '08', '08', '08', '08', '08', '08', '08', '09', '09', '09', '09', ['20', '02', '2019'],
['20', '02', '2019'], '21', '21', '21', '21', ['06', '03'],
['06', '03'], '', '', '', '', '', '', '', '', '', '', '', '', '', ''
],
datasets: [{
label: "My First dataset",
data: [35.48, 35.50, 35.5, 35.49, 35.49, 35.50, 35.50, 35.49, 35.45, 35.38, 35.42, 35.4, 35.49, 35.50, 35.49, 35.49, 35.49, 35.49, 35.50, 35.50, 35.48, 35.47, 35.48, 35.47, 35.49, 35.50, 35.50, 35.49, 35.48, 35.47, 35.48, 35.47, 35.50, 35.49, 35.49, 35.50, 37, 37, 38, 39, 40, 51, 28, 29],
},
{
label: "My second dataset",
data: [35.48, 35.50, 35.5, 35.49, 35.49, 35.50, 35.50, 35.49, 35.45, 35.38, 35.42, 35.4, 35.49, 35.50, 35.49, 35.49, 35.49, 35.49, 35.50, 35.50, 35.48, 35.47, 35.48, 35.47, 35.49, 35.50, 35.50, 35.49, 35.48, 35.47, 35.48, 35.47, 35.50, 35.49, 35.49, 35.50, 37, 37, 38, 39, 40, 51, 28, 29].map(a => a + 20),
}
]
},
options: {
scales: {
xAxes: [{
ticks: {
autoSkip: false
}
}],
},
}
};
var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, config);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.0/Chart.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<canvas id="myChart"></canvas>
Related
I am using Chart.js version 2.8.0. I have encountered a problem. The problem is that I have to change the color of the every legend label. How do I change the color of the every label in this version (2.8.0)
Note: (I have no way yo use latest version for this satiation)
data: {
labels: [
"Sep",
"oct",
"Nov",
"Dec",
2020,
"Feb",
"Mar",
"Apr",
"May",
"jun",
"Jul",
],
datasets: [
{
label: "Fees",
fill: true,
// backgroundColor: "#FCBB0A",
backgroundColor: "red",
pointBackgroundColor: "red",
borderColor: "red",
pointHighlightStroke: "red",
borderCapStyle: "butt",
stacked: true,
beginAtZero: true,
data: [-200, 200, -230, -100, -200, 190, 220, -350, 180, -200, 500],
},
{
label: "Interest",
fill: true,
// backgroundColor: "#FCBB0A",
backgroundColor: "green",
pointBackgroundColor: "green",
borderColor: "green",
pointHighlightStroke: "green",
borderCapStyle: "butt",
stacked: true,
beginAtZero: true,
data: [-200, 200, 230, -100, -200, 190, 220, 350, 180, -200, 0],
},
{
label: "Net Positive",
fill: true,
// backgroundColor: "#FCBB0A",
backgroundColor: "rgba(68,208,99,0.53)",
pointBackgroundColor: "rgba(68,208,99,0.53)",
borderColor: "rgba(68,208,99,0.53)",
pointHighlightStroke: "rgba(68,208,99,0.53)",
borderCapStyle: "butt",
stacked: true,
beginAtZero: true,
data: [-200, 200, 230, -100, -200, 190, 220, 350, 180, -200, 0],
},
{
label: "Net Negative",
fill: true,
// backgroundColor: "#FCBB0A",
backgroundColor: "rgba(243,86,86,0.53)",
pointBackgroundColor: "rgba(243,86,86,0.53)",
borderColor: "rgba(243,86,86,0.53)",
pointHighlightStroke: "rgba(243,86,86,0.53)",
borderCapStyle: "butt",
stacked: true,
beginAtZero: true,
data: [-200, 200, 230, -100, -200, 190, 220, 350, 180, -200, 0],
},
{
label: "Net Value",
fill: true,
// backgroundColor: "#FCBB0A",
backgroundColor: "rgba(86,170,243,0.53)",
pointBackgroundColor: "rgba(86,170,243,0.53)",
borderColor: "rgba(86,170,243,0.53)",
pointHighlightStroke: "rgba(86,170,243,0.53)",
borderCapStyle: "butt",
stacked: true,
beginAtZero: true,
data: [-200, 200, 230, -100, -200, 190, 220, 350, 180, -200, 0],
},
],
},
options: {
borderWidth: 0,
pointRadius: 0,
showPoints: false,
showLine: false,
stacked: true,
tooltips: {enabled: false},
responsive: true,
maintainAspectRatio: false,
line: {
borderWidth: 0,
showLine: false,
},
elements: {
point: {
radius: 0,
},
},
legend: {
position: "bottom",
align: "center",
labels: {
generateLabels: function(chart) {
let labels Chart.defaults.global.legend.labels.generateLabels(chart);
labels[0].fillStyle = 'blue';
labels[0].fontColor = '#666'
labels[0].strokeStyle = 'green'
labels[0].fontSize = 14
return labels;
}
}
},
animation: {
duration: 750,
},
},
But labels[0].fontColor = '#666' and labels[0].fontSize = 14 not working
How can solve this problem please help me anyone
Instead of trying to do it in the generateLabels part you need to set it as a property in the labels config like so:
options: {
legend: {
labels: {
fontColor: 'red'
}
}
}
Example:
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
legend: {
labels: {
fontColor: 'red'
}
}
}
}
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/2.8.0/Chart.js"></script>
</body>
Is there a way of adding lines to assist reading off a articular value in ChartJS? An example is below. I would like to enter the number on the x axis and have ChartJS draw up and then across from where is meets the curve. An example shown below.
Is this possible?
Yes, it's possible
You can check my fiddle:
https://jsfiddle.net/iphong993/gru4Lad6/14/
var canvas = document.getElementById('chart');
new Chart(canvas, {
type: 'line',
data: {
labels: ['1', '2', '3', '4', '5'],
datasets: [{
label: 'A',
yAxisID: 'A',
data: [100, 96, 84, 76, 69]
}]
},
options: {
scales: {
yAxes: [{
id: 'A',
type: 'linear',
position: 'left',
}, {
id: 'B',
type: 'linear',
position: 'right',
ticks: {
max: 1,
min: 0
}
}]
},
annotation: {
annotations: [
{
drawTime: "beforeDatasetsDraw",
type: "box",
xScaleID: "x-axis-0",
yScaleID: "B",
xMin: "3",
xMax: "5",
yMin: 0.3,
yMax: 0.3,
borderColor: "red",
borderWidth: 1,
onClick: function(e) {
console.log("Box", e.type, this);
}
},
{
drawTime: "beforeDatasetsDraw",
type: "box",
xScaleID: "x-axis-0",
yScaleID: "B",
xMin: "3",
xMax: "3",
yMin: 0,
yMax: 0.3,
borderColor: "red",
borderWidth: 1,
onClick: function(e) {
console.log("Box", e.type, this);
}
}
]
}
}
});
I have a simple line chart and I have problem when two or more points are too closer. The problem is: The tooltip show one point and the click function is for other point.
In this exemple you can see that when I click on the first point on the red line (value 2) the click event is for the black line (value 0)
Exemple:
https://codepen.io/ataufo/pen/NexppY
var canvas = document.getElementById("lineChart");
var ctx = canvas.getContext('2d');
var data = {
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
datasets: [{
label: "Stock A",
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(225,0,0,0.4)",
borderColor: "red",
borderCapStyle: 'square',
pointBorderWidth: 0.1,
pointHoverRadius: 6,
pointHoverBorderWidth: 2,
pointRadius: 1,
data: [2, 59, 80, 81, 56],
spanGaps: false,
datalabels: {
enabled: true,
allowOverlap: true,
display: true,
listeners: {
click: function(context) {
alert(context.dataIndex + " Red line");
}
}
}
}, {
label: "Stock B",
fill: false,
lineTension: 0.1,
backgroundColor: "black",
borderColor: "black",
borderCapStyle: 'square',
pointBorderWidth: 0.1,
pointHoverRadius: 6,
pointHoverBorderWidth: 2,
pointRadius: 1,
data: [0, 20, 60, 95, 64],
spanGaps: false,
datalabels: {
enabled: true,
allowOverlap: true,
display: true,
listeners: {
click: function(context) {
alert(context.dataIndex + " Black Line");
}
}
}
}
]
};
// Chart declaration:
var myBarChart = new Chart(ctx, {
showTooltips: false,
type: 'line',
data: data
});
Does anyone have one idea to solve that?
You explicitly reference dataset 0. You need to pass the dataset index like so:
canvas.onclick = function(evt) {
var activePoints = myLineChart.getElementAtEvent(evt);
//console.log(activePoints);
if (activePoints[0]) {
var chartData = activePoints[0]['_chart'].config.data;
var idx = activePoints[0]['_index'];
var label = chartData.labels[idx];
// change the following line...
//var value = chartData.datasets[0].data[idx];
// to this:
var value = chartData.datasets[activePoints[0]._chart.tooltip._active[0]._datasetIndex].data[idx];
var url = "Mês:" + label + " Valor: " + value;
alert(url);
}
};
Edit: Fully working example.
var canvas = document.getElementById("lineChart");
var ctx = canvas.getContext('2d');
canvas.onclick = function(evt) {
var activePoints = myLineChart.getElementAtEvent(evt);
console.log(activePoints);
if (activePoints[0]) {
var chartData = activePoints[0]['_chart'].config.data;
var idx = activePoints[0]['_index'];
var label = chartData.labels[idx];
//var value = chartData.datasets[0].data[idx];
var value = chartData.datasets[activePoints[0]._chart.tooltip._active[0]._datasetIndex].data[idx];
var url = "Mês:" + label + " Valor: " + value;
alert(url);
}
};
var data = {
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
datasets: [{
label: "Stock A",
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(225,0,0,0.4)",
borderColor: "red",
borderCapStyle: 'square',
pointBorderWidth: 0.1,
pointHoverRadius: 6,
pointHoverBorderWidth: 2,
pointRadius: 1,
data: [2, 59, 80, 81, 56],
spanGaps: false
}, {
label: "Stock B",
fill: false,
lineTension: 0.1,
backgroundColor: "black",
borderColor: "black",
borderCapStyle: 'square',
pointBorderWidth: 0.1,
pointHoverRadius: 6,
pointHoverBorderWidth: 2,
pointRadius: 1,
data: [0, 20, 60, 95, 64],
spanGaps: false
}
]
};
// Chart declaration:
var myLineChart = new Chart(ctx, {
type: 'line',
data: data,
options: {
tooltips: {
enabled: true
}
}
});
body{
background-color: #666;
}
#lineChart{
background-color: wheat;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<div class="container">
<br />
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-10">
<!-- Chart.js Canvas Tag -->
<canvas id="lineChart"></canvas>
</div>
<div class="col-md-1"></div>
</div>
</div>
I have tried to add different colors to the Y-axis but without success.
Is it possible to achive something like attached image shows?
In the chartjs-plugin-annotation plugin you can set the background but not scale colors.
Describing image
var config = {
type: 'line',
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'Dataset',
data: [1,2,3,4,5,6,7],
fill: false,
}]
},
options: {
responsive: true,
scales: {
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Colored scale'
},
gridLines: { color: ['rgba(255, 0, 0, 1)', 'rgba(0, 255, 0, 1)', 'rgba(0, 0, 255, 1)'] }
}]
}
}
};
window.onload = function () {
var ctx = document.getElementById('canvas').getContext('2d');
window.myLine = new Chart(ctx, config);
};
<!DOCTYPE html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.bundle.min.js"></script>
</head>
<body>
<div style="width:75%;"><canvas id="canvas"></canvas></div>
</body>
In case you want the colored rectangles spread over the entire chart, you can draw individual boxes of different background color using chartjs-plugin-annotation.js.
new Chart(document.getElementById('canvas'), {
type: 'line',
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'Dataset',
data: [1, 2, 3, 4, 5, 6, 7],
fill: false,
backgroundColor: 'rgb(0, 0, 0)',
borderColor: 'rgb(0, 0, 0)'
}]
},
options: {
responsive: true,
scales: {
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Colored scale'
}
}]
},
annotation: {
annotations: [{
drawTime: "beforeDatasetsDraw",
type: "box",
xScaleID: "x-axis-0",
yScaleID: "y-axis-0",
yMin: 4.5,
yMax: 8,
backgroundColor: "rgba(255, 0, 0, 1)",
borderWidth: 0
},
{
drawTime: "beforeDatasetsDraw",
type: "box",
xScaleID: "x-axis-0",
yScaleID: "y-axis-0",
yMin: 3.5,
yMax: 4.5,
backgroundColor: "rgba(255, 165, 0, 1)",
borderWidth: 0
},
{
drawTime: "beforeDatasetsDraw",
type: "box",
xScaleID: "x-axis-0",
yScaleID: "y-axis-0",
xMin: 0,
xMax: 100,
yMin: 0,
yMax: 3.5,
backgroundColor: "rgba(50, 250, 50, 1)",
borderWidth: 0
}
]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/0.5.7/chartjs-plugin-annotation.js"></script>
<canvas id="canvas" height="90">
You can draw individual boxes directly on the canvas using the Plugin Core API. The API offers a range of hooks that may be used for performing custom code.
In below code snippet, I use the beforeDraw hook to draw the rectangles of different background color each.
new Chart(document.getElementById('canvas'), {
type: 'line',
plugins: [{
beforeDraw: chart => {
var ctx = chart.chart.ctx;
var xAxis = chart.scales['x-axis-0'];
var yAxis = chart.scales['y-axis-0'];
ctx.save();
ctx.fillStyle = 'green';
ctx.beginPath();
var yGreen = yAxis.getPixelForValue(3.5);
ctx.fillRect(xAxis.left - 6, yGreen, 6, yAxis.bottom - yGreen);
ctx.stroke();
ctx.fillStyle = 'orange';
ctx.beginPath();
var yOrange = yAxis.getPixelForValue(4.5);
ctx.fillRect(xAxis.left - 6, yOrange, 6, yGreen - yOrange);
ctx.stroke();
ctx.fillStyle = 'red';
ctx.beginPath();
var yRed = yAxis.getPixelForValue(7);
ctx.fillRect(xAxis.left - 6, yRed, 6, yOrange- yRed);
ctx.stroke();
ctx.restore();
}
}],
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'Dataset',
data: [1, 2, 3, 4, 5, 6, 7],
fill: false
}]
},
options: {
responsive: true,
scales: {
yAxes: [{
gridLines: {
tickMarkLength: 15
},
ticks: {
padding: 6
},
scaleLabel: {
display: true,
labelString: 'Colored scale'
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<canvas id="canvas" height="90">
Trying to compare this year vs last year on bar chart and labels for dataset "this year" & "previous year" are not showing. This thing is specially handy when comparing multiple datasets side by side so any help is greatly appreciated.
By the way my chartjs version is 2.1.4
var chartdata = {
type: 'bar',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
// labels: month,
datasets: [
{
label: 'this year',
backgroundColor: '#26B99A',
data: sold1
},
{
label: 'previous year',
backgroundColor: '#03586A',
data: sold2
}
]
}
};
Set autoSkip for xAxis to false :
scales: {
xAxes: [{
beginAtZero: true,
ticks: {
autoSkip: false
}
}]
}
Check your sold1 and sold2.
console.log(sold1);
console.log(sold2);
For example this is working:
var chartdata = {
{
type: 'bar',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
// labels: month,
datasets: [
{
label: 'this year',
backgroundColor: '#26B99A',
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
},
{
label: 'previous year',
backgroundColor: '#03586A',
data: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
}
]
}
}
};
var ctx = document.getElementById('chartContainer').getContext('2d');
new Chart(ctx, chartdata);
JSFiddle https://jsfiddle.net/1davgzmh/1/
this is working for me. Try youself
var chartdata = {
type: 'bar',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
datasets: [
{
label: 'this year',
backgroundColor: '#26B99A',
data: sold1
},
{
label: 'previous year',
backgroundColor: '#03586A',
data: sold2
}
]
},
"options": {
"legend": {"position": "bottom"},
"scales": {
"xAxes": [
{
"beginAtZero": true,
"ticks": {
"autoSkip": false
}
}
]
}
}
};