How to add target line in google column chart? - google-visualization

How to add the target line in google column chart like this.

If you'd like to combine the columnchart and linechart, use ComboChart. Documentation and examples are here :
https://developers.google.com/chart/interactive/docs/gallery/combochart
basically, have the data point for the line chart as one of the columns in the DataTable and specify this column to be the "series" = "line", whereas the other columns are visualized in a ColumnChart.

You can use a Stepped Area series to achieve this. It's a little awkward but works well.
var data = google.visualization.arrayToDataTable([
['Month', 'Bolivia', 'Ecuador', 'Madagascar', 'Papua New Guinea', 'Rwanda', ''],
['2004/05', 165, 938, 522, 998, 450, 250],
['2005/06', 135, 1120, 599, 1268, 288, 250],
['2006/07', 157, 1167, 587, 807, 397, 250],
['2007/08', 139, 1110, 615, 968, 215, 250],
['2008/09', 136, 691, 629, 1026, 366, 250]
]);
var options = {
seriesType: "line",
series: {5: {
type: "steppedArea",
color: '#FF0000',
visibleInLegend: false,
areaOpacity: 0}
}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
Example
Stepped Area Google Chart Example

To avoid the ugly outline, just use: enableInteractivity: false

To make the steppedArea #Ryan suggested above a litte bit less awkward, you can setup a second (right) axis and set the base line to the value you want for the target line. The second axis will be setup for the seppedArea data. This avoids the uggly outline effect when you hover the pointer over the chart and under the line. Do something like this in the options:
var options = {
seriesType: "line",
series: {5: {
type: "steppedArea",
color: '#FF0000',
visibleInLegend: false,
areaOpacity: 0,
targetAxisIndex: 1 } //tell the series values to be shown in axe 1 bellow
},
vAxes: [ //each object in this array refers to one axe setup
{}, //axe 0 without any special configurations
{
ticks: [250], //use this if you want to show the target value
baseline: 250 //this shifts the base line to 250
}
]
};

Related

I am not able to remove legend in this google chart, also not able to change height , I putted it inside a col-md-4 div , also there help me

js code :
its to add chart to col-4 bar must have 3 columns each having space between it all different color, remove legend in this google chart
function drawBarChart() {
var data = google.visualization.arrayToDataTable([
["Retention", "Level", {
role: "style"
}],
["HIGH", 12, "#E54B4B"],
["ELEVATED", 29, "#FFB100"],
["LOW", 52, "#2BC18D"],
]);
var options = {
title: "Density of Precious Metals, in g/cm^3",
height: 600,
width: 370,
legend: 'none',
bar: {
groupWidth: "55%"
},
ticks: [0, 20, 40, 60, 80, 100],
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_divTwo'));
// Convert the Classic options to Material options.
chart.draw(data, google.charts.Bar.convertOptions(data, options));
};

How to draw a combo graph in Google Chart vertically with dual axes at top and bottom?

I've got a Google Chart ComboGraph in vertical orientation and I'd like to display the dependent axis (horizontal one in this configuration) both at the bottom and at the top of the graph.
For horizontal orientation, a hack I can use to achieve this is to create a dummy second axis - then the two axes display on left and right of the graph. But this doesn't translate to top and bottom for a vertical orientation.
I don't mind using some JS fiddling to achieve the result but it's not clear to me if this is possible - any ideas?
Here's an example graph - there's also the issue that the viewWindow doesn't seem to be respected for either axis, as can be seen by looking at the 'horizontal' view (the 'tracking' and 'candle' should fit together):
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawVisualization);
function drawVisualization() {
// Some raw data (not necessarily accurate)
var data = google.visualization.arrayToDataTable([
['Value', 'Candle', '', '', '', 'Points', 'Tracking'],
['1', 165, 938, 938, 998, 450, 938],
['2', -150, 599, 599, 1268, 288, 599],
['3', 157, 587, 587, 807, 397, 587],
['4', 139, 615, 615, 968, 215, 615],
['5', 136, 629, 629, 1026, 1200, 629]
]);
var options = {
orientation:'vertical',
title : '',
vAxis: {title: ''},
hAxis: {
0: {
title: '0',
viewWindow: { min: -200, max: 1200 },
minorGridlines: { count: 4, color:'#E9E9E9' }
},
1: {
title: '1',
viewWindow: { min: -200, max: 1200 }
}
},
seriesType: 'candlesticks',
series: {
1: {type: 'line', pointsVisible:true,lineWidth:0,color:'#FF9955'},
2: {type:'bars',color:'#AAAAFF'},
0: {color: 'blue', targetAxisIndex: 1}
}
};
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
Top-X Charts are only available via standard config options on a Material chart
However, Material charts do not support "combo" charts...
But if all you want are the same axis labels displayed at the top,
you can clone them and change the 'y' attribute manually,
when the 'ready' event fires
see following working snippet,
use chart method --> getChartLayoutInterface().getBoundingBox('chartarea')
to find the top coordinate of the chart area...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['Value', 'Candle', '', '', '', 'Points', 'Tracking'],
['1', 165, 938, 938, 998, 450, 938],
['2', -150, 599, 599, 1268, 288, 599],
['3', 157, 587, 587, 807, 397, 587],
['4', 139, 615, 615, 968, 215, 615],
['5', 136, 629, 629, 1026, 1200, 629]
]);
var options = {
orientation:'vertical',
title : '',
vAxis: {title: ''},
hAxis: {
0: {
title: '0',
viewWindow: { min: -200, max: 1200 },
minorGridlines: { count: 4, color:'#E9E9E9' }
},
1: {
title: '1',
viewWindow: { min: -200, max: 1200 }
}
},
seriesType: 'candlesticks',
series: {
1: {type: 'line', pointsVisible:true,lineWidth:0,color:'#FF9955'},
2: {type:'bars',color:'#AAAAFF'},
0: {color: 'blue', targetAxisIndex: 1}
}
};
var container = document.getElementById('chart_div');
var chart = new google.visualization.ComboChart(container);
google.visualization.events.addListener(chart, 'ready', function () {
var chartLayout = chart.getChartLayoutInterface();
var svg = container.getElementsByTagName('svg')[0];
var axisLabels = svg.getElementsByTagName('text');
var coords = chartLayout.getBoundingBox('chartarea');
Array.prototype.forEach.call(axisLabels, function(label) {
if (label.getAttribute('text-anchor') === 'middle') {
var topAxisLabel = label.cloneNode(true);
var yCoord = coords.top - parseInt(topAxisLabel.getAttribute('font-size'));
topAxisLabel.setAttribute('y', yCoord);
svg.appendChild(topAxisLabel);
}
});
});
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
note: changes made manually in this way will not show when using chart method --> getImageURI()
if you need to get an image, you can use html2canvas instead...

