Chart.js how to set start and end values to plot - javascript

I am trying to implement chart.js in my angular app.(horizontal bar chart)
I would like to add the start and end point for the data to be plotted.
In my dataset whenever i pass a value the graph plots from 0 to the given value.
data: [2,6,5,8,7,3,2]
when this data gets plotted the graph shows from 0 to 2.
But i want to show it from 1 to 2.
Is there a way to pass the start value and end value to be shown in the graph.(not the label the data inside of dataset.)
my data:
var data = {
labels: [10,9,8,7,6,5,4,3,2,1,0],
datasets: [
{
label: "My First dataset",
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1,
data: [2,6,5,8,7,3,2],
}
]
};
var options ={
barShowStroke: false,
scaleShowGridLines: true,
scaleOverride: true,
scaleStartValue: 1,
scaleStepWidth: 1,
responsive: true,
barBeginAtOrigin: true,
scales: {
xAxes: [{
time: {
unit: 'month'
},
ticks:{
min: 0,
max: 20,
stepSize: 1
}
}],
yAxes: [{
display:true}]
}
};
EDIT:
Here i want the y-axis 4 to start from 1 in the x-axis to 2 in the x-axis.
The y-axis 6 to start from 3 in the x-axis to 7 in the x-axis.

Yes possible, check the options provided for (scale) chart below :
var ctx = $("#graphTest").get(0).getContext("2d");
var options = {
barShowStroke: false,
scaleShowGridLines: true,
scaleOverride: true,
scaleStartValue: 1,
caleStepWidth: 1,
responsive: true,
barBeginAtOrigin: true
};
var data = {
labels: [10,9,8,7,6,5,4,3,2,1],
datasets: [
{
label: "My First dataset",
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1,
data: [2,6,5,8,7,3,2],
}
]
};
var myBarChart = new Chart(ctx, {
type: 'horizontalBar',
data: data,
options: options
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.js"></script>
<canvas id="graphTest" width="400" height="190" style="padding-left:10px; padding-right:10px;"></canvas>

Related

chart.js function delay not affect my graph

I am asking you because I created a chart with chart.js and I would like to assign it a delay before it appears, after reading the docs and different forums I get the following code which unfortunately does not work. Thanking you in advance for your help!
var cts = document.getElementById('myDonutChart').getContext('2d');
var myDonutChart = new Chart(cts, {
type: 'doughnut',
data: {
labels:[
//requete sql
'Red',
'Blue',
'Yellow'
],
datasets: [{
label: '# of Votes',
data: [
//requete sql
12,
19,
3,
5,
2,
3
],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
legend: { display: false },
responsive: false,
maintainAspectRatio: false,
animation: {
delay: 4000
}
}
});
Reading the doc gives me the result with the delay function or alone it doesn't work and the only solution I found was to add a setimout but I still wanted to find the solution to the delay problem

Chart.js not to be begin in Y-Axis

this is what the code looks like right now
With an 1st image preview currently using Chart.js.
Please give me some insights on how my data chart not to begin in Y axis.
ACTUAL RESULT of the Code below.
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: [<?php echo trim($labels);?>],
datasets: [{
label: 'Weekly Revenue',
data: [ 0, 100, 3000, 4000, 5000, 6000, 7000, 8000,9000,10000,],
fill: false,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 3
},
]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: function(value, index, values) {
return value.toLocaleString("en-PH",{style:"currency", currency:"PHP"});
}
}
}]
}
}
});
<div class="chart-container" style="min-width: 210px; height: 400px; margin: 0 auto">
<canvas id="myChart"></canvas>
</div>
Aiming: Data does not begin in Y-axis.
The easiest way to produce a margin between the Y axis and your first point is to add an empty string spacer in labels and a null value in data:
{
type: 'line',
data: {
labels: ['', '2020-12-27','2020-12-28','2020-12-29','2020-12-31','2020-12-31','2020-01-01','2020-01-02','2020-01-03','2020-01-04','2020-01-05'],
datasets: [{
label: 'Weekly Revenue',
data: [ null, 0, 100, 3000, 4000, 5000, 6000, 7000, 8000,9000,10000,],
fill: false,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 3
},
]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: function(value, index, values) {
return value.toLocaleString("en-PH",{style:"currency", currency:"PHP"});
}
}
}]
}
}
}

Display json format data in smarty chartjs

<script>
{literal}
var ctx = document.getElementById('myGlaciermassChart').getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'line',
// The data for our dataset
data: {
labels: [{/literal}
{foreach from=$glaciermass item=year name=glaciermassloop}
{if $smarty.foreach.glaciermassloop.last}
"{$year.Year}"
{else}
"{$year.Year}",
{/if}
{/foreach}
{literal}],
datasets: [{
label: 'Mean Glacier mass Level',
data: [{/literal}
{foreach from=$glaciermass item=year name=glaciermassloop}
{if $smarty.foreach.glaciermassloop.last}
"{$year.Mean-cumulative-mass-balance}"
{else}
"{$year.Mean-cumulative-mass-balance}",
{/if}
{/foreach}
{literal}],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
suggestedMax: 0,
suggestedMin:-30,
beginAtZero: true
}
}]
}
}
});
{/literal}
</script>
I am using Chart.js and the data from an API endpoint is not displaying the right Mean values on the screen. I have tried many different ways and I have even tried selecting an index, but it doesn't work.Currently the Mean values are showing only 0s on the chart for all the years instead of the actual values from the json - Here is the json itself. https://pkgstore.datahub.io/core/glacier-mass-balance/glaciers_json/data/6270342ca6134dadf8f94221be683bc6/glaciers_json.json.
<?php
$url ='https://pkgstore.datahub.io/core/glacier-mass-balance/glaciers_json/data/6270342ca6134dadf8f94221be683bc6/glaciers_json.json';
$response = file_get_contents($url);
$GlaciermassData= json_decode($response, true);
$smarty->assign('glaciermass', $GlaciermassData);
?>
Have you tried accessing each index by number?

