import numpy as np
from pandas import date_range
import bqplot.pyplot as plt
from bqplot import ColorScale

security_1 = np.cumsum(np.random.randn(150)) + 100.
security_2 = np.cumsum(np.random.randn(150)) + 100.

"""
Explanation: Introduction
The Lines object provides the following features:

Ability to plot a single set or multiple sets of y-values as a function of a set or multiple sets of x-values
Ability to style the line object in different ways, by setting different attributes such as the colors, line_style, stroke_width etc.
Ability to specify a marker at each point passed to the line. The marker can be a shape which is at the data points between which the line is interpolated and can be set through the markers attribute

The Lines object has the following attributes
| Attribute |  Description | Default Value  |
|:-:|---|:-:|
|  colors | Sets the color of each line, takes as input a list of any RGB, HEX, or HTML color name  | CATEGORY10  |
| opacities  | Controls the opacity of each line, takes as input a real number between 0 and 1  | 1.0  |
| stroke_width  | Real number which sets the width of all paths  | 2.0  |
| line_style  | Specifies whether a line is solid, dashed, dotted or both dashed and dotted  | 'solid'  |
| interpolation  | Sets the type of interpolation between two points  | 'linear'  |
| marker  | Specifies the shape of the marker inserted at each data point  | None  |
| marker_size  | Controls the size of the marker, takes as input a non-negative integer  | 64  |
|close_path| Controls whether to close the paths or not | False |
|fill| Specifies in which way the paths are filled. Can be set to one of {'none', 'bottom', 'top', 'inside'}| None |
|fill_colors| List that specifies the fill colors of each path | [] |                                                                       
| Data Attribute |  Description | Default Value  |
|x |abscissas of the data points | array([]) |
|y |ordinates of the data points | array([]) |
|color | Data according to which the Lines will be colored. Setting it to None defaults the choice of colors to the colors attribute | None |
pyplot's plot method can be used to plot lines with meaningful defaults
End of explanation
"""


fig = plt.figure(title='Security 1')
axes_options = {'x': {'label': 'Index'}, 'y': {'label': 'Price'}}
# x values default to range of values when not specified
line = plt.plot(security_1, axes_options=axes_options)
fig

"""
Explanation: Basic Line Chart
End of explanation
"""


line.colors = ['DarkOrange']

"""
Explanation: We can explore the different attributes by changing each of them for the plot above:
End of explanation
"""


# The opacity allows us to display the Line while featuring other Marks that may be on the Figure
line.opacities = [.5]

line.stroke_width = 2.5

"""
Explanation: In a similar way, we can also change any attribute after the plot has been displayed to change the plot. Run each of the cells below, and try changing the attributes to explore the different features and how they affect the plot.
End of explanation
"""


line.fill = 'bottom'
line.fill_opacities = [0.2]

line.line_style = 'dashed'

line.interpolation = 'basis'

"""
Explanation: To switch to an area chart, set the fill attribute, and control the look with fill_opacities and fill_colors.
End of explanation
"""


line.marker = 'triangle-down'

"""
Explanation: While a Lines plot allows the user to extract the general shape of the data being plotted, there may be a need to visualize discrete data points along with this shape. This is where the markers attribute comes in.
End of explanation
"""


# Here we define the dates we would like to use
dates = date_range(start='01-01-2007', periods=150)

fig = plt.figure(title='Time Series')
axes_options = {'x': {'label': 'Date'}, 'y': {'label': 'Security 1'}}
time_series = plt.plot(dates, security_1, 
                       axes_options=axes_options)
fig

"""
Explanation: The marker attributes accepts the values square, circle, cross, diamond, square, triangle-down, triangle-up, arrow, rectangle, ellipse. Try changing the string above and re-running the cell to see how each marker type looks.
Plotting a Time-Series
The DateScale allows us to plot time series as a Lines plot conveniently with most date formats.
End of explanation
"""


dates_new = date_range(start='06-01-2007', periods=150)

"""
Explanation: Plotting multiples sets of data
The Lines mark allows the user to plot multiple y-values for a single x-value. This can be done by passing an ndarray or a list of the different y-values as the y-attribute of the Lines as shown below.
End of explanation
"""


fig = plt.figure()
axes_options = {'x': {'label': 'Date'}, 'y': {'label': 'Price'}}
line = plt.plot(dates, [security_1, security_2], 
                labels=['Security 1', 'Security 2'],
                axes_options=axes_options,
                display_legend=True)
fig

"""
Explanation: We pass each data set as an element of a list
End of explanation
"""


line.x, line.y = [dates, dates_new], [security_1, security_2]

"""
Explanation: Similarly, we can also pass multiple x-values for multiple sets of y-values
End of explanation
"""


fig = plt.figure()
axes_options = {'x': {'label': 'Date'}, 
                'y': {'label': 'Security 1'},
                'color' : {'visible': False}}
# add a custom color scale to color the lines
plt.scales(scales={'color': ColorScale(colors=['Red', 'Green'])})

dates_color = date_range(start='06-01-2007', periods=150)

securities = 100. + np.cumsum(np.random.randn(150, 10), axis=0)
# we generate 10 random price series and 10 random positions
positions = np.random.randint(0, 2, size=10)

# We pass the color scale and the color data to the plot method
line = plt.plot(dates_color, securities.T, color=positions, 
                axes_options=axes_options)
fig

"""
Explanation: Coloring Lines according to data
The color attribute of a Lines mark can also be used to encode one more dimension of data. Suppose we have a portfolio of securities and we would like to color them based on whether we have bought or sold them. We can use the color attribute to encode this information.
End of explanation
"""


line.color = None

"""
Explanation: We can also reset the colors of the Line to their defaults by setting the color attribute to None.
End of explanation
"""


fig = plt.figure(animation_duration=1000)
patch = plt.plot([],[],
                 fill_colors=['orange', 'blue', 'red'],
                 fill='inside',
                 axes_options={'x': {'visible': False}, 'y': {'visible': False}},
                 stroke_width=10,
                 close_path=True,
                 display_legend=True)

patch.x = [[0, 2, 1.2, np.nan, np.nan, np.nan, np.nan], [0.5, 2.5, 1.7 , np.nan, np.nan, np.nan, np.nan], [4, 5, 6, 6, 5, 4, 3]], 
patch.y = [[0, 0, 1  , np.nan, np.nan, np.nan, np.nan], [0.5, 0.5, -0.5, np.nan, np.nan, np.nan, np.nan], [1, 1.1, 1.2, 2.3, 2.2, 2.7, 1.0]]
fig

patch.opacities = [0.1, 0.2]

patch.x = [[2, 3, 3.2, np.nan, np.nan, np.nan, np.nan], [0.5, 2.5, 1.7, np.nan, np.nan, np.nan, np.nan], [4,5,6, 6, 5, 4, 3]]

patch.close_path = False

"""
Explanation: Patches
The fill attribute of the Lines mark allows us to fill a path in different ways, while the fill_colors attribute lets us control the color of the fill
End of explanation
"""


