Skip to content

Commit

Permalink
fix(brush): rotate brush on rotated charts
Browse files Browse the repository at this point in the history
during the redux refactoring (elastic#281) I've forgot to compute the brush correctly on rotated charts. In
this commit I also fixed the min and max value passed to the onBrushEnd to be aligned with the
min/max value of the domain.

fix elastic#527
  • Loading branch information
markov00 committed Jan 28, 2020
1 parent 1111671 commit 02eedcc
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 110 deletions.
8 changes: 3 additions & 5 deletions .playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@
/*display: inline-block;
position: relative;
*/
width: 100%;
height: 480px;
/*
margin: 0;
*/
width: 800px;
height: 400px;
margin: 20px;
}
</style>
</head>
Expand Down
114 changes: 89 additions & 25 deletions .playground/playground.tsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,112 @@
import React from 'react';
import { Chart, Datum, Partition } from '../src';
import { Chart, LineSeries, ScaleType, Settings, Position, Axis, BarSeries, HistogramBarSeries } from '../src';
export class Playground extends React.Component<{}, { isSunburstShown: boolean }> {
chartRef: React.RefObject<Chart> = React.createRef();
state = {
isSunburstShown: true,
};
onBrushEnd = (min: number, max: number) => {
// eslint-disable-next-line no-console
console.log({ min, max });
};

render() {
return (
<>
<div className="chart">
<Chart ref={this.chartRef}>
<Partition
id={'piechart'}
<Settings rotation={90} onBrushEnd={this.onBrushEnd} />
<Axis id="y" position={Position.Left} title={'y'} />
<Axis id="x" position={Position.Bottom} title={'x'} />
<LineSeries
id={'aaa'}
xScaleType={ScaleType.Linear}
xAccessor={0}
yAccessors={[1]}
data={[
[0, 1],
[1, 2],
[2, 5],
[3, 5],
[4, 2],
[5, 6],
]}
/>
</Chart>
</div>
<div className="chart">
<Chart ref={this.chartRef}>
<Settings rotation={0} onBrushEnd={this.onBrushEnd} theme={{ chartMargins: { left: 30 } }} />
<Axis id="x" position={Position.Bottom} title={'x'} />
<Axis id="y" position={Position.Left} title={'y'} />
<LineSeries
id={'aaa'}
xScaleType={ScaleType.Linear}
xAccessor={0}
yAccessors={[1]}
data={[
[0, 1],
[1, 2],
[2, 5],
[3, 5],
[4, 2],
[5, 6],
]}
/>
</Chart>
</div>
<div className="chart">
<Chart ref={this.chartRef}>
<Settings rotation={90} onBrushEnd={this.onBrushEnd} theme={{ chartMargins: { left: 30 } }} />
<Axis id="x" position={Position.Bottom} title={'x'} />
<Axis id="y" position={Position.Left} title={'y'} />
<BarSeries
id={'aaa'}
xScaleType={ScaleType.Linear}
xAccessor={0}
yAccessors={[1]}
data={[
{ id: 'Item\u00A00', g: 'A', v: 30 },
{ id: 'Item\u00A01', g: 'A', v: 20 },
{ id: 'Item\u00A02', g: 'B', v: 50 },
[0, 1],
[1, 2],
[2, 5],
[3, 5],
// [4, 2],
[5, 6],
]}
valueAccessor={(d: Datum) => d.v}
valueFormatter={(d: Datum) => `${d}%`}
layers={[
{ groupByRollup: (d: Datum) => d.g, nodeLabel: (d: Datum) => `Group ${d}` },
{
groupByRollup: (d: Datum) => d.id,
nodeLabel: (d: Datum) => d,
fillLabel: { formatter: (d: Datum) => `${d} pct` },
},
/>
<LineSeries
id={'aaa1'}
xScaleType={ScaleType.Linear}
xAccessor={0}
yAccessors={[1]}
data={[
[0, 1],
[1, 2],
[2, 5],
[3, 5],
// [4, 2],
[5, 6],
]}
/>
</Chart>
</div>
<div className="chart">
<Chart ref={this.chartRef}>
<Partition
id={'piechart'}
data={[30, 20, 50]}
valueFormatter={(d: Datum) => `${d}%`}
layers={[
{
groupByRollup: (d: Datum, i: number) => (i < 2 ? 'A' : 'B'),
nodeLabel: (d: Datum) => `Group ${d}`,
},
{ groupByRollup: (d: Datum, i: number) => i, nodeLabel: (d: Datum) => `Item\u00A0${d}` },
<Settings rotation={90} onBrushEnd={this.onBrushEnd} theme={{ chartMargins: { left: 30 } }} />
<Axis id="x" position={Position.Bottom} title={'x'} />
<Axis id="y" position={Position.Left} title={'y'} />
<HistogramBarSeries
id={'aaa'}
xScaleType={ScaleType.Linear}
xAccessor={0}
yAccessors={[1]}
data={[
[0, 1],
[1, 2],
[2, 5],
[3, 5],
[4, 2],
[5, 6],
]}
/>
</Chart>
Expand Down
15 changes: 3 additions & 12 deletions src/chart_types/xy_chart/renderer/dom/brush.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { Layer, Rect, Stage } from 'react-konva';
import { connect } from 'react-redux';
import { Dimensions } from '../../../../utils/dimensions';
import { isInitialized } from '../../../../state/selectors/is_initialized';
import { computeChartTransformSelector } from '../../state/selectors/compute_chart_transform';
import { Transform } from '../../state/utils';
import { GlobalChartState } from '../../../../state/chart_state';
import { getBrushAreaSelector } from '../../state/selectors/get_brush_area';
import { isBrushAvailableSelector } from '../../state/selectors/is_brush_available';
Expand All @@ -14,7 +12,6 @@ import { isBrushingSelector } from '../../state/selectors/is_brushing';
interface Props {
initialized: boolean;
chartDimensions: Dimensions;
chartTransform: Transform;
isBrushing: boolean | undefined;
isBrushAvailable: boolean | undefined;
brushArea: Dimensions | null;
Expand All @@ -32,7 +29,7 @@ class BrushToolComponent extends React.Component<Props> {
};

render() {
const { initialized, isBrushAvailable, isBrushing, chartDimensions, chartTransform, brushArea } = this.props;
const { initialized, isBrushAvailable, isBrushing, chartDimensions, brushArea } = this.props;
if (!initialized || !isBrushAvailable || !isBrushing) {
return null;
}
Expand All @@ -43,8 +40,8 @@ class BrushToolComponent extends React.Component<Props> {
height={chartDimensions.height}
className="echBrushTool"
style={{
top: chartDimensions.top + chartTransform.x,
left: chartDimensions.left + chartTransform.y,
top: chartDimensions.top,
left: chartDimensions.left,
width: chartDimensions.width,
height: chartDimensions.height,
}}
Expand All @@ -70,19 +67,13 @@ const mapStateToProps = (state: GlobalChartState): Props => {
width: 0,
height: 0,
},
chartTransform: {
x: 0,
y: 0,
rotate: 0,
},
};
}
return {
initialized: state.specsInitialized,
brushArea: getBrushAreaSelector(state),
isBrushAvailable: isBrushAvailableSelector(state),
chartDimensions: computeChartDimensionsSelector(state).chartDimensions,
chartTransform: computeChartTransformSelector(state),
isBrushing: isBrushingSelector(state),
};
};
Expand Down
Loading

0 comments on commit 02eedcc

Please sign in to comment.