iOfficeAI/OfficeCLI · error · ArgumentException

Invalid referenceLine width '{widthPt.ToString("G", System.G

Error message

Invalid referenceLine width '{widthPt.ToString("G", System.Globalization.CultureInfo.InvariantCulture)}'. Expected a positive number of points, typically 0.25–10.

What it means

After successfully parsing the referenceLine width as a double, the library enforces a sanity range: widthPt must be strictly > 0 and <= 100. A non-positive width would be invisible or invalid in OOXML; an extremely large width (> 100pt) would be visually absurd and likely indicate a unit confusion (pixels vs points). The formatted value is shown in invariant (G) format.

Source

Thrown at src/officecli/Core/Chart/ChartHelper.Advanced.cs:95

        }
        else if (parts.Length >= 5)
        {
            // Canonical 5-part form: value:color:width:dash:label (extra parts after label are joined
            // back with ':' so labels containing literal colons survive a round-trip).
            var widthStr = parts[2].Trim();
            if (widthStr.Length > 0)
            {
                if (!double.TryParse(widthStr, System.Globalization.NumberStyles.Float,
                        System.Globalization.CultureInfo.InvariantCulture, out widthPt))
                    throw new ArgumentException(
                        $"Invalid referenceLine width '{widthStr}'. Expected a number in points (e.g. '1.5'), or empty for default {DefaultWidthPt}pt.");
            }
            dash = parts[3].Trim();
            label = string.Join(':', parts.Skip(4)).Trim();
        }

        if (widthPt <= 0 || widthPt > 100)
            throw new ArgumentException(
                $"Invalid referenceLine width '{widthPt.ToString("G", System.Globalization.CultureInfo.InvariantCulture)}'. Expected a positive number of points, typically 0.25–10.");

        // Warn: percent-stacked value axis is 0-1 (displayed 0%-100%). A refValue > 1
        // is almost always a mistake — user likely forgot to convert 50 → 0.5.
        // Without this check, Excel silently stretches the val axis to fit (e.g. 5000%),
        // producing a chart where the real bars are compressed to a thin sliver on the left.
        if (refValue > 1.0 && IsPercentStackedChart(plotArea))
        {
            var refMsg =
                $"referenceLine value {refValue.ToString("G", System.Globalization.CultureInfo.InvariantCulture)} "
                + "on a percent-stacked chart. The value axis is 0-1 (0%-100%); "
                + $"did you mean {(refValue / 100.0).ToString("G", System.Globalization.CultureInfo.InvariantCulture)}? "
                + "Excel will auto-scale the axis to fit, compressing the real bars.";
            // CONSISTENCY(numfmt-warning): JSON mode → envelope warnings[];
            // plain mode keeps the stderr line.
            if (WarningContext.IsActive)
                WarningContext.Add(refMsg, "referenceline_out_of_scale",
                    $"Use {(refValue / 100.0).ToString("G", System.Globalization.CultureInfo.InvariantCulture)} for a 0-1 percent axis");

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use a positive width in the range 0.25–10pt: '50:FF0000:2:dash:Label'.
  2. If you want the default width, leave the width segment empty: '50:FF0000::dash:Label'.
  3. Convert pixel values to points (1pt ≈ 1.33px at 96 DPI) before passing.

Example fix

// before
referenceline: "50:FF0000:200:dash:Target"
// after
referenceline: "50:FF0000:2:dash:Target"
Defensive patterns

Strategy: validation

Validate before calling

static bool IsRefLineWidthInRange(string spec)
{
    var parts = spec.Split(':');
    if (parts.Length < 5) return true;
    var widthStr = parts[2].Trim();
    if (widthStr.Length == 0) return true;
    if (!double.TryParse(widthStr, System.Globalization.NumberStyles.Float,
        System.Globalization.CultureInfo.InvariantCulture, out var w)) return false;
    return w > 0 && w <= 100;
}

Try / catch

try { ChartHelperAdvanced.AddReferenceLine(chart, spec); }
catch (ArgumentException ex) when (ex.Message.Contains("Expected a positive number of points"))
{
    Console.Error.WriteLine($"Width out of range. Use 0.25–10pt or leave empty for default.");
}

Prevention

When it happens

Trigger: Passing a referenceLine width of 0, a negative number, or a value > 100 in the 5-part form: '50:FF0000:0:dash:Label', '50:FF0000:-1:dash:Label', '50:FF0000:200:dash:Label'.

Common situations: Using 0 thinking it means 'default' (use empty string instead). Passing pixel values (e.g. 200px) without unit conversion. Negative widths from a calculation error. Confusing the width slot with the value slot.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/7e80deb3f9bca2be. Report an issue: GitHub.