Chartjs: Overlap of color fill between 2 line series react chartjs - chart.js

while filling the color between 2 line series the fill color overlaps the point.
https://codesandbox.io/s/react-chartjs-2-line-chart-example-forked-5ruj86
I having this issue in version 3.6.0.

Just add order property to the first dataset. Documentation
const data = {
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
datasets: [
{
data: [1000000, 900000, 800000, 750000, 700000, 650000, 600000],
color: "#008484",
backgroundColor: "#A5E1D2",
pointBorderColor: "#007B5E",
pointBackgroundColor: "#ffffff",
borderColor: "#008484",
fill: 1,
borderWidth: 3,
pointRadius: 5,
pointHoverRadius: 5,
pointBorderRadius: 5,
order: 1
},
{
data: [800000, 700000, 600000, 550000, 500000, 450000, 400000],
color: "#002E2E",
backgroundColor: "#A5E1D2",
pointBorderColor: "#002E2E",
pointBackgroundColor: "#ffffff",
borderColor: "#002E2E",
fill: 1,
borderWidth: 3,
pointRadius: 5,
pointHoverRadius: 5,
pointBorderRadius: 5
}
]
};
Checkout the codesandbox

Related

How we can draw a circle in bar chart

I am new to chart.js I want to draw a circle inside bar charts or at baseline like the picture circle in blue color below how can I achieve this
help me thanks in advance
In your existing bar chart configuration, you can define a second dataset of type: 'scatter' as follows:
{
type: 'scatter',
data: [0, 0, 0, 0, 0, 0, 0],
backgroundColor: "rgb(0, 0, 255)",
borderColor: "rgb(255, 255, 255)",
pointRadius: 7,
pointHoverRadius: 7,
borderWidth: 2
}
Please take a look at the runnable code below and see how it works.
new Chart("chart", {
type: "bar",
data: {
labels: ["A", "B", "C", "D", "E", "F", "G"],
datasets: [{
data: [65, 59, 80, 81, 56, 55, 40],
barPercentage: 0.5,
backgroundColor: "rgba(201, 203, 207, 0.2)",
borderColor: "rgb(201, 203, 207)",
borderWidth: 1
},
{
type: 'scatter',
data: [0, 0, 0, 0, 0, 0, 0],
backgroundColor: "rgb(0, 0, 255)",
borderColor: "rgb(255, 255, 255)",
pointRadius: 7,
pointHoverRadius: 7,
borderWidth: 2
}
]
},
options: {
legend: {
display: false
},
tooltips: {
enabled: false
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
canvas {
max-width: 300px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
<canvas id="chart" height="200"></canvas>

How to make multiple stacked charts to share the same yAxis?

I have 2 charts with different data, stacked on top of each other. How do I make those 2 charts share the same scale of 1 y-axis?
I configured the y-Axis as follow:
yAxes: [{
display: true,
stacked: true,
type: "linear",
scaleLabel: {
display: true,
labelString: "Amount in USD"
},
ticks: {
beginAtZero: true,
max: 150000,
callback: function(label, index, labels) {
return label / 1000 + "k";
}
}
}]
And the data set as follow:
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
backgroundColor: "#FFE0B2",
datasets: [{
label: "Amount of Sales - YTD",
data: [0, 0, 0, 0, 0, 0, 0, 104920, 87461, 22700, 0, 0],
backgroundColor: [
"#FFE0B2"
],
borderColor: [
"#FF9800"
],
borderWidth: 1,
pointBackgroundColor: "#FFE0B2",
pointBorderColor: "#FF9800",
pointBorderWidth: 2
}, {
label: "Amount of Commission - YTD",
data: [0, 0, 0, 0, 0, 0, 0, 1896, 4373, 1135, 0, 0],
backgroundColor: [
"#F8BBD0"
],
borderColor: [
"#E91E63"
],
borderWidth: 1,
pointBackgroundColor: "#F8BBD0",
pointBorderColor: "#E91E63",
pointBorderWidth: 2
}]
Please have a look at: https://jsfiddle.net/pyzaq4j3/
As you can see, in August, the point (orange) is set at ~100k, which is correct.
However, the point (pink) has the value of only ~1.8k, and the point is almost at ~100k, according to the graph.
Question: How do I set it so that the pink graph shares the same y-axis as the orange graph?
The problem is with this line in the y-axis:
stacked: true,
If you'll remove it, it won't stack the values together.
However, since you specified backgroundColor on your datasets, and the first dataset is "Amount of Sales - YTD" which has higher values, it will block the visual of your second dataset.

ChartJS do not render any legend if the label is falsy

I am using ChartJS 2.4 and I want to predefine lots of settings in the datasets already. But use an empty data array and an undefined label. Now I want to stop ChartJS from rendering the legend which is then shown as "null" or "undefined". Later I am setting a proper label and start to push data to the dataSet and finally call update() on the chart which works fine.
Here is a basic fiddle with this code:
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(75,192,192,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(75,192,192,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
pointRadius: 5,
pointHitRadius: 10,
data: [],
}
]
};
var myLineChart = new Chart("myChart", {
type: "line",
data: data
});
setTimeout(function(){
data.datasets[0].label = "The Label"
data.datasets[0].data.push(10, 20)
myLineChart.update()
}, 2000)
I have tried to overwrite generateLabels but I had no luck with that. And the setting dateaset.hidden only strikes through the legend if true but not really "hides".
EDIT 1: On the other hand setting options.legend.display to false hides all the legends and for ever, this is also not what I need.
Let us create a plugin that, at the start of every update, decides whether to display the legend or not based on whether the label of the first dataset is (respectively) defined or not. The autoDisplayLegend chart option enables the plugin, if set to true.
A working fiddle is here. The code is also available below.
var autoDisplayLegendPlugin = {
// Called at start of update.
beforeUpdate: function(chartInstance) {
if (chartInstance.options.autoDisplayLegend) {
// The first (and, assuming, only) dataset.
var dataset = chartInstance.data.datasets[0];
if (dataset.label)
chartInstance.options.legend.display = true;
else
chartInstance.options.legend.display = false;
}
}
};
Chart.pluginService.register(autoDisplayLegendPlugin);
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(75,192,192,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(75,192,192,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
pointRadius: 5,
pointHitRadius: 10,
data: [],
}]
};
var myLineChart = new Chart("myChart", {
type: "line",
data: data,
options: {
// Option to auto display the legend.
autoDisplayLegend: true
}
});
setTimeout(function() {
data.datasets[0].label = "The Label"
data.datasets[0].data.push(10, 20)
myLineChart.update()
}, 2000)
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.bundle.js"></script>
<canvas id="myChart" width="400" height="250"></canvas>
if you want, you can delete the dataset so the legend will not display:
var autoDisplayLegendPlugin = {
beforeUpdate: function(chartInstance) {
if (chartInstance.canvas.id == 'chart6') {
// The first (and, assuming, only) dataset.
var dataset = chartInstance.data.datasets[2];
if (dataset.data.length != 0){ // delete only if there is no data in dataset
console.log(chartInstance);
chartInstance.options.legend.display = true;
}
else{
chartInstance.options.legend.display = true;
chartInstance.data.datasets.pop(); // if is the last datasets you don't want the legend to appear
}
}
}};
You can filter the labels and use that to remove a falsy text
const data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(75,192,192,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(75,192,192,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
pointRadius: 5,
pointHitRadius: 10,
data: [],
}]
};
const myLineChart = new Chart("myChart", {
type: "line",
data: data,
options: {
plugins: {
legend: {
labels: {
filter: (legendItem) => (!!legendItem.text)
}
}
}
}
});
setTimeout(function() {
data.datasets[0].label = "The Label"
data.datasets[0].data.push(10, 20)
myLineChart.update()
}, 2000)
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.js"></script>
<canvas id="myChart" width="400" height="250"></canvas>
The main reason is ,it's showing
undefined
because you haven't defined the label property for your datasets.
datasets: [{
data: [1,2,6,3,8,9,5],
label:'your label',
borderColor: "red",
fill: false
}]

Unable to get Line Chart tooltip on ChartJS

i want to create multi line chart using ChartJS. multi line chart is working fine. but tool-tip only i am getting problem. please check below code and image.
var ctx = document.getElementById("myChart").getContext("2d");
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(75,192,192,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(75,192,192,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
data: [65, 59, 80, 81, 56, 55, 40],
spanGaps: false,
},
{
label: "My Second dataset",
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(255, 99, 132, 1)",
borderColor: "rgba(255, 99, 132, 1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(255, 99, 132, 1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(255, 99, 132, 1)",
pointHoverBorderColor: "rgba(255, 99, 132, 1)",
pointHoverBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
data: [40, 59, 80, 100, 56, 80, 70],
spanGaps: false,
}
]
};
var myLineChart = new Chart(ctx, {
type: 'line',
data: data
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>
January month i have two data ["My first dataset":65, "My Second Dataset": 40] so tool-tip showing correctly but in march month i have two data ["My first dataset":80, "My Second Dataset": 80] but tool-tip showing only "My First dataset" value only so i m not able to find "My Second Dataset" value.
I tried like this. now its working fine
options: {
tooltips: {
mode: 'label'
}
}

Chart JS show multiple data points for the same label

I am using Chart JS for the first time and I wanted to show multiple data points for the same label. Can I do this without creating multiple datasets each time. Also, I will know the number of datasets only on run time.
Use case: For each point on x-axis(label) plot multiple points on y-axis
Right now I am doing something like this
var ctx = $(element);
var myChart = new Chart(ctx, {
type: graphType,
data: {
labels: labels,
datasets: [
{
label: labelTeacher,
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(100,149,237,0.5)",
borderColor: "rgba(100,149,237,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(58,95,205,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(100,149,237,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
data: [65, 59, 90, 81, 56, 55, 40]
},
{
label: labelVedantu,
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(255,165,0,0.5)",
borderColor: "rgba(255,165,0,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(255,165,0,1)",
pointBackgroundColor: "rgba(255,255,255,1)",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(255,165,0,1)",
pointHoverBorderColor: "rgba(250,228,196,1)",
pointHoverBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
data: [28, 48, 40, 19, 96, 27, 100]
}
]
},
Let us say I have multiple data arrays for the same label set, is there a way to do it without adding a new dataset each time
When you say 'show', are you referring to in how the chart renders, or in the "tooltips" that appear when you hover over the chart?
If the latter, check out the mode:'index' setting for options/tooltips, as described here:
http://www.chartjs.org/docs/latest/general/interactions/modes.html#interaction-modes