Skip to content

Commit

Permalink
FEATURE: CL-12712 New area chart
Browse files Browse the repository at this point in the history
  • Loading branch information
son do committed Apr 3, 2018
1 parent 6e6c15e commit fb3cbdd
Show file tree
Hide file tree
Showing 21 changed files with 640 additions and 46 deletions.
114 changes: 114 additions & 0 deletions examples/src/components/AreaChartExample.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import React, { Component } from 'react';
import { AreaChart } from '@gooddata/react-components';

import '@gooddata/react-components/styles/css/main.css';
import CustomLoading from './utils/CustomLoading';
import CustomError from './utils/CustomError';

import {
projectId,
monthDateIdentifier,
franchiseFeesIdentifier,
franchiseFeesAdRoyaltyIdentifier,
franchiseFeesInitialFranchiseFeeIdentifier,
franchiseFeesIdentifierOngoingRoyalty
} from '../utils/fixtures';

export class AreaChartExample extends Component {
onLoadingChanged(...params) {
// eslint-disable-next-line no-console
return console.log('AreaChartExample onLoadingChanged', ...params);
}

onError(...params) {
// eslint-disable-next-line no-console
return console.log('AreaChartExample onError', ...params);
}

render() {
const measures = [
{
measure: {
localIdentifier: 'franchiseFeesIdentifier',
definition: {
measureDefinition: {
item: {
identifier: franchiseFeesIdentifier
}
}
},
format: '#,##0'
}
},
{
measure: {
localIdentifier: 'franchiseFeesAdRoyaltyIdentifier',
definition: {
measureDefinition: {
item: {
identifier: franchiseFeesAdRoyaltyIdentifier
}
}
},
format: '#,##0'
}
},
{
measure: {
localIdentifier: 'franchiseFeesInitialFranchiseFeeIdentifier',
definition: {
measureDefinition: {
item: {
identifier: franchiseFeesInitialFranchiseFeeIdentifier
}
}
},
format: '#,##0'
}
},
{
measure: {
localIdentifier: 'franchiseFeesIdentifierOngoingRoyalty',
definition: {
measureDefinition: {
item: {
identifier: franchiseFeesIdentifierOngoingRoyalty
}
}
},
format: '#,##0'
}
}
];

const attributes = [
{
visualizationAttribute: {
displayForm: {
identifier: monthDateIdentifier
},
localIdentifier: 'month'
}
}
];

return (
<div style={{ height: 300 }} className="s-area-chart">
<AreaChart
projectId={projectId}
measures={measures}
viewBy={attributes}
onLoadingChanged={this.onLoadingChanged}
onError={this.onError}
LoadingComponent={CustomLoading}
ErrorComponent={CustomError}
config={{
stacking: false
}}
/>
</div>
);
}
}

export default AreaChartExample;
114 changes: 114 additions & 0 deletions examples/src/components/StackedAreaChartExample.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import React, { Component } from 'react';
import { AreaChart } from '@gooddata/react-components';

import '@gooddata/react-components/styles/css/main.css';
import CustomLoading from './utils/CustomLoading';
import CustomError from './utils/CustomError';

import {
projectId,
monthDateIdentifier,
franchiseFeesIdentifier,
franchiseFeesAdRoyaltyIdentifier,
franchiseFeesInitialFranchiseFeeIdentifier,
franchiseFeesIdentifierOngoingRoyalty
} from '../utils/fixtures';

