|
1 | 1 | """ pyplots.ai |
2 | 2 | bubble-packed: Basic Packed Bubble Chart |
3 | | -Library: highcharts unknown | Python 3.13.11 |
4 | | -Quality: 90/100 | Created: 2025-12-23 |
| 3 | +Library: highcharts 1.10.3 | Python 3.14.3 |
| 4 | +Quality: 85/100 | Updated: 2026-02-23 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import tempfile |
|
16 | 16 | from selenium.webdriver.chrome.options import Options |
17 | 17 |
|
18 | 18 |
|
19 | | -# Data - Company market share by sector |
20 | | -# Packed bubbles group by sector with size representing market value |
| 19 | +# Data - Company market value by sector ($B) |
21 | 20 | data = [ |
22 | | - # Technology sector |
23 | 21 | { |
24 | 22 | "name": "Technology", |
25 | 23 | "data": [ |
26 | 24 | {"name": "Software", "value": 850}, |
27 | 25 | {"name": "Hardware", "value": 420}, |
28 | | - {"name": "Cloud Services", "value": 680}, |
29 | | - {"name": "Semiconductors", "value": 390}, |
30 | | - {"name": "Cybersecurity", "value": 280}, |
| 26 | + {"name": "Cloud", "value": 680}, |
| 27 | + {"name": "Semicon.", "value": 390}, |
| 28 | + {"name": "Cybersec.", "value": 280}, |
31 | 29 | ], |
32 | 30 | }, |
33 | | - # Finance sector |
34 | 31 | { |
35 | 32 | "name": "Finance", |
36 | 33 | "data": [ |
37 | 34 | {"name": "Banking", "value": 720}, |
38 | 35 | {"name": "Insurance", "value": 480}, |
39 | | - {"name": "Asset Management", "value": 350}, |
| 36 | + {"name": "Asset Mgmt", "value": 350}, |
40 | 37 | {"name": "Fintech", "value": 260}, |
41 | 38 | ], |
42 | 39 | }, |
43 | | - # Healthcare sector |
44 | 40 | { |
45 | 41 | "name": "Healthcare", |
46 | 42 | "data": [ |
47 | | - {"name": "Pharmaceuticals", "value": 580}, |
48 | | - {"name": "Medical Devices", "value": 320}, |
| 43 | + {"name": "Pharma", "value": 580}, |
| 44 | + {"name": "Med Devices", "value": 320}, |
49 | 45 | {"name": "Biotech", "value": 420}, |
50 | | - {"name": "Healthcare Services", "value": 240}, |
| 46 | + {"name": "Health Svcs", "value": 240}, |
51 | 47 | ], |
52 | 48 | }, |
53 | | - # Energy sector |
54 | 49 | { |
55 | 50 | "name": "Energy", |
56 | 51 | "data": [ |
|
59 | 54 | {"name": "Utilities", "value": 290}, |
60 | 55 | ], |
61 | 56 | }, |
62 | | - # Consumer sector |
63 | 57 | { |
64 | 58 | "name": "Consumer", |
65 | 59 | "data": [ |
66 | 60 | {"name": "Retail", "value": 460}, |
67 | | - {"name": "Food & Beverage", "value": 340}, |
| 61 | + {"name": "Food & Bev", "value": 340}, |
68 | 62 | {"name": "Automotive", "value": 510}, |
69 | | - {"name": "Entertainment", "value": 270}, |
| 63 | + {"name": "Entertain.", "value": 270}, |
70 | 64 | ], |
71 | 65 | }, |
72 | 66 | ] |
73 | 67 |
|
74 | | -# Colorblind-safe palette for sectors |
75 | | -colors = ["#306998", "#FFD43B", "#9467BD", "#17BECF", "#8C564B"] |
| 68 | +# Refined colorblind-safe palette — muted tones with strong contrast |
| 69 | +colors = ["#306998", "#D4920B", "#7B4F9E", "#0E9AA7", "#C05746"] |
76 | 70 |
|
77 | 71 | # Create chart |
78 | 72 | chart = Chart(container="container") |
79 | 73 | chart.options = HighchartsOptions() |
80 | 74 |
|
81 | | -# Chart configuration |
82 | | -chart.options.chart = {"type": "packedbubble", "width": 4800, "height": 2700, "backgroundColor": "#ffffff"} |
| 75 | +# Render at 900x900 CSS px, capture at 4x device scale → 3600x3600 (square format) |
| 76 | +chart.options.chart = { |
| 77 | + "type": "packedbubble", |
| 78 | + "width": 900, |
| 79 | + "height": 900, |
| 80 | + "backgroundColor": { |
| 81 | + "linearGradient": {"x1": 0, "y1": 0, "x2": 0, "y2": 1}, |
| 82 | + "stops": [[0, "#FAFBFD"], [1, "#F0F2F5"]], |
| 83 | + }, |
| 84 | + "style": {"fontFamily": "'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif"}, |
| 85 | + "spacing": [8, 8, 12, 8], |
| 86 | +} |
83 | 87 |
|
84 | | -# Title |
| 88 | +# Title — sizes at 1/4 of target since 4x device scale factor |
85 | 89 | chart.options.title = { |
86 | | - "text": "bubble-packed · Market Sectors · highcharts · pyplots.ai", |
87 | | - "style": {"fontSize": "64px", "fontWeight": "bold"}, |
| 90 | + "text": "bubble-packed \u00b7 highcharts \u00b7 pyplots.ai", |
| 91 | + "style": {"fontSize": "16px", "fontWeight": "700", "color": "#1a1a2e"}, |
| 92 | + "margin": 2, |
88 | 93 | } |
89 | 94 |
|
90 | 95 | # Subtitle |
91 | | -chart.options.subtitle = {"text": "Circle size represents market value ($B)", "style": {"fontSize": "36px"}} |
| 96 | +chart.options.subtitle = { |
| 97 | + "text": "Market value by sector ($B)", |
| 98 | + "style": {"fontSize": "9px", "color": "#555555", "fontWeight": "400"}, |
| 99 | +} |
| 100 | + |
| 101 | +# Hide watermark |
| 102 | +chart.options.credits = {"enabled": False} |
92 | 103 |
|
93 | 104 | # Tooltip |
94 | 105 | chart.options.tooltip = { |
95 | 106 | "useHTML": True, |
96 | | - "style": {"fontSize": "28px"}, |
| 107 | + "style": {"fontSize": "8px"}, |
97 | 108 | "pointFormat": "<b>{point.name}</b>: ${point.value}B", |
98 | 109 | } |
99 | 110 |
|
100 | | -# Legend |
101 | | -chart.options.legend = {"enabled": True, "itemStyle": {"fontSize": "36px"}, "symbolHeight": 24, "symbolWidth": 24} |
| 111 | +# Legend — compact with small symbols |
| 112 | +chart.options.legend = { |
| 113 | + "enabled": True, |
| 114 | + "layout": "horizontal", |
| 115 | + "align": "center", |
| 116 | + "verticalAlign": "bottom", |
| 117 | + "floating": False, |
| 118 | + "margin": 4, |
| 119 | + "padding": 6, |
| 120 | + "itemStyle": {"fontSize": "9px", "fontWeight": "500", "color": "#333333"}, |
| 121 | + "symbolHeight": 6, |
| 122 | + "symbolWidth": 6, |
| 123 | + "symbolRadius": 3, |
| 124 | + "itemDistance": 16, |
| 125 | +} |
102 | 126 |
|
103 | | -# Plot options for packed bubble |
| 127 | +# Plot options — selective labels on largest bubbles to prevent overlap |
104 | 128 | chart.options.plot_options = { |
105 | 129 | "packedbubble": { |
106 | | - "minSize": "60%", |
107 | | - "maxSize": "180%", |
| 130 | + "minSize": "40%", |
| 131 | + "maxSize": "200%", |
108 | 132 | "zMin": 0, |
109 | 133 | "zMax": 1000, |
110 | 134 | "layoutAlgorithm": { |
111 | 135 | "gravitationalConstant": 0.02, |
112 | | - "splitSeries": False, |
113 | | - "seriesInteraction": True, |
| 136 | + "splitSeries": True, |
| 137 | + "seriesInteraction": False, |
114 | 138 | "dragBetweenSeries": False, |
115 | | - "parentNodeLimit": False, |
| 139 | + "parentNodeLimit": True, |
| 140 | + "parentNodeOptions": {"reingold": {"gravitationalConstant": 0.08}, "marker": {"fillOpacity": 0}}, |
| 141 | + "bubblePadding": 6, |
116 | 142 | }, |
117 | 143 | "dataLabels": { |
118 | 144 | "enabled": True, |
119 | 145 | "format": "{point.name}", |
120 | | - "filter": {"property": "y", "operator": ">", "value": 230}, |
121 | | - "style": {"fontSize": "28px", "fontWeight": "bold", "color": "white", "textOutline": "2px contrast"}, |
| 146 | + "filter": {"property": "y", "operator": ">", "value": 480}, |
| 147 | + "style": {"fontSize": "10px", "fontWeight": "600", "color": "white", "textOutline": "1px rgba(0,0,0,0.4)"}, |
122 | 148 | }, |
| 149 | + "marker": {"lineWidth": 1, "lineColor": "rgba(255,255,255,0.35)"}, |
123 | 150 | } |
124 | 151 | } |
125 | 152 |
|
126 | | -# Add series with colors |
| 153 | +# Add series with colors and per-bubble opacity for visual hierarchy |
127 | 154 | series_list = [] |
128 | 155 | for i, sector in enumerate(data): |
129 | | - series_config = { |
130 | | - "type": "packedbubble", |
131 | | - "name": sector["name"], |
132 | | - "data": sector["data"], |
133 | | - "color": colors[i % len(colors)], |
134 | | - } |
135 | | - series_list.append(series_config) |
| 156 | + enriched_data = [] |
| 157 | + for item in sector["data"]: |
| 158 | + opacity = 0.7 + 0.3 * (item["value"] / 850) |
| 159 | + enriched_data.append({"name": item["name"], "value": item["value"], "marker": {"fillOpacity": opacity}}) |
| 160 | + series_list.append( |
| 161 | + {"type": "packedbubble", "name": sector["name"], "data": enriched_data, "color": colors[i % len(colors)]} |
| 162 | + ) |
136 | 163 |
|
137 | 164 | chart.options.series = series_list |
138 | 165 |
|
139 | 166 | # Download Highcharts JS and highcharts-more.js for packed bubble support |
140 | | -highcharts_url = "https://code.highcharts.com/highcharts.js" |
141 | | -highcharts_more_url = "https://code.highcharts.com/highcharts-more.js" |
| 167 | +highcharts_url = "https://cdn.jsdelivr.net/npm/highcharts/highcharts.js" |
| 168 | +highcharts_more_url = "https://cdn.jsdelivr.net/npm/highcharts/highcharts-more.js" |
142 | 169 |
|
143 | 170 | with urllib.request.urlopen(highcharts_url, timeout=30) as response: |
144 | 171 | highcharts_js = response.read().decode("utf-8") |
|
148 | 175 |
|
149 | 176 | # Generate HTML with inline scripts |
150 | 177 | html_str = chart.to_js_literal() |
| 178 | + |
| 179 | +# Post-render: center and scale the bubble cluster to fill 90% of the plot area |
| 180 | +fit_script = """ |
| 181 | +<script> |
| 182 | +setTimeout(function() { |
| 183 | + var c = Highcharts.charts[0]; |
| 184 | + if (!c || !c.seriesGroup) return; |
| 185 | + var el = c.seriesGroup.element; |
| 186 | + var bb = el.getBBox(); |
| 187 | + if (bb.width < 1 || bb.height < 1) return; |
| 188 | + var s = Math.min(c.plotWidth * 0.92 / bb.width, c.plotHeight * 0.92 / bb.height, 1.6); |
| 189 | + if (s <= 1.05) return; |
| 190 | + var tx = c.plotLeft + c.plotWidth / 2 - (bb.x + bb.width / 2) * s; |
| 191 | + var ty = c.plotTop + c.plotHeight / 2 - (bb.y + bb.height / 2) * s; |
| 192 | + el.setAttribute('transform', 'translate(' + tx + ',' + ty + ') scale(' + s + ')'); |
| 193 | +}, 6000); |
| 194 | +</script> |
| 195 | +""" |
| 196 | + |
151 | 197 | html_content = f"""<!DOCTYPE html> |
152 | 198 | <html> |
153 | 199 | <head> |
154 | 200 | <meta charset="utf-8"> |
155 | 201 | <script>{highcharts_js}</script> |
156 | 202 | <script>{highcharts_more_js}</script> |
| 203 | + <style> |
| 204 | + .highcharts-series-group, |
| 205 | + .highcharts-series-group > * {{ |
| 206 | + overflow: visible !important; |
| 207 | + }} |
| 208 | + svg.highcharts-root {{ |
| 209 | + overflow: visible !important; |
| 210 | + }} |
| 211 | + </style> |
157 | 212 | </head> |
158 | | -<body style="margin:0;"> |
159 | | - <div id="container" style="width: 4800px; height: 2700px;"></div> |
| 213 | +<body style="margin:0; overflow:hidden; background:#F0F2F5; min-height:100vh;"> |
| 214 | + <div id="container" style="width: 900px; height: 900px; overflow: visible;"></div> |
160 | 215 | <script>{html_str}</script> |
| 216 | + {fit_script} |
161 | 217 | </body> |
162 | 218 | </html>""" |
163 | 219 |
|
|
167 | 223 | <html> |
168 | 224 | <head> |
169 | 225 | <meta charset="utf-8"> |
170 | | - <script src="https://code.highcharts.com/highcharts.js"></script> |
171 | | - <script src="https://code.highcharts.com/highcharts-more.js"></script> |
| 226 | + <script src="https://cdn.jsdelivr.net/npm/highcharts/highcharts.js"></script> |
| 227 | + <script src="https://cdn.jsdelivr.net/npm/highcharts/highcharts-more.js"></script> |
172 | 228 | </head> |
173 | 229 | <body style="margin:0;"> |
174 | 230 | <div id="container" style="width: 100%; height: 100vh;"></div> |
|
187 | 243 | chrome_options.add_argument("--no-sandbox") |
188 | 244 | chrome_options.add_argument("--disable-dev-shm-usage") |
189 | 245 | chrome_options.add_argument("--disable-gpu") |
190 | | -chrome_options.add_argument("--window-size=4800,2900") |
| 246 | +chrome_options.add_argument("--window-size=1100,1100") |
| 247 | +chrome_options.add_argument("--force-device-scale-factor=4") |
191 | 248 |
|
192 | 249 | driver = webdriver.Chrome(options=chrome_options) |
193 | 250 | driver.get(f"file://{temp_path}") |
194 | | -time.sleep(5) # Wait for chart to render |
| 251 | +time.sleep(10) |
195 | 252 | driver.save_screenshot("plot_raw.png") |
196 | 253 | driver.quit() |
197 | 254 |
|
198 | | -# Crop to exact 4800x2700 dimensions |
| 255 | +# Crop to exact 3600x3600 dimensions (square format) |
199 | 256 | img = Image.open("plot_raw.png") |
200 | | -img_cropped = img.crop((0, 0, 4800, 2700)) |
| 257 | +img_cropped = img.crop((0, 0, 3600, 3600)) |
201 | 258 | img_cropped.save("plot.png") |
202 | 259 | Path("plot_raw.png").unlink() |
203 | 260 |
|
204 | | -Path(temp_path).unlink() # Clean up temp file |
| 261 | +Path(temp_path).unlink() |
0 commit comments