modern-xlsx API Reference - v1.3.0
    Preparing search index...

    Class ChartBuilder

    Fluent builder for creating chart definitions.

    const chart = new ChartBuilder('bar')
    .title('Monthly Sales')
    .addSeries({ nameLiteral: 'Revenue', catRef: 'Sheet1!$A$2:$A$5', valRef: 'Sheet1!$B$2:$B$5' })
    .legend('bottom')
    .anchor({ col: 0, row: 0 }, { col: 10, row: 20 })
    .build();
    Index
    • Set the anchor position (cell coordinates).

      Parameters

      • from: { col: number; colOff?: number; row: number; rowOff?: number }

        Top-left anchor cell (col, row, optional EMU offsets).

      • to: { col: number; colOff?: number; row: number; rowOff?: number }

        Bottom-right anchor cell (col, row, optional EMU offsets).

      Returns this

      this for chaining.

      builder.anchor({ col: 0, row: 0 }, { col: 10, row: 20 });
      
    • Build the final chart data object ready for insertion into a worksheet.

      Returns an independent deep copy; later builder calls do not affect it.

      Every chart type except 'pie' and 'doughnut' has axes, and the OOXML schema requires two axis ids for them. An axis that was not configured with ChartBuilder.catAxis or ChartBuilder.valAxis is therefore filled with its default in the returned model (category id 0, value id 1). Pie and doughnut charts keep catAxis and valAxis as null. For scatter and bubble charts the category slot is written as the bottom value axis. A ChartBuilder.secondaryChart gets no default axes: it is drawn on this chart's (under a pie or doughnut primary, on the default axes the writer adds).

      Series without an explicit idx or order are numbered here, across this chart and then its secondary chart (see ChartBuilder.addSeries).

      Returns WorksheetChartData

      A WorksheetChartData containing the chart model and anchor position.

      When the secondary chart has a secondary chart of its own.

    • Set the plot area manual layout (fractional 0.0-1.0).

      Parameters

      • x: number

        Left offset as a fraction of chart width.

      • y: number

        Top offset as a fraction of chart height.

      • w: number

        Width as a fraction of chart width.

      • h: number

        Height as a fraction of chart height.

      Returns this

      this for chaining.

    • Make this a combo chart: the chart secondary builds becomes ChartDataModel.secondaryChart, a second chart-type element drawn on this chart's axes (or on secondaryCatAxis / secondaryValAxis when they are set on the built model) (under a pie or doughnut primary, on the default axes the writer adds). Only its chart type, series and chart-type options (bar direction, grouping, scatter or radar style, data labels, hole size) are written; its own axes, title, legend and anchor are not.

      secondary is read when ChartBuilder.build runs. Its series continue this chart's numbering: with no idx or order set, a column chart with two series and a line chart with one get 0 and 1, then 2. Excel refuses a chart whose series share an order.

      Parameters

      • secondary: ChartBuilder

        The builder of the secondary chart. It must not have a secondary chart of its own, or build() throws a RangeError.

      Returns this

      this for chaining.

      const combo = new ChartBuilder('column')
      .addSeries({ nameLiteral: 'Sales', catRef: 'Sheet1!$A$2:$A$5', valRef: 'Sheet1!$B$2:$B$5' })
      .secondaryChart(
      new ChartBuilder('line').addSeries({ nameLiteral: 'Trend', valRef: 'Sheet1!$C$2:$C$5' }),
      )
      .build();
    • Set the chart title.

      Parameters

      • text: string

        The title text.

      • Optionalopts: { bold?: boolean; color?: string; fontSize?: number }

        Optional formatting: bold, fontSize in hundredths of a point (1400 = 14pt, written verbatim as DrawingML sz; the validated range is 100-400000) and color.

      Returns this

      this for chaining.

    • Set 3D rotation settings.

      Parameters

      • opts: { perspective?: number; rAngAx?: boolean; rotX?: number; rotY?: number }

        Rotation angles and perspective options.

      Returns this

      this for chaining.