Setting a hard minimum axis value in Google Charts API - javascript

I'm trying to build a Google Chart to show some uptime and downtime percentages, stacked. This works great except for one small thing - I'd like the baseline of the chart to be at 99.8, and the maximum to be 100 - since downtimes are usually less than .2, this will make the chart readable.
This seemed simple enough to me. I figured this would work:
var data = new google.visualization.DataTable();
data.addColumn('string', 'Date');
data.addColumn('number', 'Uptime');
data.addColumn('number', 'Downtime');
data.addRows([
['Dec 1, 1830', 99.875, 0.125],
['Dec 8, 1830', 99.675, 0.325],
['Dec 15, 1830', 99.975, 0.025],
['Dec 22, 1830', 100.0, 0.0]
]);
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240, isStacked: true, vAxis: { title: "Percentage Uptime", minValue: 99.8, maxValue: 100}});
Unfortunately, the chart utterly disregards the minValue on the vAxis. This seems to be expected behaviour, according to the API, but this leaves the question - how would I accomplish this? I even went so far as to transform the data - ie, to subtract 99.8 from all the uptime values, and just have the chart go from 0 to .2, and this spits out a graph that looks okay, but I can't apply labels to the vertical axis that would say 99.8, 99.85, 99.9, whatever - the axis says, quite dutifully, 0 at the bottom, and .2 at the top, and there seems to be no way to fix it this directions, either. Either solution would be acceptable, I'm sure there's SOME way to make this work?

you need to set viewWindow like this:
var data = new google.visualization.DataTable();
data.addColumn('string', 'Date');
data.addColumn('number', 'Uptime');
data.addColumn('number', 'Downtime');
data.addRows([
['Dec 1, 1830', 99.875, 0.125],
['Dec 8, 1830', 99.675, 0.325],
['Dec 15, 1830', 99.975, 0.025],
['Dec 22, 1830', 100.0, 0.0]
]);
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data,
{width: 400,
height: 240,
isStacked: true,
vAxis: {
title: "Percentage Uptime",
viewWindowMode:'explicit',
viewWindow:{
max:100,
min:99.8
}
}
}
);
//edited for newer version of api

I had a similar problem and had to specify the property as "min" not "minValue"
vAxis: {
viewWindowMode:'explicit',
viewWindow: {
max:100,
min:99.8
}
}

Related

Looking to remove the value of google pie chart on hover

var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
['Male', 51],
['Female', 49],
]);
// Set options for Sarah's pie chart.
var options = {title:"",
width:600,
height:400,
is3D: true};
Above is my code. I'm looking to remove the value on the hover but keep the percentages in the pie chart. So on my chart it says Male: 51(51%) Female: 49(49%). It won't let me attach a picture since I'm fairly new so I apologize for that as well.
Just add one more configuration tooltip in options object:
var options = {
title:"",
width:600,
height:400,
tooltip: {
text: 'percentage'
},
is3D: true
};
Here is demo : https://jsfiddle.net/fs6tc5nq/1/

Google Graphs One value for Y -axis (Stacked columns for Two charts)

