CxJS

LineGraph

import { LineGraph } from 'cx/charts'; Copied

Line charts are commonly used for data trends visualization. The LineGraph widget renders a series of data points connected by line segments, with optional area fill, smoothing, and stacking capabilities.

<div controller={PageController}>
  <Legend />
  <Svg style="width: 100%; height: 300px;">
    <Chart
      offset="20 -10 -40 40"
      axes={{
        x: { type: NumericAxis, lineStyle: "stroke: transparent" },
        y: { type: NumericAxis, vertical: true },
      }}
    >
      <Gridlines />
      <LineGraph
        name="Series 2"
        data={m.points}
        colorIndex={8}
        yField="y2h"
        y0Field="y2l"
        active={bind(m.line2, true)}
        line={false}
        area={m.showArea}
        smooth={m.smooth}
        smoothingRatio={m.smoothingRatio}
      />
      <LineGraph
        name="Series 1"
        data={m.points}
        colorIndex={0}
        area={m.showArea}
        active={bind(m.line1, true)}
        smooth={m.smooth}
        smoothingRatio={m.smoothingRatio}
        line={m.showLine}
      />
      <LineGraph
        name="Series 2"
        data={m.points}
        colorIndex={8}
        yField="y2"
        active={bind(m.line2, true)}
        smooth={m.smooth}
        smoothingRatio={m.smoothingRatio}
        line={m.showLine}
      />
    </Chart>
  </Svg>

  <LabelsTopLayout columns={4} mod="stretch">
    <LabelsTopLayoutCell colSpan={2}>
      <Slider
        label="Data points count"
        maxValue={200}
        minValue={5}
        step={1}
        value={bind(m.pointsCount, 50)}
        help={expr(m.pointsCount, (v) => `${v} points`)}
      />
    </LabelsTopLayoutCell>
    <Switch label="Area" value={m.showArea} />
    <Switch label="Line" value={m.showLine} />

    <Switch label="Smooth" value={m.smooth} style="margin-right: 20px" />
    <Slider
      label="Smoothing ratio"
      enabled={m.smooth}
      value={bind(m.smoothingRatio, 0.07)}
      maxValue={0.4}
      minValue={0}
      step={0.01}
      help={expr(m.smoothingRatio, (v) => v?.toFixed(2) ?? "")}
    />
  </LabelsTopLayout>
</div>
Series 2
Series 1
 
50 points
 
 
 
 
0.07

The example above demonstrates multiple line series with area fill, smooth curves, and a range band (using y0Field and yField to define upper and lower bounds). Use the controls to adjust the number of data points, toggle area/line visibility, and modify the smoothing effect.

Configuration

Data Properties

PropertyTypeDefaultDescription
dataarrayData for the graph. Each entry should be an object with at least two properties whose names should match xField and yField
xFieldstring"x"Name of the property which holds the x value
yFieldstring"y"Name of the property which holds the y value
y0Fieldstring/falsefalseName of the property which holds the y0 (base) value. Used for range/band charts
y0number0Base value used for area charts when y0Field is not specified

Appearance

PropertyTypeDefaultDescription
colorIndexnumberIndex of a color from the standard palette (0-15)
colorMapstringUsed to automatically assign a color based on name and the contextual ColorMap widget
colorNamestringName used to resolve the color. If not provided, name is used instead
linebooleantrueShow the line connecting data points. Set to false to hide
lineStylestring/objectAdditional styles applied to the line element
areabooleanfalseFill the area under the line
areaStylestring/objectAdditional styles applied to the area element
hiddenBasebooleanfalseIf true, clips the base of the graph to show only the value range

Smoothing

PropertyTypeDefaultDescription
smoothbooleanfalseSet to true to draw smoothed lines using cubic Bézier curves
smoothingRationumber0.05Controls the intensity of smoothing (0 to 0.4). Higher values create more curved lines

Stacking

PropertyTypeDefaultDescription
stackedbooleanfalseStack values on top of other series sharing the same stack
stackstring"stack"Name of the stack. Use different names for multiple independent stacks

Axes

PropertyTypeDefaultDescription
xAxisstring"x"Name of the horizontal axis to use
yAxisstring"y"Name of the vertical axis to use

Legend

PropertyTypeDefaultDescription
namestringName of the series as it will appear in the legend
activebooleantrueIndicates if the series is active. Inactive series are shown only in legend
legendstring/false"legend"Name of the legend to use. Set to false to hide from legend
legendActionstring"auto"Action to perform on legend item click
legendShapestring"rect"Shape to display in the legend entry