echarts について
echarts は、pyecharts と Apache ECharts でデータを可視化するスキルです。一般的なチャートや表示設定を扱い、Python による可視化向けに単体で表示できる HTML レポートを作成します。
主な機能
- 01棒、折れ線、円、ヒートマップ、散布図の作成
- 02テーマ、ラベル、凡例、ツールチップの設定
- 03単体で表示できる HTML への出力
- 04ブラウザー描画向けのリスト形式データ準備
- 05チャートのレイアウトと描画の問題解決
活用シーン
- 01データ分析レポート用のチャート作成
- 02時系列データと移動平均の可視化
- 03散布図での異常値の表示
- 04HTML 可視化レポートの生成
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ rebyteai-template/ rebyte-skills --skill echartsインストール後、Claude Code で/echartsを入力して読み込まれたか確認します。
詳細
ECharts スキル
技術スタック
- pyecharts:Apache ECharts の Python ラッパー
- Apache ECharts:JavaScript チャートライブラリ
- 出力:JS を埋め込んだ自己完結型 HTML
チャートタイプのリファレンス
棒グラフ
from pyecharts.charts import Bar
from pyecharts import options as opts
chart = Bar()
chart.add_xaxis(labels)
chart.add_yaxis("Series Name", values)
chart.set_global_opts(
title_opts=opts.TitleOpts(title="Chart Title"),
tooltip_opts=opts.TooltipOpts(trigger="axis"),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
)
折れ線グラフ
from pyecharts.charts import Line
chart = Line()
chart.add_xaxis(dates)
chart.add_yaxis("Actual", values, is_smooth=True)
chart.add_yaxis("7-Day MA", moving_avg_7, is_smooth=True, linestyle_opts=opts.LineStyleOpts(type_="dashed"))
円グラフ
from pyecharts.charts import Pie
chart = Pie()
chart.add("", list(zip(labels, values)))
chart.set_global_opts(legend_opts=opts.LegendOpts(orient="vertical", pos_left="left"))
ヒートマップ
from pyecharts.charts import HeatMap
chart = HeatMap()
chart.add_xaxis(x_labels)
chart.add_yaxis("", y_labels, value=[[x, y, val], ...])
chart.set_global_opts(
visualmap_opts=opts.VisualMapOpts(min_=0, max_=max_val),
)
散布図(異常値用)
from pyecharts.charts import Scatter
chart = Scatter()
chart.add_xaxis(dates)
chart.add_yaxis("Cost", costs, symbol_size=10)
# Add anomaly markers with different color/size
重要:ブラウザーとの互換性
JavaScript 用に必ずリストに変換してください:
# CORRECT
chart.add_xaxis(df['column'].tolist())
chart.add_yaxis("Label", df['values'].tolist())
# WRONG - causes rendering issues
chart.add_xaxis(df['column'].values) # numpy array
chart.add_xaxis(df['column']) # pandas Series
テーマオプション
pyecharts で利用可能なテーマ:
macarons(デフォルト)- カラフルでプロフェッショナルshine- 明るい色roma- 落ち着いた上品な雰囲気vintage- レトロな雰囲気dark- 暗い背景light- 明るくミニマル
使用方法:
from pyecharts.globals import ThemeType
chart = Bar(init_opts=opts.InitOpts(theme=ThemeType.MACARONS))
HTML レポートの構成
def generate_html_report(self, output_path: str, top_n: int = 10) -> str:
# Create all charts
charts = [
self.create_cost_by_service_chart(top_n),
self.create_cost_by_account_chart(),
# ... more charts
]
# Combine into page
page = Page(layout=Page.SimplePageLayout)
for chart in charts:
page.add(chart)
# Render to file
page.render(output_path)
return output_path
数値の書式設定
# Currency formatting in tooltips
tooltip_opts=opts.TooltipOpts(
trigger="axis",
formatter="{b}: ${c:,.2f}"
)
# Axis label formatting
yaxis_opts=opts.AxisOpts(
axislabel_opts=opts.LabelOpts(formatter="${value:,.0f}")
)
よくある問題と解決策
チャートが空になる
- ブラウザーのコンソールで JS エラーを確認します
- すべてのデータに
.tolist()が適用されていることを確認します - 強制再読み込みします(Ctrl+Shift+R)
- HTML ソースにデータが存在することを確認します
チャートが小さすぎる
init_opts=opts.InitOpts(width="100%", height="400px")
ラベルが重なっている
xaxis_opts=opts.AxisOpts(
axislabel_opts=opts.LabelOpts(rotate=45, interval=0)
)
凡例が長すぎる
legend_opts=opts.LegendOpts(
type_="scroll",
orient="horizontal",
pos_bottom="0%"
)
可視化のテスト
# Test chart creation
uv run pytest tests/test_visualizer.py -v
# Regenerate example report
uv run pytest tests/test_examples.py -v -s
# View in browser
open examples/example_report.html
コメント
Markdown 対応