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>
Related
I want to change the candlestick chart's color.
When fallingColor.fill and risingColor.fill are set, the vertical line color remains blue like the image.
This is the code I wrote.
<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(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['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]
// Treat first row as data as well.
], true);
var options = {
legend:'none',
candlestick: {
fallingColor: { strokeWidth: 0, fill: '#a52714' }, // red
risingColor: { strokeWidth: 0, fill: '#0f9d58' }, // green
},
};
var chart = new google.visualization.CandlestickChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
https://jsfiddle.net/fe26/t6b57vnL/7/
How can I change the color of the line?
you can use the colors option, an array of color strings.
each color in the array, is applied to each series in the data table.
in the example posted, there is only one series, so only one color is needed / allowed.
the colors option will change the color of all the chart elements.
however, the fallingColor & risingColor options will override the colors option.
leaving only the vertical lines the same color as the colors option.
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['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]
], true);
var options = {
legend:'none',
candlestick: {
fallingColor: { strokeWidth: 0, fill: '#a52714' }, // red
risingColor: { strokeWidth: 0, fill: '#0f9d58' }, // green
},
colors: ['magenta']
};
var chart = new google.visualization.CandlestickChart(document.getElementById('chart_div'));
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
you can also you the style column role.
the style role is added as an additional column to the data table.
set the value of the column to the color / style you want to display for each row.
if the style value is null, it will default back to the above scenario.
however, if it is not null, it will override both options in the above scenario.
note: you will need to provide column headings to use the style role.
var data = google.visualization.arrayToDataTable([
['x', 'low', 'open', 'close', 'high', {role: 'style', type: 'string'}],
['Mon', 20, 28, 38, 45, 'lime'],
['Tue', 31, 38, 55, 66, 'purple'],
['Wed', 50, 55, 77, 80, null],
['Thu', 77, 77, 66, 50, null],
['Fri', 68, 66, 22, 15, 'orange']
]);
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['x', 'low', 'open', 'close', 'high', {role: 'style', type: 'string'}],
['Mon', 20, 28, 38, 45, 'lime'],
['Tue', 31, 38, 55, 66, 'purple'],
['Wed', 50, 55, 77, 80, null],
['Thu', 77, 77, 66, 50, null],
['Fri', 68, 66, 22, 15, 'orange']
]);
var options = {
legend:'none',
candlestick: {
fallingColor: { strokeWidth: 0, fill: '#a52714' }, // red
risingColor: { strokeWidth: 0, fill: '#0f9d58' }, // green
},
colors: ['magenta']
};
var chart = new google.visualization.CandlestickChart(document.getElementById('chart_div'));
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
another option would be to use css, and override the svg elements for the vertical lines,
which are drawn using <rect> elements, with --> width="2"
but I would use this as a last resort.
#chart_div rect[width="2"] {
fill: #000000;
stroke: #000000;
}
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['x', 'low', 'open', 'close', 'high', {role: 'style', type: 'string'}],
['Mon', 20, 28, 38, 45, 'lime'],
['Tue', 31, 38, 55, 66, 'purple'],
['Wed', 50, 55, 77, 80, null],
['Thu', 77, 77, 66, 50, null],
['Fri', 68, 66, 22, 15, 'orange']
]);
var options = {
legend:'none',
candlestick: {
fallingColor: { strokeWidth: 0, fill: '#a52714' }, // red
risingColor: { strokeWidth: 0, fill: '#0f9d58' }, // green
},
colors: ['magenta']
};
var chart = new google.visualization.CandlestickChart(document.getElementById('chart_div'));
chart.draw(data, options);
});
#chart_div rect[width="2"] {
fill: #000000;
stroke: #000000;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
you could also listen for the chart's 'ready' event,
and manually change the chart's svg elements using your own script.
but this is not recommended...
Iam using Google Chart in Angular 6
I am using
pieSliceText: 'value'
But I need to display both label and value on the pie slice,
I tried using
pieSliceText: 'label-and-value'
similar to
pieSliceText: 'value-and-percentage'
But this doesnt work for me
a couple options...
1) use the following option to label the slices...
legend: {
position: 'labeled'
}
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
var options = {
height: 400,
is3D: true,
legend: {
position: 'labeled'
},
pieSliceText: 'value'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="piechart_3d"></div>
2) use the following option to display the value of the slice...
pieSliceText: 'value'
with the above option, the slice will display the formatted value by default.
using object notation, we can provide both the value v: and formatted value f: in the data table.
{v: 11, f: 'Work: 11'}
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', {v: 11, f: 'Work: 11'}],
['Eat', {v: 11, f: 'Eat: 2'}],
['Commute', {v: 11, f: 'Commute: 2'}],
['Watch TV', {v: 11, f: 'Watch TV: 2'}],
['Sleep', {v: 11, f: 'Sleep: 7'}]
]);
var options = {
chartArea: {
height: '100%',
width: '100%',
top: 12,
left: 12,
right: 12,
bottom: 12
},
height: 400,
is3D: true,
legend: {
position: 'none'
},
pieSliceText: 'value'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="piechart_3d"></div>
How to mix line and scatter plot on one chart?
Column 1: Y (date)
Column 2: X1 (number, as points/dots)
Column 3: X2 (number, as line)
Example JsFiddle: https://jsfiddle.net/e64y5wfj/4/
It's doesn't seems a way or example on the documentation.
use the series option to change the style of Column 2
series: {
0: {
pointSize: 8,
lineWidth: 0
}
}
see following working snippet...
google.charts.load('current', {
callback: drawMultipleTrendlineChart,
packages:['corechart']
});
function drawMultipleTrendlineChart() {
var chart;
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Sales value A');
data.addColumn('number', 'Sales value B');
data.addRows([
[new Date(2013, 3, 11), 200, 1000],
[new Date(2013, 4, 02), 500, 650],
[new Date(2013, 5, 03), 700, 550],
[new Date(2013, 6, 04), 800, 95],
[new Date(2013, 7, 05), 500, 400],
[new Date(2013, 8, 06), 900, 250],
[new Date(2014, 0, 07), 800, 300],
[new Date(2014, 1, 08), 2000, 200],
[new Date(2014, 2, 09), 1000, 312]
]);
var formatter = new google.visualization.NumberFormat({
fractionDigits: 2,
prefix: 'R$:'
});
formatter.format(data, 1);
var dateFormatter = new google.visualization.NumberFormat({
pattern: 'MMM yyyy'
});
dateFormatter.format(data, 0);
var chartHeight = 400;
var chartWidth = 600;
var chartOptions = {
tooltip: {
isHtml: true
},
title: 'multiple lines',
isStacked: true,
width: chartWidth,
height: chartHeight,
colors: ['#0000D8', '#00dddd'],
hAxis: {
title: 'example title',
slantedText: false,
slantedTextAngle: 45,
textStyle: {
fontSize: 10
},
format: 'dd-MM-yyyy'
},
chartArea: {
left: 50,
top: 20,
width: (chartWidth - 10),
height: (chartHeight - 90)
},
series: {
0: {
pointSize: 8,
lineWidth: 0
}
}
};
chart = new google.visualization.LineChart(document.getElementById('multipleTrendChart'));
chart.draw(data, chartOptions);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="multipleTrendChart"></div>
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>
im working on a report, where i need to populate a graph with dual axis, can someone help me out to find how can i impelment that, here is the sample chart that i need.
i im trying to use https://developers.google.com/chart/interactive/docs/gallery/combochart but looking this will not work for me.
Thanks.
This should get you started...
google.load("visualization", "1", {
packages: ["corechart"]
});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
["Week Ending","Actual FT", "Recommended FTE", "Other Tickets", "Router Tickets"],
["7/1", 1800, 1900, 19, 22],
["7/8", 1800, 1900, 20, 23],
["7/15", 1800, 1900, 20, 23],
["7/22", 1800, 1900, 19, 22],
// ..
["9/29", 1800, 1900, 29, 30]
]);
var options2 = {
vAxes: [{
minValue: 1200,
maxValue: 2500
}, {
minValue: 17,
maxValue: 30
}],
curveType: 'function',
hAxis: {
title: "week ending"
},
series: {
0: {
type: "bars",
targetAxisIndex: 0,
color: "blue"
},
1: {
type: "bars",
targetAxisIndex: 0,
color: "green"
},
2: {
type: "line",
targetAxisIndex: 1,
color: "red"
},
3: {
type: "line",
targetAxisIndex: 1,
color: "cyan"
}
}
};
var chart = new google.visualization.LineChart(document.getElementById("chart"));
chart.draw(data, options2);
}
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="chart" style="width: 900px; height: 300px;"></div>