|
| 1 | +""" pyplots.ai |
| 2 | +line-filled: Filled Line Plot |
| 3 | +Library: highcharts unknown | Python 3.13.11 |
| 4 | +Quality: 91/100 | Created: 2025-12-30 |
| 5 | +""" |
| 6 | + |
| 7 | +import tempfile |
| 8 | +import time |
| 9 | +import urllib.request |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import numpy as np |
| 13 | +from highcharts_core.chart import Chart |
| 14 | +from highcharts_core.options import HighchartsOptions |
| 15 | +from highcharts_core.options.series.area import AreaSeries |
| 16 | +from selenium import webdriver |
| 17 | +from selenium.webdriver.chrome.options import Options |
| 18 | + |
| 19 | + |
| 20 | +# Data - Monthly website traffic over a year |
| 21 | +np.random.seed(42) |
| 22 | +months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] |
| 23 | +# Simulating website traffic with seasonal trends (higher in summer) |
| 24 | +base_traffic = 50000 |
| 25 | +seasonal = np.sin(np.linspace(0, 2 * np.pi, 12)) * 15000 |
| 26 | +noise = np.random.normal(0, 3000, 12) |
| 27 | +traffic = base_traffic + seasonal + noise + np.linspace(0, 10000, 12) # Growth trend |
| 28 | +traffic = np.maximum(traffic, 0).astype(int) |
| 29 | + |
| 30 | +# Create chart |
| 31 | +chart = Chart(container="container") |
| 32 | +chart.options = HighchartsOptions() |
| 33 | + |
| 34 | +# Chart configuration |
| 35 | +chart.options.chart = { |
| 36 | + "type": "area", |
| 37 | + "width": 4800, |
| 38 | + "height": 2700, |
| 39 | + "backgroundColor": "#ffffff", |
| 40 | + "marginBottom": 220, |
| 41 | + "marginLeft": 150, |
| 42 | + "spacingTop": 40, |
| 43 | +} |
| 44 | + |
| 45 | +# Title |
| 46 | +chart.options.title = { |
| 47 | + "text": "line-filled · highcharts · pyplots.ai", |
| 48 | + "style": {"fontSize": "48px", "fontWeight": "bold"}, |
| 49 | +} |
| 50 | + |
| 51 | +# Subtitle |
| 52 | +chart.options.subtitle = {"text": "Monthly Website Traffic", "style": {"fontSize": "32px"}} |
| 53 | + |
| 54 | +# X-axis configuration |
| 55 | +chart.options.x_axis = { |
| 56 | + "categories": months, |
| 57 | + "title": {"text": "Month", "style": {"fontSize": "32px"}, "margin": 20}, |
| 58 | + "labels": {"style": {"fontSize": "28px"}}, |
| 59 | + "lineWidth": 2, |
| 60 | + "tickWidth": 2, |
| 61 | +} |
| 62 | + |
| 63 | +# Y-axis configuration |
| 64 | +chart.options.y_axis = { |
| 65 | + "title": {"text": "Page Views", "style": {"fontSize": "32px"}}, |
| 66 | + "labels": {"style": {"fontSize": "24px"}}, |
| 67 | + "gridLineWidth": 1, |
| 68 | + "gridLineColor": "#e0e0e0", |
| 69 | + "min": 0, |
| 70 | +} |
| 71 | + |
| 72 | +# Legend |
| 73 | +chart.options.legend = { |
| 74 | + "enabled": True, |
| 75 | + "itemStyle": {"fontSize": "28px"}, |
| 76 | + "align": "right", |
| 77 | + "verticalAlign": "top", |
| 78 | + "layout": "vertical", |
| 79 | + "x": -50, |
| 80 | + "y": 100, |
| 81 | +} |
| 82 | + |
| 83 | +# Plot options |
| 84 | +chart.options.plot_options = { |
| 85 | + "area": { |
| 86 | + "fillOpacity": 0.4, |
| 87 | + "lineWidth": 4, |
| 88 | + "marker": {"enabled": True, "radius": 8, "lineWidth": 2, "lineColor": "#ffffff"}, |
| 89 | + "states": {"hover": {"lineWidth": 5}}, |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +# Create series |
| 94 | +series = AreaSeries() |
| 95 | +series.name = "Website Traffic" |
| 96 | +series.data = [int(v) for v in traffic] |
| 97 | +series.color = "#306998" |
| 98 | +series.fill_color = { |
| 99 | + "linearGradient": {"x1": 0, "y1": 0, "x2": 0, "y2": 1}, |
| 100 | + "stops": [[0, "rgba(48, 105, 152, 0.5)"], [1, "rgba(48, 105, 152, 0.1)"]], |
| 101 | +} |
| 102 | + |
| 103 | +chart.add_series(series) |
| 104 | + |
| 105 | +# Download Highcharts JS |
| 106 | +highcharts_url = "https://code.highcharts.com/highcharts.js" |
| 107 | +with urllib.request.urlopen(highcharts_url, timeout=30) as response: |
| 108 | + highcharts_js = response.read().decode("utf-8") |
| 109 | + |
| 110 | +# Generate HTML with inline scripts |
| 111 | +html_str = chart.to_js_literal() |
| 112 | +html_content = f"""<!DOCTYPE html> |
| 113 | +<html> |
| 114 | +<head> |
| 115 | + <meta charset="utf-8"> |
| 116 | + <script>{highcharts_js}</script> |
| 117 | +</head> |
| 118 | +<body style="margin:0;"> |
| 119 | + <div id="container" style="width: 4800px; height: 2700px;"></div> |
| 120 | + <script>{html_str}</script> |
| 121 | +</body> |
| 122 | +</html>""" |
| 123 | + |
| 124 | +# Write temp HTML and take screenshot |
| 125 | +with tempfile.NamedTemporaryFile(mode="w", suffix=".html", delete=False, encoding="utf-8") as f: |
| 126 | + f.write(html_content) |
| 127 | + temp_path = f.name |
| 128 | + |
| 129 | +chrome_options = Options() |
| 130 | +chrome_options.add_argument("--headless") |
| 131 | +chrome_options.add_argument("--no-sandbox") |
| 132 | +chrome_options.add_argument("--disable-dev-shm-usage") |
| 133 | +chrome_options.add_argument("--disable-gpu") |
| 134 | +chrome_options.add_argument("--window-size=4800,2700") |
| 135 | + |
| 136 | +driver = webdriver.Chrome(options=chrome_options) |
| 137 | +driver.get(f"file://{temp_path}") |
| 138 | +time.sleep(5) |
| 139 | +driver.save_screenshot("plot.png") |
| 140 | +driver.quit() |
| 141 | + |
| 142 | +Path(temp_path).unlink() |
| 143 | + |
| 144 | +# Save HTML for interactive version |
| 145 | +with open("plot.html", "w", encoding="utf-8") as f: |
| 146 | + f.write(html_content) |
0 commit comments