-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighcharts.py
More file actions
148 lines (127 loc) · 4.42 KB
/
highcharts.py
File metadata and controls
148 lines (127 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
""" pyplots.ai
area-basic: Basic Area Chart
Library: highcharts 1.10.3 | Python 3.14.2
Quality: 91/100 | Created: 2025-12-23
"""
import tempfile
import time
import urllib.request
from pathlib import Path
import numpy as np
from highcharts_core.chart import Chart
from highcharts_core.options import HighchartsOptions
from highcharts_core.options.series.area import AreaSeries
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# Data - Daily website visitors over a month
np.random.seed(42)
days = np.arange(1, 31)
# Simulate website traffic with weekly pattern and growth trend
base_traffic = 2000 + days * 50 # Growth trend
weekly_pattern = 300 * np.sin(2 * np.pi * days / 7) # Weekly cycle
noise = np.random.normal(0, 200, len(days))
visitors = base_traffic + weekly_pattern + noise
visitors = np.clip(visitors, 500, None).astype(int)
# Create chart
chart = Chart(container="container")
chart.options = HighchartsOptions()
# Chart configuration — generous bottom margin to ensure x-axis title renders fully
chart.options.chart = {
"type": "area",
"width": 4800,
"height": 2700,
"backgroundColor": "#ffffff",
"marginBottom": 300,
"marginLeft": 220,
"spacingBottom": 40,
}
# Title
chart.options.title = {
"text": "area-basic \u00b7 highcharts \u00b7 pyplots.ai",
"style": {"fontSize": "72px", "fontWeight": "bold"},
}
# Subtitle for data context
chart.options.subtitle = {
"text": "Daily Website Visitors Over One Month",
"style": {"fontSize": "42px", "color": "#666666"},
}
# X-axis — explicit margin and offset to prevent title clipping
chart.options.x_axis = {
"title": {"text": "Day of Month", "style": {"fontSize": "48px"}, "margin": 30},
"labels": {"style": {"fontSize": "36px"}, "y": 45},
"gridLineWidth": 1,
"gridLineColor": "rgba(0, 0, 0, 0.1)",
"tickInterval": 1,
}
# Y-axis — min near data floor to maximize visual resolution of the data range
chart.options.y_axis = {
"title": {"text": "Daily Visitors (count)", "style": {"fontSize": "48px"}},
"labels": {"style": {"fontSize": "36px"}},
"gridLineWidth": 1,
"gridLineColor": "rgba(0, 0, 0, 0.1)",
"min": 1500,
"startOnTick": False,
}
# Plot options with semi-transparent fill and gradient
chart.options.plot_options = {
"area": {
"fillColor": {
"linearGradient": {"x1": 0, "y1": 0, "x2": 0, "y2": 1},
"stops": [[0, "rgba(48, 105, 152, 0.5)"], [1, "rgba(48, 105, 152, 0.05)"]],
},
"lineWidth": 4,
"marker": {"enabled": True, "radius": 6, "fillColor": "#306998"},
"color": "#306998",
"tooltip": {"headerFormat": "<b>Day {point.x}</b><br/>", "pointFormat": "Visitors: {point.y:,.0f}"},
}
}
# Legend — enabled with styling for single series identification
chart.options.legend = {
"enabled": True,
"itemStyle": {"fontSize": "36px", "fontWeight": "normal"},
"align": "right",
"verticalAlign": "top",
"layout": "horizontal",
"x": -40,
"y": 60,
}
# Credits off
chart.options.credits = {"enabled": False}
# Add series
series = AreaSeries()
series.data = [[int(d), int(v)] for d, v in zip(days, visitors, strict=True)]
series.name = "Website Visitors"
chart.add_series(series)
# Download Highcharts JS for inline embedding
highcharts_url = "https://code.highcharts.com/highcharts.js"
with urllib.request.urlopen(highcharts_url, timeout=30) as response:
highcharts_js = response.read().decode("utf-8")
# Generate HTML with inline scripts
html_str = chart.to_js_literal()
html_content = f"""<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>{highcharts_js}</script>
</head>
<body style="margin:0;">
<div id="container" style="width: 4800px; height: 2700px;"></div>
<script>{html_str}</script>
</body>
</html>"""
# Write temp HTML and take screenshot
with tempfile.NamedTemporaryFile(mode="w", suffix=".html", delete=False, encoding="utf-8") as f:
f.write(html_content)
temp_path = f.name
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--window-size=4800,2700")
driver = webdriver.Chrome(options=chrome_options)
driver.get(f"file://{temp_path}")
time.sleep(5) # Wait for chart to render
driver.save_screenshot("plot.png")
driver.quit()
Path(temp_path).unlink() # Clean up temp file