🐷

Vue 3で「実行済み+実行中+未実行」を1本の帯グラフでまとめて表示する方法

に公開3件

ジョブ・タスク管理UIなどで、行ごとに

  • 実行済み
  • 実行中
  • 未実行

といった進捗を割合バーで視覚化したい場面はよくあります。

しかし、表のUI上は

  • ヘッダは「実行済み/実行中/未実行」の3列を残したい
  • tbodyでは3列分を1本の帯グラフでまとめたい

という設計が自然な場合があります。

この記事では、Vue3(Composition API)+Tailwindの組み合わせで

  • <thead>は既存のまま
  • <tbody>で3列をcolspan="3"でまとめて帯グラフ化

する実装パターンを紹介します。


1. 表示仕様の整理

やりたいことは次のとおり。

  • ヘッダ
    進捗率 / 実行済み / 実行中 / 未実行(今のまま)

  • tbody
    実行済み+実行中+未実行 の3列を 1セル(colspan="3") に統合し
    その中で

    • 緑=実行済み
    • 青=実行中
    • 赤=未実行
      の割合バーとして描画する
  • 帯全体の横幅は
    “ヘッダの3列(実行済み+実行中+未実行)”に対応


2. <script setup>:割合を幅%に変換する関数

<script setup lang="ts">
import { ref, computed } from 'vue'

// 行ごとの集計データ
interface ProgressRow {
  processName: string      
  targetName: string       
  total: number            // 全体件数
  completed: number        // 実行済み件数
  inProgress: number       // 実行中件数
  planned: number          // 未実行件数(total - completed - inProgress)
  rate: number             // 進捗率(%)
}

// サンプルデータ
const allProgressRows = ref<ProgressRow[]>([
  { processName: 'ジョブA', targetName: 'サーバ1', total: 4, completed: 2, inProgress: 1, planned: 1, rate: 75 },
  { processName: 'ジョブB', targetName: 'サーバ2', total: 4, completed: 1, inProgress: 2, planned: 1, rate: 75 }
])

// フィルタ用
const selectedProcess = ref<string>('')
const selectedTarget = ref<string>('')

const filteredRows = computed<ProgressRow[]>(() => {
  return allProgressRows.value.filter((row) => {
    const matchProcess = !selectedProcess.value || row.processName === selectedProcess.value
    const matchTarget = !selectedTarget.value || row.targetName === selectedTarget.value
    return matchProcess && matchTarget
  })
})

// 帯グラフ用の幅%計算
const getPercentWidth = (part: number, total: number): string => {
  if (!total || total <= 0 || !part || part <= 0) return '0%'
  const pct = (part / total) * 100
  return `${pct.toFixed(1)}%`
}
</script>

3. <template>:3列を1セルで帯グラフ化

<thead class="bg-gray-100">
  <tr>
    <th rowspan="2" class="border px-4 py-2 bg-white">処理名</th>
    <th rowspan="2" class="border px-4 py-2 bg-white">対象</th>
    <th colspan="4" class="border px-4 py-2 bg-white text-center">全体進捗</th>
  </tr>
  <tr>
    <th class="border bg-gray-200 px-4 py-1 text-center">進捗率</th>
    <th class="bg-green-300 px-4 py-1 text-center">実行済み</th>
    <th class="bg-blue-300 px-4 py-1 text-center">実行中</th>
    <th class="bg-red-300 px-4 py-1 text-center">未実行</th>
  </tr>
</thead>

<tbody>
  <tr v-for="row in filteredRows" :key="row.processName + '|' + row.targetName">
    <!-- 処理名 -->
    <td class="border-r border-gray-300 bg-white px-6 py-2">
      {{ row.processName }}
    </td>

    <!-- 対象 -->
    <td class="border-r border-gray-300 bg-white px-6 py-2">
      {{ row.targetName }}
    </td>

    <!-- 進捗率 -->
    <td class="border-r border-gray-300 bg-gray-200 px-4 py-2 text-center">
      {{ row.rate }}%
    </td>

    <!-- 実行済み+実行中+未実行 を1本の帯で表示 -->
    <td class="border-r border-gray-300 bg-white px-4 py-2" colspan="3">

      <!-- 上部ラベル -->
      <div class="flex justify-between text-[11px] text-gray-700 mb-1">
        <span>実行済み</span>
        <span>実行中</span>
        <span>未実行</span>
      </div>

      <!-- 帯グラフ本体 -->
      <div class="w-full h-5 flex rounded overflow-hidden border">
        <div
          class="h-5 bg-green-300"
          :style="{ width: getPercentWidth(row.completed, row.total) }"
        ></div>

        <div
          class="h-5 bg-blue-300"
          :style="{ width: getPercentWidth(row.inProgress, row.total) }"
        ></div>

        <div
          class="h-5 bg-red-300"
          :style="{ width: getPercentWidth(row.planned, row.total) }"
        ></div>
      </div>

      <!-- 件数の補足表示 -->
      <div class="mt-1 text-[10px] text-gray-600 text-center">
        実行済み: {{ row.completed }} / 実行中: {{ row.inProgress }} / 未実行: {{ row.planned }}
        (全体: {{ row.total }})
      </div>
    </td>
  </tr>
</tbody>

4. 表示イメージ

例えば:

  • total = 4
  • completed = 1(実行済み)
  • inProgress = 2(実行中)
  • planned = 1(未実行)

なら、帯は

  • 緑25%
  • 青50%
  • 赤25%

という構成になります。

3つの列幅に対応した1本の横棒グラフとして、進捗が一目で分かります。


5. まとめ

今回の変更点:

  • itemA → processName
  • itemB → targetName
  • 進捗区分を「実行済み/実行中/未実行」に統一
  • サンプルデータ・コード・ラベルも全面反映済み

この構成は、

  • バッチジョブ管理
  • スケジューラ管理
  • 処理キューの可視化
  • モジュール別の処理状況表示

など、多くの場面で使えます。

Discussion

junerjuner

実際の進捗数を カスタムプロパティで入れてやって css で計算したくなりますね。

<div class="[&>*]:w-[calc(max(0,var(--part))_/_max(0,var(--total))_*_100%)] w-full h-5 flex rounded overflow-hidden border"
  :style="{
    `--total`: row.total,
  }">
  <div
    class="h-5 bg-green-300"
    :style="{ `--part`: row.completed }"
  ></div>

  <div
    class="h-5 bg-blue-300"
    :style="{ `--part`: row.inProgress }"
  ></div>

  <div
    class="h-5 bg-red-300"
    :style="{ `--part`: row.planned }"
  ></div>
</div>

tailwindcss で表現しているのはつまりこういうこと

& > * {
   width: calc(max(0,var(--part)) / max(0,var(--total)) * 100%);
}
tamakantamakan

コメントしていただきありがとうございます。
現在のJS側に計算を任せるという方法ではなく、css側で計算を任せるといった形にした方がよいという
ことで理解させていただきました。
今回このように他の方法を教えていただくのが初めてなので大変嬉しいです。
教えていただきありがとうございました。

1