Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

Commit

Permalink
feat: remove children from XYChartLayout parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
kristw committed Jun 5, 2019
1 parent e2ea8c8 commit cd74445
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 56 deletions.
42 changes: 23 additions & 19 deletions packages/superset-ui-preset-chart-xy/src/BoxPlot/BoxPlot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,14 @@ export default class BoxPlot extends React.PureComponent<Props> {

const isHorizontal = channels.y.definition.type === 'nominal';

const children = [
<BoxPlotSeries
key={channels.x.definition.field}
animated
data={
isHorizontal
? data.map(row => ({ ...row, y: channels.y.get(row) }))
: data.map(row => ({ ...row, x: channels.x.get(row) }))
}
fill={(datum: PlainObject) => channels.color.encode(datum, '#55acee')}
fillOpacity={0.4}
stroke={(datum: PlainObject) => channels.color.encode(datum)}
strokeWidth={1}
widthRatio={0.6}
horizontal={channels.y.definition.type === 'nominal'}
/>,
];
if (typeof channels.x.scale !== 'undefined') {
const xDomain = channels.x.getDomain(data);
channels.x.scale.setDomain(xDomain);
}
if (typeof channels.y.scale !== 'undefined') {
const yDomain = channels.y.getDomain(data);
channels.y.scale.setDomain(yDomain);
}

const layout = this.createXYChartLayout({
width,
Expand All @@ -103,7 +94,6 @@ export default class BoxPlot extends React.PureComponent<Props> {
theme,
xEncoder: channels.x,
yEncoder: channels.y,
children,
});

return layout.renderChartWithFrame((chartDim: Dimension) => (
Expand All @@ -122,7 +112,21 @@ export default class BoxPlot extends React.PureComponent<Props> {
>
{layout.renderXAxis()}
{layout.renderYAxis()}
{children}
<BoxPlotSeries
key={channels.x.definition.field}
animated
data={
isHorizontal
? data.map(row => ({ ...row, y: channels.y.get(row) }))
: data.map(row => ({ ...row, x: channels.x.get(row) }))
}
fill={(datum: PlainObject) => channels.color.encode(datum, '#55acee')}
fillOpacity={0.4}
stroke={(datum: PlainObject) => channels.color.encode(datum)}
strokeWidth={1}
widthRatio={0.6}
horizontal={channels.y.definition.type === 'nominal'}
/>
</XYChart>
));
}
Expand Down
12 changes: 11 additions & 1 deletion packages/superset-ui-preset-chart-xy/src/Line/Line.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import React, { PureComponent } from 'react';
import { kebabCase, groupBy, flatMap, uniqueId, values } from 'lodash';
import { extent as d3Extent } from 'd3-array';
import {
AreaSeries,
LinearGradient,
Expand Down Expand Up @@ -202,14 +203,23 @@ export default class LineChart extends PureComponent<Props> {
const { channels } = encoder;
const allSeries = this.createAllSeries({ encoder, data });
const children = this.createChildren(allSeries);

if (typeof channels.x.scale !== 'undefined') {
const xDomain = channels.x.getDomain(data);
channels.x.scale.setDomain(xDomain);
}
if (typeof channels.y.scale !== 'undefined') {
const yDomain = channels.y.getDomain(data);
channels.y.scale.setDomain(yDomain);
}

const layout = this.createXYChartLayout({
width,
height,
margin: this.createMargin(margin),
theme,
xEncoder: channels.x,
yEncoder: channels.y,
children,
});

return layout.renderChartWithFrame((chartDim: Dimension) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import createMarginSelector, { DEFAULT_MARGIN } from '../utils/selectors/createM
import createXYChartLayoutSelector from '../utils/selectors/createXYChartLayoutSelector';
import DefaultTooltipRenderer from './DefaultTooltipRenderer';
import convertScaleToDataUIScale from '../utils/convertScaleToDataUIScaleShape';
import { isScaleFieldDef } from '../encodeable/types/ChannelDef';

export interface TooltipProps {
datum: EncodedPoint;
Expand Down Expand Up @@ -84,11 +85,22 @@ export default class ScatterPlot extends PureComponent<Props> {
const { data, margin, theme, TooltipRenderer } = this.props;
const { channels } = this.encoder;

if (typeof channels.size.scale !== 'undefined') {
const domain = d3Extent(data, d => channels.size.get<number>(d));
if (typeof channels.x.scale !== 'undefined') {
const xDomain = channels.x.getDomain(data);
channels.x.scale.setDomain(xDomain);
}
if (typeof channels.y.scale !== 'undefined') {
const yDomain = channels.y.getDomain(data);
channels.y.scale.setDomain(yDomain);
}
if (
isScaleFieldDef(channels.size.definition) &&
channels.size.definition.type === 'quantitative'
) {
const domain = channels.size.getDomain(data) as number[];
const [min, max] = domain;
const adjustedDomain = [Math.min(min || 0, 0), Math.max(max || 1, 1)];
channels.size.scale.setDomain(adjustedDomain);
channels.size.scale!.setDomain(adjustedDomain);
}

const encodedData = data.map(d => ({
Expand All @@ -100,25 +112,13 @@ export default class ScatterPlot extends PureComponent<Props> {
data: d,
}));

const children = [
<PointSeries
key={channels.x.definition.field}
data={encodedData}
fill={(d: EncodedPoint) => d.fill}
fillOpacity={0.5}
stroke={(d: EncodedPoint) => d.stroke}
size={(d: EncodedPoint) => d.size}
/>,
];

const layout = this.createXYChartLayout({
width,
height,
margin: this.createMargin(margin),
theme,
xEncoder: channels.x,
yEncoder: channels.y,
children,
});

return layout.renderChartWithFrame((chartDim: Dimension) => (
Expand All @@ -137,7 +137,14 @@ export default class ScatterPlot extends PureComponent<Props> {
>
{layout.renderXAxis()}
{layout.renderYAxis()}
{children}
<PointSeries
key={channels.x.definition.field}
data={encodedData}
fill={(d: EncodedPoint) => d.fill}
fillOpacity={0.5}
stroke={(d: EncodedPoint) => d.stroke}
size={(d: EncodedPoint) => d.size}
/>
</XYChart>
));
}
Expand Down
20 changes: 0 additions & 20 deletions packages/superset-ui-preset-chart-xy/src/utils/XYChartLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable sort-keys, no-magic-numbers */

import React, { ReactNode } from 'react';
import collectScalesFromProps from '@data-ui/xy-chart/esm/utils/collectScalesFromProps';
import { XAxis, YAxis } from '@data-ui/xy-chart';
import { ChartTheme } from '@data-ui/theme';
import { Margin, mergeMargin, Dimension } from '@superset-ui/dimension';
Expand All @@ -11,7 +10,6 @@ import ChannelEncoder from '../encodeable/ChannelEncoder';
import { XFieldDef, YFieldDef } from '../encodeable/types/ChannelDef';
import { PlainObject } from '../encodeable/types/Data';
import { DEFAULT_LABEL_ANGLE } from './constants';
import convertScaleToDataUIScale from './convertScaleToDataUIScaleShape';
import { AxisLayout } from '../encodeable/AxisAgent';

// Additional margin to avoid content hidden behind scroll bar
Expand All @@ -25,7 +23,6 @@ export interface XYChartLayoutConfig {
margin: Margin;
xEncoder: ChannelEncoder<XFieldDef>;
yEncoder: ChannelEncoder<YFieldDef>;
children: ReactNode[];
theme: ChartTheme;
}

Expand Down Expand Up @@ -53,23 +50,9 @@ export default class XYChartLayout {
margin,
xEncoder,
yEncoder,
children,
theme,
} = config;

const { xScale, yScale } = collectScalesFromProps({
width,
height,
margin,
xScale: convertScaleToDataUIScale(xEncoder.scale!.config || {}),
yScale: convertScaleToDataUIScale(yEncoder.scale!.config || {}),
theme,
children,
});

if (typeof yEncoder.scale !== 'undefined') {
yEncoder.scale.setDomain(yScale.domain());
}
if (typeof yEncoder.axis !== 'undefined') {
this.yLayout = yEncoder.axis.computeLayout({
axisWidth: Math.max(height - margin.top - margin.bottom),
Expand All @@ -82,9 +65,6 @@ export default class XYChartLayout {
const secondMargin = this.yLayout ? mergeMargin(margin, this.yLayout.minMargin) : margin;
const innerWidth = Math.max(width - secondMargin.left - secondMargin.right, minContentWidth);

if (typeof xEncoder.scale !== 'undefined') {
xEncoder.scale.setDomain(xScale.domain());
}
if (typeof xEncoder.axis !== 'undefined') {
this.xLayout = xEncoder.axis.computeLayout({
axisWidth: innerWidth,
Expand Down

0 comments on commit cd74445

Please sign in to comment.