forked from apache-superset/superset-ui-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow legend overrides at multiple levels (apache-superset#81)
* feat: reduce legend text size * feat: compute legend data * feat: support multi-level overrides for legend
- Loading branch information
Showing
7 changed files
with
232 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 0 additions & 95 deletions
95
...s/superset-ui-plugins/packages/superset-ui-preset-chart-xy/src/components/ChartLegend.tsx
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
...set-ui-plugins/packages/superset-ui-preset-chart-xy/src/components/legend/ChartLegend.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React, { CSSProperties, PureComponent } from 'react'; | ||
import { Value } from 'vega-lite/build/src/channeldef'; | ||
import AbstractEncoder from '../../encodeable/AbstractEncoder'; | ||
import { Dataset } from '../../encodeable/types/Data'; | ||
import { ObjectWithKeysFromAndValueType } from '../../encodeable/types/Base'; | ||
import { ChannelType, EncodingFromChannelsAndOutputs } from '../../encodeable/types/Channel'; | ||
import { BaseOptions } from '../../encodeable/types/Specification'; | ||
import { | ||
LegendItemRendererType, | ||
LegendGroupRendererType, | ||
LegendItemLabelRendererType, | ||
LegendItemMarkRendererType, | ||
} from './types'; | ||
import DefaultLegendGroup from './DefaultLegendGroup'; | ||
|
||
const LEGEND_CONTAINER_STYLE: CSSProperties = { | ||
maxHeight: 100, | ||
overflowY: 'auto', | ||
paddingLeft: 14, | ||
paddingTop: 6, | ||
position: 'relative', | ||
}; | ||
|
||
type Props<Encoder, ChannelTypes> = { | ||
data: Dataset; | ||
encoder: Encoder; | ||
LegendGroupRenderer?: LegendGroupRendererType<ChannelTypes>; | ||
LegendItemRenderer?: LegendItemRendererType<ChannelTypes>; | ||
LegendItemMarkRenderer?: LegendItemMarkRendererType<ChannelTypes>; | ||
LegendItemLabelRenderer?: LegendItemLabelRendererType<ChannelTypes>; | ||
}; | ||
|
||
export default class ChartLegend< | ||
ChannelTypes extends ObjectWithKeysFromAndValueType<Outputs, ChannelType>, | ||
Outputs extends ObjectWithKeysFromAndValueType<Encoding, Value>, | ||
Encoding extends EncodingFromChannelsAndOutputs< | ||
ChannelTypes, | ||
Outputs | ||
> = EncodingFromChannelsAndOutputs<ChannelTypes, Outputs>, | ||
Options extends BaseOptions = BaseOptions | ||
> extends PureComponent< | ||
Props<AbstractEncoder<ChannelTypes, Outputs, Encoding, Options>, ChannelTypes>, | ||
{} | ||
> { | ||
render() { | ||
const { | ||
data, | ||
encoder, | ||
LegendGroupRenderer, | ||
LegendItemRenderer, | ||
LegendItemMarkRenderer, | ||
LegendItemLabelRenderer, | ||
} = this.props; | ||
|
||
const LegendGroup = | ||
typeof LegendGroupRenderer === 'undefined' ? DefaultLegendGroup : LegendGroupRenderer; | ||
|
||
return ( | ||
<div style={LEGEND_CONTAINER_STYLE}> | ||
{encoder.getLegendInfos(data).map(items => ( | ||
<LegendGroup | ||
key={items[0].field} | ||
items={items} | ||
ItemRenderer={LegendItemRenderer} | ||
ItemMarkRenderer={LegendItemMarkRenderer} | ||
ItemLabelRenderer={LegendItemLabelRenderer} | ||
/> | ||
))} | ||
</div> | ||
); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...plugins/packages/superset-ui-preset-chart-xy/src/components/legend/DefaultLegendGroup.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React, { CSSProperties } from 'react'; | ||
import { LegendGroupRendererProps } from './types'; | ||
import DefaultLegendItem from './DefaultLegendItem'; | ||
|
||
const LEGEND_GROUP_STYLE: CSSProperties = { | ||
display: 'flex', | ||
flexDirection: 'row', | ||
flexWrap: 'wrap', | ||
fontSize: '0.8em', | ||
}; | ||
|
||
export default function DefaultLegendGroupRenderer<ChannelTypes>({ | ||
items, | ||
ItemRenderer, | ||
ItemMarkRenderer, | ||
ItemLabelRenderer, | ||
}: LegendGroupRendererProps<ChannelTypes>) { | ||
const LegendItem = typeof ItemRenderer === 'undefined' ? DefaultLegendItem : ItemRenderer; | ||
|
||
return ( | ||
<div style={LEGEND_GROUP_STYLE}> | ||
{items.map(item => ( | ||
<LegendItem | ||
key={`legend-item-${item.field}-${item.value}`} | ||
item={item} | ||
MarkRenderer={ItemMarkRenderer} | ||
LabelRenderer={ItemLabelRenderer} | ||
/> | ||
))} | ||
</div> | ||
); | ||
} |
44 changes: 44 additions & 0 deletions
44
...-plugins/packages/superset-ui-preset-chart-xy/src/components/legend/DefaultLegendItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React, { CSSProperties } from 'react'; | ||
import { LegendItem, LegendLabel } from '@vx/legend'; | ||
import { LegendItemRendererProps } from './types'; | ||
|
||
const MARK_SIZE = 8; | ||
|
||
const MARK_STYLE: CSSProperties = { display: 'inline-block' }; | ||
|
||
export default function DefaultLegendItem<ChannelTypes>({ | ||
item, | ||
MarkRenderer, | ||
LabelRenderer, | ||
}: LegendItemRendererProps<ChannelTypes>) { | ||
return ( | ||
<LegendItem key={`legend-item-${item.field}-${item.value}`} margin="0 5px"> | ||
{typeof MarkRenderer === 'undefined' ? ( | ||
<svg width={MARK_SIZE} height={MARK_SIZE} style={MARK_STYLE}> | ||
<circle | ||
fill={ | ||
// @ts-ignore | ||
item.encodedValues.color || item.encodedValues.fill || '#ccc' | ||
} | ||
stroke={ | ||
// @ts-ignore | ||
item.encodedValues.stroke || 'none' | ||
} | ||
r={MARK_SIZE / 2} | ||
cx={MARK_SIZE / 2} | ||
cy={MARK_SIZE / 2} | ||
/> | ||
</svg> | ||
) : ( | ||
<MarkRenderer item={item} /> | ||
)} | ||
{typeof LabelRenderer === 'undefined' ? ( | ||
<LegendLabel align="left" margin="0 0 0 4px"> | ||
{item.value} | ||
</LegendLabel> | ||
) : ( | ||
<LabelRenderer item={item} /> | ||
)} | ||
</LegendItem> | ||
); | ||
} |
38 changes: 38 additions & 0 deletions
38
...s/superset-ui-plugins/packages/superset-ui-preset-chart-xy/src/components/legend/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Value } from 'vega-lite/build/src/channeldef'; | ||
import { ObjectWithKeysFromAndValueType } from '../../encodeable/types/Base'; | ||
import { ChannelInput } from '../../encodeable/types/Channel'; | ||
|
||
export type LegendItemInfo<ChannelTypes> = { | ||
field: string; | ||
value: ChannelInput; | ||
encodedValues: Partial<ObjectWithKeysFromAndValueType<ChannelTypes, Value | undefined>>; | ||
}; | ||
|
||
export type LegendItemMarkRendererType<ChannelTypes> = React.ComponentType<{ | ||
item: LegendItemInfo<ChannelTypes>; | ||
}>; | ||
|
||
export type LegendItemLabelRendererType<ChannelTypes> = React.ComponentType<{ | ||
item: LegendItemInfo<ChannelTypes>; | ||
}>; | ||
|
||
export type LegendItemRendererProps<ChannelTypes> = { | ||
item: LegendItemInfo<ChannelTypes>; | ||
MarkRenderer?: LegendItemMarkRendererType<ChannelTypes>; | ||
LabelRenderer?: LegendItemLabelRendererType<ChannelTypes>; | ||
}; | ||
|
||
export type LegendItemRendererType<ChannelTypes> = React.ComponentType< | ||
LegendItemRendererProps<ChannelTypes> | ||
>; | ||
|
||
export type LegendGroupRendererProps<ChannelTypes> = { | ||
items: LegendItemInfo<ChannelTypes>[]; | ||
ItemRenderer?: LegendItemRendererType<ChannelTypes>; | ||
ItemMarkRenderer?: LegendItemMarkRendererType<ChannelTypes>; | ||
ItemLabelRenderer?: LegendItemLabelRendererType<ChannelTypes>; | ||
}; | ||
|
||
export type LegendGroupRendererType<ChannelTypes> = React.ComponentType< | ||
LegendGroupRendererProps<ChannelTypes> | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters