Chart.Js - Background Bar in Chart Bar - chart.js

I use Chart.js to create the horizontal bar of my chart. At this point I have is what is the first image.
But I need to create a "BackgroundBar" with percentage, but I do not know how I can do it. Can someone help me?
This is my output right Now.
This is the chart I want..
My code snippet is like below..
var bar_ctx = document.getElementById('bar-chart').getContext('2d');
var purple_orange_gradient = bar_ctx.createLinearGradient(0, 0, 250, 0);
purple_orange_gradient.addColorStop(0.0, 'rgb(237, 28, 36)');
purple_orange_gradient.addColorStop(0.25, 'rgb(228, 81, 173)');
purple_orange_gradient.addColorStop(0.5, 'rgb(194, 112, 215)');
purple_orange_gradient.addColorStop(0.75, 'rgb(158, 143, 239)');
purple_orange_gradient.addColorStop(1.0, 'rgb(106, 159, 247)');
var bar_chart = new Chart(bar_ctx, {
type: 'horizontalBar',
data: {
labels: ["Red", "Blue"],
datasets: [{
label: '# of Votes',
data: [12, 19],
backgroundColor: purple_orange_gradient,
hoverBackgroundColor: purple_orange_gradient,
borderWidth: 0
}]
},
options: {
scales: {
yAxes: [{
categorySpacing: 0,
barThickness: 20
}],
xAxes: [{
ticks: {
beginAtZero: true
//max:100
}
}]
}
}
});
<canvas id="bar-chart" width="300" height="125"></canvas>

