Vertical line annotation in horizontal bar using Google Chart - javascript

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...

Related

Google graph show extra label with numeric value

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 :

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);

Google chart label

I have the column for the Annotation, but the annotation does not appear at the top of the bars? If the var view important to show the Label?
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({type: 'number', role: 'annotation'});
data.addColumn('number', 'Folgers Instant');
data.addColumn({type: 'number', role: 'annotation'})
data.addColumn('number', 'Nescafe Beans');
data.addColumn({type: 'number', role: 'annotation'})
data.addRows([
['2001', 321, 150, 621, 500, 600, 816],
['2002', 163, 150, 231, 500, 600, 539]
]);
var options = {
width: 800,
height: 600,
chart: {
title: 'Year-by-year coffee consumption',
subtitle: 'This data is not real'
},
vAxis: {
viewWindow: {
min: 0,
max: 1500
}
},
chartArea: { backgroundColor: '#F8F8F8' }
};
var chart = new google.charts.Bar(document.getElementById('chart_div'));
chart.draw(data, google.charts.Bar.convertOptions(options));
};
Thanks,
peXeq
first, the documentation for the annotation role says to use --> Data type: string
and unfortunately, annotations don't work on Material charts
as demonstrated below
use config option theme: 'material' to get the Core chart close to the look of a Material chart
see following working snippet, draws both chart types...
google.charts.load('current', {
callback: function () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Nescafe Instant');
data.addColumn({type: 'string', role: 'annotation'});
data.addColumn('number', 'Folgers Instant');
data.addColumn({type: 'string', role: 'annotation'})
data.addColumn('number', 'Nescafe Beans');
data.addColumn({type: 'string', role: 'annotation'})
data.addRows([
['2001', 321, '321', 621, '621', 600, '600'],
['2002', 163, '163', 231, '231', 600, '600']
]);
var options = {
width: 800,
height: 600,
chart: {
title: 'Year-by-year coffee consumption',
subtitle: 'This data is not real'
},
vAxis: {
viewWindow: {
min: 0,
max: 1500
}
},
chartArea: { backgroundColor: '#F8F8F8' },
title: 'Year-by-year coffee consumption',
theme: 'material'
};
var chart = new google.charts.Bar(document.getElementById('chart_div_material'));
chart.draw(data, google.charts.Bar.convertOptions(options));
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div_core'));
chart.draw(data, options);
},
packages: ['bar', 'corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div>Core Chart</div>
<div id="chart_div_core"></div>
<div>Material Chart</div>
<div id="chart_div_material"></div>

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/

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