Creates a line chart visualization based on the categorized items, their prices, and their descriptions. Args: categorized (dict): A dictionary where the keys are the names of the clusters and the values are lists of the items in that cluster. similar_prices (list[float]):
(similar_prices: list[float], similar_shipping: list[float], similar_descriptions: list[str], similar_conditions: list[str], listing_currency: str, listing_title: str, best_title: str)
| 158 | return difference |
| 159 | |
| 160 | def create_chart(similar_prices: list[float], similar_shipping: list[float], similar_descriptions: list[str], similar_conditions: list[str], listing_currency: str, listing_title: str, best_title: str) -> object: |
| 161 | """ |
| 162 | Creates a line chart visualization based on the categorized items, their prices, and their descriptions. |
| 163 | |
| 164 | Args: |
| 165 | categorized (dict): A dictionary where the keys are the names of the clusters and the values are lists of the items in that cluster. |
| 166 | similar_prices (list[float]): A list of prices of the items. |
| 167 | similar_shipping (list[float]): A list of shipping costs of the items. |
| 168 | similar_descriptions (list[str]): A list of descriptions of the items. |
| 169 | |
| 170 | Returns: |
| 171 | A JSON string containing the Plotly figure of the line chart. |
| 172 | """ |
| 173 | |
| 174 | sorted_indices = np.argsort(similar_shipping) |
| 175 | sorted_similar_prices = np.array([similar_prices[i] for i in sorted_indices]).reshape(-1, 1) |
| 176 | sorted_similar_shipping = np.array([similar_shipping[i] for i in sorted_indices]) |
| 177 | sorted_similar_descriptions = np.array([similar_descriptions[i] for i in sorted_indices]) |
| 178 | sorted_similar_conditions = np.array([similar_conditions[i] for i in sorted_indices]) |
| 179 | |
| 180 | fig = go.Figure() |
| 181 | fig.add_trace( |
| 182 | go.Scatter( |
| 183 | x=sorted_similar_prices[:, 0], |
| 184 | y=sorted_similar_shipping, |
| 185 | mode='markers', |
| 186 | marker=dict( |
| 187 | color=sorted_similar_prices[:, 0] + sorted_similar_shipping, |
| 188 | colorscale='RdYlGn_r', |
| 189 | colorbar=dict(title="Total Price"), |
| 190 | size=8 |
| 191 | ), |
| 192 | hovertemplate="%{text}", |
| 193 | text=[ |
| 194 | f"Product: {desc.title()}<br>Price: ${price:,.2f}<br>Shipping: ${ship:,.2f}<br>Condition: {cond}" |
| 195 | for desc, price, ship, cond in zip(sorted_similar_descriptions, sorted_similar_prices[:, 0], sorted_similar_shipping, sorted_similar_conditions) |
| 196 | ], |
| 197 | showlegend=False, |
| 198 | name="Products" |
| 199 | ) |
| 200 | ) |
| 201 | |
| 202 | fig.update_layout( |
| 203 | template='plotly_white', |
| 204 | hovermode='closest', |
| 205 | xaxis_title=f"Product Price $({listing_currency})", |
| 206 | yaxis_title=f"Shipping Cost $({listing_currency})", |
| 207 | legend_title="Categories", |
| 208 | title={ |
| 209 | 'text': f"Products Similar to: {listing_title}", |
| 210 | 'xanchor': 'center', |
| 211 | 'yanchor': 'top', |
| 212 | 'y': 0.9, |
| 213 | 'x': 0.5 |
| 214 | } |
| 215 | ) |
| 216 | |
| 217 | # Add best match annotation |