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 🙏

© 2026 – Pkg Stats / Ryan Hefner

d3-chart-khoatr

v0.1.2

Published

A D3 chart with React

Readme

KhoaTr197's D3 Chart

About

This project is a chart module built on D3.js. It provides a set of reusable chart components that can be easily customized and embedded into any React project. The goal is to simplify the process of creating interactive and visually appealing data visualizations.

Feel free to explore the code and contribute to the project!

Features

  • Supports chart types like Line, Bar, and Pie
  • Provides scale handlers
  • Provides data transformation handlers

How to use

Install the package via npm:

npm install d3_chart_khoatr

Full Example

For more details, please refer to the manual below this example.

import { data } from "./data";
import ChartD3 from "./components/ChartD3";
import ScalesD3 from "./components/ScalesD3";
import TransformD3 from "./components/TransformD3";

const App = () => {
  const [chartData] = useState(JSON.parse(data));
  const [ref, settings] = ChartD3.useChartSettings({
    marginLeft: 32,
    marginRight: 32,
    marginTop: 32,
  });

  const companyNames = chartData.map((item) => item.company);
  const initialPrices = [0, ...chartData.map((item) => item.initial_price)].sort((a, b) => a - b);

  const xScale = ScalesD3.band(companyNames, [
    settings.marginLeft,
    (settings.width || 0) - settings.marginRight,
  ]);
  const yScale = ScalesD3.linear(TransformD3.extent(initialPrices), [
    (settings.height || 0) - settings.marginBottom,
    settings.marginTop,
  ]);

  return (
    <div id="app">
      <div
        className="chart-wrap"
        style={{
          width: "90vw",
          height: "90vh",
          maxWidth: "100vw",
          maxHeight: "100vh",
        }}
        ref={ref}
      >
        <ChartD3.Line
          data={chartData}
          field={{
            x: "date",
            y: "initial_price",
          }}
          scale={{
            x: xScale,
            y: yScale,
          }}
          config={settings}
        />
      </div>
    </div>
  );
};

export default App;

Manual

ChartD3

ChartD3 contains three types of charts: Line, Bar, and Pie, along with a custom hook useChartSettings.

useChartSettings

useChartSettings is a function that handles the chart's settings, determining how the chart will be rendered. It needs to be called first before you create a chart.

useChartSettings returns [ref, settings]:

  • ref: A React ref (Read more). It needs to be assigned to the parent element containing your chart because useChartSettings can listen to resize events and automatically adjust the width and height (if not set fixed width & height) based on the element you assigned the ref to.
  • settings: An object containing the chart's information:
    • width: Chart's width
    • height: Chart's height
    • marginLeft, marginRight, marginTop, marginBottom: Space between the chart and its contents
    • boundedWidth: Chart's content width
    • boundedHeight: Chart's content height

Example

import ChartD3 from "./ChartD3";

const App = () => {
  const [ref, settings] = ChartD3.useChartSettings({
    width: 400,
    height: 300,
    marginLeft: 32,
    marginRight: 32,
    marginTop: 32,
    marginBottom: 32,
  });

  return <ChartD3.Line ref={ref} config={settings} {...otherProps} />;
};

ChartD3.[Line|Bar|Pie]

To create a chart, ChartD3 components need to be passed some props.

Common props for all charts:

  • data: Data you want to visualize (Required)
  • id: Chart's ID (Optional)
  • classes: Chart's class (Optional)
  • config: Chart's settings (Required)

Specific props for each chart type:

  • Line:

    • field: An object specifying the data attributes for the x and y axes. For example, { x: "date", y: "value" }.
    • scale: An object specifying the scales for the x and y axes. For example, { x: xScale, y: yScale }.
    • style: Line's styles
      • fontSize: Line's label font size
      • lineColor: Line's color
      • lineWidth: Line's width
  • Bar:

    • field: An object specifying the data attributes for the x and y axes. For example, { x: "category", y: "value" }.
    • scale: An object specifying the scales for the x and y axes. For example, { x: xScale, y: yScale }.
    • style: Bar's styles
      • fontSize: Bar's label font size
      • barColor: Bar's color
  • Pie:

    • style: Pie's label styles
      • fontSize: Pie's label font size

