简体   繁体   English

如何在 angular-google-charts 中将“工具提示”添加到时间轴图表?

[英]How to add 'tooltip' to Timeline chart in angular-google-charts?

I'm trying to add tooltip to Timeline chart using angular-google-charts library in Angular 7 application.我正在尝试使用 Angular 7 应用程序中的 angular-google-charts 库将工具提示添加到时间线图表。 When I add a fifth column at index = 2, angular-google-charts throws as error saying "Expecting 4 columns not 5"当我在 index = 2 处添加第五列时,angular-google-charts 抛出错误,提示“期望 4 列不是 5”

When I remove the tooltip column providing only 4 columns (Group, Row, Start, End) the Timeline visualisation works fine.当我删除仅提供 4 列(组、行、开始、结束)的工具提示列时,时间轴可视化效果很好。 However as soon as I add the Tooltip column it throws error saying expecting 4 columns but received 5.但是,一旦我添加 Tooltip 列,它就会抛出错误,说期望 4 列但收到 5。

component.ts组件.ts

type: string = 'Timeline'
chartData: Array<Array<any>> = [
  ['Group1', 'Row1', 'Tooltip1', new Date(2019, 01, 01), new Date(2019, 02, 01)],
  ['Group2', 'Row2', 'Tooltip2', new Date(2019, 03, 01), new Date(2019, 04, 01)]
]
colNames: Array<any> = ["Group", "Row", "Tooltip", "Start", "End"] 
colRoles: Array<any> = [
  { role: 'tooltip', type: 'string', index: 2, p: {html: true} },
]
options: {} = { tooltip: {isHtml: true} }

component.html组件.html

<google-chart 
    [type]="type"
    [data]="chartData" 
    [options]="options" 
    [columnNames]="colNames"
    [roles]="colRoles">
</google-chart>

app.module.ts app.module.ts

import { GoogleChartsModule } from 'angular-google-charts';
@NgModule({
...
imports: [
  GoogleChartsModule
],

I expect the tooltip to show the additional text I am putting in the column index 2. Please help me sort this out.我希望工具提示显示我在列索引 2 中添加的附加文本。请帮我解决这个问题。

when not using angular, the roles are included with the column names.不使用 angular 时,角色包含在列名中。
I've never seen the roles added separately.我从未见过单独添加的角色。

try adding the role within the column names, as follows...尝试在列名中添加角色,如下...

type: string = 'Timeline'
chartData: Array<Array<any>> = [
  ['Group1', 'Row1', 'Tooltip1', new Date(2019, 01, 01), new Date(2019, 02, 01)],
  ['Group2', 'Row2', 'Tooltip2', new Date(2019, 03, 01), new Date(2019, 04, 01)]
]
colNames: Array<any> = ["Group", "Row", { role: 'tooltip', type: 'string', p: {html: true} }, "Start", "End"] 
options: {} = { tooltip: {isHtml: true} }

and exclude the roles here...并排除这里的角色......

<google-chart 
    [type]="type"
    [data]="chartData" 
    [options]="options" 
    [columnNames]="colNames"
</google-chart>

This answer is based off @WhiteHat's answer.这个答案基于@WhiteHat 的答案。 I added two tooltips instead of one and changed the variables' names to make them more explicit.我添加了两个工具提示而不是一个,并更改了变量的名称以使其更加明确。

In the newer version of the angular-google-charts library, an import is needed to define the type:在较新版本的 angular-google-charts 库中,需要导入来定义类型:

import { ChartType } from 'angular-google-charts';

Then we have to define the type and the data.然后我们必须定义类型和数据。 The number of elements in the chartData array must match the number of elements in the colNames Array or it will cause an error. chartData 数组中的元素个数必须与chartData数组中的元素个数匹配, colNames会导致错误。

type3 =  ChartType.BarChart;

chartData3: Array<Array<any>> = [
  ['first bar', 10,  'Tooltip1', 100, 'Tooltip10'],
  ['second bar', 20,  'Tooltip2', 200, 'Tooltip20']
]

colNames3: Array<any> = [
    "Group", 
    "First number",
    { role: 'tooltip', type: 'string', p: {html: true}}, 
    "Second number" ,  
    { role: 'tooltip', type: 'string', p: {html: true} }] 

options3: {} = { isStacked: true }

Then in the html:然后在 html 中:

<google-chart 
    [type]="type3"
    [data]="chartData3" 
    [options]="options3" 
    [columns]="colNames3">
</google-chart>

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

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