|
| 1 | +""" |
| 2 | +scatter-basic: Basic Scatter Plot |
| 3 | +Library: highcharts |
| 4 | +""" |
| 5 | + |
| 6 | +import tempfile |
| 7 | +import time |
| 8 | +import urllib.request |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import numpy as np |
| 12 | +from highcharts_core.chart import Chart |
| 13 | +from highcharts_core.options import HighchartsOptions |
| 14 | +from highcharts_core.options.series.scatter import ScatterSeries |
| 15 | +from selenium import webdriver |
| 16 | +from selenium.webdriver.chrome.options import Options |
| 17 | + |
| 18 | + |
| 19 | +# Data |
| 20 | +np.random.seed(42) |
| 21 | +x = np.random.randn(100) * 2 + 10 |
| 22 | +y = x * 0.8 + np.random.randn(100) * 2 |
| 23 | + |
| 24 | +# Create chart with container specified |
| 25 | +chart = Chart(container="container") |
| 26 | +chart.options = HighchartsOptions() |
| 27 | + |
| 28 | +# Chart configuration |
| 29 | +chart.options.chart = {"type": "scatter", "width": 4800, "height": 2700, "backgroundColor": "#ffffff"} |
| 30 | + |
| 31 | +# Title |
| 32 | +chart.options.title = {"text": "Basic Scatter Plot", "style": {"fontSize": "48px"}} |
| 33 | + |
| 34 | +# Axes |
| 35 | +chart.options.x_axis = { |
| 36 | + "title": {"text": "X Value", "style": {"fontSize": "36px"}}, |
| 37 | + "labels": {"style": {"fontSize": "28px"}}, |
| 38 | + "gridLineWidth": 1, |
| 39 | + "gridLineColor": "rgba(0, 0, 0, 0.1)", |
| 40 | +} |
| 41 | + |
| 42 | +chart.options.y_axis = { |
| 43 | + "title": {"text": "Y Value", "style": {"fontSize": "36px"}}, |
| 44 | + "labels": {"style": {"fontSize": "28px"}}, |
| 45 | + "gridLineWidth": 1, |
| 46 | + "gridLineColor": "rgba(0, 0, 0, 0.1)", |
| 47 | +} |
| 48 | + |
| 49 | +# Legend |
| 50 | +chart.options.legend = {"enabled": False} |
| 51 | + |
| 52 | +# Create scatter series |
| 53 | +series = ScatterSeries() |
| 54 | +series.data = [[float(xi), float(yi)] for xi, yi in zip(x, y, strict=True)] |
| 55 | +series.name = "Data" |
| 56 | +series.color = "#306998" |
| 57 | +series.marker = {"radius": 10, "fillColor": "#306998", "lineWidth": 0, "states": {"hover": {"enabled": True}}} |
| 58 | + |
| 59 | +chart.add_series(series) |
| 60 | + |
| 61 | +# Credits |
| 62 | +chart.options.credits = {"enabled": False} |
| 63 | + |
| 64 | +# Download Highcharts JS for inline embedding |
| 65 | +highcharts_url = "https://code.highcharts.com/highcharts.js" |
| 66 | +with urllib.request.urlopen(highcharts_url, timeout=30) as response: |
| 67 | + highcharts_js = response.read().decode("utf-8") |
| 68 | + |
| 69 | +# Generate HTML with inline scripts |
| 70 | +html_str = chart.to_js_literal() |
| 71 | +html_content = f"""<!DOCTYPE html> |
| 72 | +<html> |
| 73 | +<head> |
| 74 | + <meta charset="utf-8"> |
| 75 | + <script>{highcharts_js}</script> |
| 76 | +</head> |
| 77 | +<body style="margin:0;"> |
| 78 | + <div id="container" style="width: 4800px; height: 2700px;"></div> |
| 79 | + <script>{html_str}</script> |
| 80 | +</body> |
| 81 | +</html>""" |
| 82 | + |
| 83 | +# Write temp HTML and take screenshot |
| 84 | +with tempfile.NamedTemporaryFile(mode="w", suffix=".html", delete=False, encoding="utf-8") as f: |
| 85 | + f.write(html_content) |
| 86 | + temp_path = f.name |
| 87 | + |
| 88 | +chrome_options = Options() |
| 89 | +chrome_options.add_argument("--headless") |
| 90 | +chrome_options.add_argument("--no-sandbox") |
| 91 | +chrome_options.add_argument("--disable-dev-shm-usage") |
| 92 | +chrome_options.add_argument("--disable-gpu") |
| 93 | +chrome_options.add_argument("--window-size=4800,2700") |
| 94 | + |
| 95 | +driver = webdriver.Chrome(options=chrome_options) |
| 96 | +driver.get(f"file://{temp_path}") |
| 97 | +time.sleep(5) |
| 98 | +driver.save_screenshot("plot.png") |
| 99 | +driver.quit() |
| 100 | + |
| 101 | +Path(temp_path).unlink() |
0 commit comments