export class StackedAreaChartExample extends Component {
onLoadingChanged(...params) {
// eslint-disable-next-line no-console
return console.log('StackedAreaChartExample onLoadingChanged', ...params);
}

onError(...params) {
// eslint-disable-next-line no-console
return console.log('StackedAreaChartExample onError', ...params);
}

render() {
const measures = [
{
measure: {
localIdentifier: 'franchiseFeesIdentifier',
definition: {
measureDefinition: {
item: {
identifier: franchiseFeesIdentifier
}
}
},
format: '#,##0'
}
},
{
measure: {
localIdentifier: 'franchiseFeesAdRoyaltyIdentifier',
definition: {
measureDefinition: {
item: {
identifier: franchiseFeesAdRoyaltyIdentifier
}
}
},
format: '#,##0'
}
},
{
measure: {
localIdentifier: 'franchiseFeesInitialFranchiseFeeIdentifier',
definition: {
measureDefinition: {
item: {
identifier: franchiseFeesInitialFranchiseFeeIdentifier
}
}
},
format: '#,##0'
}
},
{
measure: {
localIdentifier: 'franchiseFeesIdentifierOngoingRoyalty',
definition: {
measureDefinition: {
item: {
identifier: franchiseFeesIdentifierOngoingRoyalty
}
}
},
format: '#,##0'
}
}
];

const attributes = [
{
visualizationAttribute: {
displayForm: {
identifier: monthDateIdentifier
},
localIdentifier: 'month'
}
}
];

return (
<div style={{ height: 300 }} className="s-area-chart">
<AreaChart
projectId={projectId}
measures={measures}
viewBy={attributes}
onLoadingChanged={this.onLoadingChanged}
onError={this.onError}
LoadingComponent={CustomLoading}
ErrorComponent={CustomError}
config={{
stacking: true
}}
/>
</div>
);
}
}

export default StackedAreaChartExample;
10 changes: 10 additions & 0 deletions examples/src/routes/BasicComponents.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import ExampleWithSource from '../components/utils/ExampleWithSource';
import BarChartExample from '../components/BarChartExample';
import ColumnChartExample from '../components/ColumnChartExample';
import LineChartExample from '../components/LineChartExample';
import AreaChartExample from '../components/AreaChartExample';
import StackedAreaChartExample from '../components/StackedAreaChartExample';
import PieChartExample from '../components/PieChartExample';
import TableExample from '../components/TableExample';

import BarChartExampleSRC from '!raw-loader!../components/BarChartExample'; // eslint-disable-line import/no-webpack-loader-syntax, import/no-unresolved, import/extensions, import/first
import ColumnChartExampleSRC from '!raw-loader!../components/ColumnChartExample'; // eslint-disable-line import/no-webpack-loader-syntax, import/no-unresolved, import/extensions, import/first
import LineChartExampleSRC from '!raw-loader!../components/LineChartExample'; // eslint-disable-line import/no-webpack-loader-syntax, import/no-unresolved, import/extensions, import/first
import AreaChartExampleSRC from '!raw-loader!../components/AreaChartExample'; // eslint-disable-line import/no-webpack-loader-syntax, import/no-unresolved, import/extensions, import/first
import StackedAreaChartExampleSRC from '!raw-loader!../components/StackedAreaChartExample'; // eslint-disable-line import/no-webpack-loader-syntax, import/no-unresolved, import/extensions, import/first
import PieChartExampleSRC from '!raw-loader!../components/PieChartExample'; // eslint-disable-line import/no-webpack-loader-syntax, import/no-unresolved, import/extensions, import/first
import TableExampleSRC from '!raw-loader!../components/TableExample'; // eslint-disable-line import/no-webpack-loader-syntax, import/no-unresolved, import/extensions, import/first

