Skip to content

Commit cfa066e

Browse files
committed
Added color_parser functionality in create_gantt
1 parent 3b75d48 commit cfa066e

File tree

1 file changed

+71
-202
lines changed

1 file changed

+71
-202
lines changed

plotly/tools.py

+71-202
Original file line numberDiff line numberDiff line change
@@ -1517,8 +1517,7 @@ def _validate_gantt(df):
15171517

15181518
@staticmethod
15191519
def _gantt(chart, colors, title, bar_width, showgrid_x, showgrid_y,
1520-
height, width, tasks=None,
1521-
task_names=None, data=None):
1520+
height, width, tasks=None, task_names=None, data=None):
15221521
"""
15231522
Refer to FigureFactory.create_gantt() for docstring
15241523
"""
@@ -1636,6 +1635,8 @@ def _gantt_colorscale(chart, colors, title, index_col, show_colorbar,
16361635
if data is None:
16371636
data = []
16381637

1638+
#if chart[index_col]
1639+
16391640
for index in range(len(chart)):
16401641
task = dict(x0=chart[index]['Start'],
16411642
x1=chart[index]['Finish'],
@@ -1663,18 +1664,25 @@ def _gantt_colorscale(chart, colors, title, index_col, show_colorbar,
16631664
tasks[index]['y0'] = index - bar_width
16641665
tasks[index]['y1'] = index + bar_width
16651666

1666-
colors = FigureFactory._unlabel_rgb(colors)
1667+
# unlabel color
1668+
colors = FigureFactory._color_parser(
1669+
colors, FigureFactory._unlabel_rgb
1670+
)
16671671
lowcolor = colors[0]
16681672
highcolor = colors[1]
16691673

16701674
intermed = (chart[index][index_col])/100.0
1671-
intermed_color = FigureFactory._find_intermediate_color(lowcolor,
1672-
highcolor,
1673-
intermed)
1674-
intermed_color = FigureFactory._label_rgb(intermed_color)
1675+
intermed_color = FigureFactory._find_intermediate_color(
1676+
lowcolor, highcolor, intermed
1677+
)
1678+
intermed_color = FigureFactory._color_parser(
1679+
intermed_color, FigureFactory._label_rgb
1680+
)
16751681
tasks[index]['fillcolor'] = intermed_color
16761682
# relabel colors with 'rgb'
1677-
colors = FigureFactory._label_rgb(colors)
1683+
colors = FigureFactory._color_parser(
1684+
colors, FigureFactory._label_rgb
1685+
)
16781686

16791687
# add a line for hover text and autorange
16801688
data.append(
@@ -1798,7 +1806,6 @@ def _gantt_dict(chart, colors, title, index_col, show_colorbar, bar_width,
17981806
"""
17991807
Refer to FigureFactory.create_gantt() for docstring
18001808
"""
1801-
from plotly.graph_objs import graph_objs
18021809
if tasks is None:
18031810
tasks = []
18041811
if task_names is None:
@@ -1833,10 +1840,10 @@ def _gantt_dict(chart, colors, title, index_col, show_colorbar, bar_width,
18331840
# verify each value in index column appears in colors dictionary
18341841
for key in index_vals:
18351842
if key not in colors:
1836-
raise exceptions.PlotlyError("If you are using colors as a "
1837-
"dictionary, all of its keys "
1838-
"must be all the values in the "
1839-
"index column.")
1843+
raise exceptions.PlotlyError(
1844+
"If you are using colors as a dictionary, all of its "
1845+
"keys must be all the values in the index column."
1846+
)
18401847

18411848
for index in range(len(tasks)):
18421849
tn = tasks[index]['name']
@@ -1858,21 +1865,20 @@ def _gantt_dict(chart, colors, title, index_col, show_colorbar, bar_width,
18581865
)
18591866
)
18601867

1861-
if show_colorbar is True:
1868+
#if show_colorbar is True:
18621869
# generate dummy data for colorscale visibility
1863-
trace2 = dict(
1864-
#x=[tasks[0]['x0'], tasks[0]['x0']],
1865-
x=[2, 6],
1866-
y=[4, 2],
1867-
name='asdf',
1868-
visible='legendonly',
1869-
marker=dict(
1870-
size=10,
1871-
color='rgb(25, 50, 150)'),
1872-
showlegend=True
1873-
)
1874-
1875-
data.append(trace2)
1870+
# trace2 = dict(
1871+
# #x=[tasks[0]['x0'], tasks[0]['x0']],
1872+
# x=[2, 6],
1873+
# y=[4, 2],
1874+
# name='asdf',
1875+
# visible='legendonly',
1876+
# marker=dict(
1877+
# size=10,
1878+
# color='rgb(25, 50, 150)'),
1879+
# showlegend=True
1880+
# )
1881+
# data.append(trace2)
18761882

18771883
layout = dict(
18781884
title=title,
@@ -2049,206 +2055,69 @@ def create_gantt(df, colors=None, index_col=None, show_colorbar=False,
20492055
py.iplot(fig, filename='dictioanry colors', world_readable=True)
20502056
```
20512057
"""
2052-
plotly_scales = {'Greys': ['rgb(0,0,0)', 'rgb(255,255,255)'],
2053-
'YlGnBu': ['rgb(8,29,88)', 'rgb(255,255,217)'],
2054-
'Greens': ['rgb(0,68,27)', 'rgb(247,252,245)'],
2055-
'YlOrRd': ['rgb(128,0,38)', 'rgb(255,255,204)'],
2056-
'Bluered': ['rgb(0,0,255)', 'rgb(255,0,0)'],
2057-
'RdBu': ['rgb(5,10,172)', 'rgb(178,10,28)'],
2058-
'Reds': ['rgb(220,220,220)', 'rgb(178,10,28)'],
2059-
'Blues': ['rgb(5,10,172)', 'rgb(220,220,220)'],
2060-
'Picnic': ['rgb(0,0,255)', 'rgb(255,0,0)'],
2061-
'Rainbow': ['rgb(150,0,90)', 'rgb(255,0,0)'],
2062-
'Portland': ['rgb(12,51,131)', 'rgb(217,30,30)'],
2063-
'Jet': ['rgb(0,0,131)', 'rgb(128,0,0)'],
2064-
'Hot': ['rgb(0,0,0)', 'rgb(255,255,255)'],
2065-
'Blackbody': ['rgb(0,0,0)', 'rgb(160,200,255)'],
2066-
'Earth': ['rgb(0,0,130)', 'rgb(255,255,255)'],
2067-
'Electric': ['rgb(0,0,0)', 'rgb(255,250,220)'],
2068-
'Viridis': ['rgb(68,1,84)', 'rgb(253,231,37)']}
2069-
20702058
# validate gantt input data
20712059
chart = FigureFactory._validate_gantt(df)
20722060

20732061
if index_col:
20742062
if index_col not in chart[0]:
2075-
raise exceptions.PlotlyError("In order to use an indexing "
2076-
"column and assign colors to "
2077-
"the values of the index, you "
2078-
"must choose an actual column "
2079-
"name in the dataframe or key "
2080-
"if a list of dictionaries is "
2081-
"being used.")
2063+
raise exceptions.PlotlyError(
2064+
"In order to use an indexing column and assign colors to "
2065+
"the values of the index, you must choose an actual "
2066+
"column name in the dataframe or key if a list of "
2067+
"dictionaries is being used.")
2068+
20822069
# validate gantt index column
20832070
index_list = []
20842071
for dictionary in chart:
20852072
index_list.append(dictionary[index_col])
20862073
FigureFactory._validate_index(index_list)
20872074

20882075
# Validate colors
2089-
if colors is None:
2090-
colors = DEFAULT_PLOTLY_COLORS
2091-
2092-
if isinstance(colors, str):
2093-
if colors in plotly_scales:
2094-
colors = plotly_scales[colors]
2095-
2096-
elif 'rgb' in colors:
2097-
colors = FigureFactory._unlabel_rgb(colors)
2098-
for value in colors:
2099-
if value > 255.0:
2100-
raise exceptions.PlotlyError("Whoops! The "
2101-
"elements in your "
2102-
"rgb colors "
2103-
"tuples cannot "
2104-
"exceed 255.0.")
2105-
colors = FigureFactory._label_rgb(colors)
2106-
2107-
# put colors in list
2108-
colors_list = []
2109-
colors_list.append(colors)
2110-
colors = colors_list
2111-
2112-
elif '#' in colors:
2113-
colors = FigureFactory._hex_to_rgb(colors)
2114-
colors = FigureFactory._label_rgb(colors)
2115-
2116-
# put colors in list
2117-
colors_list = []
2118-
colors_list.append(colors)
2119-
colors = colors_list
2120-
2121-
else:
2122-
scale_keys = list(plotly_scales.keys())
2123-
raise exceptions.PlotlyError("If you input a string "
2124-
"for 'colors', it must "
2125-
"either be a Plotly "
2126-
"colorscale, an 'rgb' "
2127-
"color or a hex color."
2128-
"Valid plotly colorscale "
2129-
"names are {}".format(scale_keys))
2130-
elif isinstance(colors, tuple):
2131-
for value in colors:
2132-
if value > 1.0:
2133-
raise exceptions.PlotlyError("Whoops! The "
2134-
"elements in "
2135-
"your colors "
2136-
"tuples cannot "
2137-
"exceed 1.0.")
2138-
2139-
colors_list = []
2140-
colors_list.append(colors)
2141-
colors = colors_list
2142-
2143-
colors = FigureFactory._convert_to_RGB_255(colors)
2144-
colors = FigureFactory._label_rgb(colors)
2145-
2146-
elif isinstance(colors, list):
2147-
new_colormap = []
2148-
for color in colors:
2149-
if 'rgb' in color:
2150-
color = FigureFactory._unlabel_rgb(color)
2151-
2152-
for value in color:
2153-
if value > 255.0:
2154-
raise exceptions.PlotlyError("Whoops! The "
2155-
"elements in your "
2156-
"rgb colors "
2157-
"tuples cannot "
2158-
"exceed 255.0.")
2159-
2160-
color = FigureFactory._label_rgb(color)
2161-
new_colormap.append(color)
2162-
elif '#' in color:
2163-
color = FigureFactory._hex_to_rgb(color)
2164-
color = FigureFactory._label_rgb(color)
2165-
new_colormap.append(color)
2166-
elif isinstance(color, tuple):
2167-
for value in color:
2168-
if value > 1.0:
2169-
raise exceptions.PlotlyError("Whoops! The "
2170-
"elements in "
2171-
"your colors "
2172-
"tuples cannot "
2173-
"exceed 1.0.")
2174-
color = FigureFactory._convert_to_RGB_255(color)
2175-
color = FigureFactory._label_rgb(color)
2176-
new_colormap.append(color)
2177-
colors = new_colormap
2178-
2179-
elif isinstance(colors, dict):
2180-
for name in colors:
2181-
if 'rgb' in colors[name]:
2182-
color = FigureFactory._unlabel_rgb(colors[name])
2183-
for value in color:
2184-
if value > 255.0:
2185-
raise exceptions.PlotlyError("Whoops! The "
2186-
"elements in your "
2187-
"rgb colors "
2188-
"tuples cannot "
2189-
"exceed 255.0.")
2190-
2191-
elif '#' in colors[name]:
2192-
color = FigureFactory._hex_to_rgb(colors[name])
2193-
color = FigureFactory._label_rgb(color)
2194-
colors[name] = color
2195-
2196-
elif isinstance(colors[name], tuple):
2197-
for value in colors[name]:
2198-
if value > 1.0:
2199-
raise exceptions.PlotlyError("Whoops! The "
2200-
"elements in "
2201-
"your colors "
2202-
"tuples cannot "
2203-
"exceed 1.0.")
2204-
color = FigureFactory._convert_to_RGB_255(colors[name])
2205-
color = FigureFactory._label_rgb(color)
2206-
colors[name] = color
2207-
2076+
if isinstance(colors, dict):
2077+
colors = FigureFactory._validate_colors_dict(colors, 'rgb')
22082078
else:
2209-
raise exceptions.PlotlyError("You must input a valid colors. "
2210-
"Valid types include a plotly scale, "
2211-
"rgb, hex or tuple color, a list of "
2212-
"any color types, or a dictionary "
2213-
"with index names each assigned "
2214-
"to a color.")
2079+
colors = FigureFactory._validate_colors(colors, 'rgb')
22152080

22162081
if reverse_colors is True:
22172082
colors.reverse()
22182083

22192084
if not index_col:
22202085
if isinstance(colors, dict):
2221-
raise exceptions.PlotlyError("Error. You have set colors to "
2222-
"a dictionary but have not "
2223-
"picked an index. An index is "
2224-
"required if you are assigning "
2225-
"colors to particular values "
2226-
"in a dictioanry.")
2227-
2228-
fig = FigureFactory._gantt(chart, colors, title, bar_width,
2229-
showgrid_x, showgrid_y, height, width,
2230-
tasks=None, task_names=None, data=None)
2086+
raise exceptions.PlotlyError(
2087+
"Error. You have set colors to a dictionary but have not "
2088+
"picked an index. An index is required if you are "
2089+
"assigning colors to particular values in a dictioanry."
2090+
)
2091+
fig = FigureFactory._gantt(
2092+
chart, colors, title, bar_width, showgrid_x, showgrid_y,
2093+
height, width, tasks=None, task_names=None, data=None
2094+
)
22312095
return fig
2232-
22332096
else:
22342097
if not isinstance(colors, dict):
2235-
fig = FigureFactory._gantt_colorscale(chart, colors,
2236-
title, index_col,
2237-
show_colorbar,
2238-
bar_width, showgrid_x,
2239-
showgrid_y, height,
2240-
width, tasks=None,
2241-
task_names=None,
2242-
data=None)
2098+
# check that colors has at least 2 colors
2099+
if len(colors) < 2:
2100+
raise exceptions.PlotlyError(
2101+
"You must use at least 2 colors in 'colors' if you "
2102+
"are using a colorscale. However only the first two "
2103+
"colors given will be used for the lower and upper "
2104+
"bounds on the colormap."
2105+
)
2106+
fig = FigureFactory._gantt_colorscale(
2107+
chart, colors, title, index_col, show_colorbar, bar_width,
2108+
showgrid_x, showgrid_y, height, width,
2109+
tasks=None, task_names=None, data=None
2110+
)
22432111
return fig
22442112
else:
2245-
fig = FigureFactory._gantt_dict(chart, colors, title,
2246-
index_col, show_colorbar,
2247-
bar_width, showgrid_x,
2248-
showgrid_y, height, width,
2249-
tasks=None, task_names=None,
2250-
data=None)
2113+
fig = FigureFactory._gantt_dict(
2114+
chart, colors, title, index_col, show_colorbar, bar_width,
2115+
showgrid_x, showgrid_y, height, width,
2116+
tasks=None, task_names=None, data=None
2117+
)
2118+
return fig
22512119

2120+
@staticmethod
22522121
def _validate_colors(colors, colortype='tuple'):
22532122
"""
22542123
Validates color(s) and returns a list of color(s) of a specified type

0 commit comments

Comments
 (0)