Angular- Chart.js, how off opacity in line chart?

guys, how can i change opacity in line chart?? Look at my chart:
red, gray, and blue background. I need make it 100% invisible.
Here is my code:
<canvas id="line" class="chart chart-line" chart-data="data2" chart-labels="labels2" chart-series="series2" chart-options="options2"
chart-dataset-override="datasetOverride2" chart-click="onClick" chart-colours="colours">
</canvas>
And fragment of JS:
$scope.rysujWykres2 = function (daneX, daneY, daneZ, daneA) { //metoda rysujÄ…ca wykres
$scope.labels2 = daneX;
$scope.series2 = ['Ur', 'Ul', 'Uz'];
$scope.data2 = [
daneY,
daneZ,
daneA
];
$scope.datasetOverride2 = [{ yAxisID: 'y-axis-1' }];
$scope.options2 = {
scales: {
yAxes: [
{
id: 'y-axis-1',
type: 'linear',
display: true,
position: 'left'
}
]
},
elements: {
point: {
radius: 1
}
},
colours: [{
fillColor: 'rgba(47, 132, 71, 0.8)',
strokeColor: 'rgba(47, 132, 71, 0.8)',
highlightFill: 'rgba(47, 132, 71, 0.8)',
highlightStroke: 'rgba(47, 132, 71, 0.8)'
}]
};
}
but it doesnt work. Can u help me off this colours? I need only lines.
If you do not want to see the fill under the line, then set the fill property to false (this is part of a line chart dataset configuration). I'm not exactly sure how to do this in angular chart.js, but it looks like you could add it to your datasetOverride2 object.
$scope.datasetOverride2 = [{fill: false}, {fill: false}, {fill: false}];
If this doesn't work then you need to use the property wherever you define each dataset.
My friend,
$scope.datasetOverride2 = [{
yAxisID: 'y-axis-1',
fill: false,
}];
work but only for blue color date, look:
aww, its so easy. Look:
Solution:
$scope.datasetOverride2 = [{fill: false},{fill: false},{fill: false}];
Thanks guys for help. We did it!

