样式化Google图表表

car*_*sgg 14 javascript google-visualization

我正在尝试将CS​​S样式添加到Google Charts表中.我试过这个:

https://developers.google.com/chart/interactive/docs/gallery/table#customproperties

在第一个单元格(迈克),但它没有工作.我在options变量中将allowHtml设置为true.如何更改单个单元格的背景,文本颜色等?谢谢.

<script type="text/javascript">
        google.load('visualization', '1', {packages:['table']});
        google.setOnLoadCallback(drawTable);
        function drawTable() {
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Employee Name');
            data.addColumn('date', 'Start Date');
            data.addRows(3);
            data.setCell(0, 0, 'Mike', {style: 'background-color:red;'});
            data.setCell(0, 1, new Date(2008, 1, 28));
            data.setCell(1, 0, 'Bob');
            data.setCell(1, 1, new Date(2007, 5, 1));
            data.setCell(2, 0, 'Alice');
            data.setCell(2, 1, new Date(2006, 7, 16));

            var options = {
                allowHtml: true
            };

            // Create a formatter.
            // This example uses object literal notation to define the options.
            var formatter = new google.visualization.DateFormat({formatType: 'long'});

            // Reformat our data.
            formatter.format(data, 1);

            // Draw our data
            var table = new google.visualization.Table(document.getElementById('dateformat_div'));
            table.draw(data, options);
        }
    </script>
Run Code Online (Sandbox Code Playgroud)

J E*_* II 13

如何更改单个单元格的背景,文本颜色等?谢谢.

Per @ Bondye的评论,答案可以在开发者指南中找到.

https://developers.google.com/chart/interactive/docs/examples#custom_table_example

定义符合条件的样式规则:

<style>
.orange-background {
   background-color: orange;
  }

 .orchid-background {
  background-color: orchid;
 }

.beige-background {
 background-color: beige;
  }
</style>
Run Code Online (Sandbox Code Playgroud)

绘制时将它们应用于表格.

var cssClassNames = {
'headerRow': 'italic-darkblue-font large-font bold-font',
'tableRow': '',
'oddTableRow': 'beige-background',
'selectedTableRow': 'orange-background large-font',
'hoverTableRow': '',
'headerCell': 'gold-border',
'tableCell': '',
'rowNumberCell': 'underline-blue-font'};

var options = {'showRowNumber': true, 'allowHtml': true, 'cssClassNames': cssClassNames};

var data = new google.visualization.DataTable();

//... add data here ...

var table = new google.visualization.Table(container);
table.draw(data, options);
Run Code Online (Sandbox Code Playgroud)

  • 仅包含链接(尤其是外部资源)的答案不被认为是[so]的好答案.链接腐烂的可能性意味着如果该URL死亡(或更改),您的帖子将不包含任何有用的信息. (3认同)