Full code
You can use jsfiddle too testing.
google.load('visualization', '1.1', {
'packages': ['bar']
});
google.setOnLoadCallback(drawStuff);
function drawStuff() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Nescafe Instant');
data.addColumn('number', 'Folgers Instant');
data.addColumn('number', 'Nescafe Beans');
data.addColumn('number', 'Folgers Beans');
data.addRows([
['2001', 500, 1200, 816, 200],
['2002', 163, 231, 539, 594],
['2003', 125, 819, 200, 578],
['2004', 197, 536, 613, 500]
]);
// Set chart options
var options = {
isStacked: true,
width: 800,
height: 600,
chart: {
title: 'Year-by-year coffee consumption',
subtitle: 'This data is not real'
},
series: {
2: {
targetAxisIndex: 1
},
3: {
targetAxisIndex: 1
}
}
};
// Instantiate and draw our chart, passing in some options.
var chart = new google.charts.Bar(document.getElementById('chart_div'));
chart.draw(data, google.charts.Bar.convertOptions(options));
};
The Output
The Question
I use Google Visualization to generate above chart.
If you see the above screenshot you can see that Y- axis of above chart has two values in left side and right side. Seems like it happens because graphs use two date range for left side and right side bar.
I wanted to remove right side Y axis or print the same values for both right and left Y axis. I mean all data should be under the Y - axis which is on left side. What can I do to do it?
the options for series.n.targetAxisIndex create the second axis
removing these options will allow all series to default to the first axis
however, in Material bar charts, moving a series to a different axis,
separates the series into multiple stacks, resulting in blue and red bars
removing the options mentioned above will combine all series into one blue stack
in order to keep the stacks separated in two colors,
series.n.targetAxisIndex must be used and dual axis' will be displayed
to keep the two axis' in sync, use option --> vAxis.viewWindow
this will set an identical range for both axis'
vAxis: {
viewWindow: {
min: 0,
max: 1800
}
}
see following working snippet...
google.charts.load('current', {
packages: ['bar']
}).then(drawStuff);
function drawStuff() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Nescafe Instant');
data.addColumn('number', 'Folgers Instant');
data.addColumn('number', 'Nescafe Beans');
data.addColumn('number', 'Folgers Beans');
data.addRows([
['2001', 500, 1200, 816, 200],
['2002', 163, 231, 539, 594],
['2003', 125, 819, 200, 578],
['2004', 197, 536, 613, 500]
]);
var options = {
isStacked: true,
width: 800,
height: 600,
chart: {
title: 'Year-by-year coffee consumption',
subtitle: 'This data is not real'
},
series: {
2: {
targetAxisIndex: 1
},
3: {
targetAxisIndex: 1
}
},
vAxis: {
viewWindow: {
min: 0,
max: 1800
}
}
};
var chart = new google.charts.Bar(document.getElementById('chart_div'));
chart.draw(data, google.charts.Bar.convertOptions(options));
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
note: jsapi should no longer be used to load the charts library,
according to the release notes...
The version of Google Charts that remains available via the jsapi loader is no longer being updated consistently. The last update, for security purposes, was with a pre-release of v45. Please use the new gstatic loader.js from now on.
this will only change the load statement, see above snippet...

Google Charts - Custom Tooltip on Line Chart (Line Package) not Working