Expand All @@ -37,6 +41,12 @@ export const BasicComponents = () => (
<h2 id="line-chart">Line chart with custom colors</h2>
<ExampleWithSource for={LineChartExample} source={LineChartExampleSRC} />

<h2 id="area-chart">Area Chart</h2>
<ExampleWithSource for={AreaChartExample} source={AreaChartExampleSRC} />

<h2 id="stacked-area-chart">Stacked Area Chart</h2>
<ExampleWithSource for={StackedAreaChartExample} source={StackedAreaChartExampleSRC} />

<hr className="separator" />

<h2 id="pie-chart">Pie chart</h2>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"dependencies": {
"@gooddata/data-layer": "7.0.0-alpha4",
"@gooddata/goodstrap": "55.0.0-alpha0",
"@gooddata/indigo-visualizations": "2.0.0-alpha6",
"@gooddata/indigo-visualizations": "2.0.0-alpha7",
"@gooddata/js-utils": "^0.9.0",
"@gooddata/numberjs": "^3.1.2",
"classnames": "^2.2.5",
Expand Down
80 changes: 80 additions & 0 deletions src/components/AreaChart.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import * as React from 'react';
import { omit, get } from 'lodash';
import { Subtract } from 'utility-types';
import { VisualizationObject, AFM } from '@gooddata/typings';

import { AreaChart as AfmAreaChart } from './afm/AreaChart';

import { ICommonChartProps } from './core/base/BaseChart';
import { convertBucketsToAFM } from '../helpers/conversion';
import { generateStackedDimensions } from '../helpers/dimensions';
import { isStackedChart } from '../helpers/stacks';

export interface IAreaChartBucketProps extends ICommonChartProps {
measures: VisualizationObject.BucketItem[];
viewBy?: VisualizationObject.IVisualizationAttribute[];
stackBy?: VisualizationObject.IVisualizationAttribute[];
filters?: VisualizationObject.VisualizationObjectFilter[];
}

export interface IAreaChartProps extends ICommonChartProps, IAreaChartBucketProps {
projectId: string;
}

type IAreaChartNonBucketProps = Subtract<IAreaChartProps, IAreaChartBucketProps>;

export interface IAreaChartProps extends ICommonChartProps {
projectId: string;
}

function generateDefaultDimensions(afm: AFM.IAfm): AFM.IDimension[] {
return [
{
itemIdentifiers: ['measureGroup']
},
{
itemIdentifiers: get(afm, 'attributes', []).map(a => a.localIdentifier)
}
];
}

function getStackingResultSpec(buckets: VisualizationObject.IBucket[]): AFM.IResultSpec {
if (isStackedChart(buckets)) {
return {
dimensions: generateStackedDimensions(buckets)
};
}

return {
dimensions: generateDefaultDimensions(convertBucketsToAFM(buckets))
};
}

export function AreaChart(props: IAreaChartProps): JSX.Element {
const buckets: VisualizationObject.IBucket[] = [
{
localIdentifier: 'measures',
items: get(props, 'measures', [])
},
{
localIdentifier: 'attributes',
items: get(props, 'viewBy', [])
},
{
localIdentifier: 'stacks',
items: get(props, 'stackBy', [])
}
];

const newProps
= omit<IAreaChartProps, IAreaChartNonBucketProps>(props, ['measures', 'viewBy', 'stackBy', 'filters']);

return (
<AfmAreaChart
{...newProps}
projectId={props.projectId}
afm={convertBucketsToAFM(buckets, props.filters)}
resultSpec={getStackingResultSpec(buckets)}
/>
);
}
15 changes: 15 additions & 0 deletions src/components/afm/AreaChart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// (C) 2007-2018 GoodData Corporation
import {
dataSourceProvider,
IDataSourceProviderProps
} from './DataSourceProvider';

export {
IDataSourceProviderProps
};

import { ICommonChartProps } from '../core/base/BaseChart';
import { AreaChart as CoreAreaChart } from '../core/AreaChart';
import { generateDefaultDimensions } from './afmHelper';

export const AreaChart = dataSourceProvider<ICommonChartProps>(CoreAreaChart, generateDefaultDimensions, 'AreaChart');
16 changes: 1 addition & 15 deletions src/components/afm/BarChart.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// (C) 2007-2018 GoodData Corporation
import {
AFM
} from '@gooddata/typings';

import {
dataSourceProvider,
IDataSourceProviderProps
Expand All @@ -14,16 +10,6 @@ export {

import { ICommonChartProps } from '../core/base/BaseChart';
import { BarChart as CoreBarChart } from '../core/BarChart';

function generateDefaultDimensions(afm: AFM.IAfm): AFM.IDimension[] {
return [
{
itemIdentifiers: ['measureGroup']
},
{
itemIdentifiers: (afm.attributes || []).map(a => a.localIdentifier)
}
];
}
import { generateDefaultDimensions } from './afmHelper';

export const BarChart = dataSourceProvider<ICommonChartProps>(CoreBarChart, generateDefaultDimensions, 'BarChart');
Loading

0 comments on commit fb3cbdd

Please sign in to comment.