Skip to content

Commit 9079bee

Browse files
HDS-5289 Add simple bar chart component with Showcase example
1 parent f943c9f commit 9079bee

File tree

9 files changed

+168
-1
lines changed

9 files changed

+168
-1
lines changed

packages/components/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@
200200
"./components/hds/button-set/index.js": "./dist/_app_/components/hds/button-set/index.js",
201201
"./components/hds/button/index.js": "./dist/_app_/components/hds/button/index.js",
202202
"./components/hds/card/container.js": "./dist/_app_/components/hds/card/container.js",
203+
"./components/hds/charts/bar/index.js": "./dist/_app_/components/hds/charts/bar/index.js",
204+
"./components/hds/charts/bar/options.js": "./dist/_app_/components/hds/charts/bar/options.js",
203205
"./components/hds/charts/donut/index.js": "./dist/_app_/components/hds/charts/donut/index.js",
204206
"./components/hds/charts/donut/options.js": "./dist/_app_/components/hds/charts/donut/options.js",
205207
"./components/hds/charts/meter/index.js": "./dist/_app_/components/hds/charts/meter/index.js",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{{!
2+
Copyright (c) HashiCorp, Inc.
3+
SPDX-License-Identifier: MPL-2.0
4+
}}
5+
6+
<div {{did-insert this.setupChart}} class="hds-charts-bar" ...attributes></div>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright (c) HashiCorp, Inc.
3+
* SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
import Component from '@glimmer/component';
7+
import { action } from '@ember/object';
8+
9+
import { SimpleBarChart } from '@carbon/charts';
10+
import options from './options.js';
11+
import '@carbon/charts/styles.css';
12+
13+
export interface HdsChartsBarSignature {
14+
Args: {
15+
title?: string;
16+
data: Array<{ group: string; date: string; value: number }>;
17+
};
18+
Blocks: {
19+
default: [];
20+
};
21+
Element: HTMLDivElement;
22+
}
23+
24+
export default class HdsChartsBar extends Component<HdsChartsBarSignature> {
25+
chart: SimpleBarChart | null = null;
26+
27+
@action
28+
setupChart(element: HTMLDivElement): void {
29+
const chartData = this.args.data;
30+
31+
// Merge the dynamic options into the default options
32+
const chartOptions = {
33+
...options,
34+
title: this.args.title || options.title,
35+
};
36+
37+
// Create the SimpleBarChart instance
38+
this.chart = new SimpleBarChart(element, {
39+
data: chartData,
40+
options: chartOptions,
41+
});
42+
}
43+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { ScaleTypes } from '@carbon/charts';
2+
3+
export default {
4+
title: '',
5+
toolbar: {
6+
enabled: false, // hide toolbar
7+
},
8+
axes: {
9+
left: {
10+
mapsTo: 'value',
11+
},
12+
bottom: {
13+
mapsTo: 'date',
14+
scaleType: ScaleTypes.LABELS,
15+
},
16+
},
17+
grid: {
18+
x: {
19+
numberOfTicks: 0,
20+
},
21+
},
22+
height: '400px',
23+
legend: {
24+
enabled: false, // hide legend (for single color bar chart)
25+
},
26+
};

packages/components/src/styles/components/charts.scss

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
*/
55

66
//
7-
// CHARTS > DONUT
7+
// CHARTS
88
//
99

10+
// > DONUT
11+
1012
.hds-charts-donut {
1113
// outline: 1px dotted red; // for testing - see boundaries of chart container
1214

@@ -21,3 +23,11 @@
2123
min-height: 1.3em;
2224
}
2325
}
26+
27+
// > BAR
28+
29+
.hds-charts-bar {
30+
.graph-frame {
31+
overflow: hidden; // cut off bottom line which overflows container
32+
}
33+
}

packages/components/src/template-registry.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import type HdsCardContainerComponent from './components/hds/card/container.ts';
6868

