首页
/ GoogleVisualr 技术文档

GoogleVisualr 技术文档

2024-12-20 02:14:09作者:尤辰城Agatha

1. 安装指南

在 Rails 3/4 项目中,首先将以下代码添加到你的 Gemfile 文件中:

gem "google_visualr", ">= 2.5"

然后执行 bundle install 命令安装 gem。

2. 项目的使用说明

在 Rails 布局文件(通常是 app/views/layouts/application.html.erb)中,引入 Google Ajax API:

<script src='https://www.google.com/jsapi'></script>

在控制器中创建一个 GoogleVisualr::DataTable 对象,并填充数据:

data_table = GoogleVisualr::DataTable.new

# 添加列标题
data_table.new_column('string', 'Year')
data_table.new_column('number', 'Sales')
data_table.new_column('number', 'Expenses')

# 添加数据行
data_table.add_rows([
  ['2004', 1000, 400],
  ['2005', 1170, 460],
  ['2006', 660, 1120],
  ['2007', 1030, 540]
])

创建图表实例,配置图表选项:

option = { width: 400, height: 240, title: '公司业绩' }
@chart = GoogleVisualr::Interactive::AreaChart.new(data_table, option)

在视图中渲染图表:

<div id='chart'></div>
<%= render_chart(@chart, 'chart') %>

3. 项目 API 使用文档

GoogleVisualr::<ChartName> 的初始化器接收两个参数:data_tableoptions

data_table

data_tableGoogleVisualr::DataTable 的一个实例,包含数据表的列标题和数据行。

options

options 是一个配置选项的哈希表,例如宽度、高度、颜色等。可用的配置选项与 Google API 文档中指定的完全相同。

例如:

同时,您还可以在配置选项中指定 versionlanguagematerial

version

加载的 Google Charts 库的默认版本是 1.0。但是,某些图表(例如 Gantt 图表)需要最新版本的 Google Charts 库,因此您需要在配置选项中指定 version1.1

language

所有 Google 图表的默认语言是 en。您可以在配置选项中指定 language 来覆盖此默认设置。

material

Google 为某些图表启用了 Material 设计,您可以通过在配置选项中指定 materialtrue 来使用它。

4. 项目安装方式

请参考“安装指南”部分,将 gem 添加到 Rails 项目的 Gemfile 中,然后执行 bundle install

登录后查看全文
热门项目推荐