Is it possible to create just header tip visible bar chart with chart js just like in image

I want to change bar graph to make it look like the image one, which has only header tip visible not the whole bar, the header has the detail like shown in the image and only the header visible as a line in graph
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["2015-01", "2015-02", "2015-03"],
datasets: [{
label: '# of Tomatoes',
data: [12, 19, 13],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)'
],
borderWidth: 1
}]
},
options: {
responsive: false,
scales: {
xAxes: [{
ticks: {
maxRotation: 90,
minRotation: 80
}
}],
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
body {
background-color: #a3d5d3;
}
<canvas id="myChart" height="200"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.6/Chart.js"></script>
If you just want to display a dash for each value, you better choose a scatter chart and define dataset.pointStyle as 'line'. For scatter charts, no labels are defined but the data must be passed as objects containing x and y properties. Your xAxes would then have to be defined as a time cartesian axis.
Note that Chart.js internally uses Moment.js for the functionality of the time axis. Therefore you should use the bundled version of Chart.js that includes Moment.js in a single file.
The following code illustrates how it can be done.
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'scatter',
data: {
datasets: [{
label: '# of Tomatoes',
data: [
{ x: "2015-01", y: 12 },
{ x: "2015-02", y: 19 },
{ x: "2015-03", y: 3 },
{ x: "2015-04", y: 5 },
{ x: "2015-05", y: 2 },
{ x: "2015-06", y: 3 },
{ x: "2015-07", y: 20 },
{ x: "2015-08", y: 3 },
{ x: "2015-09", y: 5 },
{ x: "2015-10", y: 6 },
{ x: "2015-11", y: 2 },
{ x: "2015-12", y: 1 }
],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)',
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)',
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
pointStyle: 'line',
pointRadius: 10,
pointHoverRadius: 10
}]
},
options: {
responsive: false,
scales: {
xAxes: [{
offset: true,
type: 'time',
time: {
unit: 'month',
displayFormats: {
month: 'YYYY-MM'
},
tooltipFormat: 'YYYY-MM'
},
ticks: {
maxRotation: 90,
minRotation: 80
},
gridLines: {
offsetGridLines: true
}
}],
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
body {
background-color: #a3d5d3;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<canvas id="myChart" height="200"></canvas>
If I correctly understand your question, you need floating bars, which are officially available since Chart.js v2.9.0. The feature was merged into chartjs:master with pull request #6056. Individual bars can now be specified with the syntax [min, max].
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["2015-01", "2015-02", "2015-03", "2015-04", "2015-05", "2015-06", "2015-07", "2015-08", "2015-09", "2015-10", "2015-11", "2015-12"],
datasets: [{
label: '# of Tomatoes',
data: [[11,12], [18,19], [2,3], [4,5], [1,2], [2,3], [19,20], [2,3], [4,5], [5,6], [1,2], [0,1]],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)',
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)',
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
responsive: false,
scales: {
xAxes: [{
ticks: {
maxRotation: 90,
minRotation: 80
}
}],
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
body {
background-color: #a3d5d3;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" height="200"></canvas>

Chart js - Customize chart

I'm trying to edit a chart but i can't.
I want to change the canvas height to a fix height. The chart start always with 1156px X 867. And i need to change also the heigh of each bar.
So, how can i change the size of canvas and how can i change the height of each bar.
I have this code:
var ctx = document.getElementById("chart");
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
labels: data['labels'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
html:
<canvas id="chartMentes" width="400" height="300"></canvas>
How can I change the size of canvas?
Usually, ChartJS displays chart in a responsive manner, meaning it won't take canvas­'s width and height (set by user) into consideration.
To get around this, you need to set responsive property to false in your chart options, like so ...
options: {
responsive: false,
...
}
Now, you could set canvas­'s width and height as per your requirement.
<canvas id="chartMentes" width="400" height="800"></canvas>
How can I change the height of each bar?
You can change the height of each bar by setting the barThickness property accordingly for y-axis in the chart options, like so ...
options: {
scales: {
yAxes: [{
barThickness: 10, //set that suits the best
...
}
ᴅᴇᴍᴏ
var ctx = document.getElementById("chartMentes").getContext('2d');
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'],
borderWidth: 1
}]
},
options: {
responsive: false,
scales: {
yAxes: [{
barThickness: 10,
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<canvas id="chartMentes" width="300" height="200"></canvas>

Categories

Resources