6969
import type HdsChartsDonut from './components/hds/charts/donut/index.ts';
7070
import type HdsChartsMeter from './components/hds/charts/meter/index.ts';
71+
import type HdsChartsBar from './components/hds/charts/bar/index.ts';
7172

7273
import type HdsCodeEditorComponent from './components/hds/code-editor/index.ts';
7374
import type HdsCodeEditorDescriptionComponent from './components/hds/code-editor/description.ts';
@@ -444,6 +445,7 @@ export default interface HdsComponentsRegistry {
444445
// Charts
445446
'Hds::Charts::Donut': typeof HdsChartsDonut;
446447
'Hds::Charts::Meter': typeof HdsChartsMeter;
448+
'Hds::Charts::Bar': typeof HdsChartsBar;
447449

448450
// Code Block
449451
'Hds::CodeBlock': typeof HdsCodeBlockComponent;

showcase/app/routes/page-components/charts/index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,67 @@ const METER_BILLABLE_RESOURCES_DATA = [
7070
},
7171
];
7272

73+
// Bar data
74+
75+
const BAR_MANAGED_RESOURCES_DATA = [
76+
{
77+
date: '2025-01',
78+
value: 9000,
79+
},
80+
{
81+
date: '2025-02',
82+
value: 12000,
83+
},
84+
{
85+
date: '2025-03',
86+
value: 10000,
87+
},
88+
{
89+
date: '2025-04',
90+
value: 13000,
91+
},
92+
{
93+
date: '2025-05',
94+
value: 14000,
95+
},
96+
{
97+
date: '2025-06',
98+
value: 15000,
99+
},
100+
{
101+
date: '2025-07',
102+
value: 15000,
103+
},
104+
{
105+
date: '2025-08',
106+
value: 18000,
107+
},
108+
{
109+
date: '2025-09',
110+
value: 21000,
111+
},
112+
{
113+
date: '2025-10',
114+
value: 22000,
115+
},
116+
{
117+
date: '2025-11',
118+
value: 24000,
119+
},
120+
{
121+
date: '2025-12',
122+
value: 27000,
123+
},
124+
];
125+
73126
export default class PageComponentsChartsRoute extends Route {
74127
async model() {
75128
return {
76129
DONUT_SERVICE_HEALTH_DATA,
77130
DONUT_CLUSTERS_DATA,
78131
METER_DATABASE_TRANSACTIONS_DATA,
79132
METER_BILLABLE_RESOURCES_DATA,
133+
BAR_MANAGED_RESOURCES_DATA,
80134
};
81135
}
82136
}

showcase/app/templates/page-components/charts/index.hbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,10 @@
5252
/>
5353
</Hds::Card::Container>
5454
</Hds::Layout::Grid>
55+
56+
<Shw::Text::H2>Carbon Charts Bar</Shw::Text::H2>
57+
58+
<Hds::Card::Container @hasBorder={{true}} {{style padding="1.5rem"}}>
59+
<Hds::Charts::Bar @title="Average billable managed resources" @data={{this.model.BAR_MANAGED_RESOURCES_DATA}} />
60+
</Hds::Card::Container>
5561
</section>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright (c) HashiCorp, Inc.
3+
* SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
import { module, test } from 'qunit';
7+
import { setupRenderingTest } from 'showcase/tests/helpers';
8+
import { render } from '@ember/test-helpers';
9+
import { hbs } from 'ember-cli-htmlbars';
10+
11+
module('Integration | Component | hds/charts/bar/index', function (hooks) {
12+
setupRenderingTest(hooks);
13+
14+
test('it should render the component with a CSS class that matches the component name', async function (assert) {
15+
await render(hbs`<Hds::Charts::Bar id="test-charts-bar" />`);
16+
assert.dom('#test-charts-bar').hasClass('hds-charts-bar');
17+
});
18+
});

0 commit comments

Comments
 (0)