Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add stroke properties to discrete color legend (#860) #994

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/legends.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Currently following types of legends are supported:
<!-- INJECT:"VerticalDiscreteColorLegendExampleWithLink" -->

#### items (required)
Type: `Array<string|{title: string, color: String, disabled: boolean}|react element>`
Array of items that should be shown on the legend. The array should consist from either objects (`title`, optional `color` and optional `disabled` flag) or strings (treated as titles).
Type: `Array<string|{title: string, color: String, strokeDasharray: string, strokeStyle: string, strokeWidth: number, disabled: boolean}|react element>`
Array of items that should be shown on the legend. The array should consist from either objects (`title`, optional `color`, optional `strokeDasharray`, optional `strokeStyle`, optional `strokeWidth`, and optional `disabled` flag) or strings (treated as titles). The stroke properties should match those in your series (see [line series](line-mark-series.md))

#### orientation (optional)
Type: `(vertical|horizontal)`
Expand Down
4 changes: 2 additions & 2 deletions showcase/legends/searchable-discrete-color.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export default class Example extends React.Component {
super(props);
this.state = {
items: [
{title: 'Apples', color: '#3a3'},
{title: 'Apples', color: '#3a3', strokeStyle: "dashed"},
{title: 'Bananas', color: '#fc0'},
{title: 'Blueberries', color: '#337'},
{title: 'Carrots', color: '#f93'},
{title: 'Carrots', color: '#f93', strokeWidth: 6},
{title: 'Eggplants', color: '#337'},
{title: 'Limes', color: '#cf3'},
{title: 'Potatoes', color: '#766'}
Expand Down
32 changes: 26 additions & 6 deletions src/legends/discrete-color-legend-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ import React from 'react';

import PropTypes from 'prop-types';

const STROKE_STYLES = {
dashed: '6, 2',
solid: null
};

function DiscreteColorLegendItem({
color,
strokeDasharray,
strokeStyle,
strokeWidth,
disabled,
onClick,
orientation,
Expand All @@ -40,10 +48,18 @@ function DiscreteColorLegendItem({
}
return (
<div {...{className, onClick, onMouseEnter, onMouseLeave}}>
<span
className="rv-discrete-color-legend-item__color"
style={disabled ? null : {background: color}}
/>
<svg className="rv-discrete-color-legend-item__color" height={2} width={14}>
<path
className="rv-discrete-color-legend-item__color__path"
d="M 0, 1 L 14, 1"
style={{
strokeWidth,
strokeDasharray: STROKE_STYLES[strokeStyle] || strokeDasharray,
stroke: disabled ? null : color
}}

/>
</svg>
<span className="rv-discrete-color-legend-item__title">{title}</span>
</div>
);
Expand All @@ -56,10 +72,14 @@ DiscreteColorLegendItem.propTypes = {
onClick: PropTypes.func,
onMouseEnter: PropTypes.func,
onMouseLeave: PropTypes.func,
orientation: PropTypes.oneOf(['vertical', 'horizontal']).isRequired
orientation: PropTypes.oneOf(['vertical', 'horizontal']).isRequired,
strokeDasharray: PropTypes.string,
strokeWidth: PropTypes.number,
strokeStyle: PropTypes.oneOf(Object.keys(STROKE_STYLES))
};
DiscreteColorLegendItem.defaultProps = {
disabled: false
disabled: false,
strokeStyle: 'solid'
};
DiscreteColorLegendItem.displayName = 'DiscreteColorLegendItem';

Expand Down
3 changes: 3 additions & 0 deletions src/legends/discrete-color-legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ function DiscreteColorLegend({
<DiscreteColorLegendItem
title={item.title ? item.title : item}
color={item.color ? item.color : colors[i % colors.length]}
strokeDasharray={item.strokeDasharray}
strokeStyle={item.strokeStyle}
strokeWidth={item.strokeWidth}
disabled={Boolean(item.disabled)}
orientation={orientation}
key={i}
Expand Down
9 changes: 6 additions & 3 deletions src/styles/legends.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ $rv-legend-disabled-color: #b8b8b8;
}

.rv-discrete-color-legend-item__color {
background: #dcdcdc;
display: inline-block;
height: 2px;
vertical-align: middle;
width: 14px;
overflow: visible;
}

.rv-discrete-color-legend-item__color__path {
stroke: #dcdcdc;
stroke-width: 2px;
}

.rv-discrete-color-legend-item__title {
Expand Down