Skip to content

Commit 6da599f

Browse files
committed
adding **kwargs - broken state
1 parent e74996c commit 6da599f

File tree

1 file changed

+51
-42
lines changed

1 file changed

+51
-42
lines changed

plotly/figure_factory/_bullet.py

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
VALID_KEYS = ['title', 'subtitle', 'ranges', 'measures', 'markers']
1414

1515

16-
def _bullet(df, markers, measures, ranges, subtitle, title, as_rows,
17-
marker_size, marker_symbol, range_colors, measure_colors,
18-
horizontal_spacing, vertical_spacing):
16+
def _bullet(df, markers, measures, ranges, subtitle, title, orientation,
17+
range_colors, measure_colors, horizontal_spacing,
18+
vertical_spacing, scatter_options):
1919
num_of_lanes = len(df)
20-
num_of_rows = num_of_lanes if as_rows else 1
21-
num_of_cols = 1 if as_rows else num_of_lanes
20+
num_of_rows = num_of_lanes if orientation == 'h' else 1
21+
num_of_cols = 1 if orientation == 'h' else num_of_lanes
2222
if not horizontal_spacing and not vertical_spacing:
2323
horizontal_spacing = vertical_spacing = 1./num_of_lanes
2424
fig = plotly.tools.make_subplots(
@@ -32,10 +32,10 @@ def _bullet(df, markers, measures, ranges, subtitle, title, as_rows,
3232
dict(shapes=[]),
3333
showlegend=False,
3434
barmode='stack',
35-
margin=dict(l=120 if as_rows else 80),
35+
margin=dict(l=120 if orientation == 'h' else 80),
3636
)
3737

38-
if as_rows:
38+
if orientation == 'h':
3939
width_axis = 'yaxis'
4040
length_axis = 'xaxis'
4141
else:
@@ -56,13 +56,6 @@ def _bullet(df, markers, measures, ranges, subtitle, title, as_rows,
5656
if num_of_lanes <= 1:
5757
fig['layout'][width_axis + '1']['domain'] = [0.4, 0.6]
5858

59-
# marker symbol size
60-
if not marker_size:
61-
if num_of_lanes <= 4:
62-
marker_size = 18
63-
else:
64-
marker_size = 8
65-
6659
if not range_colors:
6760
range_colors = ['rgb(200, 200, 200)', 'rgb(245, 245, 245)']
6861
if not measure_colors:
@@ -75,17 +68,19 @@ def _bullet(df, markers, measures, ranges, subtitle, title, as_rows,
7568
range_colors[0], range_colors[1],
7669
len(df.iloc[row]['ranges']), 'rgb'
7770
)
78-
x = [sorted(df.iloc[row]['ranges'])[-1 - idx]] if as_rows else [0]
79-
y = [0] if as_rows else [sorted(df.iloc[row]['ranges'])[-1 - idx]]
71+
x = ([sorted(df.iloc[row]['ranges'])[-1 - idx]] if
72+
orientation == 'h' else [0])
73+
y = ([0] if orientation == 'h' else
74+
[sorted(df.iloc[row]['ranges'])[-1 - idx]])
8075
bar = go.Bar(
8176
x=x,
8277
y=y,
8378
marker=dict(
8479
color=inter_colors[-1 - idx]
8580
),
8681
name='ranges',
87-
hoverinfo='x' if as_rows else 'y',
88-
orientation='h' if as_rows else 'v',
82+
hoverinfo='x' if orientation == 'h' else 'y',
83+
orientation=orientation,
8984
width=2,
9085
base=0,
9186
xaxis='x{}'.format(row + 1),
@@ -99,9 +94,9 @@ def _bullet(df, markers, measures, ranges, subtitle, title, as_rows,
9994
measure_colors[0], measure_colors[1],
10095
len(df.iloc[row]['measures']), 'rgb'
10196
)
102-
x = ([sorted(df.iloc[row]['measures'])[-1 - idx]] if as_rows
103-
else [0.5])
104-
y = ([0.5] if as_rows
97+
x = ([sorted(df.iloc[row]['measures'])[-1 - idx]] if
98+
orientation == 'h' else [0.5])
99+
y = ([0.5] if orientation == 'h'
105100
else [sorted(df.iloc[row]['measures'])[-1 - idx]])
106101
bar = go.Bar(
107102
x=x,
@@ -110,8 +105,8 @@ def _bullet(df, markers, measures, ranges, subtitle, title, as_rows,
110105
color=inter_colors[-1 - idx]
111106
),
112107
name='measures',
113-
hoverinfo='x' if as_rows else 'y',
114-
orientation='h' if as_rows else 'v',
108+
hoverinfo='x' if orientation == 'h' else 'y',
109+
orientation=orientation,
115110
width=0.4,
116111
base=0,
117112
xaxis='x{}'.format(row + 1),
@@ -120,8 +115,8 @@ def _bullet(df, markers, measures, ranges, subtitle, title, as_rows,
120115
fig['data'].append(bar)
121116

122117
# markers
123-
x = df.iloc[row]['markers'] if as_rows else [0.5]
124-
y = [0.5] if as_rows else df.iloc[row]['markers']
118+
x = df.iloc[row]['markers'] if orientation == 'h' else [0.5]
119+
y = [0.5] if orientation == 'h' else df.iloc[row]['markers']
125120
markers = go.Scatter(
126121
x=x,
127122
y=y,
@@ -131,7 +126,7 @@ def _bullet(df, markers, measures, ranges, subtitle, title, as_rows,
131126
size=marker_size
132127
),
133128
name='markers',
134-
hoverinfo='x' if as_rows else 'y',
129+
hoverinfo='x' if orientation == 'h' else 'y',
135130
xaxis='x{}'.format(row + 1),
136131
yaxis='y{}'.format(row + 1)
137132
)
@@ -146,10 +141,11 @@ def _bullet(df, markers, measures, ranges, subtitle, title, as_rows,
146141
label = '<b>{}</b>'.format(title) + subtitle
147142
annot = utils.annotation_dict_for_label(
148143
label,
149-
(num_of_lanes - row if as_rows else row + 1),
150-
num_of_lanes, vertical_spacing if as_rows else horizontal_spacing,
151-
'row' if as_rows else 'col',
152-
True if as_rows else False,
144+
(num_of_lanes - row if orientation == 'h' else row + 1),
145+
num_of_lanes,
146+
vertical_spacing if orientation == 'h' else horizontal_spacing,
147+
'row' if orientation == 'h' else 'col',
148+
True if orientation == 'h' else False,
153149
False
154150
)
155151
fig['layout']['annotations'].append(annot)
@@ -158,19 +154,18 @@ def _bullet(df, markers, measures, ranges, subtitle, title, as_rows,
158154

159155

160156
def create_bullet(data, markers=None, measures=None, ranges=None,
161-
subtitle=None, title=None, as_rows=True, marker_size=16,
162-
marker_symbol='diamond-tall', range_colors=None,
163-
measure_colors=None, horizontal_spacing=None,
164-
vertical_spacing=None, chart_title='Bullet Chart', height=600,
165-
width=1000):
157+
subtitle=None, title=None, orientation='h',
158+
range_colors=None, measure_colors=None, horizontal_spacing=None,
159+
vertical_spacing=None, chart_title='Bullet Chart',
160+
height=600, width=1000, **scatter_options):
166161
"""
167162
Returns figure for bullet chart.
168163
169164
:param (pd.DataFrame | list) data: either a JSON list of dicts or a pandas
170165
DataFrame. All keys must be one of 'title', 'subtitle', 'ranges',
171166
'measures', and 'markers'.
172-
:param (bool) as_rows: if True, the bars are placed horizontally as rows.
173-
If False, the bars are placed vertically in the chart.
167+
:param (bool) orientation: if 'h', the bars are placed horizontally as
168+
rows. If 'v' the bars are placed vertically in the chart.
174169
:param (int) marker_size: sets the size of the markers in the chart.
175170
:param (str | int) marker_symbol: the symbol of the markers in the chart.
176171
Default='diamond-tall'
@@ -233,8 +228,8 @@ def create_bullet(data, markers=None, measures=None, ranges=None,
233228
)
234229
df = pd.DataFrame.transpose(df)
235230

236-
# make sure ranges and measures are not NAN or NONE
237-
for needed_key in ['ranges', 'measures']:
231+
# make sure ranges, measures, 'markers' are not NAN or NONE
232+
for needed_key in ['ranges', 'measures', 'markers']:
238233
for idx, r in enumerate(df[needed_key]):
239234
try:
240235
r_is_nan = math.isnan(r)
@@ -255,10 +250,24 @@ def create_bullet(data, markers=None, measures=None, ranges=None,
255250
colors_list = colors.convert_colors_to_same_type(colors_list,
256251
'rgb')[0]
257252

253+
# scatter options
254+
default_scatter_options = {
255+
'marker': {'size': 12,
256+
'symbol': 'diamond-tall',
257+
'color': 'rgb(0, 0, 0)'}
258+
}
259+
if scatter_options == {}:
260+
scatter_options.update(default_scatter_options)
261+
else:
262+
263+
264+
265+
266+
258267
fig = _bullet(
259-
df, markers, measures, ranges, subtitle, title, as_rows, marker_size,
260-
marker_symbol, range_colors, measure_colors, horizontal_spacing,
261-
vertical_spacing
268+
df, markers, measures, ranges, subtitle, title, orientation,
269+
range_colors, measure_colors, horizontal_spacing, vertical_spacing,
270+
scatter_options
262271
)
263272

264273
fig['layout'].update(

0 commit comments

Comments
 (0)