如何隐藏谷歌图表中的列

发布于 2024-12-25 19:21:32 字数 222 浏览 4 评论 0原文

我有一个 Google 图表表格,其中显示几列和几行。举例来说,我们有 4 列(分别为 A、B、C、D)。我怎样才能仍然加载 C 列的值,但只是隐藏该列以使其不显示?

提前致谢!

I've got a Google Charts Table that displays a couple of columns and rows. Say for instance we have 4 columns(A,B,C,D respectively). How would I be able to still load column C's values, but just hide the column so that it's not getting displayed?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜扑 2025-01-01 19:21:32

您必须创建一个中间的 DataView 对象,在其中过滤原始数据,而不是绘制 DataTable。像这样的事情:

//dataResponse is your Datatable with A,B,C,D columns        
var view = new google.visualization.DataView(dataResponse);
view.setColumns([0,1,3]); //here you set the columns you want to display

//Visualization Go draw!
visualizationPlot.draw(view, options);

hideColumns 方法可能比 setColumns 更好,您自己选择!

干杯

Instead of drawing the DataTable, you have to create an in-between DataView object where you filter the original data. Something like this:

//dataResponse is your Datatable with A,B,C,D columns        
var view = new google.visualization.DataView(dataResponse);
view.setColumns([0,1,3]); //here you set the columns you want to display

//Visualization Go draw!
visualizationPlot.draw(view, options);

The hideColumns method is maybe better instead of setColumns, choose yourself!

Cheers

贱人配狗天长地久 2025-01-01 19:21:32

这是使用 ChartWrapper 而不是图表的替代方案。

var opts = {
    "containerId": "chart_div",
    "dataTable": datatable,
    "chartType": "Table",
    "options": {"title": "Now you see the columns, now you don't!"}
}
var chartwrapper = new google.visualization.ChartWrapper(opts);

// set the columns to show
chartwrapper.setView({'columns': [0, 1, 4, 5]});    
chartwrapper.draw();

如果您使用 ChartWrapper,您可以轻松添加一个函数来更改隐藏列或显示所有列。要显示所有列,请将 null 作为 'columns' 的值传递。例如,

$('button').click(function() {
    // use your preferred method to get an array of column indexes or null
    var columns = eval($(this).val());

    chartwrapper.setView({'columns': columns});    
    chartwrapper.draw();
});

在您的 html 中

<button value="[0, 1, 3]" >Hide columns</button>
<button value="null">Expand All</button>

使用 jQuery (注意:使用 eval 是为了简洁。使用适合您的代码的内容。这不是重点。)

Here's an alternative using a ChartWrapper instead of a chart.

var opts = {
    "containerId": "chart_div",
    "dataTable": datatable,
    "chartType": "Table",
    "options": {"title": "Now you see the columns, now you don't!"}
}
var chartwrapper = new google.visualization.ChartWrapper(opts);

// set the columns to show
chartwrapper.setView({'columns': [0, 1, 4, 5]});    
chartwrapper.draw();

If you use a ChartWrapper, you can easily add a function to change the hidden columns, or show all the columns. To show all the columns, pass null as the value of 'columns'. For instance, using jQuery,

$('button').click(function() {
    // use your preferred method to get an array of column indexes or null
    var columns = eval($(this).val());

    chartwrapper.setView({'columns': columns});    
    chartwrapper.draw();
});

In your html,

<button value="[0, 1, 3]" >Hide columns</button>
<button value="null">Expand All</button>

(Note: eval used for conciseness. Use what suits your code. It's beside the point.)

新一帅帅 2025-01-01 19:21:32
var view = new google.visualization.DataView(dataTable); //datatable contains col and rows
view.setColumns([0,1,3,4]); //only show these column
chart.draw(view, options); //pass the view to draw chat
var view = new google.visualization.DataView(dataTable); //datatable contains col and rows
view.setColumns([0,1,3,4]); //only show these column
chart.draw(view, options); //pass the view to draw chat
烟燃烟灭 2025-01-01 19:21:32

您可以使用 CSS 来做到这一点。

“#table_div”是我的表格所在的 div。我使用它是因为同一页面上有多个表格。

#table_div .google-visualization-table table.google-visualization-table-table 
   td:nth-child(1),th:nth-child(1){
   display:none;
}

我还在图表的行上有一个事件处理程序,并且仍然可以从隐藏列中获取数据。

You can do this with CSS.

"#table_div" is the div my table is wrapped in. I use this because there a multiple tables on the same page.

#table_div .google-visualization-table table.google-visualization-table-table 
   td:nth-child(1),th:nth-child(1){
   display:none;
}

I also have an event handler on the chart's rows, and can still pick up the data from the hidden column.

只想待在家 2025-01-01 19:21:32

如果您想在控件包装器中使用特定的列值,但不想在 Google 图表中显示该列,请执行以下操作。

1) 将所有列添加到您的谷歌图表数据表中。
2)将以下内容添加到图表包装器的选项中。

   // Set chart options
    optionsUser = {
        "series": {
            0: {
                color: "white",
                visibleInLegend: false
            }
        }
    };

3) 在上面的代码系列中,0 表示折线图中的第一行。因此它将颜色设置为白色并隐藏图例中的列名称。

4)这种方式不是隐藏列的正确方式,建议使用DataView。每当您想要使用数据表中的数据向图表添加控件但又不想在图表中显示该列时,就是这样。

If you want to use particular column value in control wrapper but don't want to show that column in Google Charts then do following things.

1) Add all column to your google charts data table.
2) Add following things to options of your chartWrapper.

   // Set chart options
    optionsUser = {
        "series": {
            0: {
                color: "white",
                visibleInLegend: false
            }
        }
    };

3) In above code series, 0 means the first line in your line chart. So It will set the color to white and also hide column name in Legends.

4) This way is not the proper way to hide columns, Using DataView is recommended. Whenever you want to use data in the data table for adding controls to your chart but don't want to show that column in the chart this is the way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文