Skip to content

Commit 9e52ce4

Browse files
committed
Added dataframe all_args test for gantt//various formatting
1 parent ba65217 commit 9e52ce4

File tree

3 files changed

+166
-48
lines changed

3 files changed

+166
-48
lines changed

plotly/tests/test_core/test_tools/test_figure_factory.py

+49-48
Original file line numberDiff line numberDiff line change
@@ -1132,39 +1132,39 @@ def test_validate_gantt(self):
11321132

11331133
# validate the basic gantt inputs
11341134

1135-
df = [dict(Task='Job A',
1136-
Start='2009-02-01',
1137-
Finish='2009-08-30',
1138-
Complete='a')]
1135+
df = [{'Task': 'Job A',
1136+
'Start': '2009-02-01',
1137+
'Finish': '2009-08-30',
1138+
'Complete': 'a'}]
11391139

1140-
pattern2 = ("In order to use an indexing column and assign colors to "
1141-
"the values of the index, you must choose an actual "
1142-
"column name in the dataframe or key if a list of "
1143-
"dictionaries is being used.")
1140+
pattern2 = ('In order to use an indexing column and assign colors to '
1141+
'the values of the index, you must choose an actual '
1142+
'column name in the dataframe or key if a list of '
1143+
'dictionaries is being used.')
11441144

11451145
self.assertRaisesRegexp(PlotlyError, pattern2,
11461146
tls.FigureFactory.create_gantt,
11471147
df, index_col='foo')
11481148

11491149
df = 'foo'
11501150

1151-
pattern3 = ("You must input either a dataframe or a list of "
1152-
"dictionaries.")
1151+
pattern3 = ('You must input either a dataframe or a list of '
1152+
'dictionaries.')
11531153

11541154
self.assertRaisesRegexp(PlotlyError, pattern3,
11551155
tls.FigureFactory.create_gantt, df)
11561156

11571157
df = []
11581158

1159-
pattern4 = ("Your list is empty. It must contain at least one "
1160-
"dictionary.")
1159+
pattern4 = ('Your list is empty. It must contain at least one '
1160+
'dictionary.')
11611161

11621162
self.assertRaisesRegexp(PlotlyError, pattern4,
11631163
tls.FigureFactory.create_gantt, df)
11641164

11651165
df = ['foo']
11661166

1167-
pattern5 = ("Your list must only include dictionaries.")
1167+
pattern5 = ('Your list must only include dictionaries.')
11681168

11691169
self.assertRaisesRegexp(PlotlyError, pattern5,
11701170
tls.FigureFactory.create_gantt, df)
@@ -1173,27 +1173,27 @@ def test_gantt_index(self):
11731173

11741174
# validate the index used for gantt
11751175

1176-
df = [dict(Task='Job A',
1177-
Start='2009-02-01',
1178-
Finish='2009-08-30',
1179-
Complete=50)]
1176+
df = [{'Task': 'Job A',
1177+
'Start': '2009-02-01',
1178+
'Finish': '2009-08-30',
1179+
'Complete': 50}]
11801180

1181-
pattern = ("In order to use an indexing column and assign colors to "
1182-
"the values of the index, you must choose an actual "
1183-
"column name in the dataframe or key if a list of "
1184-
"dictionaries is being used.")
1181+
pattern = ('In order to use an indexing column and assign colors to '
1182+
'the values of the index, you must choose an actual '
1183+
'column name in the dataframe or key if a list of '
1184+
'dictionaries is being used.')
11851185

11861186
self.assertRaisesRegexp(PlotlyError, pattern,
11871187
tls.FigureFactory.create_gantt,
11881188
df, index_col='foo')
11891189

1190-
df = [dict(Task='Job A', Start='2009-02-01',
1191-
Finish='2009-08-30', Complete='a'),
1192-
dict(Task='Job A', Start='2009-02-01',
1193-
Finish='2009-08-30', Complete=50)]
1190+
df = [{'Task': 'Job A', 'Start': '2009-02-01',
1191+
'Finish': '2009-08-30', 'Complete': 'a'},
1192+
{'Task': 'Job A', 'Start': '2009-02-01',
1193+
'Finish': '2009-08-30', 'Complete': 50}]
11941194

1195-
pattern2 = ("Error in indexing column. Make sure all entries of each "
1196-
"column are all numbers or all strings.")
1195+
pattern2 = ('Error in indexing column. Make sure all entries of each '
1196+
'column are all numbers or all strings.')
11971197

