Search

Donut

Styles

Add donuts with proportional segments to your chart nodes.

Donut
View live example →

Click on the buttons to cycle through donut styles.

Donuts are similar to pie charts, showing proportional data on nodes.

Donuts can have an unlimited number of segments.

See also

import React from "react";
import { createRoot } from "react-dom/client";

import mapValues from "lodash/mapValues";
import pickBy from "lodash/pickBy";
import { Chart } from "regraph";

import { style } from "@ci/theme/rg/js/storyStyles";

import "@ci/theme/rg/css/properties.css";
import "@ci/theme/rg/css/button.css";

export const Demo = () => <DonutStyle />;

const donutProps = {
  border: {
    values: [
      undefined,
      { width: 2, color: style.colors.darkGrey },
      { width: 4, color: style.colors.charcoal },
    ],
    label: "Border",
  },
  segments: {
    values: [
      [
        { ...style.primary2, size: 2 },
        { ...style.secondary2, size: 1 },
      ],
      [
        { ...style.primary2, size: 2 },
        { ...style.secondary2, size: 2 },
        { ...style.tertiary2, size: 2 },
      ],
      [
        { ...style.primary2, size: 2 },
        { ...style.secondary2, size: 2 },
        { ...style.tertiary2, size: 2 },
        { ...style.primary0, size: 2 },
      ],
      [
        { ...style.primary2, size: 2 },
        { ...style.secondary2, size: 2 },
        { ...style.tertiary2, size: 2 },
        { ...style.primary0, size: 2 },
        { color: style.colors.midGrey, size: 2 },
      ],
    ],
    label: "Segments",
  },
  width: { values: [undefined, 20, 40], label: "Width" },
};

class DonutStyle extends React.PureComponent {
  constructor(props) {
    super(props);

    this.state = { ...mapValues(donutProps, () => 0), show: true };
  }

  update = (id) => {
    const { [id]: index } = this.state;
    const { [id]: property } = donutProps;
    const { values } = property;
    let result = index + 1;
    if (result > values.length - 1) {
      result = 0;
    }
    this.setState({ [id]: result });
  };

  toggleDonut = () => {
    const { show } = this.state;
    this.setState({ show: !show });
  };

  reset = () => {
    this.setState({ ...mapValues(donutProps, () => 0), show: true });
  };

  render = () => {
    const { show } = this.state;

    const donut = pickBy(
      mapValues(donutProps, (value, key) => {
        const { [key]: index } = this.state;
        return value.values[index || 0];
      }),
      (value) => value !== undefined
    );
    const node1 = { ...style.primary1 };
    if (show) {
      node1.donut = donut;
    }

    return (
      <div style={{ width: "100%", height: "100%" }}>
        <Chart
          items={{ node1 }}
          animation={{ animate: true, time: 250 }}
          options={{ fit: "all", navigation: false, overview: false }}
        />
        <div style={{ position: "absolute", top: 0, left: 0 }}>
          <button type="button" onClick={this.reset}>
            Reset
          </button>
          <button type="button" onClick={this.toggleDonut}>
            {show ? "Hide" : "Show"}
          </button>
          {Object.keys(donutProps).map((key) => (
            <button
              type="button"
              title={donutProps[key].label}
              key={key}
              onClick={() => this.update(key)}
            >
              {key}
            </button>
          ))}
        </div>
        <pre className="properties">{JSON.stringify({ node1 }, null, 2)}</pre>
      </div>
    );
  };
}

const root = createRoot(document.getElementById("regraph"));
root.render(<Demo />);
<!doctype html>
<html>
  <body>
    <div id="regraph" style="height: 100vh"></div>
    <script type="module" src="./code.jsx"></script>
  </body>
</html>

Terms of use

These terms do not alter or supersede any existing agreements between you (or your employer) and us.

By accessing or using any Content you agree to be bound by these Terms of Use. Please review these terms carefully before using the website.

The contents of this website, including but not limited to any text, code samples, API references, schemas, interactive tools, and other materials (collectively, the 'Content'), are made available for informational and internal evaluation purposes only. All intellectual property rights in the Content are reserved. No licence is granted to use the Content for any commercial purpose, or to copy, distribute, modify, reverse-engineer, or incorporate any part of the Content into any product or service, without our prior written consent.

This Content is provided “as is” and “as available,” without any representations, warranties, or guarantees of any kind, whether express or implied, including but not limited to implied warranties of merchantability, fitness for a particular purpose, non-infringement, or accuracy. To the fullest extent permitted by applicable law, we expressly exclude and disclaim all implied warranties, conditions, and other terms that might otherwise be implied.

We disclaim all liability for any loss or damage, whether direct, indirect, incidental, consequential, or otherwise, arising from any reliance placed on the Content or from your use of it, to the fullest extent permitted by applicable law. By continuing to access or use the Content, you acknowledge and agree to these terms.