Google graph show extra label with numeric value - javascript

I am trying to add custom label as target achieved with numeric value, which is just a value but not the calculation of graph bar/height.
I am not getting how to get the similar result as showed in screen shot,
JS Fiddle : http://jsfiddle.net/xv867tur/
google.load("visualization", "1", {packages: ["corechart"]});
google.setOnLoadCallback(drawChart);
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
}
});
}
Expected result :

Related

Google Chart, X-Axis and Line won't render in Safari, but works in Chrome

See this codepen:
https://codepen.io/rblythe/pen/OJOVeXa
In chrome it renders just fine like this:
(Although it does have that weird artifact where it shows a blue line outside of the chart on the right side...not sure if that has anything to do with why it won't work in Safari)
However, in safari it won't render the line and the x-axis doesn't render either. It seems like it doesn't accept the data points.
Any ideas?
google.load('visualization', '1', {packages: ['corechart']});
google.setOnLoadCallback(drawVisualization0);
function drawVisualization0() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'x');
data.addColumn({type: 'string', role: 'annotation'});
data.addColumn('number', 'Rating');
data.addRow([new Date('2022-01-28 11:42:50'), null, 2]);
data.addRow([new Date('2022-01-28 14:42:34'), null, 4]);
var formatter = new google.visualization.DateFormat({pattern: 'yyyy-mm-dd'});
formatter.format(data, 0);
new google.visualization.LineChart(document.getElementById('chart0')).
draw(data, {
title: '',
curveType: 'function',
vAxis: {
viewWindow: {min: 1, max: 6},
textStyle:{color: '#FFFFFF'},
gridlines: {color:"#404758"}
},
hAxis: {
textStyle:{color: '#FFFFFF'},
gridlines: {color:"#404758"}
},
colors: ['#ADC6FF'],
backgroundColor: '#272930',
gridlines: {
color: "#404758"
},
baselineColor: '#404758',
annotations: {
style: 'line',
color: "#FFFFFF",
gridlines: {color:"#404758"},
textStyle:{color: '#C4C6D0'}
},
chartArea: {
// leave room for y-axis labels
width: '94%'
}
});
}
google.load('visualization', '1', {packages: ['corechart']});
google.setOnLoadCallback(drawVisualization1);

Vertical line annotation in horizontal bar using Google Chart

Just want to ask if it's possible to add multiple line annotation per bar based on date? Then if I hover the line, it should display the date.
If it's not possible is there any way to do this?
Here's my sample code: http://jsfiddle.net/q0ftngve/
google.load('visualization', '1', {packages: ['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Display Order');
data.addColumn('date', 'Dummy');
data.addColumn('date', 'Introduction');
data.addColumn('date', 'Presentation');
data.addColumn('date', 'Demonstration');
data.addColumn('date', 'Evaluation');
data.addColumn('date', 'Negotiation');
data.addColumn('date', 'Approval');
data.addColumn('date', 'Purchase');
data.addRows([
[
'P0003-0000001',
new Date('2020-04-02'),
new Date('1970-01-14'),
new Date('1970-01-16'),
new Date('1970-01-23'),
new Date('1970-01-22'),
new Date('1970-02-03'),
new Date('1970-01-17'),
new Date('1970-02-01')
]
]);
var dateMin = new Date('2020-4-1');
new google.visualization.BarChart(document.getElementById('progress_chart')).
draw(data,
{
width: "100%",
bar: {groupWidth: "90%"},
backgroundColor: "whitesmoke",
legend: { position: "none" },
isStacked: true,
hAxis: {
viewWindow: {
// max: new Date(2020,5,1),
min: dateMin,
},
// format: 'M/d/yy',
// baseline: dateToday,
// baselineColor: 'red',
},
bar: { groupWidth: 20 }
});
}
using an annotation, with --> style: 'line'
would actually produce a horizontal line,
and would display text on the bar.
to get something to display on hover, you would also need to use annotationText
instead, it may easier to use a different series type...
see following working snippet,
a line series is used to display the lines on the bars...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Display Order');
data.addColumn('date', 'Dummy');
data.addColumn('date', 'Date');
data.addColumn('date', 'Date');
data.addColumn('date', 'Date');
data.addColumn('date', 'Date');
data.addColumn('date', 'Introduction');
data.addColumn('date', 'Presentation');
data.addColumn('date', 'Demonstration');
data.addColumn('date', 'Evaluation');
data.addColumn('date', 'Negotiation');
data.addColumn('date', 'Approval');
data.addColumn('date', 'Purchase');
data.addRow([
'P0003-0000001',
new Date('2020-04-02'),
new Date('2020-04-03'),
new Date('2020-04-04'),
new Date('2020-04-05'),
new Date('2020-04-06'),
new Date('1970-01-14'),
new Date('1970-01-16'),
new Date('1970-01-23'),
new Date('1970-01-22'),
new Date('1970-02-03'),
new Date('1970-01-17'),
new Date('1970-02-01')
]);
var dateMin = new Date('2020-4-1');
new google.visualization.BarChart(document.getElementById('progress_chart')).
draw(data, {
width: '100%',
bar: {
groupWidth: '90%'
},
backgroundColor: 'whitesmoke',
legend: {
position: 'none'
},
isStacked: true,
hAxis: {
viewWindow: {
// max: new Date(2020,5,1),
min: dateMin,
},
// format: 'M/d/yy',
// baseline: dateToday,
// baselineColor: 'red',
},
bar: {
groupWidth: 20
},
annotations: {
boxStyle: {
stroke: '#fff',
strokeWidth: 1
}
},
series: {
1: {
color: '#fff',
pointShape: {
type: 'star', sides: 2, dent: 0.05
},
pointSize: 24,
type: 'line'
},
2: {
color: '#fff',
pointShape: {
type: 'star', sides: 2, dent: 0.05
},
pointSize: 24,
type: 'line'
},
3: {
color: '#fff',
pointShape: {
type: 'star', sides: 2, dent: 0.05
},
pointSize: 24,
type: 'line'
},
4: {
color: '#fff',
pointShape: {
type: 'star', sides: 2, dent: 0.05
},
pointSize: 24,
type: 'line'
},
}
});
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="progress_chart"></div>
NOTE: recommend using loader.js, rather than jsapi to load google charts...
according to the release notes...
The version of Google Charts that remains available via the jsapi loader is no longer being updated consistently. Please use the new gstatic loader from now on.
the newer library can be found here...
<script src="https://www.gstatic.com/charts/loader.js"></script>
this will only change the load statement, see above snippet...

How implement the following chart in google chart api?

I want to implement the following chart in google chart api. how can i get it. I want to separate 4 quarters between two data points and indicators for each quarters need to display. And vertical line with number is that point has annotations. how can i get this in google chart api.
You can use an "annotation" role column to create the lines you want. Here's an example:
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'Year');
data.addColumn({type: 'string', role: 'annotation'});
data.addColumn('number', 'Value');
data.addRows([
[2009, null, 5],
[2010, '1', 4],
[2011, null, 7],
[2011.25, '2', null],
[2011.5, '3', null],
[2012, null, 7]
]);
var chart = new google.visualization.LineChart(document.querySelector('#chart_div'));
chart.draw(data, {
height: 400,
width: 600,
interpolateNulls: true,
annotation: {
1: {
style: 'line'
}
},
hAxis: {
format: '#',
minorGridlines: {
count: 3
},
ticks: [2009, 2010, 2011, 2012]
},
vAxis: {
textPosition: 'none',
minValue: 0,
maxValue: 10
},
legend: {
position: 'none'
}
});
};
google.load('visualization', '1', {packages:['corechart'], callback: drawChart});
See it working here: http://jsfiddle.net/asgallant/H5K29/

