Related
I want to create a shortcode for chart.js, when I add this code without function in a page then it works fine, however, When I add this code in fuction.php it breaks the site. I already have tried my methods.
function chartJS_shortcode(){
?><canvas id="myChart" width="400" height="400"></canvas><?php
<script>
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
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: {
y: {
beginAtZero: true
}
}
}
});
</script>
}
add_shortcode("chartJS", "chartJS_shortcode");
Always use return with shortcodes, try following it will work
<?php function chartJS_shortcode(){
ob_start();
?><canvas id="myChart" width="400" height="400"></canvas>
<script>
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
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: {
y: {
beginAtZero: true
}
}
}
});
</script>
<?php
return ob_get_clean();
}
add_shortcode("chartJS", "chartJS_shortcode"); ?>
I am trying to visualize data in my Django project using Chartjs. right now I'm only concerned with the visualize part and not the backend stuff but for some strange reason the charts refuses to display even though I took the boilerplate directly from the website just to display it before manipulating it.
<div id="chart_div" class="option-chart">
<canvas id="myChart" width="648" height="495"></canvas>
</div>
<script>
const ctx = document.getElementById('myChart');
const myChart = {
type : 'bar',
data : {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
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: {
y: {
beginAtZero: true
}
}
}
};
</script>
As you can see from the code above. its boilerplate but I just cant get it to visualize.
I think you have not added the cdn link
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"});
}
}
}]
}
}
}
I have an object that has a 10 keys (names of US states) and a value attached. I want to know how to make the key the label and the value the data in Chart.js but I can't seem to find anything on this.
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: KEYS IN OBJECT GOES HERE,
datasets: [{
label: '# of Votes',
data: VALUE OF KEYS GO HERE,
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
}
}]
}
}
});
below is my object
var stateGraphOne = {
"Oklahoma": 229,
"Oregon": 107,
"Pennsylvania": 165,
"South Carolina": 15,
"South Dakota": 96,
"Tennessee": 460,
"Texas": 2682,
"Utah": 372,
"Vermont": 13,
"Virginia": 33
};
I'm going to have multiple graphs and I have multiple objects to do the same thing with. If this would be easier using d3.js I am open to using that too. Thanks in advance.
You can get the object keys and values like so:
labels: Object.keys(stateGraphOne),
datasets: [{
label: '# of Votes',
data: Object.values(stateGraphOne),
...
A quick search and... another answer
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>