|
15 | 15 | from selenium.webdriver.chrome.options import Options |
16 | 16 |
|
17 | 17 |
|
18 | | -# Data - Monthly sales example |
19 | | -sales = [100, 150, 130, 180, 200, 220, 195, 240, 280, 310, 290, 350] |
| 18 | +# Data |
| 19 | +months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] |
| 20 | +sales = [120, 135, 148, 162, 175, 195, 210, 198, 185, 170, 158, 190] |
20 | 21 |
|
21 | | -# Create chart |
| 22 | +# Create chart with container (required for headless export) |
22 | 23 | chart = Chart(container="container") |
23 | 24 | chart.options = HighchartsOptions() |
24 | 25 |
|
25 | 26 | # Chart configuration |
26 | | -chart.options.chart = { |
27 | | - "type": "area", |
28 | | - "width": 4800, |
29 | | - "height": 2700, |
30 | | - "backgroundColor": "#ffffff", |
31 | | - "style": {"fontFamily": "Arial, sans-serif"}, |
32 | | -} |
| 27 | +chart.options.chart = {"type": "area", "width": 4800, "height": 2700, "backgroundColor": "#ffffff"} |
33 | 28 |
|
34 | 29 | # Title |
35 | | -chart.options.title = {"text": "Monthly Sales Trend", "style": {"fontSize": "48px"}} |
| 30 | +chart.options.title = {"text": "Monthly Sales", "style": {"fontSize": "48px"}} |
36 | 31 |
|
37 | | -# Axes |
| 32 | +# X-axis with categories |
38 | 33 | chart.options.x_axis = { |
| 34 | + "categories": months, |
39 | 35 | "title": {"text": "Month", "style": {"fontSize": "40px"}}, |
40 | | - "categories": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], |
41 | | - "labels": {"style": {"fontSize": "32px"}, "enabled": True}, |
42 | | - "gridLineWidth": 1, |
43 | | - "gridLineColor": "rgba(0, 0, 0, 0.1)", |
44 | | - "tickmarkPlacement": "on", |
| 36 | + "labels": {"style": {"fontSize": "32px"}}, |
45 | 37 | } |
46 | 38 |
|
| 39 | +# Y-axis |
47 | 40 | chart.options.y_axis = { |
48 | | - "title": {"text": "Sales ($)", "style": {"fontSize": "40px"}}, |
| 41 | + "title": {"text": "Sales", "style": {"fontSize": "40px"}}, |
49 | 42 | "labels": {"style": {"fontSize": "32px"}}, |
50 | | - "gridLineWidth": 1, |
51 | | - "gridLineColor": "rgba(0, 0, 0, 0.1)", |
| 43 | + "gridLineColor": "#e0e0e0", |
52 | 44 | } |
53 | 45 |
|
54 | 46 | # Legend |
|
59 | 51 | series.data = sales |
60 | 52 | series.name = "Sales" |
61 | 53 | series.color = "#306998" |
62 | | -series.fill_opacity = 0.5 |
63 | | -series.line_width = 4 |
64 | | -series.marker = {"enabled": True, "radius": 6, "fillColor": "#306998", "lineWidth": 2, "lineColor": "#ffffff"} |
| 54 | +series.fill_opacity = 0.6 |
| 55 | +series.line_width = 3 |
| 56 | +series.marker = {"enabled": True, "radius": 6, "fillColor": "#306998"} |
65 | 57 |
|
66 | 58 | chart.add_series(series) |
67 | 59 |
|
68 | | -# Download Highcharts JS |
| 60 | +# Download Highcharts JS for inline embedding |
69 | 61 | highcharts_url = "https://code.highcharts.com/highcharts.js" |
70 | 62 | with urllib.request.urlopen(highcharts_url, timeout=30) as response: |
71 | 63 | highcharts_js = response.read().decode("utf-8") |
|
94 | 86 | chrome_options.add_argument("--no-sandbox") |
95 | 87 | chrome_options.add_argument("--disable-dev-shm-usage") |
96 | 88 | chrome_options.add_argument("--disable-gpu") |
97 | | -chrome_options.add_argument("--window-size=4800,2800") |
| 89 | +chrome_options.add_argument("--window-size=4800,2700") |
98 | 90 |
|
99 | 91 | driver = webdriver.Chrome(options=chrome_options) |
100 | | -driver.set_window_size(4800, 2800) |
101 | 92 | driver.get(f"file://{temp_path}") |
102 | 93 | time.sleep(5) |
103 | | - |
104 | | -# Get the container element and screenshot it directly |
105 | | -container = driver.find_element("id", "container") |
106 | | -container.screenshot("plot.png") |
| 94 | +driver.save_screenshot("plot.png") |
107 | 95 | driver.quit() |
108 | 96 |
|
109 | 97 | Path(temp_path).unlink() |
0 commit comments