This can be achieved using a ChartJS plugin called - chartjs-plugin-annotation.
DEMO
var bar_ctx = document.getElementById('bar-chart').getContext('2d');
var purple_orange_gradient = bar_ctx.createLinearGradient(0, 0, 250, 0);
purple_orange_gradient.addColorStop(0.0, 'rgb(237, 28, 36)');
purple_orange_gradient.addColorStop(0.25, 'rgb(228, 81, 173)');
purple_orange_gradient.addColorStop(0.5, 'rgb(194, 112, 215)');
purple_orange_gradient.addColorStop(0.75, 'rgb(158, 143, 239)');
purple_orange_gradient.addColorStop(1.0, 'rgb(106, 159, 247)');
var bar_chart = new Chart(bar_ctx, {
type: 'horizontalBar',
data: {
labels: ["Red", "Blue"],
datasets: [{
label: '# of Votes',
data: [12, 19],
backgroundColor: purple_orange_gradient,
hoverBackgroundColor: purple_orange_gradient,
borderWidth: 0
}]
},
options: {
scales: {
yAxes: [{
categorySpacing: 0,
barThickness: 20
}],
xAxes: [{
ticks: {
beginAtZero: true
}
}]
},
annotation: {
annotations: [{
type: 'box',
drawTime: 'beforeDatasetsDraw',
id: 'bg-bar-1',
xScaleID: 'x-axis-0',
xMin: 0,
xMax: 10,
backgroundColor: '#7f7f7f',
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<script src="https://cdn.rawgit.com/chartjs/chartjs-plugin-annotation/master/chartjs-plugin-annotation.min.js"></script>
<canvas id="bar-chart" width="300" height="125"></canvas>
To learn more about this plugin and it­'s use cases, refer here.

Related

Draw stacked horizontal bar chart with Average line using ChartJS

I am trying to create a stacked horizontal bar chart with an average line on each bar using the ChartJS library. I tried several times and searched on the Internet but I did not find out any satisfying answers. I appreciate your helping. Thanks.
Example:
.
In this example, the red line on each bar implies the average value of its group.
To create the stacked horizontal bar chart, please see
https://jsfiddle.net/lamtn862004/9wm1krqf/10/.
var data = {
labels: ["January", "February", "March"],
datasets: [
{
label: "Dogs",
backgroundColor: "rgba(237, 192, 169)",
data: [20, 10, 25],
stack: 1,
xAxisID: 'x-axis-0',
yAxisID: 'y-axis-0'
},
{
label: "Cats",
backgroundColor: "rgba(253, 234, 175)",
data: [70, 85, 65],
stack: 1,
xAxisID: 'x-axis-0',
yAxisID: 'y-axis-0'
},
{
label: "Birds",
backgroundColor: "rgba(179, 211, 200)",
data: [10, 5, 10],
stack: 1,
xAxisID: 'x-axis-0',
yAxisID: 'y-axis-0'
},
]
};
var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, {
type: 'groupableHBar',
data: data,
options: {
scales: {
yAxes: [{
stacked: true,
type: 'category',
id: 'y-axis-0'
}],
xAxes: [{
stacked: true,
type: 'linear',
ticks: {
beginAtZero:true
},
gridLines: {
display: false,
drawTicks: true,
},
id: 'x-axis-0'
},
{
stacked: true,
position: 'top',
type: 'linear',
ticks: {
beginAtZero:true
},
id: 'x-axis-1',
gridLines: {
display: true,
drawTicks: true,
},
display: false
}]
}
}
});
Now, I want to add the average line to each bar (as shown in the image).
Also, do you know the other solutions with open-source libraries for doing this?
The lines can be drawn using floating bars tied to the separate axis y2. To do so, you need to add another dataset as follows (I don't exactly understand what values you want to display, hence I randomly chose 40, 65 and 55):
{
label: "Lines",
backgroundColor: "rgb(255, 0, 0)",
data: [40, 65, 55].map(v => [v - 0.3, v + 0.3]),
yAxisID: 'y2',
order: -1
}
Further you must define a new scales option as shown below:
y2: {
display: false
}
Please take a look at your amended runnable code and see how it works.
var data = {
labels: ["January", "February", "March"],
datasets: [{
label: "Dogs",
backgroundColor: "rgba(237, 192, 169)",
data: [20, 10, 25]
},
{
label: "Cats",
backgroundColor: "rgba(253, 234, 175)",
data: [70, 85, 65]
},
{
label: "Birds",
backgroundColor: "rgba(179, 211, 200)",
data: [10, 5, 10]
},
{
label: "Lines",
backgroundColor: "rgb(255, 0, 0)",
data: [40, 65, 55].map(v => [v - 0.3, v + 0.3]),
yAxisID: 'y2',
order: -1
}
]
};
new Chart('myChart', {
type: 'bar',
data: data,
options: {
indexAxis: 'y',
plugins: {
legend: {
display: false
}
},
scales: {
y: {
stacked: true,
grid: {
display: false
}
},
y2: {
display: false
},
x: {
stacked: true,
beginAtZero: true,
grid: {
display: false
}
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/chart.min.js"></script>
<canvas id="myChart" height="100"></canvas>

break y axis in line chart chart.js

I am trying to render a line graph with two datasets
My sample chart
<canvas id="myChart"></canvas>
<script>
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'line',
// The data for our dataset
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [
{
label: 'My First dataset',
borderColor: 'rgb(255, 99, 132)',
fill: false,
data: [9000, 9020, 9010, 9004, 9011, 9040, 9050]
},
{
label: 'My Second dataset',
borderColor: 'rgb(0, 99, 132)',
fill: false,
data: [1000, 1020, 1010, 1004, 1011, 1040, 1050]
}
]
},
options: {
scales: {
yAxes: [{
ticks: {
stepSize: 10
}
}]
}
}
});
</script>
This is how it looks currently current result
This the graph will be easy to read if it has axis break like this
expected result
There is no build in way to do this. The best thing you can do is use a second Y axes and link the second dataset to the other axes.
datasets: [
{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1,
yAxisID: 'first'
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1,
yAxisID: 'second'
}
]
options: {
scales: {
yAxes: [{
id: 'first'
},{
id: 'second'
}]
}
}

Is there anyway add 2 or more chart with foreach loop

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
</head>
<body>
<script>
<!--bar stacked-->
var data2 = {
CC: [{
code: '123ASD',
labels: ["7/2/2018", "7/3/2018", "7/4/2018", "7/5/2018", "7/6/2018", "7/9/2018", "7/10/2018"],
datasets: [
{
label: 'AAA',
backgroundColor: "rgba(255,0,0,1)",
data: [32,41,21,78,0,0,0]
},
{
label: 'BBB',
backgroundColor: "rgba(0,255,0,1)",
data: [10,80,12,70,65,44,18]
},
{
label: 'CCC',
backgroundColor: "rgba(0,0,205,1)",
data: [915,400,233,398,334,766,1071]
},
]}
,
{
code: 'ASD123',
labels: ["7/2/2018", "7/3/2018", "7/4/2018", "7/5/2018", "7/6/2018", "7/9/2018", "7/10/2018"],
datasets: [
{
label: 'AAA',
backgroundColor: "rgba(255,0,0,1)",
data: [55,22,40,2,0,0,0]
},
{
label: 'BBB',
backgroundColor: "rgba(0,255,0,1)",
data: [10,2,100,0,17,55,74]
},
{
label: 'CCC',
backgroundColor: "rgba(0,0,205,1)",
data: [232,555,800,900,723,1001,942]
},
]
},
]
};
data2.CC.forEach( function(i, item){
document.body.innerHTML += '<canvas id="'+i.code+'"></canvas>'
var ctx2 = document.getElementById(i.kodu).getContext('2d');
window.myBar = new Chart(ctx2, {
type: 'bar',
data: data2.AA[0],
options: {
title: {
display: true,
text: i.code
},
tooltips: {
mode: 'index',
intersect: false
},
responsive: true,
scales: {
xAxes: [{
stacked: true,
}],
yAxes: [{
stacked: true
}]
}
}
});
});
</script>
</body>
</html>
when I run I get this error: Uncaught TypeError: Cannot read property 'currentStyle' of null and my first chart can't load
I try Uncaught TypeError: Cannot read property 'currentStyle' of null in ChartJS but my first chart again dont load
Is there anyone help?
You had a couple of issues:
Chart reference was being overridden by the last chart in the collection
A few references to variables did not exist from your original source dump
Chart data source reference needed to be changed
document.addEventListener('DOMContentLoaded', function(){
var chartData = {
CC: [{
code: '123ASD',
labels: ["7/2/2018", "7/3/2018", "7/4/2018", "7/5/2018", "7/6/2018", "7/9/2018", "7/10/2018"],
datasets: [{
label: 'AAA',
backgroundColor: "rgba(255,0,0,1)",
data: [32,41,21,78,0,0,0]
},{
label: 'BBB',
backgroundColor: "rgba(0,255,0,1)",
data: [10,80,12,70,65,44,18]
},{
label: 'CCC',
backgroundColor: "rgba(0,0,205,1)",
data: [915,400,233,398,334,766,1071]
}]
},{
code: 'ASD123',
labels: ["7/2/2018", "7/3/2018", "7/4/2018", "7/5/2018", "7/6/2018", "7/9/2018", "7/10/2018"],
datasets: [{
label: 'AAA',
backgroundColor: "rgba(255,0,0,1)",
data: [55,22,40,2,0,0,0]
},{
label: 'BBB',
backgroundColor: "rgba(0,255,0,1)",
data: [10,2,100,0,17,55,74]
},{
label: 'CCC',
backgroundColor: "rgba(0,0,205,1)",
data: [232,555,800,900,723,1001,942]
}]
}]
};
chartData.CC.forEach(function(data, index){
var canvas = document.createElement('canvas'),
chartId = 'chart' + data.code;
canvas.id = chartId;
document.body.appendChild(canvas);
var context = document.getElementById(chartId).getContext('2d');
window[chartId] = new Chart(context, {
type: 'bar',
data: data,
options: {
title: {
display: true,
text: data.code
},
tooltips: {
mode: 'index',
intersect: false
},
responsive: true,
scales: {
xAxes: [{
stacked: true,
}],
yAxes: [{
stacked: true
}]
}
}
});
});
});
Stack snippet is throwing a script error because of chart.js so I created a Codepen
You charts are not being loaded because your code should be item instead of data2.AA[0]. Also, you do not have any property with name kodu in your data object. Check out working version below:
var data2 = {
CC: [{
code: '123ASD',
labels: ["7/2/2018", "7/3/2018", "7/4/2018", "7/5/2018", "7/6/2018", "7/9/2018", "7/10/2018"],
datasets: [{
label: 'AAA',
backgroundColor: "rgba(255,0,0,1)",
data: [32, 41, 21, 78, 0, 0, 0]
},
{
label: 'BBB',
backgroundColor: "rgba(0,255,0,1)",
data: [10, 80, 12, 70, 65, 44, 18]
},
{
label: 'CCC',
backgroundColor: "rgba(0,0,205,1)",
data: [915, 400, 233, 398, 334, 766, 1071]
},
]
},
{
code: 'ASD123',
labels: ["7/2/2018", "7/3/2018", "7/4/2018", "7/5/2018", "7/6/2018", "7/9/2018", "7/10/2018"],
datasets: [{
label: 'AAA',
backgroundColor: "rgba(255,0,0,1)",
data: [55, 22, 40, 2, 0, 0, 0]
},
{
label: 'BBB',
backgroundColor: "rgba(0,255,0,1)",
data: [10, 2, 100, 0, 17, 55, 74]
},
{
label: 'CCC',
backgroundColor: "rgba(0,0,205,1)",
data: [232, 555, 800, 900, 723, 1001, 942]
},
]
},
]
};
data2.CC.forEach(function(item, arr) {
var chartId = item.code;
var canvas = document.createElement("Canvas");
var ctx = canvas.getContext('2d');
var chart = new Chart(ctx, {
type: 'bar',
data: item,
options: {
title: {
display: true,
text: chartId
},
tooltips: {
mode: 'index',
intersect: false
},
responsive: true,
scales: {
xAxes: [{
stacked: true,
}],
yAxes: [{
stacked: true
}]
}
}
});
document.body.appendChild(canvas);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>

Change line point to triangle in Chart.js

I have a line chart of which the data points are rounded, but I want it to be triangle. Is there any way to do this?
Here is my chart code:
var lineChart = new Chart(ctx, {
type: 'line',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
datasets: [{
label: 'Sales Point',
data: salesData,
borderColor: bdrColor,
fill: false
}]
},
Thanks in advance.
Set pointStyle property to triangle for your dataset, like so :
...
datasets: [{
label: 'Sales Point',
data: salesData,
borderColor: bdrColor,
fill: false,
pointStyle: 'triangle'
}]
...
ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩
var chart = new Chart(ctx, {
type: 'line',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
datasets: [{
label: 'Sales Point',
data: [3, 1, 4, 2, 5],
backgroundColor: 'rgba(0, 119, 290, 0.2)',
borderColor: 'rgba(0, 119, 290, 0.6)',
fill: false,
pointRadius: 10,
pointStyle: 'triangle'
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
stepSize: 1
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx"></canvas>

ChartJS row size

How to set a fixed row size in horizontal bar. I tried with
yAxes: [{
barPercentage: 1.0,
categoryPercentage: 1.0
}]
but it resized bar and i need fixed size.
The perfect would be chart with fixed row size (bar, label) and independent of the amount of data (scrollbar). Can I implement it with chartJS?
horizontal bar
code
Thanks.
Here is the update JS code. which will fix the fixed row size in horizontal bar.
https://jsfiddle.net/1knt2md8/
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'horizontalBar',
data: {
labels: [
['Label_1_Line_1', 'Label_1_Line_2', 'Label_1_Line_3'],
['Label_2_Line_1', 'Label_2_Line_2', 'Label_2_Line_3'],
['Label_3_Line_1', 'Label_3_Line_2', 'Label_3_Line_3'],
['Label_4_Line_1', 'Label_4_Line_2', 'Label_4_Line_3'],
['Label_5_Line_1', 'Label_5_Line_2', 'Label_5_Line_3'],
['Label_6_Line_1', 'Label_6_Line_2', 'Label_6_Line_3'],
['Label_7_Line_1', 'Label_7_Line_2', 'Label_7_Line_3'],
],
datasets: [{
label: "My First dataset",
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: [0, 10, 5, 2, 20, 30, 70],
}]
},
options: {
scales: {
yAxes: [{
maxBarThickness : 80
}],
xAxes: [{
ticks: {
mix: 0,
max: 40,
stepSize: 5
}
}]
}
}
});