Assigning Colors to Specific Columns in Google Charts

I would like to assign a specific color to each column in a Google Bar Chart.
I was wondering what the best way is to go about doing this. I would like to include a color Hex code to each data point.
I have a jsfiddle below:
http://jsfiddle.net/zYS27/
var option = {
animation: {duration: 1000, easing: 'out',},
hAxis: {textStyle:{
fontName: 'Nunito',fontSize: '16' }},
legend: { position: "none" },
tooltip: {trigger: "none", textStyle:{
fontName: 'Nunito',fontSize: '16' }, isHtml: true},
bar: {groupWidth: "80%"},
vAxis: {format:'0%', minValue:0, viewWindowMode:'maximized', textStyle:{
fontName: 'Nunito',fontSize: '16' }, maxValue:1},
};
var formatter = new google.visualization.NumberFormat({
fractionDigits: 3,
pattern:'#,###%',
});
var data = new google.visualization.DataTable();
data.addColumn('string', 'N');
data.addColumn('number', 'Value');
data.addRow(['Orange', 0]);
data.addRow(['Red', 0]);
data.addRow(['Green', 0]);
data.addRow(['Blue', 0]);
data.addRow(['Purple', 0]);
// Create and draw the visualization.
var chart = new google.visualization.ColumnChart(document.getElementById('chart'));
chart.draw(data, option);
data.setValue(0, 1, 0.1);
data.setValue(1, 1, 0.3);
data.setValue(2, 1, 0.4);
data.setValue(3, 1, 0.4);
data.setValue(4, 1, 0.4);
chart.draw(data, option);
}
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
Use a "style" role column:
var data = new google.visualization.DataTable();
data.addColumn('string', 'N');
data.addColumn('number', 'Value');
data.addColumn({type: 'string', role: 'style'});
data.addRow(['Orange', 0, 'color: #FF8000']);
data.addRow(['Red', 0, 'color: #FF0000']);
data.addRow(['Green', 0, 'color: #00FF00']);
data.addRow(['Blue', 0, 'color: #0000FF']);
data.addRow(['Purple', 0, 'color: #800080']);
fiddle: http://jsfiddle.net/asgallant/zYS27/1/
You can also specify the option.colors
option.color = ['#0f0', '#00f'];

How to display the point value in the line chart stroke of google api chart?

I am using linechart in google api chart. In this chart i need to display the vaxis value above point.but i am using annotation.It's create point value in near haxis .But i need above the point.
Actual chart:
Expected chart:
<script type="text/javascript">
google.load("visualization", "1", {packages: ["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Sales');
data.addColumn({type: 'number', role: 'annotation'});
data.addRows([
['2008', 23, 23],
['2009', 145, 145],
['2010', 245, 245],
['2011', 350, 350]
]);
var options = {
width: 400,
height: 100,
pointSize: 4,
legend: {position: 'none'},
chartArea: {
left: 0,
top: 10,
width: 400,
height: 50},
vAxis: {
baselineColor: '#fff',
gridlines: {color: 'transparent'}
},
tooltip: {trigger: 'none'},
annotation: {
1: {
style: 'none'
}
}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
You simply have to correct the order of the column defintion:
data.addColumn('string', 'Year');
data.addColumn('number', 'Sales');
data.addColumn({type: 'number', role: 'annotation'});
Edit: See asgallant's comment on the correct order of columns. My text below isn't fully correct.
Although Google's documentation isn't that clear about it, you should always specify the x-axis first, then the values and put stuff like annotations at the end.

Categories

Resources