Example

import { data } from "./data";
import ChartD3 from "./components/ChartD3";
import ScalesD3 from "./components/ScalesD3";

const App = () => {
  const [chartData] = useState(JSON.parse(data));
  const [ref, settings] = ChartD3.useChartSettings({
    marginLeft: 32,
    marginRight: 32,
    marginTop: 32,
    marginBottom: 32,
  });

  const xScale = ScalesD3.band(
    chartData.map((d) => d.category),
    [settings.marginLeft, settings.width - settings.marginRight]
  );
  const yScale = ScalesD3.linear(
    [0, Math.max(...chartData.map((d) => d.value))],
    [settings.height - settings.marginBottom, settings.marginTop]
  );

  return (
    <div ref={ref} style={{ width: "100%", height: "100%" }}>
      <ChartD3.Line
        data={chartData}
        field={{ x: "date", y: "value" }}
        scale={{ x: xScale, y: yScale }}
        config={settings}
        style={{
          fontSize: "21px",
          lineColor: "#000",
          lineWidth: "steelblue",
        }}
      />
      <ChartD3.Bar
        data={chartData}
        field={{ x: "category", y: "value" }}
        scale={{ x: xScale, y: yScale }}
        config={settings}
        style={{
          fontSize: "12px",
          barColor: "steelblue",
        }}
      />
      <ChartD3.Pie
        data={chartData}
        config={settings}
        style={{
          fontSize: "8px",
        }}
      />
    </div>
  );
};

export default App;

ScalesD3

ScalesD3 provides a set of functions to create scales for your charts. These scales are essential for mapping data values to visual positions.

  • band(domain, range): Creates a band scale, typically used for categorical data.

    • domain: An array of values in the data domain (e.g., categories).
    • range: An array of values in the output range (e.g., pixel values).
  • linear(domain, range): Creates a linear scale, typically used for continuous numerical data.

    • domain: An array of two values representing the input data range.
    • range: An array of two values representing the output range.
  • utc(domain, range): Creates a UTC scale, typically used for time-based data.

    • domain: An array of two values representing the input data range (e.g., dates).
    • range: An array of two values representing the output range (e.g., pixel values).

Example

import ScalesD3 from "./components/ScalesD3";

const categories = ["A", "B", "C"];
const values = [10, 20, 30];

const xScale = ScalesD3.band(categories, [0, 300]);
const yScale = ScalesD3.linear([0, Math.max(...values)], [300, 0]);
const zScale = ScalesD3.utc(
  [new Date("2023-02-20"), new Date("2024-05-14")],
  [0, 1000]
);

console.log(xScale.scale("A"));
console.log(yScale.scale(10));
console.log(zScale.scale(new Date("2023-12-31")));

TransformD3

TransformD3 provides utility functions to transform and manipulate your data for better visualization.

  • extent(array): Calculates the minimum and maximum values in an array.

    • array: An array of numerical values.
  • group(array, key): Groups data by a specified key.

    • array: An array of objects.
    • key: The key to group by.
  • sort(array, sortBy): Sorts an array by a specified order.

    • array: An array of numerical values.
    • sortBy: The order to sort by ("asc" for ascending, "desc" for descending).

Example

import TransformD3 from "./components/TransformD3";

const data = [
  { category: "A", value: 10 },
  { category: "B", value: 20 },
  { category: "A", value: 30 },
];

const extent = TransformD3.extent(data.map((d) => d.value));
const groupedData = TransformD3.group(data, "category");
const sortedArr = TransformD3.sort([5, 3, 6, 32, 21], "asc");

console.log(extent); // [10, 30]
console.log(groupedData); // { A: [{ category: "A", value: 10 }, { category: "A", value: 30 }], B: [{ category: "B", value: 20 }] }
console.log(sortedArr); // [3, 5, 6, 21, 32]