Files
ppt/ppt_manager_v2/plugins/generators/gdp_generator.py

54 lines
1.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
from plugins.base_generator import BaseGenerator
import pandas as pd
import numpy as np
from typing import Dict, Any
class GDPGenerator(BaseGenerator):
generator_id = "gdp_chart"
generator_name = "GDP趋势图表生成器"
description = "生成GDP季度增长率折线图原生数据"
version = "2.0.0"
params_schema = {
'year': {'type': 'int', 'default': 2026, 'description': '年份'},
'quarter': {'type': 'str', 'default': 'Q2', 'description': '季度'}
}
def fetch_data(self, params: Dict[str, Any] = None) -> bool:
p = {**self.params, **(params or {})}
year = p.get('year', 2026)
quarters = [f"{year-1}Q3", f"{year-1}Q4", f"{year}Q1", f"{year}Q2"]
self._data = pd.DataFrame({
'quarter': quarters,
'GDP同比': [5.2, 4.8, 5.0 + np.random.randn() * 0.3, 5.1 + np.random.randn() * 0.3],
'GDP环比': [1.6, 1.4, 1.2 + np.random.randn() * 0.2, 1.3 + np.random.randn() * 0.2]
})
self.logger.info(f"GDP数据获取成功{len(self._data)} 条记录")
return True
def render(self) -> Dict[str, Any]:
if self._data is None:
self.fetch_data()
categories = self._data['quarter'].tolist()
series = {
'GDP同比增长(%)': self._data['GDP同比'].round(2).tolist(),
'GDP环比增长(%)': self._data['GDP环比'].round(2).tolist()
}
return {
'chart_type': 'line',
'categories': categories,
'series': series,
'dataframe': self._data,
'anchor': 'chart_gdp',
'title': 'GDP增长趋势'
}