Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloudwatch): legend positions in GraphWidgets #7809

Merged
merged 2 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ dashboard.addWidgets(new GraphWidget({
}));
```

The graph legend can be adjusted from the default position at bottom of the widget.

```ts
dashboard.addWidgets(new GraphWidget({
// ...
// ...

legendPosition: LegendPosition.RIGHT,
}));
```

### Alarm widget

An alarm widget shows the graph and the alarm line of a single alarm:
Expand Down
28 changes: 28 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/lib/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ export interface GraphWidgetProps extends MetricWidgetProps {
* @default - None
*/
readonly rightYAxis?: YAxisProps;

/**
* Position of the legend
*
* @default - bottom
*/
readonly legendPosition?: LegendPosition;
}

/**
Expand Down Expand Up @@ -210,6 +217,7 @@ export class GraphWidget extends ConcreteWidget {
left: this.props.leftYAxis !== undefined ? this.props.leftYAxis : undefined,
right: this.props.rightYAxis !== undefined ? this.props.rightYAxis : undefined,
},
legend: this.props.legendPosition !== undefined ? { position: this.props.legendPosition } : undefined,
},
}];
}
Expand Down Expand Up @@ -349,6 +357,26 @@ export class Color {
public static readonly RED = '#d62728';
}

/**
* The position of the legend on a GraphWidget.
*/
export enum LegendPosition {
/**
* Legend appears below the graph (default).
*/
BOTTOM = 'bottom',

/**
* Add shading above the annotation
*/
RIGHT = 'right',

/**
* Add shading below the annotation
*/
HIDDEN = 'hidden'
}

function mapAnnotation(yAxis: string): ((x: HorizontalAnnotation) => any) {
return (a: HorizontalAnnotation) => {
return { ...a, yAxis };
Expand Down
31 changes: 30 additions & 1 deletion packages/@aws-cdk/aws-cloudwatch/test/test.graphs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Stack } from '@aws-cdk/core';
import { Test } from 'nodeunit';
import { Alarm, AlarmWidget, Color, GraphWidget, LogQueryWidget, Metric, Shading, SingleValueWidget } from '../lib';
import { Alarm, AlarmWidget, Color, GraphWidget, LegendPosition, LogQueryWidget, Metric, Shading, SingleValueWidget } from '../lib';

export = {
'add stacked property to graphs'(test: Test) {
Expand Down Expand Up @@ -393,4 +393,33 @@ export = {
test.deepEqual(stack.resolve(widget.toJson())[0].properties.annotations.horizontal[0], { yAxis: 'left', value: 100, color: '#d62728' });
test.done();
},

'legend position is respected in constructor'(test: Test) {
// WHEN
const stack = new Stack();
const widget = new GraphWidget({
left: [new Metric({ namespace: 'CDK', metricName: 'Test' }) ],
legendPosition: LegendPosition.RIGHT,
});

// THEN
test.deepEqual(stack.resolve(widget.toJson()), [{
type: 'metric',
width: 6,
height: 6,
properties: {
view: 'timeSeries',
region: { Ref: 'AWS::Region' },
metrics: [
['CDK', 'Test'],
],
yAxis: {},
legend: {
position: 'right',
},
},
}]);

test.done();
},
};