谷歌可视化API -隐藏数据列从图形
google visualization API - Hide data column from graphic
我想知道是否有人能够或知道的任何方式,我可以隐藏/显示一行从AreaChart从谷歌可视化API加载后。
更详细地说,假设我们在面积图中有8条线,但因为它们太多了,这使得图形几乎无法阅读和混淆。所以我想要一段JavaScript代码,可以打开或关闭这些行后,图表已经建成,并在浏览器中提供给用户。
当我说开/关时,任何东西都可以,改变不透明度,删除线条,或者任何让它从图形中消失的东西。
我在构建图表时使用CSS样式(在构建它的JavaScript上),在chart.draw(data, options)
上使用选项(系列)。
我不能改变我拥有的数据数组。所以我不是在寻找一个动态的数据,CSS和JavaScript的解决方案。
找到这段代码,我能够将其适应为AreaChart。希望对别人有所帮助。
http://jsfiddle.net/asgallant/6gz2Q/<div id="chart_div"></div>
<div id="creativeCommons" style="text-align: center; width: 400px;">
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/InteractiveResource" property="dct:title" rel="dct:type">Code to turn on or off data series by clicking on legend entries</span> by <span xmlns:cc="http://creativecommons.org/ns#" property="cc:attributionName">Andrew Gallant</span> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US">Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License</a>.
</div>
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Y 1');
data.addColumn({type: 'boolean', role: 'certainty'});
data.addColumn('number', 'Y 2');
data.addColumn({type: 'string', role: 'annotation'});
data.addColumn({type: 'boolean', role: 'certainty'});
data.addColumn('number', 'Y 3');
data.addColumn({type: 'boolean', role: 'certainty'});
// add random data
var y1 = 50, y2 = 50, y3 = 50;
for (var i = 0; i < 30; i++) {
y1 += ~~(Math.random() * 5) * Math.pow(-1, ~~(Math.random() * 2));
y2 += ~~(Math.random() * 5) * Math.pow(-1, ~~(Math.random() * 2));
y3 += ~~(Math.random() * 5) * Math.pow(-1, ~~(Math.random() * 2));
data.addRow([i, y1, (~~(Math.random() * 2) == 1), y2, y2.toString(), (~~(Math.random() * 2) == 1), y3, (~~(Math.random() * 2) == 1)]);
}
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.ChartWrapper({
chartType: 'LineChart',
containerId: 'chart_div',
dataTable: data,
options: {
width: 600,
height: 400
}
});
// create columns array
var columns = [0];
/* the series map is an array of data series
* "column" is the index of the data column to use for the series
* "roleColumns" is an array of column indices corresponding to columns with roles that are associated with this data series
* "display" is a boolean, set to true to make the series visible on the initial draw
*/
var seriesMap = [{
column: 1,
roleColumns: [2],
display: true
}, {
column: 3,
roleColumns: [4, 5],
display: true
}, {
column: 6,
roleColumns: [7],
display: false
}];
var columnsMap = {};
var series = [];
for (var i = 0; i < seriesMap.length; i++) {
var col = seriesMap[i].column;
columnsMap[col] = i;
// set the default series option
series[i] = {};
if (seriesMap[i].display) {
// if the column is the domain column or in the default list, display the series
columns.push(col);
}
else {
// otherwise, hide it
columns.push({
label: data.getColumnLabel(col),
type: data.getColumnType(col),
sourceColumn: col,
calc: function () {
return null;
}
});
// backup the default color (if set)
if (typeof(series[i].color) !== 'undefined') {
series[i].backupColor = series[i].color;
}
series[i].color = '#CCCCCC';
}
for (var j = 0; j < seriesMap[i].roleColumns.length; j++) {
columns.push(seriesMap[i].roleColumns[j]);
}
}
chart.setOption('series', series);
function showHideSeries () {
var sel = chart.getChart().getSelection();
// if selection length is 0, we deselected an element
if (sel.length > 0) {
// if row is undefined, we clicked on the legend
if (sel[0].row == null) {
var col = sel[0].column;
if (typeof(columns[col]) == 'number') {
var src = columns[col];
// hide the data series
columns[col] = {
label: data.getColumnLabel(src),
type: data.getColumnType(src),
sourceColumn: src,
calc: function () {
return null;
}
};
// grey out the legend entry
series[columnsMap[src]].color = '#CCCCCC';
}
else {
var src = columns[col].sourceColumn;
// show the data series
columns[col] = src;
series[columnsMap[src]].color = null;
}
var view = chart.getView() || {};
view.columns = columns;
chart.setView(view);
chart.draw();
}
}
}
google.visualization.events.addListener(chart, 'select', showHideSeries);
// create a view with the default columns
var view = {
columns: columns
};
chart.draw();
}
google.load('visualization', '1', {packages: ['corechart'], callback: drawChart});
相关文章:
- D3.js强制布局带有外部数据的图形空白
- 通过变量将数据添加到图形中
- 在同一html页面中使用不同数据复制图表/图形
- D3:“bubble.nodes”未将正确的图形数据应用于数据集
- 如何接受用户数据并在提交后以图形形式返回
- 如何在渲染之前操作 cytoscape.js 图形数据
- 如何在 d3.js 中的给定数据中设计图形
- 使用Plotly中的新数据更新图形的高性能方法
- 图形未呈现我的日期数据
- D3.js 和来自 CSV 文件图形宽度的数据可视化
- 如何在数据更新时更新可重用的 d3.js 图形
- 对高图表图形中的特定数据执行样式操作
- 如何从返回的Facebook图形API哈希中提取数据
- 使用新数据重绘 d3 图形
- 使用 Jqplot 绘制图形和来自 Sqlite 数据库的数据
- 点击bokeh按钮,从文件中加载图形数据
- 图形数据是在后端生成还是前端生成?
- 最好的方式,可视化的图形数据结构写在一个网页上的Java良好的图形
- 在Node.js/Express框架中,将数据传递给d3以呈现图形数据的正确方式是什么?
- 渲染新的图形数据添加新的图形,而不是替换现有的图形