11981198
self.assertRaisesRegexp(PlotlyError, pattern2,
11991199
tls.FigureFactory.create_gantt,
@@ -1203,13 +1203,13 @@ def test_gantt_validate_colors(self):
12031203

12041204
# validate the gantt colors variable
12051205

1206-
df = [dict(Task='Job A', Start='2009-02-01',
1207-
Finish='2009-08-30', Complete=75, Resource='A'),
1208-
dict(Task='Job B', Start='2009-02-01',
1209-
Finish='2009-08-30', Complete=50, Resource='B')]
1206+
df = [{'Task': 'Job A', 'Start': '2009-02-01',
1207+
'Finish': '2009-08-30', 'Complete': 75, 'Resource': 'A'},
1208+
{'Task': 'Job B', 'Start': '2009-02-01',
1209+
'Finish': '2009-08-30', 'Complete': 50, 'Resource': 'B'}]
12101210

1211-
pattern = ("Whoops! The elements in your rgb colors tuples cannot "
1212-
"exceed 255.0.")
1211+
pattern = ('Whoops! The elements in your rgb colors tuples cannot '
1212+
'exceed 255.0.')
12131213

12141214
self.assertRaisesRegexp(PlotlyError, pattern,
12151215
tls.FigureFactory.create_gantt, df,
@@ -1218,8 +1218,8 @@ def test_gantt_validate_colors(self):
12181218
self.assertRaises(PlotlyError, tls.FigureFactory.create_gantt,
12191219
df, index_col='Complete', colors='foo')
12201220

1221-
pattern2 = ("Whoops! The elements in your colors tuples cannot "
1222-
"exceed 1.0.")
1221+
pattern2 = ('Whoops! The elements in your colors tuples cannot '
1222+
'exceed 1.0.')
12231223

12241224
self.assertRaisesRegexp(PlotlyError, pattern2,
12251225
tls.FigureFactory.create_gantt, df,
@@ -1229,8 +1229,8 @@ def test_gantt_validate_colors(self):
12291229
# values in the index column
12301230
colors_dict = {75: 'rgb(1, 2, 3)'}
12311231

1232-
pattern3 = ("If you are using colors as a dictionary, all of its "
1233-
"keys must be all the values in the index column.")
1232+
pattern3 = ('If you are using colors as a dictionary, all of its '
1233+
'keys must be all the values in the index column.')
12341234

12351235
self.assertRaisesRegexp(PlotlyError, pattern3,
12361236
tls.FigureFactory.create_gantt, df,
@@ -1239,9 +1239,9 @@ def test_gantt_validate_colors(self):
12391239
# check: index is set if colors is a dictionary
12401240
colors_dict_good = {50: 'rgb(1, 2, 3)', 75: 'rgb(5, 10, 15)'}
12411241

1242-
pattern4 = ("Error. You have set colors to a dictionary but have not "
1243-
"picked an index. An index is required if you are "
1244-
"assigning colors to particular values in a dictioanry.")
1242+
pattern4 = ('Error. You have set colors to a dictionary but have not '
1243+
'picked an index. An index is required if you are '
1244+
'assigning colors to particular values in a dictioanry.')
12451245

12461246
self.assertRaisesRegexp(PlotlyError, pattern4,
12471247
tls.FigureFactory.create_gantt, df,
@@ -1272,14 +1272,15 @@ def test_gantt_validate_colors(self):
12721272
def test_gantt_all_args(self):
12731273

12741274
# check if gantt chart matches with expected output
1275-
df = [dict(Task="Run",
1276-
Start='2010-01-01',
1277-
Finish='2011-02-02',
1278-
Complete=0),
1279-
dict(Task="Fast",
1280-
Start='2011-01-01',
1281-
Finish='2012-06-05',
1282-
Complete=25)]
1275+
1276+
df = [{'Task': 'Run',
1277+
'Start': '2010-01-01',
1278+
'Finish': '2011-02-02',
1279+
'Complete': 0},
1280+
{'Task': 'Fast',
1281+
'Start': '2011-01-01',
1282+
'Finish': '2012-06-05',
1283+
'Complete': 25}]
12831284

12841285
test_gantt_chart = tls.FigureFactory.create_gantt(
12851286
df, colors='Blues', index_col='Complete', reverse_colors=True,

plotly/tests/test_optional/test_figure_factory.py

+89
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,95 @@ def test_df_dataframe(self):
12751275
df1 = pd.DataFrame([[2, 'Apple']], columns=['Numbers', 'Fruit'])
12761276
self.assertRaises(PlotlyError, tls.FigureFactory.create_gantt, df1)
12771277

1278+
def test_df_dataframe_all_args(self):
1279+
1280+
# check if gantt chart matches with expected output
1281+
1282+
df = pd.DataFrame([['Job A', '2009-01-01', '2009-02-30', 'A'],
1283+
['Job B', '2009-03-05', '2009-04-15', 'B']],
1284+
columns=['Task', 'Start', 'Finish', 'Resource'])
1285+
1286+
test_gantt_chart = tls.FigureFactory.create_gantt(
1287+
df, colors='Blues', index_col='Resource'
1288+
)
1289+
1290+
exp_gantt_chart = {
1291+
'data': [{'marker': {'color': 'white'},
1292+
'name': '',
1293+
'x': ['2009-01-01', '2009-02-30'],
1294+
'y': [0, 0]},
1295+
{'marker': {'color': 'white'},
1296+
'name': '',
1297+
'x': ['2009-03-05', '2009-04-15'],
1298+
'y': [1, 1]}],
1299+
'layout': {'height': 600,
1300+
'hovermode': 'closest',
1301+
'shapes': [{'fillcolor': 'rgb(5.0, 10.0, 172.0)',
1302+
'line': {'width': 0},
1303+
'opacity': 1,
1304+
'type': 'rect',
1305+
'x0': '2009-01-01',
1306+
'x1': '2009-02-30',
1307+
'xref': 'x',
1308+
'y0': -0.2,
1309+
'y1': 0.2,
1310+
'yref': 'y'},
1311+
{'fillcolor': 'rgb(220.0, 220.0, 220.0)',
1312+
'line': {'width': 0},
1313+
'opacity': 1,
1314+
'type': 'rect',
1315+
'x0': '2009-03-05',
1316+
'x1': '2009-04-15',
1317+
'xref': 'x',
1318+
'y0': 0.8,
1319+
'y1': 1.2,
1320+
'yref': 'y'}],
1321+
'showlegend': False,
1322+
'title': 'Gantt Chart',
1323+
'width': 900,
1324+
'xaxis': {'rangeselector': {'buttons': [
1325+
{'count': 7,
1326+
'label': '1w',
1327+
'step': 'day',
1328+
'stepmode': 'backward'},
1329+
{'count': 1,
1330+
'label': '1m',
1331+
'step': 'month',
1332+
'stepmode': 'backward'},
1333+
{'count': 6,
1334+
'label': '6m',
1335+
'step': 'month',
1336+
'stepmode': 'backward'},
1337+
{'count': 1,
1338+
'label': 'YTD',
1339+
'step': 'year',
1340+
'stepmode': 'todate'},
1341+
{'count': 1,
1342+
'label': '1y',
1343+
'step': 'year',
1344+
'stepmode': 'backward'},
1345+
{'step': 'all'}
1346+
]},
1347+
'showgrid': False,
1348+
'type': 'date',
1349+
'zeroline': False},
1350+
'yaxis': {'autorange': False,
1351+
'range': [-1, 3],
1352+
'showgrid': False,
1353+
'ticktext': ['Job A', 'Job B'],
1354+
'tickvals': [0, 1],
1355+
'zeroline': False}}
1356+
}
1357+
1358+
self.assertEqual(test_gantt_chart['data'][0],
1359+
exp_gantt_chart['data'][0])
1360+
1361+
self.assertEqual(test_gantt_chart['data'][1],
1362+
exp_gantt_chart['data'][1])
1363+
1364+
self.assertEqual(test_gantt_chart['layout'],
1365+
exp_gantt_chart['layout'])
1366+
12781367

12791368
class TestViolin(NumpyTestUtilsMixin, TestCase):
12801369

plotly/tools.py

+28
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,34 @@ def create_gantt(df, colors=None, index_col=None, show_colorbar=False,
20932093
# Plot the data
20942094
py.iplot(fig, filename='dictioanry colors', world_readable=True)
20952095
```
2096+
2097+
Example 5: Use a pandas dataframe
2098+
```
2099+
import plotly.plotly as py
2100+
from plotly.tools import FigureFactory as FF
2101+
2102+
# Make data as a dataframe
2103+
df = [{'Task': 'Run',
2104+
'Start': '2010-01-01',
2105+
'Finish': '2011-02-02',
2106+
'Complete': 10},
2107+
{'Task': 'Fast',
2108+
'Start': '2011-01-01',
2109+
'Finish': '2012-06-05',
2110+
'Complete': 55},
2111+
{'Task': 'Eat',
2112+
'Start': '2012-01-05',
2113+
'Finish': '2013-07-05',
2114+
'Complete': 94}]
2115+
2116+
# Create a figure with Plotly colorscale
2117+
fig = FF.create_gantt(df, colors='Blues', index_col='Complete',
2118+
show_colorbar=True, bar_width=0.5,
2119+
showgrid_x=True, showgrid_y=True)
2120+
2121+
# Plot the data
2122+
py.iplot(fig, filename='data with dataframe', world_readable=True)
2123+
```
20962124
"""
20972125
# validate gantt input data
20982126
chart = FigureFactory._validate_gantt(df)

0 commit comments

Comments
 (0)