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(scatter): Add Scatter to the Angular package and storybook #301

Merged
merged 3 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ npm install @carbon/charts --save
| Curved Line | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark:
| Pie | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark:
| Step | :white_check_mark: | :white_check_mark: | :white_check_mark: | :hourglass_flowing_sand:
stanislavgeorgiev marked this conversation as resolved.
Show resolved Hide resolved
| Scatter | :white_check_mark: | :hourglass_flowing_sand: | - | -
| Combo | :hourglass_flowing_sand: | - | - | -
| Area | Soon | - | - | - |

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@
"email": "iliadm@ca.ibm.com"
}
]
}
}
7 changes: 5 additions & 2 deletions packages/angular/src/charts.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DonutChartComponent } from "./donut-chart.component";
import { PieChartComponent } from "./pie-chart.component";
import { BarChartComponent } from "./bar-chart.component";
import { LineChartComponent } from "./line-chart.component";
import { ScatterChartComponent } from "./scatter-chart.component";

@NgModule({
imports: [
Expand All @@ -19,13 +20,15 @@ import { LineChartComponent } from "./line-chart.component";
DonutChartComponent,
PieChartComponent,
BarChartComponent,
LineChartComponent
LineChartComponent,
ScatterChartComponent
],
exports: [
DonutChartComponent,
PieChartComponent,
BarChartComponent,
LineChartComponent
LineChartComponent,
ScatterChartComponent
]
})

Expand Down
37 changes: 37 additions & 0 deletions packages/angular/src/scatter-chart.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
Component,
AfterViewInit
} from "@angular/core";

import { BaseChart } from "./base-chart.component";

import { ScatterChart } from "@carbon/charts";

/**
* Wrapper around `ScatterChart` in carbon charts library
*
* Most functions just call their equivalent from the chart library.
*/
@Component({
selector: "n-scatter-chart",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally never caught this ... we'll want to switch the prefix to either ibm- (to match carbon-angular) or c- (for carbon) ... the easiest way going forwards will be "ibm-scatter-chart, n-scatter-chart" (or whatever prefix) until we hit v1.

Doesn't stop this PR, but just something we should sort out sooner rather than later.

template: `
<div #nChart class='n-chart-container'>
</div>
`
})
export class ScatterChartComponent extends BaseChart implements AfterViewInit {
/**
* Runs after view init to create a chart, attach it to `chartRef` and draw it.
*/
ngAfterViewInit() {
this.chart = new ScatterChart(
this.chartRef.nativeElement,
{
data: this.data,
options: this.options
}
);

Object.assign(this, this.chart);
}
}
20 changes: 20 additions & 0 deletions packages/angular/stories/scatter.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { storiesOf } from "@storybook/angular";

import { colors } from "./helpers/commons";

import { ChartsModule } from "../src/charts.module";
import { ScatterComponent } from "./scatter/scatter.component";

import { scatterData, lineOptions } from "./../../core/demo/demo-data/line";

const scatterStories = storiesOf("Scatter", module);
scatterStories.add("Basic", () => ({
component: ScatterComponent,
moduleMetadata: {
imports: [ChartsModule]
},
props: {
scatterData: scatterData,
scatterOptions: lineOptions
}
}));
11 changes: 11 additions & 0 deletions packages/angular/stories/scatter/scatter.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div style="margin: 30px;">
<n-scatter-chart
class="n-chart"
[data]="scatterData"
[options]="scatterOptions"
style="height: 500px;"
#scatterChart>
</n-scatter-chart>

<button class="btn--primary" (click)="changeDemoData()">Change Data</button>
</div>
31 changes: 31 additions & 0 deletions packages/angular/stories/scatter/scatter.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Component, ViewChild } from "@angular/core";

import { colors, randomizeValue } from "../helpers/commons";
import { Input } from "@angular/core";

@Component({
selector: "app-scatter",
templateUrl: "./scatter.component.html"
})
export class ScatterComponent {
@ViewChild("scatterChart") scatterChart;

@Input() scatterOptions = {};
@Input() scatterData = {};

changeDemoData() {
const oldData = this.scatterChart.data;

// Randomize old data values
const newData = Object.assign({}, oldData);
newData.datasets = oldData.datasets.map(dataset => {
const datasetNewData = dataset.data.map(dataPoint => randomizeValue(dataPoint, false));

const newDataset = Object.assign({}, dataset, { data: datasetNewData });

return newDataset;
});

this.scatterData = newData;
}
}