def create_graph(locID, height='520px', width='100%'): df = getAllData(location_id=locID) latitude, longiude = getLatLong(locID) months = df['month'].to_list() prcp = df['prcp'].to_list() avgTemp = df['tave'].to_list() minTemp = df['tmin'].to_list() maxTemp = df['tmax'].to_list() avgTemp = [(x * 9/5) + 32 for x in avgTemp] minTemp = [(x * 9/5) + 32 for x in minTemp] maxTemp = [(x * 9/5) + 32 for x in maxTemp] prcp = [(x/25.4) for x in prcp] for i in range(len(prcp)): if i != 0: prcp[i] = prcp[i] + prcp[i-1] data = [go.Scatter(x=months, y=avgTemp, name="Avg Temperature"), go.Scatter(x=months, y=minTemp, name="Min Temperature"), go.Scatter(x=months, y=maxTemp, name="Max Temperature")] layout = { 'title': 'Tepmerature Data for {0}, {1}'.format(latitude, longiude), 'xaxis': {'title': 'Time'}, 'yaxis': {'title': 'Temperature (F)'} } figure = {'data': data, 'layout': layout} temperature_plot = PlotlyView(figure, height=height, width=width) data = [go.Scatter(x=months, y=prcp, name="Precipitation")] layout = { 'title': 'Cumulative Precipitation for {0}, {1}'.format(latitude, longiude), 'xaxis': {'title': 'Time'}, 'yaxis': {'title': 'Precipitation (in)'} } figure = {'data': data, 'layout': layout} precipitation_plot = PlotlyView(figure, height=height, width=width) return temperature_plot, precipitation_plot
def create_revenue_piechart(rev_scenario): height = '520px' width = '100%' """ Generates a plotly view of projects """ # Get objects from database Session = app.get_persistent_store_database('primary_db', as_sessionmaker=True) session = Session() revenue_list = session.query(Revenue).all() df = pd.DataFrame(columns=['Scenario', 'Source', 'Monetary Value', 'Year']) for entry in revenue_list: if entry.scenario == rev_scenario: df = df.append({'Scenario': entry.scenario, 'Source': entry.revenue_source, 'Monetary Value': int(json.loads(entry.monetary_Value)), 'Year': entry.year}, ignore_index=True) # Build up Plotly plot piechart_go = go.Figure(go.Pie( name="", values=df["Monetary Value"], labels=df["Source"], text=df["Source"], customdata=df["Year"], hovertemplate="Source: %{label} <br> Scenario: %{text} <br> Year: %{customdata} <br>Revenue: %{value}" )) piechart_plot = PlotlyView(piechart_go, divid="pie-plot-rev-div", height=height, width=width) session.close() return piechart_plot
def create_capital_costs_bargraph(height='520 px', width='100%'): """ Generates a plotly view of projects """ # Get objects from database Session = app.get_persistent_store_database('primary_db', as_sessionmaker=True) session = Session() projects = session.query(Project).all() df = pd.DataFrame(columns=['Construction Year', 'Projected Cost', 'Facility ID', 'Project', 'Category']) for project in projects: if project.recur_checkbox_val == "true": y = 0 for x in range(len(project.const_cost) - 1): df = df.append({'Construction Year': int(project.const_year) + y, 'Projected Cost': int(json.loads(project.const_cost[x + 1])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category}, ignore_index=True) y = y + 1 else: df = df.append( {'Construction Year': int(project.const_year), 'Projected Cost': int(json.loads(project.const_cost[0])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category}, ignore_index=True) # Build up Plotly plot bargraph_px = px.bar( df, hover_data=["Facility ID", "Category", "Project", "Projected Cost"], x="Construction Year", y="Projected Cost", color='Category', color_discrete_map={ "Water": "#056eb7", "Wastewater": "#b3c935", "Existing Debt": "#001f5b", "Facilities": "#074768", "Golf": "#ac162c", "Transportation": "#232525", "Storm": "#F78F07"} # colors="black" # name='Category', # title="Future Project Costs" ) bargraph_px.update_layout( yaxis=dict(title='Construction Cost (USD)',), xaxis=dict(title='Construction Year'), legend=dict(title='Category') ) bargraph_plot = PlotlyView(bargraph_px, height=height, width=width) session.close() return bargraph_plot
def create_revenue_vs_requirements_piechart(height='520 px', width='100%'): """ Generates a plotly view of projects """ # Get objects from database Session = app.get_persistent_store_database('primary_db', as_sessionmaker=True) session = Session() projects = session.query(Project).all() dfrevreq = pd.DataFrame(columns=['Construction Year', 'Projected Cost', 'Facility ID', 'Project', 'Category']) for project in projects: if project.debt_checkbox_val == "true": z = 1 for c in range(len(project.const_cost) - 1): dfrevreq = dfrevreq.append({'Construction Year': int(project.const_year) + z, 'Projected Cost': int(json.loads(project.const_cost[c + 1])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category}, ignore_index=True) z = z + 1 elif project.recur_checkbox_val == "true": y = 0 for x in range(len(project.const_cost) - 1): dfrevreq = dfrevreq.append({'Construction Year': int(project.const_year) + y, 'Projected Cost': int(json.loads(project.const_cost[x + 1])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category}, ignore_index=True) y = y + 1 else: dfrevreq = dfrevreq.append( {'Construction Year': int(project.const_year), 'Projected Cost': int(json.loads(project.const_cost[0])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category}, ignore_index=True) print("cost data ready") # Build up Plotly plot piechart_px = px.pie( df, values="Projected Cost", labels="Category", names="Category", hovertemplate=["Facility ID", "Project", "Construction Year", "Cost"], color="Category", color_discrete_map={ "Water": "#056eb7", "Wastewater": "#b3c935", "Existing Debt": "#001f5b", "Facilities": "#074768", "Golf": "#ac162c", "Transportation": "#232525", "Storm": "#F78F07"} ) print("piechart done") piechart_plot = PlotlyView(piechart_px, height=height, width=width) print("Plot rendered") session.close() return piechart_plot
def cq_event_plot(event_id, sub_event, height='520px', width='100%'): # Build up Plotly plot time, flow, concentration, segments = get_conc_flow_seg(event_id) if len(segments) == 0: d = {} d['start'] = 0 d['end'] = len(flow) segments.append(d) if sub_event >= len(segments): sub_event = len(segments) - 1 if sub_event < 0: sub_event = 0 event_flow = flow[segments[sub_event]['start']:segments[sub_event]['end']] event_concentration = concentration[ segments[sub_event]['start']:segments[sub_event]['end']] hysteresis_go = go.Scatter( x=event_flow, y=event_concentration, mode='lines', name='Hysteresis', line=dict( width=10, color='#1f77b4', ), ) data = [hysteresis_go] layout = dict( width=400, height=400, autosize=False, title='Hysteresis for event {0}'.format(str(sub_event)), scene=dict(xaxis=dict(gridcolor='rgb(255, 255, 255)', zerolinecolor='rgb(255, 255, 255)', showbackground=True, backgroundcolor='rgb(230, 230,230)', title='Time (T)'), yaxis=dict(gridcolor='rgb(255, 255, 255)', zerolinecolor='rgb(255, 255, 255)', showbackground=True, backgroundcolor='rgb(230, 230,230)', title='Discharge (Q)'), aspectratio=dict(x=1, y=1), aspectmode='manual'), ) figure = {'data': data, 'layout': layout} hydrograph_plot = PlotlyView(figure, height='200px', width='100%') return hydrograph_plot
def create_revenue_vs_requirements_sunburst(height='520 px', width='100%'): """ Generates a plotly view of projects """ # Get objects from database Session = app.get_persistent_store_database('primary_db', as_sessionmaker=True) session = Session() projects = session.query(Project).all() dfrevreq = pd.DataFrame(columns=['Construction Year', 'Projected Cost', 'Facility ID', 'Project', 'Category']) for project in projects: if project.debt_checkbox_val == "true": z = 1 for c in range(len(project.const_cost) - 1): dfrevreq = dfrevreq.append({'Construction Year': int(project.const_year) + z, 'Projected Cost': float(json.loads(project.const_cost[c + 1])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category}, ignore_index=True) z = z + 1 elif project.recur_checkbox_val == "true": y = 0 for x in range(len(project.const_cost) - 1): dfrevreq = dfrevreq.append({'Construction Year': int(project.const_year) + y, 'Projected Cost': float(json.loads(project.const_cost[x + 1])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category}, ignore_index=True) y = y + 1 else: dfrevreq = dfrevreq.append( {'Construction Year': int(project.const_year), 'Projected Cost': float(json.loads(project.const_cost[0])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category}, ignore_index=True) # Build up Plotly plot sunburst_px = px.sunburst( dfrevreq, path=['Category', 'Facility ID','Project'], values='Projected Cost', color='Category', labels=dfrevreq["Category"], # text=df["Project"], # customdata=df["Construction Year"], # hovertemplate="Facility ID: %{label} <br>Project: %{path[2]} <br>Construction Year: %{customdata} <br>Cost: %{value}" ) sunburst_plot = PlotlyView(sunburst_px, height=height, width=width) session.close() return sunburst_plot
def create_revenue_sunburst(height='520 px', width='100%'): """ Generates a plotly view of projects """ # Get objects from database Session = app.get_persistent_store_database('primary_db', as_sessionmaker=True) session = Session() revenue_list = session.query(Revenue).all() dfhigh = pd.DataFrame(columns=['Scenario', 'Source', 'Monetary Value', 'Year']) dfmed = pd.DataFrame(columns=['Scenario', 'Source', 'Monetary Value', 'Year']) dflow = pd.DataFrame(columns=['Scenario', 'Source', 'Monetary Value', 'Year']) for entry in revenue_list: if entry.scenario == "high": dfhigh = dfhigh.append({'Scenario': entry.scenario, 'Source': entry.revenue_source, 'Monetary Value': int(json.loads(entry.monetary_Value)), 'Year': entry.year}, ignore_index=True) elif entry.scenario == "medium": dfmed = dfmed.append({'Scenario': entry.scenario, 'Source': entry.revenue_source, 'Monetary Value': int(json.loads(entry.monetary_Value)), 'Year': entry.year}, ignore_index=True) elif entry.scenario == "low": dflow = dflow.append({'Scenario': entry.scenario, 'Source': entry.revenue_source, 'Monetary Value': int(json.loads(entry.monetary_Value)), 'Year': entry.year}, ignore_index=True) # Build up Plotly plot sunburst_px = px.sunburst( dfmed, path=['Source', 'Year','Monetary Value'], values='Monetary Value', color='Source', labels=dfmed["Source"], # text=df["Project"], # customdata=df["Construction Year"], # hovertemplate="Facility ID: %{label} <br>Project: %{path[2]} <br>Construction Year: %{customdata} <br>Cost: %{value}" ) sunburst_plot = PlotlyView(sunburst_px, divid="sun-plot-rev-div",height=height, width=width) session.close() return sunburst_plot
def create_capital_costs_sunburst(height='520 px', width='100%'): """ Generates a plotly view of projects """ # Get objects from database Session = app.get_persistent_store_database('primary_db', as_sessionmaker=True) session = Session() projects = session.query(Project).all() df = pd.DataFrame(columns=['Construction Year', 'Projected Cost', 'Facility ID', 'Project', 'Category']) for project in projects: if project.recur_checkbox_val == "true": y = 0 for x in range(len(project.const_cost)-1): df = df.append({'Construction Year': int(project.const_year) + y, 'Projected Cost': int(json.loads(project.const_cost[x + 1])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category}, ignore_index=True) y = y+1 else: df = df.append({'Construction Year': int(project.const_year), 'Projected Cost': int(json.loads(project.const_cost[0])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category}, ignore_index=True) # Build up Plotly plot sunburst_px = px.sunburst( df, path=['Category', 'Facility ID','Project'], values='Projected Cost', color='Category', labels=df["Category"], color_discrete_map={ "Water": "#056eb7", "Wastewater": "#b3c935", "Existing Debt": "#001f5b", "Facilities": "#074768", "Golf": "#ac162c", "Transportation": "#232525", "Storm": "#F78F07"} # text=df["Project"], # customdata=df["Construction Year"], # hovertemplate="Facility ID: %{label} <br>Project: %{path[2]} <br>Construction Year: %{customdata} <br>Cost: %{value}" ) sunburst_plot = PlotlyView(sunburst_px, height=height, width=width) session.close() return sunburst_plot
def create_ozone_graph(ozone_graph_id, height='520px', width='100%'): """ Generates a plotly view of a ozone graph. """ # Get objects from database Session = app.get_persistent_store_database('sensor_db', as_sessionmaker=True) session = Session() ozone_graph = session.query(OzoneGraph).get(ozone_graph_id) ozone_points = session.query(OzonePoint).filter( OzonePoint.ozone_graph == ozone_graph, OzonePoint.time >= ozone_graph.updatets - timedelta(days=7)) time = [] ppb = [] error = [] for point in ozone_points: time.append(point.time) ppb.append(point.ppb) error.append(point.std) # Build up Plotly plot ozone_graph_go = go.Scatter( x=time, y=ppb, name='Ozone Graph for Sensor {0}'.format(ozone_graph_id), mode='markers', marker={ 'color': '#0080ff', 'size': 10 }, ) data = [ozone_graph_go] layout = { 'title': 'Ozone Graph for Sensor {0}'.format(ozone_graph_id), 'xaxis': { 'title': 'Time', 'range': [max(time) - timedelta(days=7), max(time)] }, 'yaxis': { 'title': 'ppb' }, } figure = {'data': data, 'layout': layout} ozone_graph_plot = PlotlyView(figure, height=height, width=width) session.close() return ozone_graph_plot
def create_temperature_graph(temperature_graph_id, height='520px', width='100%'): """ Generates a plotly view of a temperature graph. """ # Get objects from database Session = app.get_persistent_store_database('sensor_db', as_sessionmaker=True) session = Session() temperature_graph = session.query(TemperatureGraph).get( int(temperature_graph_id)) sensor = temperature_graph.sensor time = [] temp = [] for point in temperature_graph.points: time.append(point.time.hour) temp.append(point.temperature) # Build up Plotly plot temperature_graph_go = go.Scatter( x=time, y=temp, name='Temperature Graph for Sensor {0}'.format(sensor.id), line={ 'color': '#0080ff', 'width': 4, 'shape': 'spline' }, ) data = [temperature_graph_go] layout = { 'title': 'Temperature Graph for Sensor {0}'.format(sensor.id), 'xaxis': { 'title': 'Time (min)', 'range': [max(time) - timedelta(days=30), max(time)] }, 'yaxis': { 'title': 'Temp (F)' }, } figure = {'data': data, 'layout': layout} temperature_graph_plot = PlotlyView(figure, height=height, width=width) session.close() return temperature_graph_plot
def create_revenue_bargraph(rev_scenario): height = '520px' width = '100%' """ Generates a plotly view of projects """ # Get objects from database Session = app.get_persistent_store_database('primary_db', as_sessionmaker=True) session = Session() revenue_list = session.query(Revenue).all() df = pd.DataFrame(columns=['Scenario', 'Source', 'Monetary Value', 'Year']) for entry in revenue_list: if entry.scenario == rev_scenario: df = df.append({'Scenario': entry.scenario, 'Source': entry.revenue_source, 'Monetary Value': int(json.loads(entry.monetary_Value)), 'Year': entry.year}, ignore_index=True) # Build up Plotly plot bargraph_px = px.bar( df, hover_data=["Source", "Monetary Value", "Year"], x="Year", y="Monetary Value", color="Source" # title="Revenue" ) bargraph_px.update_layout( yaxis=dict(title='Revenue (USD)'), xaxis=dict(title='Year'), legend=dict(title='Source of Revenue') ) bargraph_plot = PlotlyView(bargraph_px, height=height, width=width) session.close() return bargraph_plot
def create_hydrograph(hydrograph_id, height='520px', width='100%'): """ Generates a plotly view of a hydrograph. """ # Get objects from database Session = app.get_persistent_store_database('primary_db', as_sessionmaker=True) session = Session() hydrograph = session.query(Hydrograph).get(int(hydrograph_id)) dam = hydrograph.dam time = [] flow = [] for hydro_point in hydrograph.points: time.append(hydro_point.time) flow.append(hydro_point.flow) # Build up Plotly plot hydrograph_go = go.Scatter( x=time, y=flow, name='Hydrograph for {0}'.format(dam.name), line={ 'color': '#0080ff', 'width': 4, 'shape': 'spline' }, ) data = [hydrograph_go] layout = { 'title': 'Hydrograph for {0}'.format(dam.name), 'xaxis': { 'title': 'Time (hr)' }, 'yaxis': { 'title': 'Flow (cfs)' }, } figure = {'data': data, 'layout': layout} hydrograph_plot = PlotlyView(figure, height=height, width=width) session.close() return hydrograph_plot
def cqt_event_plot(event_id, sub_event, height='520px', width='100%'): # Build up Plotly plot time, flow, concentration, segments = get_conc_flow_seg(event_id) if len(segments) == 0: d = {} d['start'] = 0 d['end'] = len(flow) segments.append(d) if sub_event >= len(segments): sub_event = len(segments) - 1 if sub_event < 0: sub_event = 0 event_flow = flow[segments[sub_event]['start']:segments[sub_event]['end']] event_concentration = concentration[ segments[sub_event]['start']:segments[sub_event]['end']] event_time = time[segments[sub_event]['start']:segments[sub_event]['end']] trajectory_go = go.Scatter3d( x=event_time, y=event_flow, z=event_concentration, mode='lines', name='Trajectory', line=dict( width=10, color=np.arange(0, len(event_flow)), colorscale='Viridis', ), ) data = [trajectory_go] layout = dict( width=400, height=400, autosize=False, title='Trajectory for event {0}'.format(str(sub_event)), scene=dict(xaxis=dict(gridcolor='rgb(255, 255, 255)', zerolinecolor='rgb(255, 255, 255)', showbackground=True, backgroundcolor='rgb(230, 230,230)', title='Time (T)'), yaxis=dict(gridcolor='rgb(255, 255, 255)', zerolinecolor='rgb(255, 255, 255)', showbackground=True, backgroundcolor='rgb(230, 230,230)', title='Discharge (Q)'), zaxis=dict(gridcolor='rgb(255, 255, 255)', zerolinecolor='rgb(255, 255, 255)', showbackground=True, backgroundcolor='rgb(230, 230,230)', title='Concentration (C)'), camera=dict(up=dict(x=0, y=0, z=1), eye=dict( x=-1.7428, y=1.0707, z=0.7100, )), aspectratio=dict(x=1, y=1, z=1), aspectmode='manual'), ) figure = {'data': data, 'layout': layout} hydrograph_plot = PlotlyView(figure, height='200px', width='100%') return hydrograph_plot
def create_revenue_vs_req_compare_bargraph(rev_scenario): height = '520px' width = '100%' """ Generates a plotly view of projects """ # Get objects from database Session = app.get_persistent_store_database('primary_db', as_sessionmaker=True) session = Session() projects = session.query(Project).all() revenue_list = session.query(Revenue).all() dfrevreq = pd.DataFrame(columns=[ 'Construction Year', 'Projected Cost', ]) print("difference data started") for project in projects: if project.debt_checkbox_val == "true": z = 1 for c in range(len(project.const_cost) - 1): dfrevreq = dfrevreq.append( { 'Construction Year': int(project.const_year) + z, 'Projected Cost': float(json.loads(project.const_cost[c + 1])) }, ignore_index=True) z = z + 1 elif project.recur_checkbox_val == "true": y = 0 for x in range(len(project.const_cost) - 1): dfrevreq = dfrevreq.append( { 'Construction Year': int(project.const_year) + y, 'Projected Cost': float(json.loads(project.const_cost[x + 1])), }, ignore_index=True) y = y + 1 else: dfrevreq = dfrevreq.append( { 'Construction Year': int(project.const_year), 'Projected Cost': float(json.loads(project.const_cost[0])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category }, ignore_index=True) dfrev = pd.DataFrame( columns=['Scenario', 'Source', 'Monetary Value', 'Year']) for entry in revenue_list: if entry.scenario == rev_scenario: dfrev = dfrev.append( { 'Scenario': entry.scenario, 'Source': entry.revenue_source, 'Monetary Value': float(json.loads(entry.monetary_Value)), 'Year': int(entry.year) }, ignore_index=True) dfrevreq_rnm = dfrevreq.rename(columns={"Construction Year": "Year"}) dfrev_sum = dfrev.groupby("Year").agg(total_rev=('Monetary Value', 'sum')) dfrevreq_sum = dfrevreq_rnm.groupby("Year").agg( total_cost=('Projected Cost', 'sum')) minyr = min(int(dfrevreq_rnm["Year"].min()), int(dfrev["Year"].min())) maxyr = max(int(dfrevreq_rnm["Year"].max()), int(dfrev["Year"].max())) year_range = [] for i in range(maxyr - minyr + 1): year_range.append(minyr + i) dfdiff = pd.DataFrame(columns=["Year", "Difference"]) for year in year_range: dfdiff = dfdiff.append({ 'Year': year, "Difference": 0 }, ignore_index=True) dfdiff = dfdiff.merge(dfrev_sum, how="left", on="Year") dfdiff = dfdiff.merge(dfrevreq_sum, how="left", on="Year") dfdiff["total_rev"] = dfdiff["total_rev"].fillna(0) dfdiff["total_cost"] = dfdiff["total_cost"].fillna(0) dfdiff["Difference"] = (dfdiff["total_rev"] - dfdiff["total_cost"]) dfdiff["Loss Or Gain"] = [ "Gain" if difval >= 0 else "Loss" for difval in dfdiff["Difference"] ] print("difference data finished") # Build up Plotly plot bargraph_px = px.bar( dfdiff, hover_data=["Year", "total_rev", "total_cost", "Difference"], x="Year", y="Difference", color="Loss Or Gain", color_discrete_map={ "Loss": "#ac162c", "Gain": "#001f5b" } # title="Revenue" ) bargraph_px.update_layout(yaxis=dict(title='Revenue (USD)'), xaxis=dict(title='Year'), legend=dict(title='Source of Revenue')) bargraph_plot = PlotlyView(bargraph_px, height=height, width=width) session.close() return bargraph_plot
def create_revenue_vs_requirements_bargraph(rev_scenario): height = '520px' width = '100%' """ Generates a plotly view of projects """ # Get objects from database Session = app.get_persistent_store_database('primary_db', as_sessionmaker=True) session = Session() projects = session.query(Project).all() revenue_list = session.query(Revenue).all() dfrevreq = pd.DataFrame(columns=[ 'Construction Year', 'Projected Cost', 'Facility ID', 'Project', 'Category' ]) for project in projects: if project.debt_checkbox_val == "true": z = 1 for c in range(len(project.const_cost) - 1): dfrevreq = dfrevreq.append( { 'Construction Year': int(project.const_year) + z, 'Projected Cost': float(json.loads(project.const_cost[c + 1])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category }, ignore_index=True) z = z + 1 elif project.recur_checkbox_val == "true": y = 0 for x in range(len(project.const_cost) - 1): dfrevreq = dfrevreq.append( { 'Construction Year': int(project.const_year) + y, 'Projected Cost': float(json.loads(project.const_cost[x + 1])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category }, ignore_index=True) y = y + 1 else: dfrevreq = dfrevreq.append( { 'Construction Year': int(project.const_year), 'Projected Cost': float(json.loads(project.const_cost[0])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category }, ignore_index=True) print("bargraph data ready") # Build up Plotly plot bargraph_px = px.bar( dfrevreq, hover_data=["Facility ID", "Category", "Project", "Projected Cost"], x="Construction Year", y="Projected Cost", color='Category', color_discrete_map={ "Water": "#056eb7", "Wastewater": "#b3c935", "Existing Debt": "#001f5b", "Facilities": "#074768", "Golf": "#ac162c", "Transportation": "#232525", "Storm": "#F78F07" } # title="Future Project Costs" ) bargraph_px.update_layout(yaxis=dict(title='Construction Cost (USD)', ), xaxis=dict(title='Construction Year'), legend=dict(title='Category')) print("bargraphdone") dfrev = pd.DataFrame( columns=['Scenario', 'Source', 'Monetary Value', 'Year']) for entry in revenue_list: if entry.scenario == rev_scenario: dfrev = dfrev.append( { 'Scenario': entry.scenario, 'Source': entry.revenue_source, 'Monetary Value': int(json.loads(entry.monetary_Value)), 'Year': int(entry.year) }, ignore_index=True) print("rev done") dfrev_sum = dfrev.groupby("Year").agg(Revenue=('Monetary Value', 'sum')) dfrevfinal = pd.DataFrame(columns=["Year"]) minyr = dfrev["Year"].min() maxyr = dfrev["Year"].max() year_range = [] for i in range(maxyr - minyr + 1): year_range.append(minyr + i) for year in year_range: dfrevfinal = dfrevfinal.append({'Year': year}, ignore_index=True) dfrevfinal = dfrevfinal.merge(dfrev_sum, how="left", on="Year") print("groupby done") bargraph_px.add_scatter(y=dfrevfinal["Revenue"], x=dfrevfinal["Year"], mode="lines", line=dict(width=3, color="Red"), name=rev_scenario) print("scatter done") bargraph_plot = PlotlyView(bargraph_px, height=height, width=width) print("graph generated") session.close() return bargraph_plot
def candq_event_plot(event_id, sub_event, height='520px', width='100%'): # Build up Plotly plot time, flow, concentration, segments = get_conc_flow_seg(event_id) if len(segments) == 0: d = {} d['start'] = 0 d['end'] = len(flow) segments.append(d) if sub_event >= len(segments): sub_event = len(segments) - 1 if sub_event < 0: sub_event = 0 event_flow = flow[segments[sub_event]['start']:segments[sub_event]['end']] event_concentration = concentration[ segments[sub_event]['start']:segments[sub_event]['end']] flow_go = go.Scatter( x=np.arange(0, len(event_flow)), y=event_flow, mode='lines', name='Discharge', line=dict( width=5, color='#1f77b4', ), ) concentration_go = go.Scatter( x=np.arange(0, len(event_flow)), y=event_concentration, mode='lines', name='concentration', yaxis="y2", line=dict( width=5, color="#ff7f0e", ), ) data = [] data.append(flow_go) data.append(concentration_go) layout = dict(xaxis=dict(domain=[0.3, 0.7]), yaxis=dict(title="Discharge (Q)", titlefont=dict(color="#1f77b4"), tickfont=dict(color="#1f77b4")), yaxis2=dict(title="Concentration (C)", titlefont=dict(color="#ff7f0e"), tickfont=dict(color="#ff7f0e"), overlaying="y", side="right")) figure = {'data': data, 'layout': layout} hydrograph_plot = PlotlyView(figure, height='520px', width='100%') return hydrograph_plot
def cqt_cq_event_plot(event_id, sub_event, height='800px', width='100%'): # Build up Plotly plot time, flow, concentration, segments = get_conc_flow_seg(event_id) if len(segments) == 0: d = {} d['start'] = 0 d['end'] = len(flow) segments.append(d) if sub_event >= len(segments): sub_event = len(segments) - 1 if sub_event < 0: sub_event = 0 event_flow = flow[segments[sub_event]['start']:segments[sub_event]['end']] event_concentration = concentration[ segments[sub_event]['start']:segments[sub_event]['end']] event_time = time[segments[sub_event]['start']:segments[sub_event]['end']] # Initialize figure with subplots fig = make_subplots( rows=2, cols=2, #Scott, column width and row with as you pointed out are ratio, we have two columns and two rows.. column_widths=[0.6, 0.4], row_heights=[0.4, 0.6], #Scott - horizontal spacing, is used to remove the gap between C-Q and 3-D plot.. horizontal_spacing=0.05, specs=[[{ "type": "xy", "secondary_y": True, "colspan": 2 }, None], [{ "type": "scatter" }, { "type": "scatter3d" }]]) # Add traces fig.add_trace( go.Scatter( x=event_time, y=event_flow, name="Discharge (Q)", line=dict( width=4, color='blue', ), ), row=1, col=1, secondary_y=False, ) fig.add_trace( go.Scatter( x=event_time, y=event_concentration, name="Concentration (C)", line=dict( width=4, color='orange', ), ), row=1, col=1, secondary_y=True, ) # Add scattergeo globe map of volcano locations fig.add_trace(go.Scatter(x=event_flow, y=event_concentration, mode='lines+markers', name='Hysteresis', fill='toself', line=dict( width=6, color='#1f77b4', ), marker=dict( size=10, color=np.arange(0, len(event_flow)), colorscale='Viridis', )), row=2, col=1) # Add 3d surface of volcano fig.add_trace(go.Scatter3d(x=event_time, y=event_flow, z=event_concentration, mode='lines', name='trajectory', line=dict( width=10, color=np.arange(0, len(event_flow)), colorscale='Viridis', )), row=2, col=2) #Scott - height 800 and width 950. I am using these to adjust height and width. I think if you want height to be greater or small, i would also change the height in line "PlotlyView(fig, height='800px', width='100%')" to match. fig['layout'].update(height=800, width=950) # Set theme, margin, and annotation in layout fig.update_layout( template= "plotly_dark", #ggplot2, plotly_dark, seaborn, plotly, plotly_white, presentation, xgridoff margin=dict(r=10, t=25, b=40, l=60), autosize=True, #automargin=True, annotations=[ go.layout.Annotation(text="Source: CAUHSI - HEDA Tool", showarrow=False, xref="paper", yref="paper", x=0, y=0) ], scene1=dict(xaxis_title='Time', yaxis_title='Discharge', zaxis_title='Concentration'), #showlegend=False, #Scott - I moved the legend to top because it was messing with space utilization of figure. #orientation "h" makes it a horizontal legend. legend_orientation="h", #location coordinates of legend. legend=dict(x=-.1, y=1.1), ) fig.update_yaxes(title_text="Concentration", row=2, col=1) fig.update_xaxes(title_text="Discharge", row=2, col=1) fig.update_xaxes(title_text="Time", row=1, col=1) fig.update_yaxes(title_text="Discharge", row=1, col=1, secondary_y=False) fig.update_yaxes(title_text="Concentration", row=1, col=1, secondary_y=True) hydrograph_plot = PlotlyView(fig, height='800px', width='100%') return hydrograph_plot
def create_hydrograph(event_id, height='520px', width='100%'): # Build up Plotly plot time, flow, concentration, segments = get_conc_flow_seg(event_id) if len(segments) == 0: d = {} d['start'] = 0 d['end'] = len(flow) segments.append(d) fig = make_subplots(specs=[[{"secondary_y": True}]]) #add all the flow fig.add_trace( go.Scatter( x=time, y=flow, name='Hydrograph', mode='lines', line={ 'color': 'blue', 'width': 1 }, ), secondary_y=False, ) fig.add_trace( go.Scatter( x=time, y=concentration, name='Concentration graph', mode='lines', line={ 'color': 'orange', 'width': 1 }, ), secondary_y=True, ) flow = np.asarray(flow) event_counter = 1 for segment in segments: event_flow = flow[segment['start']:segment['end']] event_time = time[segment['start']:segment['end']] #event_concentration = concentration[segment['start']:concentration['end']] fig.add_trace( go.Scatter( x=event_time, y=event_flow, mode='lines', line={ 'color': 'red', 'width': 4, 'shape': 'spline' }, name="", ), secondary_y=False, ) event_counter = event_counter + 1 #this is a comment for syncing if event_id != '1': title = 'Number of events segmented: {0} '.format(str(len(segments))) else: title = 'Hydrograph to validate segmentation' fig.update_layout( xaxis=go.layout.XAxis(rangeselector=dict(buttons=list([ dict(count=1, label="1m", step="month", stepmode="backward"), dict(count=6, label="6m", step="month", stepmode="backward"), dict(count=1, label="YTD", step="year", stepmode="todate"), dict(count=1, label="1y", step="year", stepmode="backward"), dict(step="all") ])), rangeslider=dict(visible=True), type="date"), showlegend=False, title_text=title, ) # Set x-axis title fig.update_xaxes(title_text="Time") # Set y-axes titles fig.update_yaxes(title_text="Flow (cfs)", secondary_y=False) fig.update_yaxes(title_text="Concentration", showgrid=False, secondary_y=True) hydrograph_plot = PlotlyView(fig, height='520px', width='100%') return hydrograph_plot
def revenue_vs_requirements(request): """ Controller for the Plots Page. """ height = '520px' width = '100%' """ Generates a plotly view of projects """ # Get objects from database Session = app.get_persistent_store_database('primary_db', as_sessionmaker=True) session = Session() projects = session.query(Project).all() revenue_list = session.query(Revenue).all() dfrevreq = pd.DataFrame(columns=[ 'Construction Year', 'Projected Cost', 'Facility ID', 'Project', 'Category' ]) for project in projects: if project.debt_checkbox_val == "true": z = 1 for c in range(len(project.const_cost) - 1): dfrevreq = dfrevreq.append( { 'Construction Year': int(project.const_year) + z, 'Projected Cost': float(json.loads(project.const_cost[c + 1])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category }, ignore_index=True) z = z + 1 elif project.recur_checkbox_val == "true": y = 0 for x in range(len(project.const_cost) - 1): dfrevreq = dfrevreq.append( { 'Construction Year': int(project.const_year) + y, 'Projected Cost': float(json.loads(project.const_cost[x + 1])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category }, ignore_index=True) y = y + 1 else: dfrevreq = dfrevreq.append( { 'Construction Year': int(project.const_year), 'Projected Cost': float(json.loads(project.const_cost[0])), 'Facility ID': project.facility_id, 'Project': project.project, 'Category': project.category }, ignore_index=True) # Build up Plotly plot bargraph_high_px = px.bar( dfrevreq, hover_data=["Facility ID", "Category", "Project", "Projected Cost"], x="Construction Year", y="Projected Cost", color='Category', color_discrete_map={ "Water": "#056eb7", "Wastewater": "#b3c935", "Existing Debt": "#8065ba", "Facilities": "#074768", "Golf": "#749f34", "Transportation": "#ac162c", "Stormwater": "#F78F07" } # title="Future Project Costs" ) bargraph_high_px.update_layout(yaxis=dict( title='Revenue/Revenue Required (USD)', ), xaxis=dict(title='Year'), legend=dict(title='Category')) bargraph_med_px = px.bar( dfrevreq, hover_data=["Facility ID", "Category", "Project", "Projected Cost"], x="Construction Year", y="Projected Cost", color='Category', color_discrete_map={ "Water": "#056eb7", "Wastewater": "#b3c935", "Existing Debt": "#001f5b", "Facilities": "#074768", "Golf": "#ac162c", "Transportation": "#232525", "Stormwater": "#F78F07" } # title="Future Project Costs" ) bargraph_med_px.update_layout(yaxis=dict( title='Revenue/Revenue Required (USD)', ), xaxis=dict(title='Year'), legend=dict(title='Category')) bargraph_low_px = px.bar( dfrevreq, hover_data=["Facility ID", "Category", "Project", "Projected Cost"], x="Construction Year", y="Projected Cost", color='Category', color_discrete_map={ "Water": "#056eb7", "Wastewater": "#b3c935", "Existing Debt": "#001f5b", "Facilities": "#074768", "Golf": "#ac162c", "Transportation": "#232525", "Stormwater": "#F78F07" } # title="Future Project Costs" ) bargraph_low_px.update_layout(yaxis=dict( title='Revenue/Revenue Required (USD)', ), xaxis=dict(title='Year'), legend=dict(title='Category')) dfrevhigh = pd.DataFrame( columns=['Scenario', 'Source', 'Monetary Value', 'Year']) dfrevmed = pd.DataFrame( columns=['Scenario', 'Source', 'Monetary Value', 'Year']) dfrevlow = pd.DataFrame( columns=['Scenario', 'Source', 'Monetary Value', 'Year']) for entry in revenue_list: if entry.scenario == "high": dfrevhigh = dfrevhigh.append( { 'Scenario': entry.scenario, 'Source': entry.revenue_source, 'Monetary Value': int(json.loads(entry.monetary_Value)), 'Year': int(entry.year) }, ignore_index=True) elif entry.scenario == "medium": dfrevmed = dfrevmed.append( { 'Scenario': entry.scenario, 'Source': entry.revenue_source, 'Monetary Value': int(json.loads(entry.monetary_Value)), 'Year': int(entry.year) }, ignore_index=True) elif entry.scenario == "low": dfrevlow = dfrevlow.append( { 'Scenario': entry.scenario, 'Source': entry.revenue_source, 'Monetary Value': int(json.loads(entry.monetary_Value)), 'Year': int(entry.year) }, ignore_index=True) dfrevhigh_sum = dfrevhigh.groupby("Year").agg( HighRevenue=('Monetary Value', 'sum')) dfrevmed_sum = dfrevmed.groupby("Year").agg(MedRevenue=('Monetary Value', 'sum')) dfrevlow_sum = dfrevlow.groupby("Year").agg(LowRevenue=('Monetary Value', 'sum')) dfrevfinal = pd.DataFrame(columns=["Year"]) minhyr = dfrevhigh["Year"].min() maxhyr = dfrevhigh["Year"].max() minmyr = dfrevmed["Year"].min() maxmyr = dfrevmed["Year"].max() minlyr = dfrevlow["Year"].min() maxlyr = dfrevlow["Year"].max() maxyr = max(maxlyr, maxmyr, maxhyr) minyr = min(minlyr, minmyr, minhyr) year_revrange = [] for i in range(maxyr - minyr + 1): year_revrange.append(minyr + i) for yearrev in year_revrange: dfrevfinal = dfrevfinal.append({'Year': yearrev}, ignore_index=True) dfrevfinal = dfrevfinal.merge(dfrevlow_sum, how="left", on="Year") dfrevfinal = dfrevfinal.merge(dfrevmed_sum, how="left", on="Year") dfrevfinal = dfrevfinal.merge(dfrevhigh_sum, how="left", on="Year") bargraph_high_px.add_scatter(y=dfrevfinal["HighRevenue"], x=dfrevfinal["Year"], mode="lines", line=dict(width=3, color="Red"), name="High Revenue Scenario") bargraph_med_px.add_scatter(y=dfrevfinal["MedRevenue"], x=dfrevfinal["Year"], mode="lines", line=dict(width=3, color="Red"), name="Medium Revenue Scenario") bargraph_low_px.add_scatter(y=dfrevfinal["LowRevenue"], x=dfrevfinal["Year"], mode="lines", line=dict(width=3, color="Red"), name="Low Revenue Scenario") bargraph_high_plot = PlotlyView(bargraph_high_px, height=height, width=width) bargraph_med_plot = PlotlyView(bargraph_med_px, height=height, width=width) bargraph_low_plot = PlotlyView(bargraph_low_px, height=height, width=width) dfrevreq_rnm = dfrevreq.rename(columns={"Construction Year": "Year"}) dfrevreq_sum = dfrevreq_rnm.groupby("Year").agg( total_cost=('Projected Cost', 'sum')) minfyr = min(int(dfrevreq_rnm["Year"].min()), int(minyr)) maxfyr = max(int(dfrevreq_rnm["Year"].max()), int(maxyr)) year_range = [] for i in range(maxfyr - minfyr + 1): year_range.append(minfyr + i) dfdiff = pd.DataFrame(columns=[ "Year", "Difference High", "Difference Medium", "Difference Low" ]) for yearf in year_range: dfdiff = dfdiff.append( { 'Year': yearf, "Difference High": 0, "Difference Medium": 0, "Difference Low": 0 }, ignore_index=True) dfdiff = dfdiff.merge(dfrevfinal, how="left", on="Year") dfdiff = dfdiff.merge(dfrevreq_sum, how="left", on="Year") dfdiff["HighRevenue"] = dfdiff["HighRevenue"].fillna(0) dfdiff["MedRevenue"] = dfdiff["MedRevenue"].fillna(0) dfdiff["LowRevenue"] = dfdiff["LowRevenue"].fillna(0) dfdiff["total_cost"] = dfdiff["total_cost"].fillna(0) dfdiff["Difference High"] = (dfdiff["HighRevenue"] - dfdiff["total_cost"]) dfdiff["Difference Medium"] = (dfdiff["MedRevenue"] - dfdiff["total_cost"]) dfdiff["Difference Low"] = (dfdiff["LowRevenue"] - dfdiff["total_cost"]) dfdiff["High Deficit Or Surplus"] = [ "Surplus" if difvalh >= 0 else "Deficit" for difvalh in dfdiff["Difference High"] ] dfdiff["Medium Deficit Or Surplus"] = [ "Surplus" if difvalm >= 0 else "Deficit" for difvalm in dfdiff["Difference Medium"] ] dfdiff["Low Deficit Or Surplus"] = [ "Surplus" if difvall >= 0 else "Deficit" for difvall in dfdiff["Difference Low"] ] bargraphc_high_px = px.bar( dfdiff, hover_data=["Year", "HighRevenue", "total_cost", "Difference High"], x="Year", y="Difference High", color="High Deficit Or Surplus", color_discrete_map={ "Deficit": "#ac162c", "Surplus": "#001f5b" } # title="Revenue" ) bargraphc_high_px.update_layout(yaxis=dict(title='Monetary Value (USD)'), xaxis=dict(title='Year'), legend=dict(title='Deficit or Surplus')) bargraphc_med_px = px.bar( dfdiff, hover_data=["Year", "MedRevenue", "total_cost", "Difference Medium"], x="Year", y="Difference Medium", color="Medium Deficit Or Surplus", color_discrete_map={ "Deficit": "#ac162c", "Surplus": "#001f5b" } # title="Revenue" ) bargraphc_med_px.update_layout(yaxis=dict(title='Monetary Value (USD)'), xaxis=dict(title='Year'), legend=dict(title='Deficit or Surplus')) bargraphc_low_px = px.bar( dfdiff, hover_data=["Year", "LowRevenue", "total_cost", "Difference Low"], x="Year", y="Difference Low", color="Low Deficit Or Surplus", color_discrete_map={ "Deficit": "#ac162c", "Surplus": "#001f5b" } # title="Revenue" ) bargraphc_low_px.update_layout(yaxis=dict(title='Monetary Value (USD)'), xaxis=dict(title='Year'), legend=dict(title='Deficit or Surplus')) bargraph_compare_high_plot = PlotlyView(bargraphc_high_px, height=height, width=width) bargraph_compare_med_plot = PlotlyView(bargraphc_med_px, height=height, width=width) bargraph_compare_low_plot = PlotlyView(bargraphc_low_px, height=height, width=width) context = { 'bargraph_low_plot': bargraph_low_plot, 'bargraph_med_plot': bargraph_med_plot, 'bargraph_high_plot': bargraph_high_plot, 'bargraph_compare_low_plot': bargraph_compare_low_plot, 'bargraph_compare_med_plot': bargraph_compare_med_plot, 'bargraph_compare_high_plot': bargraph_compare_high_plot, # 'can_add_projects': has_permission(request, 'add_projects') } session.close() return render(request, 'project_inventory/revenue_vs_requirements.html', context)
def create_hydrograph_old(event_id, height='520px', width='100%'): # Build up Plotly plot print(event_id) time, flow, concentration, segments = get_conc_flow_seg(event_id) if len(segments) == 0: d = {} d['start'] = 0 d['end'] = len(flow) segments.append(d) event_segments = [] flow_go = go.Scatter( x=time, y=flow, name='Hydrograph', mode='lines', line={ 'color': 'blue', 'width': 1 }, ) event_segments.append(flow_go) flow = np.asarray(flow) for segment in segments: event_flow = flow[segment['start']:segment['end']] event_time = time[segment['start']:segment['end']] event_go = go.Scatter( x=event_time, y=event_flow, mode='lines', line={ 'color': 'red', 'width': 4, 'shape': 'spline' }, ) event_segments.append(event_go) data = event_segments if event_id != '1': title = 'Hydrograph with {0} events'.format(str(len(segments))) else: title = 'Hydrograph to validate segmentation' layout = { 'title': title, 'xaxis': { 'title': 'Time' }, 'yaxis': { 'title': 'Flow (cfs)' }, 'showlegend': False, 'xaxis_range': [time[0], time[-1]], 'xaxis': go.layout.XAxis(rangeselector=dict(buttons=list([ dict(count=1, label="1m", step="month", stepmode="backward"), dict(count=6, label="6m", step="month", stepmode="backward"), dict(count=1, label="YTD", step="year", stepmode="todate"), dict(count=1, label="1y", step="year", stepmode="backward"), dict(step="all") ])), rangeslider=dict(visible=True), type="date") } figure = {'data': data, 'layout': layout} hydrograph_plot = PlotlyView(figure, height='520px', width='100%') return hydrograph_plot