0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PgAdmin4でグラフを出力する

0
Posted at

PgAdmin4にグラフ描画できる機能が大分前に追加されていることを知ったので試してみます。

今回は分かりやすくエネルギー消費量の折れ線チャートを引いてみます

データはこちらからダウンロードします

データの中身については精査していないので、集計の妥当性については考慮外とします。

createdb -U postgres energy
psql -U postgres -d energy
energy=# CREATE TABLE "metrics"(
energy(#     created timestamp with time zone default now() not null,
energy(#     type_id integer                                not null,
energy(#     value   double precision                       not null
energy(# );
CREATE TABLE
energy=# \copy metrics FROM 'metrics.csv' DELIMITER ',' CSV HEADER;
COPY 2523725

データの準備が出来たらPgAdmin4を起動し、グラフ化したいクエリを実行します。

select
  created,
  sum(value)
from
  (
    select
      date_trunc('hour', created) as created,
      value
    from
      metrics
  )
group by
  created;

1.png

実行後、右下の波線アイコンをクリックします。
2.png

グラフの種類と軸を設定し、Generateボタンを押すとグラフが生成されます。
また、画像としてダウンロードもできます。
graph_visualiser-1725803366018.png

折れ線チャート以外も作成できます。

select
  type_id,
  sum(value)
from
  metrics
group by
  type_id;

graph_visualiser-1725803556220.png

わざわざCSVを出力して、Excelやダッシュボードに取り込まなくても簡単なグラフなら作成できるので、良い機能だと思いました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?