|
8 | 8 | import urllib.request |
9 | 9 | from pathlib import Path |
10 | 10 |
|
11 | | -import numpy as np |
12 | 11 | from highcharts_core.chart import Chart |
13 | 12 | from highcharts_core.options import HighchartsOptions |
14 | 13 | from highcharts_core.options.series.scatter import ScatterSeries |
15 | | -from PIL import Image |
16 | 14 | from selenium import webdriver |
17 | 15 | from selenium.webdriver.chrome.options import Options |
18 | 16 |
|
19 | 17 |
|
20 | 18 | # Data |
21 | | -np.random.seed(42) |
22 | | -x = np.random.randn(100) * 2 + 10 |
23 | | -y = x * 0.8 + np.random.randn(100) * 2 |
| 19 | +x = [1, 2, 3, 4, 5, 6, 7, 8] |
| 20 | +y = [2.1, 4.3, 3.2, 5.8, 4.9, 7.2, 6.1, 8.5] |
24 | 21 |
|
25 | | -# Create chart |
| 22 | +# Create chart with container |
26 | 23 | chart = Chart(container="container") |
27 | 24 | chart.options = HighchartsOptions() |
28 | 25 |
|
29 | 26 | # Chart configuration |
30 | | -chart.options.chart = { |
31 | | - "type": "scatter", |
32 | | - "width": 4800, |
33 | | - "height": 2700, |
34 | | - "backgroundColor": "#ffffff", |
35 | | - "spacing": [20, 20, 60, 20], |
36 | | -} |
| 27 | +chart.options.chart = {"type": "scatter", "width": 4800, "height": 2700, "backgroundColor": "#ffffff"} |
37 | 28 |
|
38 | 29 | # Title |
39 | | -chart.options.title = {"text": "Basic Scatter Plot", "style": {"fontSize": "48px"}} |
| 30 | +chart.options.title = {"text": "Basic Scatter Plot", "style": {"fontSize": "60px"}} |
40 | 31 |
|
41 | 32 | # Axes |
42 | 33 | chart.options.x_axis = { |
43 | | - "title": {"text": "X Value", "style": {"fontSize": "40px"}}, |
44 | | - "labels": {"style": {"fontSize": "32px"}}, |
| 34 | + "title": {"text": "X Value", "style": {"fontSize": "48px"}}, |
| 35 | + "labels": {"style": {"fontSize": "40px"}}, |
45 | 36 | "gridLineWidth": 1, |
46 | 37 | "gridLineColor": "rgba(0, 0, 0, 0.1)", |
47 | 38 | } |
48 | 39 | chart.options.y_axis = { |
49 | | - "title": {"text": "Y Value", "style": {"fontSize": "40px"}}, |
50 | | - "labels": {"style": {"fontSize": "32px"}}, |
| 40 | + "title": {"text": "Y Value", "style": {"fontSize": "48px"}}, |
| 41 | + "labels": {"style": {"fontSize": "40px"}}, |
51 | 42 | "gridLineWidth": 1, |
52 | 43 | "gridLineColor": "rgba(0, 0, 0, 0.1)", |
53 | 44 | } |
54 | 45 |
|
55 | | -# Legend (not needed for single series but show it for clarity) |
| 46 | +# Legend (not needed for single series, but kept minimal) |
56 | 47 | chart.options.legend = {"enabled": False} |
57 | 48 |
|
58 | | -# Series |
| 49 | +# Add series |
59 | 50 | series = ScatterSeries() |
60 | | -series.data = [[float(xi), float(yi)] for xi, yi in zip(x, y, strict=True)] |
| 51 | +series.data = list(zip(x, y, strict=False)) |
61 | 52 | series.name = "Data" |
62 | | -series.color = "#306998" |
63 | | -series.marker = {"radius": 8, "fillColor": "#306998", "lineWidth": 0, "states": {"hover": {"enabled": True}}} |
64 | | - |
| 53 | +series.marker = {"radius": 20, "fillColor": "#306998", "lineWidth": 2, "lineColor": "#306998"} |
65 | 54 | chart.add_series(series) |
66 | 55 |
|
67 | 56 | # Download Highcharts JS for inline embedding |
|
93 | 82 | chrome_options.add_argument("--no-sandbox") |
94 | 83 | chrome_options.add_argument("--disable-dev-shm-usage") |
95 | 84 | chrome_options.add_argument("--disable-gpu") |
96 | | -chrome_options.add_argument("--window-size=4900,2800") |
| 85 | +chrome_options.add_argument("--window-size=5000,3000") |
97 | 86 |
|
98 | 87 | driver = webdriver.Chrome(options=chrome_options) |
99 | 88 | driver.get(f"file://{temp_path}") |
100 | 89 | time.sleep(5) |
101 | 90 |
|
102 | | -# Screenshot the full window and crop to exact dimensions |
103 | | -driver.save_screenshot("plot_raw.png") |
| 91 | +# Screenshot the chart container element for exact dimensions |
| 92 | +container = driver.find_element("id", "container") |
| 93 | +container.screenshot("plot.png") |
104 | 94 | driver.quit() |
105 | 95 |
|
106 | | -# Crop to exact dimensions (4800 x 2700) |
107 | | -with Image.open("plot_raw.png") as img: |
108 | | - cropped = img.crop((0, 0, 4800, 2700)) |
109 | | - cropped.save("plot.png") |
110 | | -Path("plot_raw.png").unlink() |
111 | | - |
112 | 96 | Path(temp_path).unlink() |
0 commit comments