I am using google charts API to draw scatter, is there a way to label each node in the scatter diagram. I guess it only uses the numeric values.....Any other tools I can use instead.
Example:
Name Age Response
Allen 12 40
Tom 16 45
Sim 17 60
X and y axis will be age and response respectively, but each node on the graph can I label as Allen, Tom, Sim
I was having same issue to plot labels for points on chart itself.
Google chart have solved this problem now. You can add one more property as annotation. By which you can add labels.
See how it looks like. Generally I do annotation in number and then explain what those number are about.
var data = google.visualization.arrayToDataTable([
['Age', 'Weight', {role: 'annotation'}],
[ 8, 12, 'Point 1'],
[ 1, 5.5, 'Point 2'],
[ 11, 14, 'Point 3'],
[ 4, 5, 'Point 4'],
[ 3, 3.5, 'Point 5'],
[ 6.5, 7, 'Point 6']
]);
You can label the points in a Google ScatterChart by adding a tooltip. Tooltips show up when you mouseover a data point.
The code for your data table should look something like this:
var dt = new google.visualization.DataTable(
{
cols: [{id: 'A', label: 'Age', type: 'number'},
{id: 'B', label: 'Response', type: 'number'},
{id: 'C', label: 'Name', type:'tooltip', p:{role:'tooltip'}}
],
rows: [{c:[{v: 12}, {v: 40}, {v:'Allen'}]},
{c:[{v: 16}, {v: 45}, {v:'Tom'}]},
{c:[{v: 17}, {v: 60}, {v:'Sim'}]}
]
},
0.6
)
When you mouseover the points, the name will show up.
Link to Tooltips:
https://developers.google.com/chart/interactive/docs/roles#tooltiprole
Link to DataTable class (for formatting data):
https://developers.google.com/chart/interactive/docs/reference#DataTable
NOTE: if you're trying to plot multiple data series, you have to specify a tooltip for each one. For example, if you add a separate data series for Average Response, the code would change to:
var dt = new google.visualization.DataTable(
{
cols: [{id: 'A', label: 'Age', type: 'number'},
{id: 'B', label: 'Response', type: 'number'},
{id: 'C', label: 'Name', type:'tooltip', p:{role:'tooltip'}},
{id: 'D', label: 'AvgResp', type: 'number'},
{id: 'E', label: 'Category', type:'tooltip', p:{role:'tooltip'}}
],
rows: [{c:[{v: 12}, {v: 40}, {v:'Allen'}, {v:null}, {v:null}]},
{c:[{v: 16}, {v: 45}, {v:'Tom'}, {v:null}, {v:null}]},
{c:[{v: 17}, {v: 60}, {v:'Sim'}, {v:null}, {v:null}]},
{c:[{v: 12}, {v: null}, {v:null}, {v: 35}, {v:'Avg. 12yo'}]},
{c:[{v: 16}, {v: null}, {v:null}, {v: 48}, {v:'Avg. 16yo'}]},
{c:[{v: 17}, {v: null}, {v:null}, {v: 52}, {v:'Avg. 17yo'}]}
]
},
0.6
)
To do it using the visualization API, just use a cell_object (https://google-developers.appspot.com/chart/interactive/docs/reference#cell_object). The google API playground was useful for me, might be for you: https://code.google.com/apis/ajax/playground
Here's an example of some code:
var data = google.visualization.arrayToDataTable([
['Age','Response'],
[ {v:12, f: 'Allen'}, 40],
[ {v:16, f: 'Tom'}, 45],
[ {v:17, f: 'Sim'}, 60]
]);
Hope that helps!
https://developers.google.com/chart/image/docs/gallery/scatter_charts#chart_types
This page documents coloring the plots and adding a legend.
With Google charts you can't, short of using the legend and cross referencing. I don't know of any scatter chart creator that will label the individual plots.
Related
I would like to make the lines of the linechart to start from the left-bottom part of the graph and then go up to the first item (right), instead of just starting from the first item (left).
I tried adding a [null, 0, 0], but that just creates a new column, which adds some space between the beginning of the HAxis and the beginning of the lines.
Demo
in order for the line to begin at zero, at point must exist at 0, 0
but I think you will have better luck using an x-axis that is continuous (numeric) vs. discrete (string)
we can use object notation to provide a numeric value while still displaying a string
where v is the value and f is the formatted value
{v: 1, f: 'Value 1'}
we can use this in both the data table values, as well as the axis ticks.
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['Values', 'Line 1', 'Line 2'],
[{v: 0, f: ''}, 0, 0],
[{v: 1, f: 'Value 1'}, 20, 10],
[{v: 2, f: 'Value 2'}, 40, 50],
[{v: 3, f: 'Value 3'}, 50, null]
]);
var options = {
hAxis: {
ticks: [ // ticks to display on the x-axis
{v: 0, f: ''},
{v: 1, f: 'Value 1'},
{v: 2, f: 'Value 2'},
{v: 3, f: 'Value 3'},
{v: 4, f: ''}
],
title: 'HAxis'
},
vAxis: {
title: 'VAxis'
},
curveType: 'function'
};
var chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>
(obviously, the above snippet is not angular, but works the same...)
I have a bar chart that compares a budget with the money spent, for diferent categories.
The problem is, budget and progress can be zero, so the groups are hard to figure out, as can be seen in this JSFiddle.
It would be great to have a gridline that separates the groups, more obviously than with just empty space.
I can do it vertically, but it doesn't work horizontally.
hAxis: {
title: '$',
minValue: 0,
gridlines: {color: 'cyan'}
},
vAxis: {
title: 'Food',
gridlines: {color: 'red'}
}
Can anyone help?
gridlines are only supported by a continuous axis (number, date, etc.)
they will not appear for a discrete axis (string)
in the data table, you can use object notation in order to use a continuous axis,
but still display a string value. ({v: 4, f: 'Pizza'})
['Food', 'Budgeted', 'Spent'],
[{v: 4, f: 'Pizza'}, 8175000, 7854695],
[{v: 3, f: 'Hamburger'}, 0, 2792000],
[{v: 2, f: 'Milkshake'}, 2695000, 0],
[{v: 1, f: 'Paella'}, 0, 0]
where v: is the value, and f: is the formatted value.
the value is used to control the order,
the formatted value will be displayed in the tooltip.
then, in order to display the labels on the axis,
we must use object notation in the ticks option.
ticks: [
{v: 0.5, f: ''},
{v: 1, f: 'Paella'},
{v: 1.5, f: ''},
{v: 2, f: 'Milkshake'},
{v: 2.5, f: ''},
{v: 3, f: 'Hamburger'},
{v: 3.5, f: ''},
{v: 4, f: 'Pizza'},
{v: 4.5, f: ''}
],
the ticks with the half value will add a line between.
but you won't be able to add grid lines only between.
because we must add a tick for the label itself.
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['Food', 'Budgeted', 'Spent'],
[{v: 4, f: 'Pizza'}, 8175000, 7854695],
[{v: 3, f: 'Hamburger'}, 0, 2792000],
[{v: 2, f: 'Milkshake'}, 2695000, 0],
[{v: 1, f: 'Paella'}, 0, 0]
]);
var options = {
title: 'Food budgeted vs food spent',
bar: {
groupWidth: '75%',
},
chartArea: {width: '50%'},
hAxis: {
title: '$',
minValue: 0,
gridlines: {color: 'red'}
},
vAxis: {
title: 'Food',
ticks: [
{v: 0.5, f: ''},
{v: 1, f: 'Paella'},
{v: 1.5, f: ''},
{v: 2, f: 'Milkshake'},
{v: 2.5, f: ''},
{v: 3, f: 'Hamburger'},
{v: 3.5, f: ''},
{v: 4, f: 'Pizza'},
{v: 4.5, f: ''}
],
gridlines: {color: 'red'}
}
};
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
I have a DataTable, a DateRangeFilter and a aggregation object.
I was hoping I could use DateRangeFilter to filter the data in the DataTable and to limit the aggregation to the FILTERED DataTable. But that's not happening.
Am I doing something wrong (maybe forgot to refresh/draw), or something like that is not possible.
My code is here:
https://jsfiddle.net/v0w5ehsc/
var dashboard = new google.visualization.Dashboard(document.getElementById('daterange_div'));
dashboard.bind(slider, tableChart);
dashboard.draw(dataAll);
var grouped_data = google.visualization.data.group(
dataAll,
[0, 2],
[
{'column': 3, 'aggregation': google.visualization.data.sum, 'type': 'number'}
]
);
you can only bind one data table to a dashboard / filter.
in order to aggregate the filtered data table...
Wait for the 'ready' event on the ChartWrapper
Aggregate the filtered data table from the ChartWrapper
see following example building off previous question...
google.charts.load('current', {
callback: drawVisualization,
packages:['corechart', 'table', 'controls']
});
function drawVisualization() {
var data = new google.visualization.DataTable({
cols: [
{id: 'dat_ym', label: 'Start Date', type: 'date'},
{id: 'user-id', label: 'User-Id', type: 'string'},
{id: 'customer-id', label: 'Customer-Id', type: 'string'},
{id: 's_minutes', label: 'minutes', type: 'number'}
],
rows: [
{c:[{v: new Date('2016, 01, 01')}, {v: '44836'}, {v: '67205'}, {v: 1122} ]},
{c:[{v: new Date('2016, 01, 01')}, {v: '86495'}, {v: '67205'}, {v: 332} ]},
{c:[{v: new Date('2016, 01, 01')}, {v: '44836'}, {v: '228626'}, {v: 0} ]},
{c:[{v: new Date('2016, 01, 01')}, {v: '86495'}, {v: '228626'}, {v: 334} ]},
{c:[{v: new Date('2016, 02, 01')}, {v: '44836'}, {v: '67205'}, {v: 554} ]},
{c:[{v: new Date('2016, 02, 01')}, {v: '86495'}, {v: '67205'}, {v: 0} ]},
{c:[{v: new Date('2016, 02, 01')}, {v: '44836'}, {v: '228626'}, {v: 0} ]},
{c:[{v: new Date('2016, 02, 01')}, {v: '86495'}, {v: '228626'}, {v: 544} ]},
]
});
var options = {
hAxis: {title: '', textStyle: { fontSize: '13' }, textPosition: 'in'},
vAxis: {title: '', textStyle: { fontSize: '10' }},
seriesType: 'bars',
legend: {position: 'top', textStyle: {color: 'black', fontSize: 14}},
isStacked: true
};
// build dashboard
var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard-div'));
var table = new google.visualization.ChartWrapper({
chartType: 'Table',
containerId: 'table-div',
options: {
allowHtml: true
}
});
var dateSlider = new google.visualization.ControlWrapper({
controlType: 'DateRangeFilter',
containerId: 'slider-div',
options: {
filterColumnIndex: 0
}
});
// Table 'ready' event
google.visualization.events.addListener(table, 'ready', function () {
// group data by date, customer
var grouped_data = google.visualization.data.group(
// get data table from ChartWrapper
table.getDataTable(),
[0, 2],
[
{'column': 3, 'aggregation': google.visualization.data.sum, 'type': 'number'}
]
);
// sort grouped data
grouped_data.sort([{column: 0},{column: 1}]);
// get unique customers
var custGroup = google.visualization.data.group(
data,
[2]
);
// build customer data table
var custData = new google.visualization.DataTable({
cols: [
{id: 'dat_ym', label: 'Start Date', type: 'date'},
]
});
// add column for each customer
for (var i = 0; i < custGroup.getNumberOfRows(); i++) {
custData.addColumn(
{label: custGroup.getValue(i, 0), type: 'number'}
);
}
// add row for each date / customer
var rowDate;
var rowIndex;
for (var i = 0; i < grouped_data.getNumberOfRows(); i++) {
if (rowDate !== grouped_data.getValue(i, 0).getTime()) {
rowDate = grouped_data.getValue(i, 0).getTime();
rowIndex = custData.addRow();
custData.setValue(rowIndex, 0, new Date(rowDate));
}
for (var x = 1; x < custData.getNumberOfColumns(); x++) {
if (custData.getColumnLabel(x) === grouped_data.getValue(i, 1)) {
custData.setValue(rowIndex, x, grouped_data.getValue(i, 2));
}
}
}
// draw agg table
new google.visualization.ChartWrapper({
chartType: 'Table',
containerId: 'agg-div',
dataTable: custData,
options: {
allowHtml: true
}
}).draw();
});
// draw dashboard
dashboard.bind(dateSlider, table);
dashboard.draw(data);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="dashboard-div">
<div id="slider-div"></div>
<div id="table-div"></div>
<br/>
<div id="agg-div"></div>
</div>
I have a MySQL recordset like:
Date;Customer;Turnover
2016-03-01;Cust1;120
2016-03-01;Cust2;120
2016-04-01;Cust1;130
2016-04-01;Cust2;125
2016-05-01;Cust1;135
2016-05-01;Cust2;110
My goal is to have a stacked bar chart with Year/Month on the x-axis (type of date so I can use date filters), and customer values stacked on top of each other. In addition I need to filter the customers I want to hide from the display.
I've been trying to group/aggregate the data, also played with dashboard filter controls, but I haven't made a breakthrough yet so I was hoping someone might give me a hint on where to start with this.
in order to display the date on the x-axis, you need a column for each customer
Group the data by Date, Customer
Sort grouped data by Date, Customer
Get unique Customer Id's
Build new table with column for each Customer
see example, taken from the provided fiddle...
google.charts.load('current', {
callback: drawVisualization,
packages:['corechart', 'table', 'controls']
});
function drawVisualization() {
var data = new google.visualization.DataTable({
cols: [
{id: 'dat_ym', label: 'Start Date', type: 'date'},
{id: 'user-id', label: 'User-Id', type: 'string'},
{id: 'customer-id', label: 'Customer-Id', type: 'string'},
{id: 's_minutes', label: 'minutes', type: 'number'}
],
rows: [
{c:[{v: new Date('2016, 01, 01')}, {v: '44836'}, {v: '67205'}, {v: 1122} ]},
{c:[{v: new Date('2016, 01, 01')}, {v: '86495'}, {v: '67205'}, {v: 332} ]},
{c:[{v: new Date('2016, 01, 01')}, {v: '44836'}, {v: '228626'}, {v: 0} ]},
{c:[{v: new Date('2016, 01, 01')}, {v: '86495'}, {v: '228626'}, {v: 334} ]},
{c:[{v: new Date('2016, 02, 01')}, {v: '44836'}, {v: '67205'}, {v: 554} ]},
{c:[{v: new Date('2016, 02, 01')}, {v: '86495'}, {v: '67205'}, {v: 0} ]},
{c:[{v: new Date('2016, 02, 01')}, {v: '44836'}, {v: '228626'}, {v: 0} ]},
{c:[{v: new Date('2016, 02, 01')}, {v: '86495'}, {v: '228626'}, {v: 544} ]},
]
});
// group data by date, customer
var grouped_data = google.visualization.data.group(
data,
[0, 2],
[
{'column': 3, 'aggregation': google.visualization.data.sum, 'type': 'number'}
]
);
// sort grouped data
grouped_data.sort([{column: 0},{column: 1}]);
// get unique customers
var custGroup = google.visualization.data.group(
data,
[2]
);
// build customer data table
var custData = new google.visualization.DataTable({
cols: [
{id: 'dat_ym', label: 'Start Date', type: 'date'},
]
});
// add column for each customer
for (var i = 0; i < custGroup.getNumberOfRows(); i++) {
custData.addColumn(
{label: custGroup.getValue(i, 0), type: 'number'}
);
}
// add rows for each date
var rowDate;
var rowIndex;
for (var i = 0; i < grouped_data.getNumberOfRows(); i++) {
// add row when date changes
if (rowDate !== grouped_data.getValue(i, 0).getTime()) {
rowDate = grouped_data.getValue(i, 0).getTime();
rowIndex = custData.addRow();
custData.setValue(rowIndex, 0, new Date(rowDate));
}
// find customer column
for (var x = 1; x < custData.getNumberOfColumns(); x++) {
if (custData.getColumnLabel(x) === grouped_data.getValue(i, 1)) {
custData.setValue(rowIndex, x, grouped_data.getValue(i, 2));
}
}
}
var options = {
hAxis: {title: '', textStyle: { fontSize: '13' }, textPosition: 'in'},
vAxis: {title: '', textStyle: { fontSize: '10' }},
seriesType: 'bars',
legend: {position: 'top', textStyle: {color: 'black', fontSize: 14}},
isStacked: true
};
var tableTE = new google.visualization.Table(document.getElementById('table_div_te'));
tableTE.draw(data, {showRowNumber: true, frozenColumns: 1, width: '100%', height: '300'});
var tableTEAgg = new google.visualization.Table(document.getElementById('table_div_teagg'));
tableTEAgg.draw(custData, {showRowNumber: true, frozenColumns: 1, width: '100%', height: '300'});
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(custData, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="table_div_te"></div>
<div id="table_div_teagg"></div>
<div id="chart_div" style="width: 1600px; height: 300px;"></div>
I'm using Google Charts. I gave it this data:
var data = new google.visualization.DataTable(
{
cols: [{id: 'date', label: 'Date of like', type: 'date'},
{id: 'likes', label: 'Number of likes', type: 'number'}],
rows: [{c:[{v: new Date(2012, 7, 10, 00, 00)}, {v: 2}]},
{c:[{v: new Date(2012, 7, 10, 11, 20)}, {v: 2}]},
{c:[{v: new Date(2012, 7, 10, 11, 25)}, {v: 4}]},
{c:[{v: new Date(2012, 7, 10, 11, 30)}, {v: 15}]},
{c:[{v: new Date(2012, 7, 10, 12, 45)}, {v: 30}]},
{c:[{v: new Date(2012, 7, 10, 13, 15)}, {v: 45}]},
{c:[{v: new Date(2012, 7, 10, 14, 05)}, {v: 63}]},
{c:[{v: new Date(2012, 7, 10, 16, 20)}, {v: 69}]},
{c:[{v: new Date(2012, 7, 10, 18, 45)}, {v: 74}]},
{c:[{v: new Date(2012, 7, 10, 20, 05)}, {v: 76}]},
{c:[{v: new Date(2012, 7, 10, 23, 45)}, {v: 77}]},
{c:[{v: new Date(2012, 7, 10, 24, 00)}, {v: 77}]}
]
}
In the Date function, the last 2 numbers are the hour and the number of minutes. The problem is Google Charts seems to only allow me to display the day of the dates as values (gridlines) in the horizontal axis, but I'd like to display the hours within a day. Google Charts doesn't seem to scale lower than the unit of 'one day'.
Is there a way I could display hours in the horizontal axis? Thanks in advance.