Categories
React

Add Charts into Our React App with Victory — Bar Labels, Pie, and Polar Charts

The Victory lets us add charts and data visualization into our React app.

In this article, we’ll look at how to add charts into our React app with Victory.

Multiple Bar Labels

We can add multiple bar labels with an array of text.

For instance, we can write:

import React from "react";
import { Bar, VictoryBar, VictoryChart, VictoryLabel } from "victory";

const data = [
  { x: 1, y: 3, label: ["first", "label"] },
  { x: 2, y: 4, label: ["second", "label"] },
  { x: 3, y: 2, label: ["third", "final", "label"] }
];

export default function App() {
  return (
    <VictoryChart domainPadding={{ x: 40, y: 40 }}>
      <VictoryBar
        style={{ data: { fill: "#c43a31" } }}
        data={data}
        labels={({ datum }) => `y: ${datum.y}`}
        labelComponent={
          <VictoryLabel
            backgroundStyle={[{ fill: "orange" }, { fill: "gold" }]}
            backgroundPadding={{ left: 5, right: 5 }}
          />
        }
        dataComponent={
          <Bar tabIndex={0} ariaLabel={({ datum }) => `x: ${datum.x}`} />
        }
      />
    </VictoryChart>
  );
}

We have the label property in the data array.

And we add an array of styles into the backgroundStyle prop.

Polar Chart with Labels

We can add a polar chart with labels with the VictoryBar ‘s polar prop.

For instance, we can write:

import React from "react";
import { VictoryBar, VictoryLabel, VictoryTooltip } from "victory";

const data = [
  { x: 1, y: 3, label: ["first", "label"] },
  { x: 2, y: 4, label: ["second", "label"] },
  { x: 3, y: 2, label: ["third", "final", "label"] }
];

export default function App() {
  return (
    <VictoryBar
      polar
      data={data}
      style={{ data: { width: 40, fill: "tomato" } }}
      labelComponent={
        <VictoryTooltip
          active
          labelPlacement="perpendicular"
          pointerLength={30}
          pointerWidth={0}
          flyoutPadding={0}
          labelComponent={
            <VictoryLabel
              verticalAnchor="end"
              dy={8}
              backgroundStyle={{ fill: "white" }}
              backgroundPadding={8}
            />
          }
        />
      }
    />
  );
}

Our 1abelComponent prop has the VictoryTooltip component.

We set the labelPlacement to perpendicular to place the label to be flush with the polar segments.

We also set the pointerLength , pointerWidth to set the length and width of the label.

VictoryLabel has the label text.

Pie Chart with Labels

We can add pie charts with labels with the VictoryPie and VictoryTooltip components.

For instance, we can write:

import React from "react";
import { VictoryPie, VictoryTooltip } from "victory";

const data = [
  { x: 1, y: 3, placement: "vertical" },
  { x: 2, y: 4, placement: "parallel" },
  { x: 3, y: 2, placement: "perpendicular" }
];

export default function App() {
  return (
    <VictoryPie
      colorScale="warm"
      radius={120}
      style={{ labels: { padding: 5, fontSize: 15 } }}
      data={data}
      labels={({ datum }) => `${datum.placement}nlabel`}
      labelPlacement={({ datum }) => datum.placement}
      labelPosition="startAngle"
      labelComponent={<VictoryTooltip active />}
    />
  );
}

We set the label placement with the labelPlacement prop.

It gets the placement property value and returns it.

We render the label with th labelComponent .

And we place the labels flush to the pie segments with the labelPosition prop.

Conclusion

We can add multiple bar labels and other custom label options in our React app with Victory.

Categories
React

Add Charts into Our React App with Victory — Bar Colors and Labels

The Victory lets us add charts and data visualization into our React app.

In this article, we’ll look at how to add charts into our React app with Victory.

Change Bar Colors

We can change the colors of the bars with the VictoryStack component’s colorScale prop.

For instance, we can write:

import React from "react";
import {
  VictoryBar,
  VictoryChart,
  VictoryAxis,
  VictoryStack,
  VictoryTheme
} from "victory";

const data2018 = [
  { quarter: 1, earnings: 13000 },
  { quarter: 2, earnings: 16500 },
  { quarter: 3, earnings: 14250 },
  { quarter: 4, earnings: 19000 }
];

const data2019 = [
  { quarter: 1, earnings: 15000 },
  { quarter: 2, earnings: 12500 },
  { quarter: 3, earnings: 19500 },
  { quarter: 4, earnings: 13000 }
];

