How implement the following chart in google chart api? - javascript

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/

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 line chart: How to compare values from different time periods?

Let us suppose the following:
I have a dataset counting daily ticket sales.
It's relatively easy to use Google Line Chart and draw a chart displaying the count of tickets per day.
What I would like to be able to do is draw multiple lines comparing different time periods; for example, a blue line showing the daily ticket sales between 01-01-2020 through 01-31-2020, and then a red line showing the daily ticket sales between 02-01-2020 through 03-02-2020.
Is this something Google Line Charts naturally supports?
EDITED TO ADD EXAMPLE DATASET BECAUSE COMMENTS ARE HARD TO READ
Well, in the same way as you can have multiple Y-axes values across the same X-axes, this would be multiple X and Y axes sets of values, where the X axis values all have a 1:1 correspondence with one another.
So, one representation of the dataset would look like
[
[ ['2020-01-01', 50], ['2020-01-02, 75] ],
[ ['2020-02-01', 25], ['2020-02-02', 35] ]
]
(which would then be turned into a DataTable)
And would draw a chart with two lines; one corresponding to the January data, and one corresponding to the February data, where the dates share the x-axis
in google charts, the first column of the data table represents the x-axis values.
each additional column represents the y-axis values.
to plot two lines, requires three data table columns.
data.addColumn('number', 'Day');
data.addColumn('number', 'Jan')
data.addColumn('number', 'Feb')
and each line would need the same value for the x-axis.
you could just use 1 for the first day of the month.
[1, 50, 75]
google charts does have some features we can use to customize the appearance of the chart.
for instance, if you would like the tooltips for each line to display a different x-axis value,
you can use object notation when providing the data.
we can provide the value (v:) and the formatted value (f:).
the tooltips display the formatted value.
as such, the following data would plot on the same x-axis value,
however, the tooltips for each would display a different date.
[{v: 1, f: '2020-01-01'}, 50, null],
[{v: 1, f: '2020-02-01'}, null, 75],
see following working snippet for an example...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn('number', 'Day');
data.addColumn('number', 'Jan')
data.addColumn('number', 'Feb')
data.addRows([
[{v: 1, f: '2020-01-01'}, 50, null],
[{v: 1, f: '2020-02-01'}, null, 75],
[{v: 2, f: '2020-01-02'}, 25, null],
[{v: 2, f: '2020-02-02'}, null, 35],
[{v: 3, f: '2020-01-03'}, 20, null],
[{v: 3, f: '2020-02-03'}, null, 50],
]);
var xAxisTicks = [];
for (var i = 1; i <= 31; i++) {
xAxisTicks.push(i);
}
var options = {
aggregationTarget: 'none',
chartArea: {
left: 64,
top: 48,
right: 32,
bottom: 64,
height: '100%',
width: '100%'
},
hAxis: {
ticks: xAxisTicks,
title: 'Day of Month'
},
height: '100%',
interpolateNulls: true,
legend: {
alignment: 'start',
position: 'top'
},
selectionMode: 'multiple',
tooltip: {
trigger: 'both'
},
vAxis: {
title: 'Daily Ticket Sales'
},
width: '100%'
};
var chart = new google.visualization.LineChart(document.getElementById('chart'));
google.visualization.events.addListener(chart, 'ready', function () {
chart.setSelection([
{row: 4, column: 1},
{row: 5, column: 2}
]);
});
chart.draw(data, options);
window.addEventListener('resize', function () {
chart.draw(data, options);
});
});
html, body {
height: 100%;
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 0px;
}
#chart {
height: 100%;
min-height: 400px;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>
EDIT
another option is to use the column role feature,
which allows us to add multiple domain columns.
if not specified, the first column of the data table is used as the domain column.
by explicitly setting the role type,
we can have multiple domains, or x-axis values.
in this scenario, we do not have to perform any manipulation,
in order to display the correct values in the tooltips.
and all the values can be set in the same row...
data.addColumn({label: 'Date', role: 'domain', type: 'string'});
data.addColumn({label: 'Jan', type: 'number'});
data.addColumn({label: 'Date', role: 'domain', type: 'string'});
data.addColumn({label: 'Feb', type: 'number'});
data.addRows([
['2020-02-01', 75, '2020-01-01', 50],
['2020-02-02', 35, '2020-01-02', 25],
['2020-02-03', 50, '2020-01-03', 20],
]);
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = new google.visualization.DataTable();
data.addColumn({label: 'Date', role: 'domain', type: 'string'});
data.addColumn({label: 'Jan', type: 'number'});
data.addColumn({label: 'Date', role: 'domain', type: 'string'});
data.addColumn({label: 'Feb', type: 'number'});
data.addRows([
['2020-02-01', 75, '2020-01-01', 50],
['2020-02-02', 35, '2020-01-02', 25],
['2020-02-03', 50, '2020-01-03', 20],
]);
var xAxisTicks = [];
for (var i = 1; i <= 31; i++) {
xAxisTicks.push(i);
}
var options = {
aggregationTarget: 'none',
chartArea: {
left: 64,
top: 48,
right: 32,
bottom: 48,
height: '100%',
width: '100%'
},
hAxis: {
ticks: xAxisTicks,
title: 'Day of Month'
},
height: '100%',
legend: {
alignment: 'start',
position: 'top'
},
selectionMode: 'multiple',
tooltip: {
trigger: 'both'
},
vAxis: {
title: 'Daily Ticket Sales'
},
width: '100%'
};
var chart = new google.visualization.LineChart(document.getElementById('chart'));
google.visualization.events.addListener(chart, 'ready', function () {
chart.setSelection([
{row: 2, column: 1},
{row: 2, column: 3}
]);
});
chart.draw(data, options);
window.addEventListener('resize', function () {
chart.draw(data, options);
});
});
html, body {
height: 100%;
margin: 0px 0px 0px 0px;
overflow: hidden;
padding: 0px 0px 0px 0px;
}
#chart {
height: 100%;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>

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