Skip to content

Commit

Permalink
feat(scatter): Add Scatter to the Angular package and storybook
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislavgeorgiev committed Jul 9, 2019
1 parent 1863e14 commit 2aac850
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ npm install @carbon/charts --save
| Line | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark:
| 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:
| Scatter | :white_check_mark: | :white_check_mark: | :white_check_mark: | :hourglass_flowing_sand:
| Step | :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",
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);
}
}
31 changes: 31 additions & 0 deletions packages/angular/stories/scatter.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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 pieStories = storiesOf("Scatter", module);
pieStories.add("Basic", () => ({
component: ScatterComponent,
moduleMetadata: {
imports: [ChartsModule]
},
props: {
scatterData: scatterData,
scatterOptions: lineOptions
}
}));

pieStories.add("Accessible", () => ({
component: ScatterComponent,
moduleMetadata: {
imports: [ChartsModule]
},
props: {
scatterData: scatterData,
scatterOptions: Object.assign({}, lineOptions, {accessibility: true})
}
}));
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;
}
}

0 comments on commit 2aac850

Please sign in to comment.