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 🙏

© 2025 – Pkg Stats / Ryan Hefner

chrt-object

v0.0.19

Published

Universal Object components of Chrt

Readme

chrt-object

Base component for all chrt visualization elements. chrt-object provides the foundational structure and common methods that are inherited by all chrt components (line charts, bar charts, axes, grids, etc.).

This module is used internally and you typically won't need to interact with it directly. However, understanding its methods is useful as they are available in all chrt components.

Common Methods

All chrt components inherit these methods from chrt-object:

Data and Scales

.data([data[, accessor]])

Sets or gets the data for the component. One of chrt's key features is its flexible data management: data can be passed either to the chart itself or to individual components.

Data at Chart Level

When data is set at the chart level, all components will use this data by default:

// Set data at chart level
chrt
  .Chrt()
  .data([1, 2, 3, 4, 5])
  .add(chrt.line()) // will use chart's data
  .add(chrt.bars()); // will use chart's data
Data at Component Level

Each component can have its own data, overriding the chart's data:

// Different data for different components
chrt
  .Chrt()
  .add(chrt.line().data([1, 2, 3, 4, 5]))
  .add(chrt.bars().data([5, 4, 3, 2, 1]));
Using Accessors

Accessors allow you to map your data structure to the required format:

// Complex data with accessor at chart level
const data = [
  { date: "2021-01", sales: 100, profit: 20 },
  { date: "2021-02", sales: 150, profit: 30 },
  { date: "2021-03", sales: 200, profit: 40 },
];

chrt
  .Chrt()
  .data(data, (d) => ({
    x: d.date,
    y: d.sales,
  }))
  .add(chrt.line());

// Different accessors for different components
chrt
  .Chrt()
  .data(data)
  .add(
    chrt.line().data(data, (d) => ({
      x: d.date,
      y: d.sales, // line shows sales
    })),
  )
  .add(
    chrt.bars().data(data, (d) => ({
      x: d.date,
      y: d.profit, // bars show profit
    })),
  );
Multiple Series with Same Data

You can use the same data with different accessors to create multiple series:

// Multiple series from same dataset
const data = [
  { month: "Jan", revenue: 100, costs: 80, profit: 20 },
  { month: "Feb", revenue: 120, costs: 90, profit: 30 },
  { month: "Mar", revenue: 140, costs: 100, profit: 40 },
];

chrt
  .Chrt()
  .add(
    chrt
      .line()
      .data(data, (d) => ({
        x: d.month,
        y: d.revenue,
      }))
      .stroke("#00ff00"),
  )
  .add(
    chrt
      .line()
      .data(data, (d) => ({
        x: d.month,
        y: d.costs,
      }))
      .stroke("#ff0000"),
  )
  .add(
    chrt
      .bars()
      .data(data, (d) => ({
        x: d.month,
        y: d.profit,
      }))
      .fill("#0000ff"),
  );

This flexible data management allows you to:

  • Use different data sources for different components
  • Transform data differently for each component
  • Create multiple visualizations from the same dataset
  • Mix different types of visualizations with different data structures

.x([scaleName]) / .y([scaleName])

Sets or gets the scale to be used for the x/y dimension.

// Use custom scale for x-axis
chart.x("customScale");

// Get current x scale name
const xScale = chart.x();

Styling and Display

.class([className])

Adds CSS class(es) to the component.

// Add single class to a line component
chrt.Chrt().add(chrt.line().class("highlight"));

// Add multiple classes to a bar component
chrt.Chrt().add(chrt.bars().class("highlight bold"));

// Add array of classes to an axis
chrt.Chrt().add(chrt.xAxis().class(["highlight", "bold"]));

.show() / .hide()

Shows or hides the component.

// Hide a line component
chrt.Chrt().add(chrt.line().hide());

// Show a bar component
chrt.Chrt().add(chrt.bars().show());

.attr(name[, value])

Gets or sets custom attributes.

// Set attribute on a line component
chrt.Chrt().add(chrt.line().attr("opacity", 0.5));

// Set attribute with function on a bar component
chrt.Chrt().add(chrt.bars().attr("color", (d, i) => (i % 2 ? "red" : "blue")));

DOM and Rendering

.node([element])

Gets or sets the DOM element containing the chart.

// Set container element
chart.node(document.getElementById("chart"));

// Get current element
const element = chart.node();

.id([value])

Gets or sets the ID of the component.

// Set ID on a line component
chrt.Chrt().add(chrt.line().id("mainLine"));

// Set ID on an axis
chrt.Chrt().add(chrt.xAxis().id("xAxis"));

.parent([object])

Gets or sets the parent object of the component.

// Get parent of a line component
const line = chrt.line();
chrt.Chrt().add(line);
const parent = line.parent();

.render([parent])

Renders the component, optionally within a parent component.

// Render a line component within a chart
const line = chrt.line();
line.render(chart);

Utility Methods

.curve([interpolationFunction])

Sets the interpolation function for line-based visualizations. Uses chrt's own interpolation system through the chrt-interpolations module.

// Set curve interpolation using chrt's spline interpolation
chrt.Chrt().add(chrt.line().curve(chrt.interpolations.spline))

// Other available interpolations
chrt.Chrt().add(chrt.line().curve(chrt.interpolations.linear))
chrt.Chrt().add(chrt.line().curve(chrt.interpolations.step)

.aria([label])

Sets ARIA label for accessibility.

// Set ARIA label on a line component
chrt.Chrt().add(chrt.line().aria("Line showing trend over time"));

Internal Usage

If you're developing components for chrt, you can extend chrt-object:

import chrtObject from "chrt-object";

function MyComponent() {
  chrtObject.call(this);
  this.type = "my-component";

  // Add component-specific methods and properties
  this.draw = () => {
    // Drawing logic
  };
}

MyComponent.prototype = Object.create(chrtObject.prototype);
MyComponent.prototype.constructor = MyComponent;

Testing

The module includes comprehensive tests for all functionality. Run tests using:

npm test

Contributing

When adding new methods to chrt-object, ensure they are:

  1. Generic enough to be useful for multiple components
  2. Well-documented with JSDoc comments
  3. Covered by tests
  4. Following the existing patterns for getters/setters