google visualization bar chart stacking two bars

I have a google visualization bar chart sample here where the data format is as given below.
var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Expenses', 'Profit'],
['2014', 1000, 400, 200],
['2015', 1170, 460, 250],
['2016', 660, 1120, 300],
['2017', 1030, 540, 350]
]);
How to make expenses and profits bar stacked where as sales is a separate bar?
At the moment the Google Charts API doesn't have this in built feature, but with some remodeling of the DataTable and Chart Options you can still achieve this.
My Solution
The Stacked Bar should contain values of only Expenses and Profit, to avoid a Stacked bar with Sales the value in the data column for Sales is represented as zero. A separate Sales bar is created by having a similar data row, but with the value of Sales present, and the rest zero. The Date data type needs to be specified in order to group all bar charts with the same date, if this isn't implemented, then there will be a gap between each bar chart with the same year.
More information on Date representation of Columns is available here.
var data = google.visualization.arrayToDataTable([
[ {label: 'Year', id: 'year', type: 'date'},
{label: 'Sales', id: 'Sales', type: 'number'},
{label: 'Expenses', id: 'Expenses', type: 'number'},
{label: 'Profit', id: 'Profit', type: 'number'}],
[{v:new Date('2014'), f:'2014'}, 0, 400, 200],
[{v:new Date('2014'), f:'2014'}, 1000, 0, 0],
[{v:new Date('2015'), f:'2015'}, 0, 460, 250],
[{v:new Date('2015'), f:'2015'}, 1170, 0, 0],
[{v:new Date('2016'), f:'2016'}, 0, 1120, 300],
[{v:new Date('2016'), f:'2016'}, 600, 0, 0],
[{v:new Date('2017'), f:'2017'}, 0, 540, 350],
[{v:new Date('2017'), f:'2017'}, 1030, 0, 0]
]);
To achieve the stacked bar, the google charts configuration option isStacked: true is used. To avoid the vertical axis acting like a timeline with months and days, the vertical axis is formatted to display the Year using vAxis: {format: 'yyyy'}. More information on formatting is available here. To avoid spacing between different Year Bars, the feature bar: {groupWidth: '90%'} is used.
var options = {
chart: {
title: 'Company Performance',
subtitle: 'Sales, Expenses, and Profit: 2014-2017',
},
bars: 'horizontal', // Required for Material Bar Charts.
hAxis: {format: 'decimal'},
vAxis: {
format: 'yyyy'
},
height: 400,
colors: ['#1b9e77', '#d95f02', '#7570b3'],
isStacked: true,
bar: {groupWidth: '90%'}
};

Chart.js - How to set a line chart dataset as disabled on load

Using chart.js v2, is it possible to mark a dataset in a line chart as being disabled on initial load?
Didn't find an option for it in the documentation.
Yes, there is a "hidden" flag in ChartJS.
eg.
data:
{
datasets: [
{
data: [1,2,3],
label: 'My First Dataset',
hidden: true,
},
],
}
See this issue on GitHub: https://github.com/chartjs/Chart.js/issues/689
The accepted solution has the downside, that hiding/unhiding signals might sometimes fail after initializing the chart like that.
It might be a better idea to change it in the current metadata of the dataSet, which holds the actual data that is used by the chart:
chart.data.datasets.forEach((dataSet, i) => {
var meta = chart.getDatasetMeta(i);
meta.hidden = (<your-condition-here>);
});
this.chart.update();
If you are using angular-chartjs, then you can add the properties of the dataset in the chart-dataset-override property:
For example:
HTML:
<div class="container" ng-app="app" ng-controller="ChartCtrl">
<canvas id="bar" class="chart chart-bar" chart-data="data" chart-labels="labels" chart-series="series" chart-dataset-override="datasetOverride">
</canvas>
</div>
Javascript:
Chart.defaults.global.legend.display = true;
angular.module("app", ["chart.js"])
.controller("ChartCtrl", function($scope) {
$scope.labels = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
$scope.series = ['Series A', 'Series B'];
$scope.data = [
[65, 59, 80, 81, 56, 55, 40],
[28, 48, 40, 19, 86, 27, 90]
];
$scope.datasetOverride = [{}, {
hidden: true,
}];
});