npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chrt-label

v0.0.44

Published

Label for XY charts component for Chrt

Downloads

156

Readme

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"),
    ),
);