I'm using Google Charts (Visualization, 1.1, Line Package) to create a Simple Chart with 3 Lines and Month and Costs Axis. Everything works fine, except Tooltip customization:
Here's my code
google.load('visualization', '1.1', {packages: ['line']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Month');
data.addColumn('number', "Line 1");
data.addColumn('number', "Line 2");
data.addColumn('number', "Line 3");
data.addColumn({'type': 'string', 'role': 'tooltip', 'p': {'html': true}});
data.addRows([
[new Date(2015, 5), 1000, 980, 800, 'Custom Content 1'],
[new Date(2015, 6), 1100, 1000, 970, 'Custom Content 2'],
[new Date(2015, 7), 1550, 1420, 1200, 'Custom Content 3'],
[new Date(2015, 8), 1050, 1200, 930, 'Custom Content 4'],
[new Date(2015, 9), 1280, 1120, 1070, 'Custom Content 5'],
[new Date(2015, 10), 1100, 999, 880, 'Custom Content 6'],
]);
var options = {
chart: {
title: 'Custom ToolTips',
subtitle: 'not working!'
},
focusTarget: 'category',
tooltip: {isHtml: true},
width: 900,
height: 500
};
var chart = new google.charts.Line(document.getElementById('linechart_material'));
chart.draw(data, google.charts.Line.convertOptions(options));
}
You can test on JSFiddle
When you Hover the first bottom line you'll get:
But I Want to remove the Date on Top and Calculate Total Cost (Value * 2):
This is possible?
I already tried everything I could, search on Internet and try other codes but it looks like this isn't possible using th Line Package (Instead of CoreChart Package), is that right?
Can anyone help me out?
copied from this answer: https://stackoverflow.com/a/29148517/4966682
After looking deeper into Google's Material Chart information I found on their website:
The Material Charts are in beta. The appearance and interactivity are
largely final, but the way options are declared is not.
Trendlines and tooltips fall under the options section of creating charts since they need the options structure to further define them. Again, as of this date (March 2015) the Google Materials Charts do not support these features. If you want to use things like trendlines and tooltips you need to use non material charts (e.g. packages['corechart'] and not packages['scatter']).

google charts api - column chart - percentage above vertical bar along with normal values on left side of vaxis

I am using google charts to generate a column chart as shown in this image:
http://postimg.org/image/mt7tzwwob/
Here, data will be like [['a', 1], ['b', 2], ['c', 3]]
Here, I am getting values 1,2,3 on left left side of vaxis which is ok for me.
What I want extra is: The percentages at the top of the vertical bar.
x+2x+3x = 100, means, x=16, 2x=33, 3x=50. So, 16% should be at top of vertical bar with value 1.
How can I get these percentages ?
The ColumnChart's don't support adding labels like a percentage above the columns, but there is a work-around that involves using a ComboChart and a hidden line to add them in. Here's some example code that adds in labels (you can replace the labels with percents in you want):
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Name');
data.addColumn('number', 'Value');
data.addColumn({type: 'string', role: 'annotation'});
data.addRows([
['Foo', 53, 'Foo text'],
['Bar', 71, 'Bar text'],
['Baz', 36, 'Baz text'],
['Cad', 42, 'Cad text'],
['Qud', 87, 'Qud text'],
['Pif', 64, 'Pif text']
]);
var view = new google.visualization.DataView(data);
view.setColumns([0, 1, 1, 2]);
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(view, {
height: 400,
width: 600,
series: {
0: {
type: 'bars'
},
1: {
type: 'line',
color: 'grey',
lineWidth: 0,
pointSize: 0,
visibleInLegend: false
}
},
vAxis: {
maxValue: 100
}
});
}
see it working here: http://jsfiddle.net/asgallant/QjQNX/
This only works well for charts that have a single series of data (as the labels will be misaligned if there is more than one series).

Google area chart - setting minimum value on vAxis

I have a simple Google area chart with only one series and 2 columns - Date and Money.
The only problem is when all the values in the "Money" column are 0, 1 or 2.
Then, duplicate values in the vertical axes appear.
For example when all the values are 0, the vAxis is:
1
1
0
When all the values are 1, the vAxis is:
1
1
1
0
0
It's really strange and I don't know what am I missing and I couldn't find anywhere the same problem.
The API is loaded like that:
google.load('visualization', '1.0', {packages: ['corechart'], 'callback':drawVisualization});
The data of the chart is this for example:
var data = new google.visualization.DataTable();
data.addColumn('string','Date');
data.addColumn('number', 'Money');
data.addRows([
['March 1', 0],
['March 2', 0],
['March 3', 0],
['March 4', 0]
]);
The code for the creation of the chart is this:
ac.draw(data, {
chartArea: {width: '90%', height: '80%'},
legend: {position: 'none'},
colors:['#24b3b0'],
isStacked:false,
pointSize: 10,
width: 938,
height: 400,
areaOpacity: 0.2,
hAxis:{
showTextEvery:7,
maxAlternation:1,
maxTextLines:1
},
vAxis:{
viewWindowMode:'explicit',
viewWindow:{
min:0
},
format:'$###,###,###',
gridlines:{
color:'#ccc'
},
baselineColor:'#cfcfcf'
}
});
I'm using viewWindow.min to set the minimum value of vAxis, because vAxis.minValue had no effect as described in this question: Setting a hard minimum axis value in Google Charts API

Categories

Resources