You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Plotly doesn't have an outline for their fonts so I had do improvise and place a bigger text underneath the original one.
This does not look the best, I could think about injecting HTML directly in a dash app (see chatgpt dump)
importdashfromdashimportdcc, htmlimportplotly.graph_objectsasgofrompathlibimportPathclassSolutionPlotter:
COLOR_LIST= [
"blue", "red", "green", "purple", "orange", "yellow", "pink",
"brown", "grey", "black", "cyan", "magenta"
]
def__init__(self, num_vehicles, routes, locations, location_names, capacities, use_capacity, total_distance, depot, use_depot):
self.num_vehicles=num_vehiclesself.routes=routesself.locations=locationsself.location_names=location_namesself.capacities=capacitiesself.use_capacity=use_capacityself.total_distance=total_distanceself.depot=depotself.use_depot=use_depotdefdisplay(self, file_name: str=None, results_path: str="results"):
""" Display the solution using a plotly figure. Saves the figure to an HTML file if a file name is provided. """fig=go.Figure()
forvehicle_idinrange(self.num_vehicles):
route_coordinates= [
(self.locations[node][0], self.locations[node][1])
fornodeinself.routes[vehicle_id]
]
color=self.COLOR_LIST[vehicle_id%len(self.COLOR_LIST)]
capacity= (
self.capacities[vehicle_id]
ifisinstance(self.capacities, list)
elseself.capacities
)
legend_group=f"Vehicle {vehicle_id+1}"legend_name= (
f"Vehicle {vehicle_id+1} ({capacity})"ifself.use_capacityelsef"Vehicle {vehicle_id+1}"
)
# Draw routesfig.add_trace(
go.Scatter(
x=[loc[0] forlocinroute_coordinates],
y=[loc[1] forlocinroute_coordinates],
mode="lines",
line=dict(width=5, color=color),
name=legend_name,
legendgroup=legend_group,
)
)
# Draw annotationsforiinrange(len(route_coordinates) -1):
loc_name=self.location_names[self.routes[vehicle_id][i]]
self.plot_direction(
fig,
route_coordinates[i],
route_coordinates[i+1],
color,
5,
legend_group,
)
self.plot_location(
fig,
route_coordinates[i],
color,
loc_name,
legend_group,
vehicle_id,
i,
)
ifnotself.use_depotandlen(route_coordinates) >0:
self.plot_location(
fig,
route_coordinates[-1],
color,
self.location_names[self.routes[vehicle_id][-1]],
legend_group,
vehicle_id,
len(route_coordinates) -1,
)
ifself.use_depot:
self.plot_location(
fig, self.locations[self.depot], "gray", self.location_names[self.depot]
)
fig.update_layout(
xaxis_title="X Coordinate",
yaxis_title="Y Coordinate",
legend=dict(
title=f"Total Distance: {self.total_distance}m",
orientation="h",
yanchor="bottom",
y=1.02,
),
)
app=dash.Dash(__name__)
app.layout=html.Div(
[
dcc.Graph(id="scatter-plot", figure=fig),
# Add HTML styled text for each location*[
html.Div(
loc_name,
style={
"position": "absolute",
"left": f"{loc[0]}px",
"top": f"{loc[1]}px",
"transform": "translate(-50%, -50%)",
"color": "white",
"font-size": "15px",
"text-shadow": "-1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000",
},
)
forloc, loc_nameinzip(self.locations, self.location_names)
]
],
style={"position": "relative", "width": "100%", "height": "100%"},
)
app.run_server(debug=True)
iffile_nameisnotNone:
html_path=f"{results_path}/html"Path(html_path).mkdir(parents=True, exist_ok=True)
fig.write_html(f"{html_path}/{file_name}.html")
defplot_direction(self, fig, loc1, loc2, color, line_width, legend_group=None):
""" Plot an arrow representing the direction from coord1 to coord2 with the given color and line width. """x_mid= (loc1[0] +loc2[0]) /2y_mid= (loc1[1] +loc2[1]) /2fig.add_trace(
go.Scatter(
x=[loc1[0], x_mid],
y=[loc1[1], y_mid],
mode="lines+markers",
line=dict(width=line_width, color=color),
marker=dict(size=20, symbol="arrow-up", angleref="previous"),
hoverinfo="skip",
showlegend=False,
legendgroup=legend_group,
)
)
defplot_location(
self,
fig,
loc,
color,
name,
legend_group=None,
vehicle_id=None,
route_id=None,
):
""" Plot a location with the given color and legend group. """hovertext= (
"Starting Point"ifvehicle_idisNoneelse (
f"Vehicle {vehicle_id+1}: {self.loads[vehicle_id][route_id]} passengers"ifself.use_capacityelsef"Vehicle {vehicle_id+1}"
)
)
fig.add_trace(
go.Scatter(
x=[loc[0]],
y=[loc[1]],
mode="markers+text",
marker=dict(size=50, symbol="circle", color=color, line_width=2),
text=name,
textposition="middle center",
textfont=dict(color="white", size=15),
showlegend=False,
hoverinfo="text",
hovertext=hovertext,
legendgroup=legend_group,
)
)
The text was updated successfully, but these errors were encountered:
Plotly doesn't have an outline for their fonts so I had do improvise and place a bigger text underneath the original one.
This does not look the best, I could think about injecting HTML directly in a dash app (see chatgpt dump)
The text was updated successfully, but these errors were encountered: