Search

Chart and Stacked Bars

Time Bar

Visualize multiple groups of chart items with stacked bars.

Chart and Stacked Bars
View live example →

Click on a node to separate its category into a stack group, or select multiple colors to see more stack groups.

The order that you select categories controls the stacking order.

See also

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

import forEach from "lodash/forEach";
import isUndefined from "lodash/isUndefined";
import { Chart, TimeBar } from "regraph";

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

export const Demo = () => <ChartAndStackedBars items={styledItems()} />;

const COLORS = {
  x: style.primary1.color,
  y: style.secondary1.color,
  z: style.tertiary1.color,
};

const ChartAndStackedBars = (props) => {
  const { items } = props;
  const [selectedItems, setSelectedItems] = useState(null);
  const [styles, setStyles] = useState(null);

  useEffect(() => {
    if (!isUndefined(selectedItems)) {
      const stackStyles = {};
      forEach(selectedItems, (item) => {
        const stackIdentifier = item.data.id;
        if (!stackStyles[stackIdentifier]) {
          stackStyles[stackIdentifier] = { color: COLORS[stackIdentifier] };
        }
      });
      setStyles(stackStyles);
    }
  }, [selectedItems]);

  return (
    <div style={{ height: "100%", backgroundColor: "white" }}>
      <Chart
        items={items}
        onChange={({ selection }) => {
          setSelectedItems(selection);
        }}
        options={{ handMode: false, navigation: false, overview: false }}
        style={{ height: "calc(100% - 200px)" }}
      />
      <TimeBar
        items={items}
        options={{
          style: { hoverColor: "#A2A2A2" },
          sliders: { type: "none" },
          stack: { by: "id", styles },
        }}
        style={{
          margin: "0 auto",
          height: "200px",
          width: "50%",
        }}
      />
    </div>
  );
};

function styledItems() {
  const items = data();
  forEach(items, (item) => {
    item.color = COLORS[item.data.id];
  });
  return items;
}

function data() {
  return {
    id1: { times: [{ time: Date.now(), value: 2 }], data: { id: "x" } },
    id2: { times: [{ time: Date.now() + 1000 * 60 * 60 }], data: { id: "x" } },
    id3: { times: [{ time: Date.now() + 1000 * 60 * 60 * 3, value: 3 }], data: { id: "x" } },
    id4: { times: [{ time: Date.now(), value: 2.5 }], data: { id: "y" } },
    id5: { times: [{ time: Date.now() + 1000 * 60 * 60 }], data: { id: "y" } },
    id6: { times: [{ time: Date.now() + 1000 * 60 * 60 * 3, value: 2 }], data: { id: "y" } },
    id7: { times: [{ time: Date.now() + 1000 * 60 * 60 * 3, value: 1.5 }], data: { id: "z" } },
  };
}

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.