const data2020 = [
  { quarter: 1, earnings: 11500 },
  { quarter: 2, earnings: 13250 },
  { quarter: 3, earnings: 20000 },
  { quarter: 4, earnings: 15500 }
];

const data2021 = [
  { quarter: 1, earnings: 18000 },
  { quarter: 2, earnings: 13250 },
  { quarter: 3, earnings: 15000 },
  { quarter: 4, earnings: 12000 }
];

export default function App() {
  return (
    <VictoryChart domainPadding={20}>
      <VictoryAxis
        tickValues={[1, 2, 3, 4]}
        tickFormat={["Quarter 1", "Quarter 2", "Quarter 3", "Quarter 4"]}
      />
      <VictoryAxis dependentAxis tickFormat={(x) => `$${x / 1000}k`} />
      <VictoryStack colorScale="warm">
        <VictoryBar data={data2018} x="quarter" y="earnings" />
        <VictoryBar data={data2019} x="quarter" y="earnings" />
        <VictoryBar data={data2020} x="quarter" y="earnings" />
        <VictoryBar data={data2021} x="quarter" y="earnings" />
      </VictoryStack>
    </VictoryChart>
  );
}

Add Bar Labels

For instance, we can write:

import React from "react";
import { Bar, VictoryBar, VictoryChart, VictoryTooltip } from "victory";

const data = [
  { x: 1, y: 13000 },
  { x: 2, y: 16500 },
  { x: 3, y: 14250 },
  { x: 4, y: 19000 }
];

export default function App() {
  return (
    <VictoryChart domainPadding={{ x: 40, y: 40 }}>
      <VictoryBar
        style={{ data: { fill: "#c43a31" } }}
        data={data}
        labels={({ datum }) => `y: ${datum.y}`}
        labelComponent={<VictoryTooltip />}
        dataComponent={
          <Bar tabIndex={0} ariaLabel={({ datum }) => `x: ${datum.x}`} />
        }
      />
    </VictoryChart>
  );
}

We render the bar chart by setting the labels prop to get the data from the datum property.

We do the same with tyhe x-axis by setting the arialLabel prop.

And we set the labelComponent prop to add a tooltip for the bar when we hover over it to show its value.

Backgrounds for Labels

We can add styled bar labels by using the VictoryLabel component.

For instance, we can write:

import React from "react";
import { Bar, VictoryBar, VictoryChart, VictoryLabel } from "victory";

const data = [
  { x: 1, y: 13000, label: "first label" },
  { x: 2, y: 16500, label: "second label" },
  { x: 3, y: 14250, label: "3rd label" },
  { x: 4, y: 19000, label: "4th label" }
];

export default function App() {
  return (
    <VictoryChart domainPadding={{ x: 40, y: 40 }}>
      <VictoryBar
        style={{ data: { fill: "#c43a31" } }}
        data={data}
        labels={({ datum }) => `y: ${datum.y}`}
        labelComponent={
          <VictoryLabel
            dy={-20}
            backgroundStyle={{ fill: "tomato", opacity: 0.6 }}
            backgroundPadding={{ bottom: 5, top: 5, left: 10, right: 10 }}
          />
        }
        dataComponent={
          <Bar tabIndex={0} ariaLabel={({ datum }) => `x: ${datum.x}`} />
        }
      />
    </VictoryChart>
  );
}

to add the VictoryLabel component to set the backgroundStyle and backgroundPadding props with the styles we want.

The label property will be automatically displayed as the label text.

Conclusion

We can customize bar charts with labels and colors in our React app with Victory.

Categories
React

Add Charts into Our React App with Victory

The Victory lets us add charts and data visualization into our React app.

In this article, we’ll look at how to add charts into our React app with Victory.

Installation

We can install Victory by running:

npm install victory

Getting Started

We can create a bar chart with the VictoryBar component.

To do this, we write:

import React from "react";
import { VictoryBar, VictoryChart } from "victory";

const data = [
  { quarter: 1, earnings: 13000 },
  { quarter: 2, earnings: 16500 },
  { quarter: 3, earnings: 14250 },
  { quarter: 4, earnings: 19000 }
];

export default function App() {
  return (
    <VictoryChart domainPadding={20}>
      <VictoryBar data={data} x="quarter" y="earnings" />
    </VictoryChart>
  );
}

to add a bar chart.

We have an array of objects as our chart data.

And we pass that as the value of the data prop.

Then we set the property names for the x and y-axis values with the x and y props respectively.

