简体   繁体   English

Google图表表格的动态样式

[英]Dynamic Styling of Google Charts Table

I am looking dynamically apply styling to specific rows in a Google Charts Table. 我正在寻找将样式动态应用于Google Charts Table中特定行的样式。 Specifically, I am looking to bold the text of a row if it contains a specific string. 具体来说,如果行中包含特定字符串,我希望将其粗体显示。 For example, if the text ' total ' appears in a row, I would want to have all text of this row be bold . 例如,如果文本“ 合计 ”出现在一行中,则我希望该行的所有文本都以粗体显示

This table is being populated with results from a Keen IO query, so I cannot be sure of where in the table the text of interest may occur. 该表中填充了Keen IO查询的结果,因此我不确定该表中可能出现感兴趣的文本。

I was looking the documation found at: https://developers.google.com/chart/interactive/docs/gallery/table#customproperties 我正在寻找在以下位置找到的文档: https : //developers.google.com/chart/interactive/docs/gallery/table#customproperties
This page suggested applying CSS styling and an example of this can be seen at: https://developers.google.com/chart/interactive/docs/examples 该页面建议应用CSS样式,其示例可在以下网址查看: https : //developers.google.com/chart/interactive/docs/examples

However, this example was applying the CSS during population of the data in the table. 但是,此示例在表中的数据填充期间应用了CSS。 My data is the result of a dynamic query, so I am not going to be able to use this approach. 我的数据是动态查询的结果,因此我将无法使用这种方法。 I may need to inject inline CSS at a later stage of the process. 在此过程的后期,我可能需要插入内联CSS。

Example

I am going demonostrate the scenario with an example. 我将通过示例演示该场景。 Suppose I have a query to Keen IO fetched the versions and counts of various 'devices'.This data is in JSON form. 假设我有一个查询Keen IO,获取了各种``设备''的版本和计数,这些数据是JSON格式的。 When the data is fed into the Google Chart tool, the following table is outputted.. 将数据输入Google图表工具后,将输出下表。

device  version count  
item1   1.0     4
item1   1.1     3  
item1   total   7  
item2   5.4     8  
item2   5.5     2  
item2   6.0     14  
item2   total   24  

I also have the ability to specify Chart Options, via the Keen API, that are then passed through to the underling Google Chart engine. 我还可以通过Keen API指定图表选项,然后将这些选项传递给基础Google图表引擎。 These options are also in JSON form. 这些选项也是JSON形式。 For example 例如

{
  "chartOptions": {
    "page": "enable",
    "pageSize": 25
  }
}

Would cause the resulting table to include paging with a page size of 25. 将导致结果表包括页面大小为25的分页。

Additional References 其他参考

Keen Visualizaion Documentation: https://github.com/keen/keen-js/blob/master/docs/visualization.md 敏锐的可视化文档: https : //github.com/keen/keen-js/blob/master/docs/visualization.md


Similar, but different questions: 类似但不同的问题:
Applying CSS to Google Visualization Table 将CSS应用于Google可视化表
Styling Google Charts Table 设置Google图表表格的样式


Typically you will define the Keen query, then create the visualization object, then lastly run both the query and visualization. 通常,您将定义Keen查询,然后创建可视化对象,最后运行查询和可视化。 Here's an example of how you can manipulate the data being charted: 这是如何处理图表数据的示例:

 Keen.ready(function(){ // Create a new Query instance var query = new Keen.Query("count", { event_collection: "user_action", group_by: "user.name", timeframe: { start: "2013-12-01", end: "2014-12-01" } }); // Create a new Dataviz instance var table = new Keen.Dataviz() .chartType('table') .el(document.getElementById('table')) .chartOptions({ width: '100%' }); // Run the query and the result client.run(query, function(err, res){ table .parseRequest(this) .dateFormat(function(googleDataTable){ // .setProperty(rowIndex, colIndex, 'className', 'your-class-here') googleDataTable.setProperty(3, 0, 'className', 'custom custom-left'); googleDataTable.setProperty(3, 1, 'className', 'custom custom-right'); return googleDataTable; }) .render(); }); 

The particular line of JS code that sets a line in the table to a different format is: .setProperty(rowIndex, colIndex, 'className', 'your-class-here') 将表中的行设置为其他格式的JS代码的特定行是:.setProperty(rowIndex,colIndex,'className','your-class-here')

I included a link to a working JavaScript fiddle that runs this code and shows the table being dynamically formatted to highlight a row: http://jsfiddle.net/keen/6qnpuwLx/ 我提供了一个运行该代码的JavaScript小提琴的链接,该小提琴运行该代码并显示动态格式化的表格以突出显示一行: http : //jsfiddle.net/keen/6qnpuwLx/

If you're interested in adding logic, just include your if statement in the client.run() function. 如果您有兴趣添加逻辑,只需在client.run()函数中包含if语句即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM