Skip to content

Bullet Chart FF #872

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

Merged
merged 22 commits into from
Nov 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [2.2.2] - TBA
### Added
- bullet chart figure factory. Call `help(plotly.figure_factory.create_bullet)` for examples and how to get started making bullet charts with the API.

## [2.2.1] - 2017-10-26
### Fixed
- presentation objects now added to setup.py
Expand Down
46 changes: 36 additions & 10 deletions plotly/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,21 +442,37 @@ def make_colorscale(colors, scale=None):
return colorscale


def find_intermediate_color(lowcolor, highcolor, intermed):
def find_intermediate_color(lowcolor, highcolor, intermed, colortype='tuple'):
"""
Returns the color at a given distance between two colors

This function takes two color tuples, where each element is between 0
and 1, along with a value 0 < intermed < 1 and returns a color that is
intermed-percent from lowcolor to highcolor
intermed-percent from lowcolor to highcolor. If colortype is set to 'rgb',
the function will automatically convert the rgb type to a tuple, find the
intermediate color and return it as an rgb color.
"""
if colortype == 'rgb':
# convert to tuple color, eg. (1, 0.45, 0.7)
lowcolor = unlabel_rgb(lowcolor)
highcolor = unlabel_rgb(highcolor)

diff_0 = float(highcolor[0] - lowcolor[0])
diff_1 = float(highcolor[1] - lowcolor[1])
diff_2 = float(highcolor[2] - lowcolor[2])

return (lowcolor[0] + intermed * diff_0,
lowcolor[1] + intermed * diff_1,
lowcolor[2] + intermed * diff_2)
inter_med_tuple = (
lowcolor[0] + intermed * diff_0,
lowcolor[1] + intermed * diff_1,
lowcolor[2] + intermed * diff_2
)

if colortype == 'rgb':
# back to an rgb string, e.g. rgb(30, 20, 10)
inter_med_rgb = label_rgb(inter_med_tuple)
return inter_med_rgb

return inter_med_tuple


def unconvert_from_RGB_255(colors):
Expand Down Expand Up @@ -498,29 +514,39 @@ def convert_to_RGB_255(colors):
return (rgb_components[0], rgb_components[1], rgb_components[2])


def n_colors(lowcolor, highcolor, n_colors):
def n_colors(lowcolor, highcolor, n_colors, colortype='tuple'):
"""
Splits a low and high color into a list of n_colors colors in it

Accepts two color tuples and returns a list of n_colors colors
which form the intermediate colors between lowcolor and highcolor
from linearly interpolating through RGB space
from linearly interpolating through RGB space. If colortype is 'rgb'
the function will return a list of colors in the same form.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a typo? i thought tuple returned a list (well, a tuple) of colors?

Copy link
Contributor Author

@Kully Kully Nov 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see how there's confusion.

If colortype='tuple', lowcolor and highcolor must be what I have called tuple(-like) colors i.e. tuples of the form (a,b,c) where a, b, c are between 0 and 1, where (0,0,0) == rgb(0,0,0), etc.

If the input islowcolor=(0,0,0), highcolor=(1,1,1), n_colors=3 then the output is:

[(0, 0, 0), (0.5, 0.5, 0.5), (1, 1, 1)]

If colortype='rgb', then the list above will contain the rgb equivalent colors in a list

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this function will error if you input tuple-colors and colortype is set to rgb. Another thing to fix.

"""
if colortype == 'rgb':
# convert to tuple
lowcolor = unlabel_rgb(lowcolor)
highcolor = unlabel_rgb(highcolor)

diff_0 = float(highcolor[0] - lowcolor[0])
incr_0 = diff_0/(n_colors - 1)
diff_1 = float(highcolor[1] - lowcolor[1])
incr_1 = diff_1/(n_colors - 1)
diff_2 = float(highcolor[2] - lowcolor[2])
incr_2 = diff_2/(n_colors - 1)
color_tuples = []
list_of_colors = []

for index in range(n_colors):
new_tuple = (lowcolor[0] + (index * incr_0),
lowcolor[1] + (index * incr_1),
lowcolor[2] + (index * incr_2))
color_tuples.append(new_tuple)
list_of_colors.append(new_tuple)

if colortype == 'rgb':
# back to an rgb string
list_of_colors = color_parser(list_of_colors, label_rgb)

return color_tuples
return list_of_colors


def label_rgb(colors):
Expand Down
1 change: 1 addition & 0 deletions plotly/figure_factory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from plotly.figure_factory._2d_density import create_2d_density
from plotly.figure_factory._annotated_heatmap import create_annotated_heatmap
from plotly.figure_factory._bullet import create_bullet
from plotly.figure_factory._candlestick import create_candlestick
from plotly.figure_factory._dendrogram import create_dendrogram
from plotly.figure_factory._distplot import create_distplot
Expand Down
Loading