We set the domainPadding to shift the bars so that they don’t overlap with the axes.

We can customize the axis with the VictoryAxis component:

import React from "react";
import { VictoryBar, VictoryChart, VictoryAxis } from "victory";

const data = [
  { quarter: 1, earnings: 13000 },
  { quarter: 2, earnings: 16500 },
  { quarter: 3, earnings: 14250 },
  { quarter: 4, earnings: 19000 }
];

export default function App() {
  return (
    <VictoryChart domainPadding={20}>
      <VictoryAxis
        tickValues={[1, 2, 3, 4]}
        tickFormat={["Quarter 1", "Quarter 2", "Quarter 3", "Quarter 4"]}
      />
      <VictoryAxis dependentAxis tickFormat={(x) => `$${x / 1000}k`} />
      <VictoryBar data={data} x="quarter" y="earnings" />
    </VictoryChart>
  );
}

The dependentAxis prop lets us modify the y-axis.

tickFormat has a function that returns the content we want to display on the axis ticks.

We can also pass in an array of values to display on the axis.

Stacked Bar Charts

We can add a stacked bar chart with the VictoryStack component.

To do this, we write:

import React from "react";
import {
  VictoryBar,
  VictoryChart,
  VictoryAxis,
  VictoryStack,
  VictoryTheme
} from "victory";

const data2018 = [
  { quarter: 1, earnings: 13000 },
  { quarter: 2, earnings: 16500 },
  { quarter: 3, earnings: 14250 },
  { quarter: 4, earnings: 19000 }
];

const data2019 = [
  { quarter: 1, earnings: 15000 },
  { quarter: 2, earnings: 12500 },
  { quarter: 3, earnings: 19500 },
  { quarter: 4, earnings: 13000 }
];

const data2020 = [
  { quarter: 1, earnings: 11500 },
  { quarter: 2, earnings: 13250 },
  { quarter: 3, earnings: 20000 },
  { quarter: 4, earnings: 15500 }
];

const data2021 = [
  { quarter: 1, earnings: 18000 },
  { quarter: 2, earnings: 13250 },
  { quarter: 3, earnings: 15000 },
  { quarter: 4, earnings: 12000 }
];

export default function App() {
  return (
    <VictoryChart domainPadding={20} theme={VictoryTheme.material}>
      <VictoryAxis
        tickValues={[1, 2, 3, 4]}
        tickFormat={["Quarter 1", "Quarter 2", "Quarter 3", "Quarter 4"]}
      />
      <VictoryAxis dependentAxis tickFormat={(x) => `$${x / 1000}k`} />
      <VictoryStack>
        <VictoryBar data={data2018} x="quarter" y="earnings" />
        <VictoryBar data={data2019} x="quarter" y="earnings" />
        <VictoryBar data={data2020} x="quarter" y="earnings" />
        <VictoryBar data={data2021} x="quarter" y="earnings" />
      </VictoryStack>
    </VictoryChart>
  );
}

We just nest the VictoryBar in the VictoryStack component.

Conclusion

We can add bar charts easily into our React app with the Victory library.

Categories
React

Even More Icons We can Add into Our React App with React Icons

React Icons is a library with lots of icons we can add to our React app.

In this article, we’ll look at how to add icons to our React app with React Icons.

Material Design Icons

We can add Material Design icons from the react-icons/md collection.

For instance, we can write:

import React from "react";
import { Md3DRotation } from "react-icons/md";

export default function App() {
  return (
    <h3>
      <Md3DRotation />
    </h3>
  );
}

to add an icon from the collection.

The full list of icons can be found at https://react-icons.github.io/react-icons/icons?name=md

Remix Icon

React Icons comes with the Remix Icon collection.

We can import them from the react-icons/ri module.

For instance, we can write:

import React from "react";
import { RiAncientGateLine } from "react-icons/ri";

export default function App() {
  return (
    <h3>
      <RiAncientGateLine />
    </h3>
  );
}

The full list of icons can be found at https://react-icons.github.io/react-icons/icons?name=ri

Simple Icons

The Simple Icons collection is included with the React Icon’s react-icons/si module.

For instance, we can write:

import React from "react";
import { Si1001Tracklists } from "react-icons/si";

export default function App() {
  return (
    <h3>
      <Si1001Tracklists />
    </h3>
  );
}

to add the icon.

The full list of icons is at https://react-icons.github.io/react-icons/icons?name=si

Typicons

The Typicons collection is included with the React Icons’s react-icons/ti module.

For instance, we can write:

