|
| 1 | +""" |
| 2 | +histogram-basic: Basic Histogram |
| 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.bar import ColumnSeries |
| 15 | +from selenium import webdriver |
| 16 | +from selenium.webdriver.chrome.options import Options |
| 17 | + |
| 18 | + |
| 19 | +# Data |
| 20 | +np.random.seed(42) |
| 21 | +values = np.random.normal(100, 15, 500) # 500 values, mean=100, std=15 |
| 22 | + |
| 23 | +# Calculate histogram bins |
| 24 | +bins = 30 |
| 25 | +counts, bin_edges = np.histogram(values, bins=bins) |
| 26 | + |
| 27 | +# Create bin labels (center of each bin) |
| 28 | +bin_centers = [(bin_edges[i] + bin_edges[i + 1]) / 2 for i in range(len(counts))] |
| 29 | +bin_labels = [f"{bin_edges[i]:.1f}-{bin_edges[i + 1]:.1f}" for i in range(len(counts))] |
| 30 | + |
| 31 | +# Create chart with container ID |
| 32 | +chart = Chart(container="container") |
| 33 | +chart.options = HighchartsOptions() |
| 34 | + |
| 35 | +# Chart configuration |
| 36 | +chart.options.chart = { |
| 37 | + "type": "column", |
| 38 | + "width": 4800, |
| 39 | + "height": 2700, |
| 40 | + "backgroundColor": "#ffffff", |
| 41 | + "spacingBottom": 120, # Add space for x-axis title |
| 42 | +} |
| 43 | + |
| 44 | +# Title |
| 45 | +chart.options.title = {"text": "Basic Histogram", "style": {"fontSize": "48px", "fontWeight": "bold"}} |
| 46 | + |
| 47 | +# X-axis configuration |
| 48 | +chart.options.x_axis = { |
| 49 | + "categories": bin_labels, |
| 50 | + "title": {"text": "Value", "style": {"fontSize": "40px"}}, |
| 51 | + "labels": { |
| 52 | + "rotation": 315, # 315 degrees = -45 degrees |
| 53 | + "style": {"fontSize": "28px"}, |
| 54 | + "step": 3, # Show every 3rd label to avoid overlap |
| 55 | + }, |
| 56 | +} |
| 57 | + |
| 58 | +# Y-axis configuration |
| 59 | +chart.options.y_axis = { |
| 60 | + "title": {"text": "Frequency", "style": {"fontSize": "40px"}}, |
| 61 | + "min": 0, |
| 62 | + "gridLineWidth": 1, |
| 63 | + "gridLineDashStyle": "Dot", |
| 64 | + "gridLineColor": "rgba(0, 0, 0, 0.15)", |
| 65 | + "labels": {"style": {"fontSize": "32px"}}, |
| 66 | +} |
| 67 | + |
| 68 | +# Create series with histogram data |
| 69 | +series = ColumnSeries() |
| 70 | +series.data = counts.tolist() |
| 71 | +series.name = "Frequency" |
| 72 | +series.color = "#306998" # Python Blue |
| 73 | +series.border_color = "white" |
| 74 | +series.border_width = 1 |
| 75 | + |
| 76 | +# Plot options for histogram appearance |
| 77 | +chart.options.plot_options = {"column": {"pointPadding": 0, "groupPadding": 0, "borderWidth": 1, "opacity": 0.8}} |
| 78 | + |
| 79 | +chart.add_series(series) |
| 80 | + |
| 81 | +# Legend (single series, hide) |
| 82 | +chart.options.legend = {"enabled": False} |
| 83 | + |
| 84 | +# Credits |
| 85 | +chart.options.credits = {"enabled": False} |
| 86 | + |
| 87 | +# Download Highcharts JS (required for headless Chrome) |
| 88 | +highcharts_url = "https://code.highcharts.com/highcharts.js" |
| 89 | +with urllib.request.urlopen(highcharts_url, timeout=30) as response: |
| 90 | + highcharts_js = response.read().decode("utf-8") |
| 91 | + |
| 92 | +# Generate HTML with inline scripts |
| 93 | +html_str = chart.to_js_literal() |
| 94 | +html_content = f"""<!DOCTYPE html> |
| 95 | +<html> |
| 96 | +<head> |
| 97 | + <meta charset="utf-8"> |
| 98 | + <script>{highcharts_js}</script> |
| 99 | +</head> |
| 100 | +<body style="margin:0;"> |
| 101 | + <div id="container" style="width: 4800px; height: 2700px;"></div> |
| 102 | + <script>{html_str}</script> |
| 103 | +</body> |
| 104 | +</html>""" |
| 105 | + |
| 106 | +# Write temp HTML and take screenshot |
| 107 | +with tempfile.NamedTemporaryFile(mode="w", suffix=".html", delete=False, encoding="utf-8") as f: |
| 108 | + f.write(html_content) |
| 109 | + temp_path = f.name |
| 110 | + |
| 111 | +chrome_options = Options() |
| 112 | +chrome_options.add_argument("--headless") |
| 113 | +chrome_options.add_argument("--no-sandbox") |
| 114 | +chrome_options.add_argument("--disable-dev-shm-usage") |
| 115 | +chrome_options.add_argument("--disable-gpu") |
| 116 | +chrome_options.add_argument("--window-size=4800,2800") # Slightly larger to capture full chart |
| 117 | + |
| 118 | +driver = webdriver.Chrome(options=chrome_options) |
| 119 | +driver.get(f"file://{temp_path}") |
| 120 | +time.sleep(5) # Wait for chart to render |
| 121 | +driver.save_screenshot("plot.png") |
| 122 | +driver.quit() |
| 123 | + |
| 124 | +Path(temp_path).unlink() # Clean up temp file |
0 commit comments