# GraphWidget

A dashboard widget that displays metrics.

Extends: [ConcreteWidget](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudwatch.ConcreteWidget.html) › GraphWidget
Implements: [IWidget](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudwatch.IWidget.html)

- Source: [`aws-cloudwatch/lib/graph.ts`](https://github.com/aws/aws-cdk/blob/v2.277.0/packages/aws-cdk-lib/aws-cloudwatch/lib/graph.ts#L449)

## Example

TypeScript:

```ts
declare const dashboard: cloudwatch.Dashboard;

const hiddenSearch = new cloudwatch.SearchExpression({
  expression: "SEARCH('{AWS/EC2,InstanceId} MetricName=\"CPUUtilization\"', 'Average', 300)",
  label: 'EC2 CPU Utilization',
  visible: false,
});

dashboard.addWidgets(new cloudwatch.GraphWidget({
  left: [hiddenSearch],
}));
```

Python:

```python
# dashboard: cloudwatch.Dashboard

hidden_search = cloudwatch.SearchExpression(
    expression="SEARCH('{AWS/EC2,InstanceId} MetricName=\"CPUUtilization\"', 'Average', 300)",
    label="EC2 CPU Utilization",
    visible=False
)

dashboard.add_widgets(cloudwatch.GraphWidget(
    left=[hidden_search]
))
```

Java:

```java
Dashboard dashboard;

SearchExpression hiddenSearch = SearchExpression.Builder.create()
        .expression("SEARCH('{AWS/EC2,InstanceId} MetricName=\"CPUUtilization\"', 'Average', 300)")
        .label("EC2 CPU Utilization")
        .visible(false)
        .build();

dashboard.addWidgets(GraphWidget.Builder.create()
        .left(List.of(hiddenSearch))
        .build());
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
Dashboard dashboard;

var hiddenSearch = new SearchExpression(new SearchExpressionProps {
    Expression = "SEARCH('{AWS/EC2,InstanceId} MetricName=\"CPUUtilization\"', 'Average', 300)",
    Label = "EC2 CPU Utilization",
    Visible = false
});

dashboard.AddWidgets(new GraphWidget(new GraphWidgetProps {
    Left = new [] { hiddenSearch }
}));
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var dashboard Dashboard

hiddenSearch := cloudwatch.NewSearchExpression(&SearchExpressionProps{
	Expression: jsii.String("SEARCH('{AWS/EC2,InstanceId} MetricName=\"CPUUtilization\"', 'Average', 300)"),
	Label: jsii.String("EC2 CPU Utilization"),
	Visible: jsii.Boolean(false),
})

dashboard.AddWidgets(cloudwatch.NewGraphWidget(&GraphWidgetProps{
	Left: []IMetric{
		hiddenSearch,
	},
}))
```

## Initializer

TypeScript:

```ts
new GraphWidget(props: GraphWidgetProps)
```

Python:

```python
GraphWidget(props: GraphWidgetProps)
```

Java:

```java
new GraphWidget(GraphWidgetProps props)
```

C# / .NET:

```csharp
new GraphWidget(GraphWidgetProps props)
```

Golang:

```go
NewGraphWidget(Props GraphWidgetProps)
```

## Properties

- `height`: number — The amount of vertical grid units the widget will take up.

- `warnings`: string[] — Any warnings that are produced as a result of putting together this widget.

- `warningsV2`: { [key: string]: string } — Any warnings that are produced as a result of putting together this widget.

- `width`: number — The amount of horizontal grid units the widget will take up.

## Methods

- `addLeftMetric(metric: IMetric): void` — Add another metric to the left Y axis of the GraphWidget.

  **Parameters**

  - `metric` — the metric to add.

- `addRightMetric(metric: IMetric): void` — Add another metric to the right Y axis of the GraphWidget.

  **Parameters**

  - `metric` — the metric to add.

- `position(x: number, y: number): void` — Place the widget at a given position.

- `toJson(): any[]` — Return the widget JSON for use in the dashboard.
