Add vertical line to stacked horizontal bar chart - charts

I'd like to do the same thing described in this question, except that I want to do it for a horizontal bar chart (the bars are horizontal instead of vertical). I'll copy the image from that question here:
I'd like to have a stacked horizontal bar chart similar to this with a vertical line.
It seems that ComboCharts doesn't support horizontal bar charts so I was hoping there might be another way.

using orientation: 'vertical' will rotate the axes so that a column chart becomes a bar chart
see following working snippet...
google.charts.load('current', {
callback: function () {
var container = document.getElementById('chart_div');
var chart = new google.visualization.ComboChart(container);
var data = google.visualization.arrayToDataTable([
['Genre', 'Fantasy & Sci Fi', 'Romance', 'Mystery/Crime', 'General', 'Western', 'Literature', 'Minimum', 'Maximum'],
['2010', 10, 24, 20, 32, 18, 5, 90, 140],
['2020', 16, 22, 23, 30, 16, 9, 90, 140],
['2030', 28, 19, 29, 30, 12, 13, 90, 140]
]);
var options = {
bar: {
groupWidth: '75%'
},
height: 400,
isStacked: true,
legend: {
position: 'bottom'
},
orientation: 'vertical',
series: {
6: {
color: '#000000',
type: 'line'
},
7: {
color: '#000000',
type: 'line'
}
},
seriesType: 'bars',
width: 600,
};
chart.draw(data, options);
},
packages: ['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Related

How to add space between lables and horizontal axis in google line chart

have add google chart with below options, I want spacing between labels at horizontal axis and actual axis, is there option available in chart library or any other way?
var options = { colors: ['#4ED8DA'], curveType: 'none', fontName: 'Montserrat', chartArea: {left: 40, top: 10, bottom: 30, width: '95%', height: '100%'}, legend: {position: 'none'}, pointSize: 5, lineWidth: 3, hAxis: {textPosition: 'out', minTextLines: '2', slantedText: false, baselineColor: '#D6D6D6', gridlines: {color: '#D6D6D6'}, textStyle: {color: '#383939', fontSize: '12', bold: true}}, vAxis: {baselineColor: '#D6D6D6', gridlines: {color: '#D6D6D6', interval: 1}, textStyle: {color: '#383939', fontSize: '12'}} };
there are no standard config options to create space between the x-axis labels and the axis.
however, you can manually move the labels on the chart's 'ready' event.
first, we find the x-axis labels, then increase the "y" attribute of the label.
the following example increases the distance from the axis by the value of the font size.
google.visualization.events.addListener(chart, 'ready', function () {
// get all chart labels
var labels = chart.getContainer().getElementsByTagName('text');
Array.prototype.forEach.call(labels, function (label) {
// determine if x-axis label
if (label.getAttribute('text-anchor') === 'middle') {
// increase distance from axis by the font size of the label
var fontSize = parseFloat(label.getAttribute('font-size'));
var yCoord = parseFloat(label.getAttribute('y'));
label.setAttribute('y', yCoord + fontSize);
}
});
});
note: depending on how far you move the labels,
you may need to increase the area at the bottom of the chart.
chartArea.bottom
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['x', 'y0'],
[0, 0],
[1, 0],
[2, 0],
[3, 1],
[4, 1],
[5, 2],
[6, 2],
[7, 3],
[8, 3],
[9, 3]
]);
var options = {
colors: ['#4ED8DA'],
curveType: 'none',
fontName: 'Montserrat',
chartArea: {left: 40, top: 10, bottom: 30, width: '95%', height: '100%'},
legend: {position: 'none'},
pointSize: 5,
lineWidth: 3,
hAxis: {textPosition: 'out', minTextLines: '2', slantedText: false, baselineColor: '#D6D6D6', gridlines: {color: '#D6D6D6'}, textStyle: {color: '#383939', fontSize: '12', bold: true}},
vAxis: {baselineColor: '#D6D6D6', gridlines: {color: '#D6D6D6', interval: 1}, textStyle: {color: '#383939', fontSize: '12'}}
};
var chart = new google.visualization.LineChart(document.getElementById('chart'));
google.visualization.events.addListener(chart, 'ready', function () {
var labels = chart.getContainer().getElementsByTagName('text');
Array.prototype.forEach.call(labels, function (label) {
if (label.getAttribute('text-anchor') === 'middle') {
var fontSize = parseFloat(label.getAttribute('font-size'));
var yCoord = parseFloat(label.getAttribute('y'));
label.setAttribute('y', yCoord + fontSize);
}
});
});
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>

google charts - when single row chart is zoomed out

I have a chart with a single row
{"cols":[{"id":"time","label":"Time","type":"datetime"},{"id":"Value","label":"Value","type":"number"}],"rows":[{"c":[{"v":"Date(2018,7,31, 14, 49, 48, 0)"}]}]}. When I view the chart on my screen I am present with a "zoomed out" version. I much rather have something that would be "zoomed in" with correct date displayed on the axis. How can this be achieved?
I have tried hAxis.viewWindowMode: 'pretty' with no luck.
you can use hAxis.viewWindow.min & max to set the range of the axis.
and hAxis.ticks to control which axis label(s) appear.
see following working snippet...
google.charts.load('current', {
packages:['corechart']
}).then(function () {
var data = new google.visualization.DataTable({
"cols":[
{"id":"time","label":"Time","type":"datetime"},
{"id":"Value","label":"Value","type":"number"},
{"id":"Value","label":"Value","type":"number"},
{"id":"Value","label":"Value","type":"number"},
{"id":"Value","label":"Value","type":"number"},
{"id":"Value","label":"Value","type":"number"},
],
"rows":[
{"c":[{"v":"Date(2018,7,31, 14, 49, 48, 0)"}, {"v": 50}, {"v": 31}, {"v": 16}, {"v": 14}, {"v": 5}]}
]
});
var dateRange = data.getColumnRange(0);
var options = {
chartArea: {
height: '100%',
width: '100%',
top: 24,
right: 16,
bottom: 60,
left: 60
},
hAxis: {
format: 'MM/dd/yyyy HH:mm:ss',
viewWindow: {
min: new Date(dateRange.min.getFullYear(), dateRange.min.getMonth(), dateRange.min.getDate() - 30),
max: new Date(dateRange.max.getFullYear(), dateRange.max.getMonth(), dateRange.max.getDate() + 30)
},
ticks: data.getDistinctValues(0),
title: 'Time'
},
vAxis: {
ticks: [0, 15, 30, 45, 60]
},
};
var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Material design google charts change color column

When using Google charts, usually you can change the colors of individual columns by using the "role: style" function, but when I attempt it with the new material design charts I only get blue columns.
google.charts.load('current', {'packages':['bar']});
google.charts.setOnLoadCallback(drawStuff);
function drawStuff() {
var data = new google.visualization.arrayToDataTable([
['', '', { role: 'style' } ],
["Strongly disagree", 0, 'color: black'],
["Disagree", 0, 'color: #000000'],
["Neutral", 6, 'color: #212121'],
["Agree", 45, 'color: #212121'],
['Stongly agree', 98, 'color: black']
]);
var options = {
title: 'Instructor presented the subject matter clearly',
width: 900,
legend: { position: 'none' },
chart: { subtitle: 'General physics 221 CSUSB winter 2017' },
axes: {
},
bar: { groupWidth: "90%" }
};
var chart = new google.charts.Bar(document.getElementById('top_x_div'));
// Convert the Classic options to Material options.
chart.draw(data, google.charts.Bar.convertOptions(options));
};
You can try with netx code:
google.charts.load('current', {packages: ['corechart', 'bar']});
google.charts.setOnLoadCallback(drawColColors);
function drawColColors() {
var data = new google.visualization.arrayToDataTable([
['', '', { role: 'style' } ],
["Strongly disagree", 12, 'brown'],
["Disagree", 30, 'gray'],
["Neutral", 26, 'blue'],
["Agree", 45, 'color: #F05921'],
['Stongly agree', 58, 'black']
]);
var options = {
title: 'Instructor presented the subject matter clearly',
width: 900,
legend: { position: 'none' },
chart: { subtitle: 'General physics 221 CSUSB winter 2017' },
axes: {
},
bar: { groupWidth: "90%" }
};
//var chart = new google.charts.Bar(document.getElementById('chart_div'));
// Convert the Classic options to Material options.
//chart.draw(data, google.charts.Bar.convertOptions(options));
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
};
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Google charts - ComboChart - bars and candlesticks

I try create ComboChart where are bars and series of waterfall(candlestick) chart.
I dont know how to create data input for this.
Candlestick needs format like:
['Mon', 20, 28, 38, 45],
['Tue', 31, 38, 55, 66],
['Wed', 50, 55, 77, 80],
['Thu', 77, 77, 66, 50],
['Fri', 68, 66, 22, 15]
and bars in my case:
['name', 'value', { role: 'style' }],
['test1', 8, '#00f'],
['test2', 10,'#000'],
['test3', 19,'#f00'],
Is it even possible to do it ? Join bars and candlesticks ?
use the series option to control the chart type for each series
each series is represented by a column, or a set of columns, in the data
starting with the first column that is not the x-axis or a column role
example: the columns for the chart you describe might resemble the following...
"cols":[
// x-axis
{"label":"Category","type":"string"},
// bar series -- 0
{"label":"Bar 0","type":"number"},
// bar series -- 1
{"label":"Bar 1","type":"number"},
// waterfall series -- 2
{"label":"WF 2 - Bottom 1","type":"number"},
{"label":"WF 2 - Bottom 2","type":"number"},
{"label":"WF 2 - Top 1","type":"number"},
{"label":"WF 2 - Top 2","type":"number"},
{"role":"style","type":"string","p":{"role":"style"}},
// bar series -- 3
{"label":"Bar 3","type":"number"}
],
the candlestick chart requires 4 columns, in the above example, a style column role is also used,
as such, all five columns are considered part of series number 2
when building data for a combochart,
use null for the series values,
when they do not coincide with the same x-axis value
example data:
"rows":[
// bar (series 0 & 1) data
{"c":[{"v":"YTD"},{"v":14807893.054},{"v":11684139.912},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null}]},
{"c":[{"v":"Last Year"},{"v":16307893.054},{"v":20684139.912},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null}]},
// waterfall (series 2) data
{"c":[{"v":"Budget"},{"v":null},{"v":null},{"v":0},{"v":0},{"v":22707893.613},{"v":22707893.613},{"v":"#007fff"},{"v":null}]},
{"c":[{"v":"Contract Labor"},{"v":null},{"v":null},{"v":22707893.613},{"v":22707893.613},{"v":22534350.429},{"v":22534350.429},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Contract Non Labor"},{"v":null},{"v":null},{"v":22534350.429},{"v":22534350.429},{"v":22930956.493},{"v":22930956.493},{"v":"#922b21"},{"v":null}]},
{"c":[{"v":"Matls & Equip"},{"v":null},{"v":null},{"v":22930956.493},{"v":22930956.493},{"v":22800059.612},{"v":22800059.612},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Other"},{"v":null},{"v":null},{"v":22800059.612},{"v":22800059.612},{"v":21993391.103},{"v":21993391.103},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Labor"},{"v":null},{"v":null},{"v":21993391.103},{"v":21993391.103},{"v":21546003.177999996},{"v":21546003.177999996},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Travel"},{"v":null},{"v":null},{"v":21546003.177999996},{"v":21546003.177999996},{"v":21533258.930999994},{"v":21533258.930999994},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Training"},{"v":null},{"v":null},{"v":21533258.930999994},{"v":21533258.930999994},{"v":21550964.529999994},{"v":21550964.529999994},{"v":"#922b21"},{"v":null}]},
{"c":[{"v":"Actual"},{"v":null},{"v":null},{"v":0},{"v":0},{"v":21550964.52999999},{"v":21550964.52999999},{"v":"#007fff"},{"v":null}]},
// bar (series 3) data
{"c":[{"v":"FCST"},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":16182561.25}]}
]
following is a working example of a combo chart with bars and candlesticks
google.charts.load('current', {
callback: function () {
drawChart();
$(window).resize(drawChart);
},
packages:['corechart', 'table']
});
function drawChart() {
var dataChart = new google.visualization.DataTable({
"cols":[
// x-axis
{"label":"Category","type":"string"},
// bar series -- 0
{"label":"Bar 0","type":"number"},
// bar series -- 1
{"label":"Bar 1","type":"number"},
// waterfall series -- 2
{"label":"WF 2 - Bottom 1","type":"number"},
{"label":"WF 2 - Bottom 2","type":"number"},
{"label":"WF 2 - Top 1","type":"number"},
{"label":"WF 2 - Top 2","type":"number"},
{"role":"style","type":"string","p":{"role":"style"}},
// bar series -- 3
{"label":"Bar 3","type":"number"}
],
"rows":[
// bar (series 0 & 1) data
{"c":[{"v":"YTD"},{"v":14807893.054},{"v":11684139.912},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null}]},
{"c":[{"v":"Last Year"},{"v":16307893.054},{"v":20684139.912},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null}]},
// waterfall (series 2) data
{"c":[{"v":"Budget"},{"v":null},{"v":null},{"v":0},{"v":0},{"v":22707893.613},{"v":22707893.613},{"v":"#007fff"},{"v":null}]},
{"c":[{"v":"Contract Labor"},{"v":null},{"v":null},{"v":22707893.613},{"v":22707893.613},{"v":22534350.429},{"v":22534350.429},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Contract Non Labor"},{"v":null},{"v":null},{"v":22534350.429},{"v":22534350.429},{"v":22930956.493},{"v":22930956.493},{"v":"#922b21"},{"v":null}]},
{"c":[{"v":"Matls & Equip"},{"v":null},{"v":null},{"v":22930956.493},{"v":22930956.493},{"v":22800059.612},{"v":22800059.612},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Other"},{"v":null},{"v":null},{"v":22800059.612},{"v":22800059.612},{"v":21993391.103},{"v":21993391.103},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Labor"},{"v":null},{"v":null},{"v":21993391.103},{"v":21993391.103},{"v":21546003.177999996},{"v":21546003.177999996},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Travel"},{"v":null},{"v":null},{"v":21546003.177999996},{"v":21546003.177999996},{"v":21533258.930999994},{"v":21533258.930999994},{"v":"#1e8449"},{"v":null}]},
{"c":[{"v":"Training"},{"v":null},{"v":null},{"v":21533258.930999994},{"v":21533258.930999994},{"v":21550964.529999994},{"v":21550964.529999994},{"v":"#922b21"},{"v":null}]},
{"c":[{"v":"Actual"},{"v":null},{"v":null},{"v":0},{"v":0},{"v":21550964.52999999},{"v":21550964.52999999},{"v":"#007fff"},{"v":null}]},
// bar (series 3) data
{"c":[{"v":"FCST"},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":null},{"v":16182561.25}]}
]
});
var comboChart = new google.visualization.ChartWrapper({
chartType: 'ComboChart',
containerId: 'chart_div',
dataTable: dataChart,
options: {
animation: {
duration: 1500,
easing: 'inAndOut',
startup: true
},
backgroundColor: 'transparent',
bar: {
group: {
width: '85%'
}
},
chartArea: {
backgroundColor: 'transparent',
bottom: 42,
height: '100%',
left: 60,
top: 24,
width: '100%'
},
hAxis: {
textStyle: {
fontSize: 12
}
},
height: 400,
legend: 'none',
seriesType: 'bars',
series: {
2: {
type: 'candlesticks'
}
},
tooltip: {
isHtml: true,
trigger: 'both'
},
vAxis: {
format: 'short',
textStyle: {
color: '#616161'
},
viewWindow: {
min: 10000000,
max: 24000000
}
},
width: '100%'
}
});
comboChart.draw();
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div id="chart_div"></div>
note: you'll notice when running the snippet in full page mode,
the chart doesn't do a very good job of aligning the x-axis labels
you may be able to manipulate the options to get them to align better,
but it will take some manipulation...
you might have better results, creating three separate charts, side-by-side
just need to make sure each has the same vAxis values,
you could hide the extra vAxis labels if needed, etc...
again, manipulation is required
example: snippet displayed best in full page mode...
google.charts.load('current', {
callback: function () {
drawChart();
$(window).resize(drawChart);
},
packages:['corechart', 'table']
});
function drawChart() {
var dataBar0 = new google.visualization.DataTable({
"cols":[
{"label":"Category","type":"string"},
{"label":"Bar 0","type":"number"},
{"label":"Bar 1","type":"number"}
],
"rows":[
// bar 0 data
{"c":[{"v":"YTD"},{"v":22807893.054},{"v":18684139.912}]}
]
});
var barChart0 = new google.visualization.ChartWrapper({
chartType: 'ColumnChart',
containerId: 'bar_chart_0',
dataTable: dataBar0,
options: {
animation: {
duration: 1500,
easing: 'inAndOut',
startup: true
},
backgroundColor: 'transparent',
bar: {
gap: 20,
width: 60
},
chartArea: {
backgroundColor: 'transparent',
},
height: 400,
legend: 'none',
theme: 'maximized',
tooltip: {
isHtml: true,
trigger: 'both'
},
vAxis: {
format: 'short',
viewWindow: {
min: 10000000,
max: 24000000
}
}
}
});
barChart0.draw();
var dataWF0 = new google.visualization.DataTable({
"cols":[
{"label":"Category","type":"string"},
{"label":"WF 2 - Bottom 1","type":"number"},
{"label":"WF 2 - Bottom 2","type":"number"},
{"label":"WF 2 - Top 1","type":"number"},
{"label":"WF 2 - Top 2","type":"number"},
{"role":"style","type":"string","p":{"role":"style"}}
],
"rows":[
{"c":[{"v":"Budget"},{"v":0},{"v":0},{"v":22707893.613},{"v":22707893.613},{"v":"#007fff"}]},
{"c":[{"v":"Other"},{"v":22800059.612},{"v":22800059.612},{"v":21993391.103},{"v":21993391.103},{"v":"#1e8449"}]},
{"c":[{"v":"Labor"},{"v":21993391.103},{"v":21993391.103},{"v":21546003.177999996},{"v":21546003.177999996},{"v":"#1e8449"}]},
{"c":[{"v":"Travel"},{"v":21546003.177999996},{"v":21546003.177999996},{"v":21533258.930999994},{"v":21533258.930999994},{"v":"#1e8449"}]},
{"c":[{"v":"Training"},{"v":21533258.930999994},{"v":21533258.930999994},{"v":21550964.529999994},{"v":21550964.529999994},{"v":"#922b21"}]},
{"c":[{"v":"Actual"},{"v":0},{"v":0},{"v":21550964.52999999},{"v":21550964.52999999},{"v":"#007fff"}]}
]
});
var wfChart0 = new google.visualization.ChartWrapper({
chartType: 'CandlestickChart',
containerId: 'wf_chart_0',
dataTable: dataWF0,
options: {
animation: {
duration: 1500,
easing: 'inAndOut',
startup: true
},
backgroundColor: 'transparent',
bar: {
gap: 20,
width: 60
},
chartArea: {
backgroundColor: 'transparent'
},
height: 400,
legend: 'none',
theme: 'maximized',
tooltip: {
isHtml: true,
trigger: 'both'
},
vAxis: {
format: 'short',
viewWindow: {
min: 10000000,
max: 24000000
}
},
width: '100%'
}
});
wfChart0.draw();
var dataBar1 = new google.visualization.DataTable({
"cols":[
{"label":"Category","type":"string"},
{"label":"Bar 0","type":"number"},
{"label":"Bar 1","type":"number"}
],
"rows":[
// bar 0 data
{"c":[{"v":"YTD"},{"v":20807893.054},{"v":19684139.912}]}
]
});
var barChart1 = new google.visualization.ChartWrapper({
chartType: 'ColumnChart',
containerId: 'bar_chart_1',
dataTable: dataBar1,
options: {
animation: {
duration: 1500,
easing: 'inAndOut',
startup: true
},
backgroundColor: 'transparent',
bar: {
gap: 20,
width: 60
},
chartArea: {
backgroundColor: 'transparent',
},
height: 400,
legend: 'none',
theme: 'maximized',
tooltip: {
isHtml: true,
trigger: 'both'
},
vAxis: {
format: 'short',
viewWindow: {
min: 10000000,
max: 24000000
}
}
}
});
barChart1.draw();
}
.max-chart {
display: inline-block;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="max-chart" id="bar_chart_0"></div>
<div class="max-chart" id="wf_chart_0"></div>
<div class="max-chart" id="bar_chart_1"></div>

Google charts - change axis text color

I'm trying to create a black chart with Google Charts, but I can't seem to change the text color of the axis. I tried some code pieces I found on the web, like:
hAxis: {
color: '#FFF'
}
But it just doesn't work. I've managed to change the title and legend color, but not the axis text. I'm trying to set the axis text color to white, to contrast with the backgroud:
google.load("visualization", "1", { packages: ["corechart"] });
setTimeout(function() {
var options = {
title: 'Test Chart',
backgroundColor: '#000',
legendTextStyle: { color: '#FFF' },
titleTextStyle: { color: '#FFF' },
hAxis: {
color: '#FFF',
}
};
var chart = new google.visualization.LineChart(document.querySelector(".chart"));
chart.draw(google.visualization.arrayToDataTable(
[
["Year", "T1", "T2", "T3"],
[0, 10, 20, 30],
[1, 10, 20, 30],
[2, 10, 20, 30],
[3, 10, 20, 30],
[4, 10, 20, 30]
]
), options);
}, 100);
.chart {
width: 100%;
height: 200px;
}
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div class="chart"></div>
Correct usage for hAxis is using the textStyle options, in which you want the color:
hAxis: {
textStyle:{color: '#FFF'}
}
I would also recommend using google.setOnLoadCallback(drawChart); function for rendering the chart instead of timeout, at least for me 100 milliseconds was not enough
I manage to change all the chart texts with one CSS.
I think this way is more confortable than configure every chart text type (title, legend, vAxis, hAxis, others).
Maybe it will be usefull for someone.
Some code (Remember to change "#chart_div" for your chart id):
#chart_div text {
fill: red !important;
}