Skip to content

Commit

Permalink
feat(cloudwatch): legend positions in GraphWidgets
Browse files Browse the repository at this point in the history
Allows specifying a position for the legend for a GraphWidget. If omitted,
defaults to the bottom of the graph.

fixes #3625
  • Loading branch information
njlynch committed May 6, 2020
1 parent 207a8ec commit ada0de1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/README.md
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
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
@@ -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();
},
};

0 comments on commit ada0de1

Please sign in to comment.