0.0.44 • Published 8 months ago

chrt-label v0.0.44

Weekly downloads
73
License
MIT
Repository
github
Last release
8 months ago

chrt-label

Component for creating and managing labels in chrt charts. Labels can be added automatically to chart elements (using chrtLabels) or manually positioned at specific coordinates (using chrtLabel). The module provides two main components:

  • chrtLabels: Automatically generates and positions labels for multiple data points
  • chrtLabel: Creates individual labels with specific positioning

Observable Examples and Documentation:

Installing

For use with Webpack, Rollup, or other Node-based bundlers, chrt-label can be installed as a standalone module via a package manager such as Yarn or npm.

npm install chrt-label chrt-core

chrt-label can be used as part of the chrt package:

npm install chrt

Usage

ES6 / Bundlers (Webpack, Rollup, etc.)

import Chrt from "chrt-core";
import { chrtLabel, chrtLabels } from "chrt-label";

// Add automatic labels to points
Chrt().add(
  chrt
    .chrtPoints()
    .data(data)
    .add(chrtLabels().value((d) => d.value)),
);

// Add a single manual label
Chrt().add(chrt.columns().add(chrtLabel("United States").left("1970").top(14)));

API Reference

chrtLabels

Creation and Data

chrtLabels()

Creates a new labels component that automatically generates labels for chart elements.

// Basic labels
chrtLabels().value((d) => d.value);

// Filtered labels
chrtLabels()
  .filter((d) => d.value > 100)
  .value((d) => d.value);

.value([accessor])

Sets the text content for labels using a data accessor function.

// Use simple value
chrtLabels().value((d) => d.value);

// Format value
chrtLabels().value((d) => `$${d.value.toFixed(2)}`);

Positioning and Alignment

.align([value]) / .valign([value])

Sets horizontal and vertical alignment of labels.

chrtLabels()
  .align("start") // "start", "middle", "end"
  .valign("middle"); // "top", "middle", "bottom"

.offset([x, y])

Sets the offset of labels from their anchor points.

// Fixed offset
chrtLabels().offset(5, 0);

// Dynamic offset
chrtLabels().offset((d, i) => [i * 2, 0]);

Filtering Methods

.filter([condition]) / .show([condition])

Controls which labels are displayed.

// Show labels for specific values
chrtLabels().filter((d) => d.value > 100);

// Show labels based on index
chrtLabels().filter((d, i) => i % 2 === 0);

.firstLabel() / .lastLabel()

Shows only first or last label.

// Show only first label
chrtLabels().firstLabel();

// Show only last label
chrtLabels().lastLabel();

Styling

.color([value]) / .fill([value])

Sets the color of labels.

// Single color
chrtLabels().color("#333333");

// Color based on data
chrtLabels().color((d) => (d.value > 100 ? "red" : "blue"));

chrtLabel

Creation and Text

chrtLabel(text)

Creates a single label with specified text.

// Create label with text
chrtLabel("United States").left("1970").top(14);

.value(text) / .text(text)

Sets or updates the label text.

chrtLabel().value("Label Text").color("#333");

Positioning

.position([x, y]) / .left(value) / .top(value)

Sets the position of the label.

// Using position
chrtLabel("Text").position({ x: 100, y: 50 });

// Using left/top
chrtLabel("Text").left("1970").top(14);

Styling and Alignment

.align([value]) / .valign([value])

Sets text alignment.

chrtLabel("Text")
  .align("start") // "start", "middle", "end"
  .valign("middle"); // "top", "middle", "bottom"

.color([value]) / .fill([value])

Sets the color of the label.

chrtLabel("Text").color("#ffffff").align("start");

Examples

Automatic Labels on Points

Chrt().add(
  chrt
    .dotPlot()
    .data(data)
    .add(
      chrtLabels()
        .filter((d) => d.category === "A")
        .value((d) => d.value.toFixed(1))
        .offset(5, 0)
        .align("start")
        .valign("middle")
        .color("#999"),
    ),
);

Mixed Manual and Automatic Labels

Chrt().add(
  chrt
    .columns()
    .data(data)
    .add(
      chrtLabels()
        .value((d) => d.value)
        .offset(0, -5),
    )
    .add(
      chrtLabel("Maximum")
        .left(maxX)
        .top(maxY)
        .align("middle")
        .valign("bottom"),
    ),
);
0.0.44

8 months ago

0.0.42

1 year ago

0.0.43

1 year ago

0.0.41

1 year ago

0.0.40

3 years ago

0.0.38

4 years ago

0.0.39

3 years ago

0.0.37

4 years ago

0.0.35

4 years ago

0.0.36

4 years ago

0.0.34

4 years ago

0.0.33

4 years ago

0.0.32

4 years ago

0.0.31

4 years ago

0.0.30

4 years ago

0.0.29

4 years ago

0.0.28

4 years ago

0.0.27

4 years ago

0.0.26

4 years ago

0.0.20

4 years ago

0.0.21

4 years ago

0.0.22

4 years ago

0.0.23

4 years ago

0.0.24

4 years ago

0.0.25

4 years ago

0.0.19

4 years ago

0.0.16

4 years ago

0.0.17

4 years ago

0.0.18

4 years ago

0.0.15

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago