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

PowerShell柱状图添加顶部二级X轴(Time字段)技术问题

Fixing Secondary X-Axis (Time) Binding in PowerShell Chart

Got it, let's get that secondary Time axis working with your chart! The issue is that DataBindXY only handles the primary X/Y axes by default—we need to manually map your CSV's Time values to the secondary X-axis after binding the main data.

Here's the modified code with explanations:

First, let's adjust the chart area to ensure the secondary X-axis is enabled (it might be disabled by default):

$chartarea.AxisX2.Enabled = [System.Windows.Forms.DataVisualization.Charting.AxisEnabled]::True
$chartarea.AxisX2.Interval = 1  # Match primary X-axis interval for perfect alignment

Then, after binding your main data, loop through each data point and assign the corresponding Time value to the secondary axis label:

# Bind primary X (Date) and Y (TotalMinutes) axes first
$chart1.Series["Elapse"].Points.DataBindXY($date, $totalMinutes)

# Map Time values to secondary X-axis (AxisX2)
for ($i = 0; $i -lt $chart1.Series["Elapse"].Points.Count; $i++) {
    $chart1.Series["Elapse"].Points[$i].AxisLabel2 = $time[$i]
}

Full modified PowerShell code:

#CONSTANTS
$directory = "C:\Users\gamble.pw\Desktop\powershell"
$csvFile = "logfile.csv"

#chart object
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")
$chart1 = New-object System.Windows.Forms.DataVisualization.Charting.Chart
$chart1.Width = 2000
$chart1.Height = 1000
$chart1.BackColor = [System.Drawing.Color]::White

#title
[void]$chart1.Titles.Add("Login Time")
$chart1.Titles[0].Font = "segoeuilight,20pt"
$chart1.Titles[0].Alignment = "topLeft"

#chart area
$chartarea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
$chartarea.Name = "ChartArea1"
$chartarea.AxisY.Title = "Elapse Time in Minutes"
$chartarea.AxisX.Title = "Date"
$chartarea.AxisX2.Title = "Time"
# Enable secondary X-axis and set interval for alignment
$chartarea.AxisX2.Enabled = [System.Windows.Forms.DataVisualization.Charting.AxisEnabled]::True
$chartarea.AxisX2.Interval = 1
$chartarea.AxisY.Interval = 1
$chartarea.AxisX.Interval = 1
$chart1.ChartAreas.Add($chartarea)

#data source
$datasource = Import-Csv "$directory\$csvFile"
$date = $datasource.Date
$time = $datasource.Time
$totalMinutes = $datasource.TotalMinutes

#data series
[void]$chart1.Series.Add("Elapse")
$chart1.Series["Elapse"].BorderWidth = 2
# Bind primary axes
$chart1.Series["Elapse"].Points.DataBindXY($date, $totalMinutes)

# Assign Time values to secondary X-axis labels
for ($i = 0; $i -lt $chart1.Series["Elapse"].Points.Count; $i++) {
    $chart1.Series["Elapse"].Points[$i].AxisLabel2 = $time[$i]
}

$chart1.SaveImage("$directory\Bar_Chart_Login.png","png")

Key Notes:

  • The AxisLabel2 property of each data point is specifically designed for the secondary X-axis (AxisX2)
  • We enable AxisX2 explicitly to ensure it renders on the chart (it’s disabled by default)
  • The loop uses index matching to keep Date/Time/TotalMinutes values perfectly synced across both axes

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 21:02:37