You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派环境下如何通过Highcharts JSON文件自动生成PNG图表?

Using Highcharts Hosted Export API to Generate PNGs from Your JSON Config

Absolutely! You don't need to struggle with running the self-hosted Highcharts export server on your Raspberry Pi. Highcharts provides an official hosted export API that lets you send your chart JSON configuration directly and get a PNG image in return. Here's a step-by-step breakdown of how to implement this, including setting up the daily 21:00 automation:

Core Approach

The hosted export API accepts HTTP POST requests with your chart configuration, export type (PNG, in your case), and other optional parameters (like width). It processes the request and returns the generated image directly (or a URL if using async mode).

Step 1: Validate Your Chart JSON

First, make sure your saved JSON file is a complete, valid Highcharts configuration. It should include all necessary fields like title, xAxis, yAxis, and series with your database data embedded. For example, a valid snippet might look like:

{
  "title": { "text": "Daily Database Metrics" },
  "xAxis": { "categories": ["Mon", "Tue", "Wed", "Thu", "Fri"] },
  "yAxis": { "title": { "text": "Value" } },
  "series": [{ "name": "Metric A", "data": [12, 19, 15, 21, 18] }]
}

Step 2: Send the JSON to the Export API

You can use command-line tools (like curl) or a script (like Python) to send the request. Both work great on a Raspberry Pi.

Option 1: Using curl (Quick Terminal Test)

Run this command in your terminal, replacing your-chart-config.json with your actual file path:

curl -X POST \
  https://export.highcharts.com/ \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "async=false&type=png&width=800&options=$(cat your-chart-config.json | tr -d '\n')" \
  -o daily-chart.png

Let’s break down the parameters:

  • async=false: Tells the API to return the image directly (instead of a URL to download later)
  • type=png: Specifies the export format
  • width=800: Sets the image width (adjust as needed)
  • options=$(cat ... | tr -d '\n'): Reads your JSON file and removes newlines to avoid formatting issues
  • -o daily-chart.png: Saves the output to a PNG file

Option 2: Using Python (Better for Automation)

Create a simple Python script to handle the request—this is easier to integrate with a daily cron job. First, install the requests library if you don’t have it:

pip3 install requests

Then create a script (e.g., generate-chart.py):

import requests
import json

# Load your chart configuration from the JSON file
try:
    with open('your-chart-config.json', 'r') as config_file:
        chart_options = json.load(config_file)
except FileNotFoundError:
    print("Error: Chart config file not found!")
    exit(1)

# Define API parameters
payload = {
    'async': 'false',
    'type': 'png',
    'width': 800,
    'options': json.dumps(chart_options)
}

# Send POST request to the export API
try:
    response = requests.post('https://export.highcharts.com/', data=payload)
    response.raise_for_status()  # Raise error for HTTP status codes >=400
except requests.exceptions.RequestException as e:
    print(f"Request failed: {e}")
    exit(1)

# Save the generated PNG
with open('daily-chart.png', 'wb') as image_file:
    image_file.write(response.content)

print("Success: Chart PNG saved as daily-chart.png")

Step 3: Set Up Daily Automation on Raspberry Pi

Use cron to run the script automatically at 21:00 every day:

  1. Open the crontab editor:
crontab -e
  1. Add this line at the end (replace /path/to/generate-chart.py with the full path to your script):
0 21 * * * python3 /path/to/generate-chart.py
  1. Save and exit the editor—cron will automatically apply the schedule.

Key Notes

  • API Limits: The free hosted export API has rate limits (check Highcharts docs for current limits). If you hit these, you might need to look into a paid plan, but for daily use, it should work fine.
  • JSON Formatting: Ensure your JSON has no syntax errors—use tools like jsonlint.com to validate if you run into issues.
  • Image Quality: Adjust the width parameter to get the resolution you need (higher width = larger, sharper image).

内容的提问来源于stack exchange,提问作者tgraf2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:14:18