import React from "react";
import { TiAdjustBrightness } from "react-icons/ti";

export default function App() {
  return (
    <h3>
      <TiAdjustBrightness />
    </h3>
  );
}

to add the icon.

The full list of icons is at https://react-icons.github.io/react-icons/icons?name=ti

VS Code Icons

React Icons comes with the icons used by the Visual Studio Code code editor.

The icons are included in the react-icons/vsc collection.

For example, we can write:

import React from "react";
import { VscAccount } from "react-icons/vsc";

export default function App() {
  return (
    <h3>
      <VscAccount />
    </h3>
  );
}

to add an icon from the collection.

The full list of icons is at https://react-icons.github.io/react-icons/icons?name=vsc

Weather Icons

We can add weather icons from the Weather Icons collection.

They’re included in the react-icons/wi collection.

To add one, we write:

import React from "react";
import { WiAlien } from "react-icons/wi";

export default function App() {
  return (
    <h3>
      <WiAlien />
    </h3>
  );
}

The full list of icons can be found at https://react-icons.github.io/react-icons/icons?name=wi

css.gg

React Icons comes with the css.gg icon collection.

They’re included in the react-icons/gg module.

To add one, we write:

import React from "react";
import { CgAbstract } from "react-icons/cg";

export default function App() {
  return (
    <h3>
      <CgAbstract />
    </h3>
  );
}

to add an icon from the collection.

The full list of icons can be found at https://react-icons.github.io/react-icons/icons?name=cg

Conclusion

We can add icons from various icon libraries into our React app with React Icons.

Categories
React

More Icons We can Add into Our React App with React Icons

React Icons is a library with lots of icons we can add to our React app.

In this article, we’ll look at how to add icons to our React app with React Icons.

Game Icons

We can add icons from the Game Icons library with the react-icons/gi module.

For instance, we write:

import React from "react";
import { Gi3DGlasses } from "react-icons/gi";

export default function App() {
  return (
    <h3>
      <Gi3DGlasses />
    </h3>
  );
}

The full list of icons can be found at https://react-icons.github.io/react-icons/icons?name=gi

Github Octicons icons

We can add Github Octicons icons into our React app with the react-icons/go module.

For example, we write:

import React from "react";
import { GoAlert } from "react-icons/go";

export default function App() {
  return (
    <h3>
      <GoAlert />
    </h3>
  );
}

We can find a list of Github Octicons icons at https://react-icons.github.io/react-icons/icons?name=go

Grommet-Icons

React Icons comes with a collection of Grommet-Icons.

We can add them with the react-icons/gr module.

For instance, we can write:

import React from "react";
import { GrAccessibility } from "react-icons/gr";

export default function App() {
  return (
    <h3>
      <GrAccessibility />
    </h3>
  );
}

to add an icon from the module.

The full list of icons can be found at https://react-icons.github.io/react-icons/icons?name=gr

Heroicons

React Icons comes with the Heroicons icon collection. We can import them from the react-icons/hi module.

For instance, we can write:

import React from "react";
import { HiAdjustments } from "react-icons/hi";

export default function App() {
  return (
    <h3>
      <HiAdjustments />
    </h3>
  );
}

to add the icon.

The full list of Heroicons can be found at https://react-icons.github.io/react-icons/icons?name=hi

IcoMoon Free

React Icons comes with the IcoMoon Free icon collection.

We can import them from the react-icons/im module.

For instance, we can write:

import React from "react";
import { ImHome } from "react-icons/im";

export default function App() {
  return (
    <h3>
      <ImHome />
    </h3>
  );
}

The full list of icons is at https://react-icons.github.io/react-icons/icons?name=im

Ionicons 4

React Icons comes with the Ionicons 4 icon collection.

For example, we can add it by writing:

import React from "react";
import { IoIosAddCircleOutline } from "react-icons/io";

export default function App() {
  return (
    <h3>
      <IoIosAddCircleOutline />
    </h3>
  );
}

to add an icon.

The full list of icons can be found at https://react-icons.github.io/react-icons/icons?name=io

Ionicons 5

We can add icons from the Ionicons 5 collection from the react-icons/io5 module.

For example, we can write:

import React from "react";
import { IoAccessibilityOutline } from "react-icons/io5";

export default function App() {
  return (
    <h3>
      <IoAccessibilityOutline />
    </h3>
  );
}

to add an icon from the collection.

The full list of icons can be found at https://react-icons.github.io/react-icons/icons?name=io5

Conclusion

We can add icons from various icon libraries into our React app with React Icons.