I am working on google chart API. I have a column chart where i want to show a small line near every label shown near h-axis as shown in the sample image below.
What modifications need to be done to show the small line near each label shown on h-axis.
Demo : https://jsfiddle.net/8tvm9qk5/
js code:
google.charts.load('current', {packages: ['corechart', 'bar']});
google.charts.setOnLoadCallback(drawStacked);
function drawStacked() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Departamentos');
data.addColumn('number', 'Entregados');
data.addColumn('number', 'En Stock');
data.addRows([
['abdasdasa', 925, 786],
['bbdasdas', 652, 156],
['cbdasdas', 300, 200],
['ddasdasb', 925, 786],
['edasdb', 652, 156],
['fasdasb', 300, 200],
['gdasdasdb', 925, 786],
['abdasdasa', 925, 786],
['bbdasdas', 652, 156],
['cbdasdas', 300, 200],
['ddasdasb', 925, 786],
['edasdb', 652, 156],
['fasdasb', 300, 200],
['gdasdasdb', 925, 786]
]);
var options = {
title: 'Motivation and Energy Level Throughout the Day',
isStacked: true,
height:600,
chartArea: {
height:300,
top:100,
},
hAxis: {
title: 'Departamentos',
titleTextStyle: {
color: '#FF0000',
},
slantedText:true,
slantedTextAngle:45,
},
vAxis: {
title: 'Kits'
}
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
--EDITED--
I tried as shown below, but could not get the expected result.
sample code:
var data = new google.visualization.DataTable();
data.addColumn('string', 'Departamentos');
data.addColumn('number', 'Entregados');
data.addColumn('number', 'En Stock');
data.addColumn('number', 'dumy');
data.addColumn({type:'string', role:'annotation'});
data.addRows([
['abdasdasa', 925, 786,0, ''],
['bbdasdas', 652, 156,0, ''],
['cbdasdas', 300, 200,0, ''],
.....]);
PS: I tried by interchanging the column sequence too:
...
data.addColumn({type:'string', role:'annotation'});
data.addColumn('number', 'dumy');
Demo:https://jsfiddle.net/1xq2nj16/
the dummy series needs to be first when --> isStacked: true
or change the series to a different type to break from the stack...
(see series option in following snippet)
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Departamentos');
data.addColumn('number', 'dumy');
data.addColumn({type:'string', role:'annotation'}); // interval role col.
data.addColumn('number', 'Entregados');
data.addColumn('number', 'En Stock');
data.addRows([
['abdasdasa', 0, '', 925, 786],
['bbdasdas', 0, '', 652, 156],
['cbdasdas', 0, '', 300, 200],
['ddasdasb', 0, '', 925, 786],
['edasdb', 0, '', 652, 156],
['fasdasb', 0, '', 300, 200],
['gdasdasdb', 0, '', 925, 786],
['abdasdasa', 0, '', 925, 786],
['bbdasdas', 0, '', 652, 156],
['cbdasdas', 0, '', 300, 200],
['ddasdasb', 0, '', 925, 786],
['edasdb', 0, '', 652, 156],
['fasdasb', 0, '', 300, 200],
['gdasdasdb', 0, '', 925, 786]
]);
var options = {
annotations: {
stem: {
color: '#000000'
},
style: 'line',
textStyle: {
fontSize: 10
},
},
title: 'Motivation and Energy Level Throughout the Day',
isStacked: true,
height: 600,
chartArea: {
height: 300,
top: 100
},
hAxis: {
title: 'Departamentos',
pointSize: 0,
titleTextStyle: {
color: '#ff0000',
},
slantedText: true,
slantedTextAngle: 45
},
series: {
0: {
color: 'transparent',
type: 'line',
visibleInLegend: false,
enableInteractivity: false
}
},
vAxis: {
title: 'Kits'
}
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
further adjustments can be made to get the tick mark below the x-axis,
use a negative dummy value, and tweak the y-axis to allow room for negative values...
(see vAxis options in the following snippet)
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Departamentos');
data.addColumn('number', 'dumy');
data.addColumn({type:'string', role:'annotation'}); // interval role col.
data.addColumn('number', 'Entregados');
data.addColumn('number', 'En Stock');
data.addRows([
['abdasdasa', -32, '', 925, 786],
['bbdasdas', -32, '', 652, 156],
['cbdasdas', -32, '', 300, 200],
['ddasdasb', -32, '', 925, 786],
['edasdb', -32, '', 652, 156],
['fasdasb', -32, '', 300, 200],
['gdasdasdb', -32, '', 925, 786],
['abdasdasa', -32, '', 925, 786],
['bbdasdas', -32, '', 652, 156],
['cbdasdas', -32, '', 300, 200],
['ddasdasb', -32, '', 925, 786],
['edasdb', -32, '', 652, 156],
['fasdasb', -32, '', 300, 200],
['gdasdasdb', -32, '', 925, 786]
]);
var options = {
annotations: {
stem: {
color: '#000000'
},
style: 'line',
textStyle: {
fontSize: 10
},
},
title: 'Motivation and Energy Level Throughout the Day',
isStacked: true,
height: 600,
chartArea: {
height: 300,
top: 100
},
hAxis: {
title: 'Departamentos',
pointSize: 0,
titleTextStyle: {
color: '#ff0000',
},
slantedText: true,
slantedTextAngle: 45
},
series: {
0: {
color: 'transparent',
type: 'line',
visibleInLegend: false,
enableInteractivity: false
}
},
vAxis: {
ticks: [0, 500, 1000, 1500, 2000],
title: 'Kits',
viewWindow: {
min: -64
}
}
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
Related
I have the below ChartJS code and I would like to hide the grid vertical lines only and not the horizontal ones and keep only one vertical line per two like in the capture below:
image]1
Thanks.
<canvas id="line-chart" width="800" height="450"></canvas>
new Chart(document.getElementById("line-chart"), {
type: 'line',
data: {
labels: [1500,1600,1700,1750,1800,1850,1900,1950,1999,2050],
datasets: [{
data: [86,114,106,106,107,111,133,221,783,2478],
label: "Africa",
borderColor: "#3e95cd",
fill: false
}, {
data: [282,350,411,502,635,809,947,1402,3700,5267],
label: "Asia",
borderColor: "#8e5ea2",
fill: false
}, {
data: [168,170,178,190,203,276,408,547,675,734],
label: "Europe",
borderColor: "#3cba9f",
fill: false
}, {
data: [40,20,10,16,24,38,74,167,508,784],
label: "Latin America",
borderColor: "#e8c3b9",
fill: false
}, {
data: [6,3,2,2,7,26,82,172,312,433],
label: "North America",
borderColor: "#c45850",
fill: false
}
]
},
options: {
title: {
display: true,
text: 'World population per region (in millions)'
}
}
});
Just add grid color as function in options for x axis scale and conditionally change the colors like its done below:
var chartInstance = new Chart(document.getElementById("chartJSContainer"), {
type: 'line',
data: {
labels: [1500, 1600, 1700, 1750, 1800, 1850, 1900, 1950, 1999, 2050],
datasets: [{
data: [86, 114, 106, 106, 107, 111, 133, 221, 783, 2478],
label: "Africa",
borderColor: "#3e95cd",
fill: false
}, {
data: [282, 350, 411, 502, 635, 809, 947, 1402, 3700, 5267],
label: "Asia",
borderColor: "#8e5ea2",
fill: false
}, {
data: [168, 170, 178, 190, 203, 276, 408, 547, 675, 734],
label: "Europe",
borderColor: "#3cba9f",
fill: false
}, {
data: [40, 20, 10, 16, 24, 38, 74, 167, 508, 784],
label: "Latin America",
borderColor: "#e8c3b9",
fill: false
}, {
data: [6, 3, 2, 2, 7, 26, 82, 172, 312, 433],
label: "North America",
borderColor: "#c45850",
fill: false
}]
},
options: {
title: {
display: true,
text: 'World population per region (in millions)'
},
scales: {
x: {
grid: {
display: true, //Make it false to remove all the vertical lines.
color: function(context) {
if (context.tick.value %2 == 0) {
return 'gray';
} else {
return 'white';
}
},
}
}
}
}
});
<script src="https://cdn.jsdelivr.net/npm/chart.js#3.3.1/dist/chart.js"></script>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
I don’t really use chart.js so there may be a better way to do this. I think there may be a built in way to use a callback for this but I couldn’t figure it out. Here’s what does work.
Add this function at the top to create an array of alternating colors.
let colors = [];
function gridColors() {
for (let i=0; i<10; i++) {
colors.push("rgba(0,0,0,0)")
colors.push("rgba(0,0,0,0.1)")
}
}
gridColors()
And in your options add the following
options: {
title: {
display: true,
text: 'World population per region (in millions)'
},
scales: {
x: {
grid: {
beginAtZero: true,
color: colors,
}
}
}
}
});
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>
I'm trying to specify maximum, minimum and step size values for my Chart.js graph.
Can anyone explain to me why the code below produces this graph?
graph
I'm specifically writing:
max: 12,
min: 0,
stepSize: 1.0,
according to the documentation so I have no idea why my settings are being ignored.
Any help will be much appreciated.
Thanks!
Code
<canvas id="h-t"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
d3.csv('h.csv')
.then(makeChart);
function makeChart(hdata) {
var t = hdata.map(function(d) {return d.t});
var h = hdata.map(function(d) {return d.h});
var chart = new Chart('h-t', {
type: 'line',
data: {
labels: t,
datasets: [
{
data: h,
backgroundColor: '#2a54a9',
borderColor: '#2a54a9',
fill: false,
// pointRadius: 10,
// pointHoverRadius: 15,
showLine: false // no line shown
}
]
},
options: {
scales: {
xAxes: [{
gridLines: {
drawOnChartArea: false,
color: "#111111"
},
afterFit: function(scale) {
scale.height = 80 //<-- set value as you wish
},
scaleLabel: {
display: true,
labelString: 'Tiempo (min)',
fontSize: 18,
// fontFamily: 'Cabin Sketch',
fontColor: '#111111'
},
ticks: {
fontSize: 16,
// fontFamily: 'EB Garamond',
fontColor: '#111111',
max: 12,
min: 0,
stepSize: 1.0,
padding: 10
}
}],
yAxes: [{
gridLines: {
drawOnChartArea: false,
color: "#111111"
},
scaleLabel: {
display: true,
labelString: 'Altitud (km)',
fontSize: 18,
// fontFamily: 'Cabin Sketch',
fontColor: '#111111'
},
ticks: {
beginAtZero: true,
fontSize: 16,
// fontFamily: 'EB Garamond',
fontColor: '#111111',
padding: 10,
stepSize: 50,
suggestedMax: 250,
suggestedMin: 0
}
}]
},
legend: {
display: false
},
}
});
}
</script>
Edit
Forgot to add the contents of the input file h.csv:
t,h
0,0
0.266666666666667,0.4
0.433333333333333,1.2
0.6,2.5
0.766666666666667,4.4
0.933333333333333,6.8
1.1,9.6
1.26666666666667,12.8
1.43333333333333,16.9
1.6,22
1.76666666666667,28.3
1.93333333333333,35.7
2.1,44.2
2.26666666666667,53.9
2.43333333333333,64.7
2.6,76.4
2.76666666666667,88.3
2.93333333333333,100
3.1,110
3.26666666666667,121
3.43333333333333,130
3.6,139
3.76666666666667,148
3.93333333333333,156
4.1,163
4.26666666666667,169
4.43333333333333,175
4.6,180
4.76666666666667,184
4.93333333333333,188
5.1,191
5.26666666666667,194
5.43333333333333,196
5.6,198
5.76666666666667,200
5.93333333333333,200
6.1,201
6.26666666666667,201
6.43333333333333,202
6.6,201
6.76666666666667,201
6.93333333333333,201
7.1,200
7.26666666666667,200
7.43333333333333,199
7.6,199
7.76666666666667,198
7.93333333333333,198
8.1,198
8.26666666666667,198
8.43333333333333,198
8.6,197
8.76666666666667,197
8.93333333333333,198
9.1,198
9.26666666666667,198
9.43333333333333,198
9.6,198
9.76666666666667,198
9.93333333333333,199
10.1,199
10.2666666666667,199
10.4333333333333,199
10.6,199
10.7666666666667,199
10.9333333333333,200
11.1,200
11.2666666666667,200
11.4333333333333,200
11.6,200
11.7666666666667,200
11.9333333333333,201
12.1,201
The problem is that you've built the X axis of the line chart with labels, which are categorical and not numeric.
In order to apply a numeric range to the X axis, you need to do two things:
Set the xAxis type: "linear"
Change dataset.data to {x, y} coordinates.
In other words, the data that you pass to Chart.js has to look like this:
[
{x: 0, y: 0},
{x: 0.26, y: 0.4},
{x: 0.43, y: 1.2},
...
]
I've modified your example below to show this in action, including the X axis range from 0 to 12. Here's what it looks like:
Here's the modified code. Note that I just included your data directly instead of using D3:
const ctx = document.getElementById('myChart').getContext('2d');
const series = [
[0, 0],
[0.266666666666667, 0.4],
[0.433333333333333, 1.2],
[0.6, 2.5],
[0.766666666666667, 4.4],
[0.933333333333333, 6.8],
[1.1, 9.6],
[1.26666666666667, 12.8],
[1.43333333333333, 16.9],
[1.6, 22],
[1.76666666666667, 28.3],
[1.93333333333333, 35.7],
[2.1, 44.2],
[2.26666666666667, 53.9],
[2.43333333333333, 64.7],
[2.6, 76.4],
[2.76666666666667, 88.3],
[2.93333333333333, 100],
[3.1, 110],
[3.26666666666667, 121],
[3.43333333333333, 130],
[3.6, 139],
[3.76666666666667, 148],
[3.93333333333333, 156],
[4.1, 163],
[4.26666666666667, 169],
[4.43333333333333, 175],
[4.6, 180],
[4.76666666666667, 184],
[4.93333333333333, 188],
[5.1, 191],
[5.26666666666667, 194],
[5.43333333333333, 196],
[5.6, 198],
[5.76666666666667, 200],
[5.93333333333333, 200],
[6.1, 201],
[6.26666666666667, 201],
[6.43333333333333, 202],
[6.6, 201],
[6.76666666666667, 201],
[6.93333333333333, 201],
[7.1, 200],
[7.26666666666667, 200],
[7.43333333333333, 199],
[7.6, 199],
[7.76666666666667, 198],
[7.93333333333333, 198],
[8.1, 198],
[8.26666666666667, 198],
[8.43333333333333, 198],
[8.6, 197],
[8.76666666666667, 197],
[8.93333333333333, 198],
[9.1, 198],
[9.26666666666667, 198],
[9.43333333333333, 198],
[9.6, 198],
[9.76666666666667, 198],
[9.93333333333333, 199],
[10.1, 199],
[10.2666666666667, 199],
[10.4333333333333, 199],
[10.6, 199],
[10.7666666666667, 199],
[10.9333333333333, 200],
[11.1, 200],
[11.2666666666667, 200],
[11.4333333333333, 200],
[11.6, 200],
[11.7666666666667, 200],
[11.9333333333333, 201],
[12.1, 201],
];
new Chart(ctx, {
type: 'line',
data: {
datasets: [{
data: series.map(datum => ({
x: datum[0],
y: datum[1]
})),
backgroundColor: '#2a54a9',
borderColor: '#2a54a9',
fill: false,
// pointRadius: 10,
// pointHoverRadius: 15,
showLine: false // no line shown
}]
},
options: {
scales: {
xAxes: [{
type: "linear",
gridLines: {
drawOnChartArea: false,
color: "#111111"
},
afterFit: function(scale) {
scale.height = 80 //<-- set value as you wish
},
scaleLabel: {
display: true,
labelString: 'Tiempo (min)',
fontSize: 18,
// fontFamily: 'Cabin Sketch',
fontColor: '#111111'
},
ticks: {
fontSize: 16,
// fontFamily: 'EB Garamond',
fontColor: '#111111',
max: 12,
min: 0,
stepSize: 1.0,
padding: 10
}
}],
yAxes: [{
gridLines: {
drawOnChartArea: false,
color: "#111111"
},
scaleLabel: {
display: true,
labelString: 'Altitud (km)',
fontSize: 18,
// fontFamily: 'Cabin Sketch',
fontColor: '#111111'
},
ticks: {
beginAtZero: true,
fontSize: 16,
// fontFamily: 'EB Garamond',
fontColor: '#111111',
padding: 10,
stepSize: 50,
suggestedMax: 250,
suggestedMin: 0
}
}]
},
legend: {
display: false
},
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.js"></script>
<body>
<canvas id="myChart" width="600" height="400"></canvas>
</body>
Excellent answer - congrats.
For those interested, here code adapted to work with latest chart.js version v3.2.1
(chart.js v3.xx is not backwards compatible with v2.xx)
instead of v2.9.3
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.js"></script>
now v3.x
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
(gets you the latest version of chart.js (now at v3.2.1)
const ctx = document.getElementById('myChart').getContext('2d');
const series = [
[0, 0],
[0.266666666666667, 0.4],
[0.433333333333333, 1.2],
[0.6, 2.5],
[0.766666666666667, 4.4],
[0.933333333333333, 6.8],
[1.1, 9.6],
[1.26666666666667, 12.8],
[1.43333333333333, 16.9],
[1.6, 22],
[1.76666666666667, 28.3],
[1.93333333333333, 35.7],
[2.1, 44.2],
[2.26666666666667, 53.9],
[2.43333333333333, 64.7],
[2.6, 76.4],
[2.76666666666667, 88.3],
[2.93333333333333, 100],
[3.1, 110],
[3.26666666666667, 121],
[3.43333333333333, 130],
[3.6, 139],
[3.76666666666667, 148],
[3.93333333333333, 156],
[4.1, 163],
[4.26666666666667, 169],
[4.43333333333333, 175],
[4.6, 180],
[4.76666666666667, 184],
[4.93333333333333, 188],
[5.1, 191],
[5.26666666666667, 194],
[5.43333333333333, 196],
[5.6, 198],
[5.76666666666667, 200],
[5.93333333333333, 200],
[6.1, 201],
[6.26666666666667, 201],
[6.43333333333333, 202],
[6.6, 201],
[6.76666666666667, 201],
[6.93333333333333, 201],
[7.1, 200],
[7.26666666666667, 200],
[7.43333333333333, 199],
[7.6, 199],
[7.76666666666667, 198],
[7.93333333333333, 198],
[8.1, 198],
[8.26666666666667, 198],
[8.43333333333333, 198],
[8.6, 197],
[8.76666666666667, 197],
[8.93333333333333, 198],
[9.1, 198],
[9.26666666666667, 198],
[9.43333333333333, 198],
[9.6, 198],
[9.76666666666667, 198],
[9.93333333333333, 199],
[10.1, 199],
[10.2666666666667, 199],
[10.4333333333333, 199],
[10.6, 199],
[10.7666666666667, 199],
[10.9333333333333, 200],
[11.1, 200],
[11.2666666666667, 200],
[11.4333333333333, 200],
[11.6, 200],
[11.7666666666667, 200],
[11.9333333333333, 201],
[12.1, 201],
];
new Chart(ctx, {
type: 'line',
data: {
datasets: [{
data: series.map(datum => ({
x: datum[0],
y: datum[1]
})),
backgroundColor: '#2a54a9',
borderColor: '#2a54a9',
fill: false,
showLine: false
}]
},
options: {
scales: {
x: { // as of v3.x now object instead of array, 'x: {' instead of 'xAxis: [{'
type: "linear",
max: 12, // as of v3.x now here
min: 0, // as of v3.x now here
gridLines: {
drawOnChartArea: false,
color: "#111111"
},
afterFit: function(scale) {
scale.height = 80
},
scaleLabel: {
display: true,
labelString: 'Tiempo (min)',
fontSize: 18,
fontColor: '#111111'
},
ticks: {
fontSize: 16,
fontColor: '#111111',
//max: 12, // as of v3.x not here anymore
//min: 0, // as of v3.x not here anymore
stepSize: 1.0,
padding: 10
}
},
y: { // as of v3.x now object instead of array, 'y: {' instead of 'yAxis: [{'
gridLines: {
drawOnChartArea: false,
color: "#111111"
},
scaleLabel: {
display: true,
labelString: 'Altitud (km)',
fontSize: 18,
fontColor: '#111111'
},
ticks: {
beginAtZero: true,
fontSize: 16,
fontColor: '#111111',
padding: 10,
stepSize: 50,
suggestedMax: 250,
suggestedMin: 0
}
}
},
legend: {
display: false
},
}
});
</script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<body>
<canvas id="myChart" width="600" height="400"></canvas>
</body>
I have a strange issue. I'm getting the date from the database. All visualization was good. Then I deleted one record and add another one on the same date and time. But that record moved on the right in the chart instead to be ordered on left:
The js code is this:
$(document).ready(function(){
$.ajax({
url: "js/json/otv_fil_iskar/data.php",
method: "POST",
success: function(data) {
console.log(data);
var progress = document.getElementById('animationProgress');
var dateANDtime = [];
var Gblok9osx = [];
var Gblok9osy = [];
var Gblok11osx = [];
var Gblok11osy = [];
var Gfilblok10 = [];
var Gfilblok11 = [];
var Gcvn = [];
var Gndk2 = [];
for(var i in data) {
dateANDtime.push(data[i].to_char);
Gblok9osx.push(data[i].blok9osx);
Gblok9osy.push(data[i].blok9osy);
Gblok11osx.push(data[i].blok11osx);
Gblok11osy.push(data[i].blok11osy);
Gfilblok10.push(data[i].filblok10);
Gfilblok11.push(data[i].filblok11);
Gcvn.push(data[i].cvn);
Gndk2.push(data[i].ndk2);
}
var chartdata = {
labels: dateANDtime,
datasets : [
{fill: false,
label: 'Отвес блок 9 x',
backgroundColor: 'rgba(199, 228, 238, 0.75)',
borderColor: 'rgba(1, 150, 200, 0.75)',
//hoverBackgroundColor: 'rgba(200, 200, 200, 0.75)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: Gblok9osx,
hidden: true
},
{fill: false,
label: 'Отвес блок 9 y',
backgroundColor: 'rgba(163, 147, 222, 0.75)',
borderColor: 'rgba(50, 10, 200, 0.75)',
//hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: Gblok9osy,
hidden: true
},
{fill: false,
label: 'Отвес блок 11 x',
backgroundColor: 'rgba(221, 221, 241, 0.75)',
borderColor: 'rgba(100, 100, 200, 0.75)',
//hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: Gblok11osx,
hidden: true
},
{fill: false,
label: 'Отвес блок 11 y',
backgroundColor: 'rgba(147, 227, 227, 0.75)',
borderColor: 'rgba(1, 200, 200, 0.75)',
//hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: Gblok11osy,
hidden: true
},
{fill: false,
label: 'Филтрация блок 10',
backgroundColor: 'rgba(139, 105, 132, 0.75)',
borderColor: 'rgba(255, 1, 200, 0.75)',
//hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: Gfilblok10,
hidden: true
},
{fill: false,
label: 'Филтрация блок 11',
backgroundColor: 'rgba(0, 200, 1, 1)',
borderColor: 'rgba(0, 101,1, 0.75)',
hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(50, 50, 50, 1)',
data: Gfilblok11,
hidden: true
},
{fill: false,
label: 'КВН',
backgroundColor: 'rgba(30, 100, 100, 1)',
borderColor: 'rgba(100, 200,1, 0.75)',
hoverBackgroundColor: 'rgba(101, 200, 200, 1)',
hoverBorderColor: 'rgba(50, 50, 50, 1)',
data: Gcvn,
hidden: false
},
{fill: false,
label: 'Ниво дренажен кладенец',
backgroundColor: 'rgba(30, 100, 100, 1)',
borderColor: 'rgba(100, 200,1, 0.75)',
hoverBackgroundColor: 'rgba(101, 200, 200, 1)',
hoverBorderColor: 'rgba(50, 50, 50, 1)',
data: Gndk2,
hidden: true
}
]
};
$('#reset_zoom').click(function() {
barGraph.resetZoom();
})
$("#save-btn").click(function() {
$("#mycanvas").get(0).toBlob(function(blob) {
saveAs(blob, "chart_1.png");
});
});
var ctx = document.getElementById("mycanvas");
var barGraph = new Chart(ctx, {
type: 'line',
data: chartdata,
options: {
title: {
display: true,
text: 'Диаграма - Отвеси и филтрации'
},
scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: '[mm] , [l/s]'
},
ticks: {
beginAtZero:false
}
}],
xAxes: [{
scaleLabel: {
display: true,
labelString: 'Дата/Час'
},
ticks: {
beginAtZero:true
}
}]
},
// Container for pan options
pan: {
// Boolean to enable panning
enabled: true,
// Panning directions. Remove the appropriate direction to disable
// Eg. 'y' would only allow panning in the y direction
mode: 'y'
},
// Container for zoom options
zoom: {
// Boolean to enable zooming
enabled: true,
// Zooming directions. Remove the appropriate direction to disable
// Eg. 'y' would only allow zooming in the y direction
mode: 'xy'
},
animation: {
duration: 2000,
onProgress: function(animation) {
progress.value = animation.currentStep / animation.numSteps;
},
onComplete: function() {
window.setTimeout(function() {
progress.value = 0;
}, 2000);
}
},
legend: {
display: true,
labels: {
//fontColor: 'rgb(255, 99, 132)'
usePointStyle: true
}
}
},
plugins: [{
beforeDraw: function(c) {
var reset_zoom = document.getElementById("reset_zoom"); //reset button
var ticks = c.scales['x-axis-0', 'y-axis-0'].ticks.length; //x-axis ticks array
var labels = c.data.labels.length; //labels array
if (ticks < labels) reset_zoom.hidden = false;
else reset_zoom.hidden = true;
}
}]
}); //end data
},
error: function(data) {
console.log(data);
}
});
});
The same think is happening when i don't have records in particular time of the day.
I changed xAxes option in the same graph and it's similar output:
xAxes: [{
type: 'time',
time: {
displayFormats: {
'hour': 'DD MMM YYYY г. HH:MM:SS ч.'
}
},
scaleLabel: {
display: true,
labelString: 'Дата/Час'
},
ticks: {
beginAtZero: true
}
}]
The order in the dataset is important because chart.js does not do any sorting of the values. Make sure that you fetch data from the database ordered by date and not id which usually is default.
I'm using google visualization
var data2 = new google.visualization.DataTable();
data2.addColumn('string', 'time');
data2.addColumn('number', 'amount');
data2.addColumn({ type: 'string', role: 'tooltip' });
data2.addRows(rows_data);
var options2 = {
vAxis: { textPosition: 'none', title: '', textStyle: { fontName: 'arial'} },
hAxis: { slantedText: false, textStyle: { color: '#E6EFFA' }, gridlines: { color: '#E6EFFA', count: 20} },
backgroundColor: '#E6EFFA',
legend: 'none',
chartArea: { top: 0 },
colors: ['#435988'],
chartArea: { width: 800 }
};
chart2 = new google.visualization.LineChart(document.getElementById('chart_div_volume'));
I want the vAxis position to be on the right.
is it possible ?
Short Answer: Yes, but it's tricky.
Long Answer:
You need to set up a multi-axis chart. Basically, you create a dummy axis with no labels or anything to make it look like an axis. Then you configure a secondary axis. You create one set of dummy values (hidden) to put on the first axis, and plot your real data on the second.
Here is an example:
function drawVisualization() {
// Create and populate the data table.
var data = google.visualization.arrayToDataTable([
['Year', 'Dummy', 'Sales', 'Expenses'],
['2004', 0, 1000, 400],
['2005', null, 1170, 460],
['2006', null, 660, 1120],
['2007', null, 1030, 540]
]);
var options = {
title: 'Company Performance',
series: { 0: {targetAxisIndex: 0, visibleInLegend: false, pointSize: 0, lineWidth: 0},
1: {targetAxisIndex: 1},
2: {targetAxisIndex: 1}
},
vAxes: {
0: {textPosition: 'none'},
1: {},
}
};
var chart = new google.visualization.LineChart(document.getElementById('visualization'));
chart.draw(data, options);
}
google.charts.load('current', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawVisualization);
function drawVisualization() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Productivity');
data.addColumn('number', 'Composite');
data.addColumn({ type: 'number', role: 'annotation' });
data.addColumn('number', 'Average(N=5)');
var compositeDataArry = [];
compositeDataArry.push(["Ravi", 11, 11, 5]);
compositeDataArry.push(["Wasif", 5, 5, 5]);
compositeDataArry.push(["Vipin", 2, 2, 5]);
compositeDataArry.push(["Ankur", 3, 3, 5]);
compositeDataArry.push(["Pankaj", 1, 1, 5]);
compositeDataArry.push(["Dheeraj", 4, 4, 5]);
data.addRows(compositeDataArry);
var options = {
title: 'My Chart',
titleTextStyle: { color: '#264158', fontSize: 24 },
seriesType: 'bars',
annotations: {
alwaysOutside: true,
textStyle: {
color: '#000000',
fontSize: 15
}
},
hAxis: {
slantedText: true,
slantedTextAngle: -45
},
series: {
0: { targetAxisIndex: 0, },
1: { targetAxisIndex: 1, type: 'line' }
},
vAxes: {
0: { textPosition: 'none' },
1: {}
}
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
</head>
<body>
<div id="chart_div" style="height: 500px; width: 100%"></div>
</body>
</html>