Categories
React

Add Charts into Our React App with Nivo — Marimekko Chart

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 Nivo.

Marimekko Chart

Nivo comes with code to let us add a Marimekko chart into our React app.

To install the required packages, we run:

npm i @nivo/marimekko

Then we can add the chart by writing:

import React from "react";
import { ResponsiveMarimekko } from "@nivo/marimekko";

const data = [
  {
    statement: "it's good",
    participation: 19,
    stronglyAgree: 12,
    agree: 30,
    disagree: 19,
    stronglyDisagree: 31
  },
  {
    statement: "it's sweet",
    participation: 11,
    stronglyAgree: 30,
    agree: 2,
    disagree: 1,
    stronglyDisagree: 16
  },
  {
    statement: "it's spicy",
    participation: 19,
    stronglyAgree: 15,
    agree: 22,
    disagree: 4,
    stronglyDisagree: 18
  }
];

const MyResponsiveMarimekko = ({ data }) => (
  <ResponsiveMarimekko
    data={data}
    id="statement"
    value="participation"
    dimensions={[
      {
        id: "disagree strongly",
        value: "stronglyDisagree"
      },
      {
        id: "disagree",
        value: "disagree"
      },
      {
        id: "agree",
        value: "agree"
      },
      {
        id: "agree strongly",
        value: "stronglyAgree"
      }
    ]}
    innerPadding={9}
    axisTop={null}
    axisRight={{
      orient: "right",
      tickSize: 5,
      tickPadding: 5,
      tickRotation: 0,
      legend: "",
      legendOffset: 0
    }}
    axisBottom={{
      orient: "bottom",
      tickSize: 5,
      tickPadding: 5,
      tickRotation: 0,
      legend: "participation",
      legendOffset: 36,
      legendPosition: "middle"
    }}
    axisLeft={{
      orient: "left",
      tickSize: 5,
      tickPadding: 5,
      tickRotation: 0,
      legend: "opinions",
      legendOffset: -40,
      legendPosition: "middle"
    }}
    margin={{ top: 40, right: 80, bottom: 100, left: 80 }}
    colors={{ scheme: "spectral" }}
    borderWidth={1}
    borderColor={{ from: "color", modifiers: [["darker", 0.2]] }}
    defs={[
      {
        id: "lines",
        type: "patternLines",
        background: "rgba(0, 0, 0, 0)",
        color: "inherit",
        rotation: -45,
        lineWidth: 4,
        spacing: 8
      }
    ]}
    fill={[
      {
        match: {
          id: "agree strongly"
        },
        id: "lines"
      },
      {
        match: {
          id: "disagree strongly"
        },
        id: "lines"
      }
    ]}
    legends={[
      {
        anchor: "bottom",
        direction: "row",
        justify: false,
        translateX: 0,
        translateY: 80,
        itemsSpacing: 0,
        itemWidth: 140,
        itemHeight: 18,
        itemTextColor: "#999",
        itemDirection: "right-to-left",
        itemOpacity: 1,
        symbolSize: 18,
        symbolShape: "square",
        effects: [
          {
            on: "hover",
            style: {
              itemTextColor: "#000"
            }
          }
        ]
      }
    ]}
  />
);

export default function App() {
  return (
    <div style={{ width: 400, height: 300 }}>
      <MyResponsiveMarimekko data={data} />
    </div>
  );
}

We have the data array which has the bar segment values.

The bar segment values are the numeric properties.

In the MyResponsiveMarimekko component, we set the data prop to the data array.

value has the chart title.

dimensions have the property names with the bar segment values as the value of value .

innerPadding has the passing of the chart.

axisRight have the right axis.

We set the tick styles with tickSize , tickPadding , and tickRotation .

axisBottom have the bottom axis styles.

axisLeft have the left axis styles.

We shift the legend text with legendOffset .

margin has the margins.

colors have the colors.

borderColor have the chart border color.

defs has the styles for the background lines.

fill has the fill we want to set for some bar segments.

legends have the legend styles.

itemSpacing , itemWidth , itemHeight , itemTextColor , itemDirection have the item styles.

effects have the animation effect when we hover the legend items.

In App , we set width and height so that we can render the chart.

Conclusion

We can add bar charts with custom width bars with the Marimekko component that comes with Nivo.

Categories
React

Add Charts into Our React App with Nivo — Line Chart

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 Nivo.

Line Chart

Nivo comes with code to let us add a line chart into our React app.

To install the required packages, we run:

npm i @nivo/line

Then we can add the chart by writing:

import React from "react";
import { ResponsiveLine } from "@nivo/line";

const data = [
  {
    id: "japan",
    color: "hsl(70, 70%, 50%)",
    data: [
      {
        x: "plane",
        y: 140
      },
      {
        x: "helicopter",
        y: 80
      },
      {
        x: "boat",
        y: 134
      },
      {
        x: "train",
        y: 202
      },
      {
        x: "subway",
        y: 143
      },
      {
        x: "bus",
        y: 266
      },
      {
        x: "car",
        y: 223
      },
      {
        x: "moto",
        y: 100
      }
    ]
  },
  {
    id: "france",
    color: "hsl(89, 70%, 50%)",
    data: [
      {
        x: "plane",
        y: 267
      },
      {
        x: "helicopter",
        y: 192
      },
      {
        x: "boat",
        y: 259
      },
      {
        x: "train",
        y: 40
      },
      {
        x: "subway",
        y: 34
      },
      {
        x: "bus",
        y: 1
      },
      {
        x: "car",
        y: 100
      },
      {
        x: "moto",
        y: 194
      }
    ]
  }
];

const MyResponsiveLine = ({ data }) => (
  <ResponsiveLine
    data={data}
    margin={{ top: 50, right: 110, bottom: 50, left: 60 }}
    xScale={{ type: "point" }}
    yScale={{
      type: "linear",
      min: "auto",
      max: "auto",
      stacked: true,
      reverse: false
    }}
    yFormat=" >-.2f"
    axisTop={null}
    axisRight={null}
    axisBottom={{
      orient: "bottom",
      tickSize: 5,
      tickPadding: 5,
      tickRotation: 0,
      legend: "transportation",
      legendOffset: 36,
      legendPosition: "middle"
    }}
    axisLeft={{
      orient: "left",
      tickSize: 5,
      tickPadding: 5,
      tickRotation: 0,
      legend: "count",
      legendOffset: -40,
      legendPosition: "middle"
    }}
    pointSize={10}
    pointColor={{ theme: "background" }}
    pointBorderWidth={2}
    pointBorderColor={{ from: "serieColor" }}
    pointLabelYOffset={-12}
    useMesh={true}
    legends={[
      {
        anchor: "bottom-right",
        direction: "column",
        justify: false,
        translateX: 100,
        translateY: 0,
        itemsSpacing: 0,
        itemDirection: "left-to-right",
        itemWidth: 80,
        itemHeight: 20,
        itemOpacity: 0.75,
        symbolSize: 12,
        symbolShape: "circle",
        symbolBorderColor: "rgba(0, 0, 0, .5)",
        effects: [
          {
            on: "hover",
            style: {
              itemBackground: "rgba(0, 0, 0, .03)",
              itemOpacity: 1
            }
          }
        ]
      }
    ]}
  />
);

export default function App() {
  return (
    <div style={{ width: 400, height: 300 }}>
      <MyResponsiveLine data={data} />
    </div>
  );
}

We have the data array that we render in our line chart.

It’s set as the value of the data prop.

margin have the margins for the chart.

xScale has the scale for the x-axis.

yScale has the scale for the y-axis.

yFormat has the format for the y-axis values.

axisBottom has the bottom axis.

legend has the axis bottom text.

legendPosition has the position of the legend.

tickSize , tickPadding , tickRotation has the tick spacing and rotation angles.

We have the same settings for axisLeft .

pointSize has the point size for the points.

pointColor have the point color.

pointBorderWidth and pointBorderColor have the point borders.

pointLabelYOffset has the offset of the point label.

useMesh set to true adds the grid to the background.

legends has legends settings. We translate the legend with the translateX and translateY properties.

itemSpacing , itemWidth , itemHeight , and itemOpacity set each item in the settings.

symbolSize and symbolShape has the settings for the symbol to the left of the legend text.

effects has the animation effect when we hover over the legend items.

Conclusion

We can add line charts into our React app with Nivo.

Categories
React

Add Charts into Our React App with Nivo — Heat Map

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 Nivo.

Heat Map

Nivo comes with code to let us add a heat map into our React app.

To install the required packages, we run:

npm i @nivo/heatmap

Then we can add the chart by writing:

import React from "react";
import { ResponsiveHeatMap } from "@nivo/heatmap";

const data = [
  {
    country: "AD",
    "hot dog": 93,
    "hot dogColor": "hsl(354, 70%, 50%)",
    burger: 91,
    burgerColor: "hsl(313, 70%, 50%)",
    sandwich: 66,
    sandwichColor: "hsl(102, 70%, 50%)",
    kebab: 85,
    kebabColor: "hsl(114, 70%, 50%)",
    fries: 70,
    friesColor: "hsl(37, 70%, 50%)",
    donut: 50,
    donutColor: "hsl(126, 70%, 50%)",
    junk: 27,
    junkColor: "hsl(342, 70%, 50%)",
    sushi: 45,
    sushiColor: "hsl(185, 70%, 50%)",
    ramen: 31,
    ramenColor: "hsl(139, 70%, 50%)",
    curry: 22,
    curryColor: "hsl(165, 70%, 50%)",
    udon: 66,
    udonColor: "hsl(181, 70%, 50%)"
  },
  {
    country: "AE",
    "hot dog": 55,
    "hot dogColor": "hsl(304, 70%, 50%)",
    burger: 93,
    burgerColor: "hsl(14, 70%, 50%)",
    sandwich: 54,
    sandwichColor: "hsl(224, 70%, 50%)",
    kebab: 85,
    kebabColor: "hsl(328, 70%, 50%)",
    fries: 52,
    friesColor: "hsl(305, 70%, 50%)",
    donut: 86,
    donutColor: "hsl(252, 70%, 50%)",
    junk: 61,
    junkColor: "hsl(39, 70%, 50%)",
    sushi: 77,
    sushiColor: "hsl(122, 70%, 50%)",
    ramen: 85,
    ramenColor: "hsl(230, 70%, 50%)",
    curry: 79,
    curryColor: "hsl(176, 70%, 50%)",
    udon: 44,
    udonColor: "hsl(129, 70%, 50%)"
  },
  {
    country: "AF",
    "hot dog": 49,
    "hot dogColor": "hsl(349, 70%, 50%)",
    burger: 10,
    burgerColor: "hsl(319, 70%, 50%)",
    sandwich: 28,
    sandwichColor: "hsl(219, 70%, 50%)",
    kebab: 37,
    kebabColor: "hsl(83, 70%, 50%)",
    fries: 21,
    friesColor: "hsl(242, 70%, 50%)",
    donut: 96,
    donutColor: "hsl(62, 70%, 50%)",
    junk: 19,
    junkColor: "hsl(43, 70%, 50%)",
    sushi: 73,
    sushiColor: "hsl(107, 70%, 50%)",
    ramen: 31,
    ramenColor: "hsl(288, 70%, 50%)",
    curry: 21,
    curryColor: "hsl(176, 70%, 50%)",
    udon: 99,
    udonColor: "hsl(20, 70%, 50%)"
  }
];

const MyResponsiveHeatMap = ({ data }) => (
  <ResponsiveHeatMap
    data={data}
    keys={[
      "hot dog",
      "burger",
      "sandwich",
      "kebab",
      "fries",
      "donut",
      "junk",
      "sushi",
      "ramen",
      "curry",
      "udon"
    ]}
    indexBy="country"
    margin={{ top: 100, right: 60, bottom: 60, left: 60 }}
    forceSquare={true}
    axisTop={{
      orient: "top",
      tickSize: 5,
      tickPadding: 5,
      tickRotation: -90,
      legend: "",
      legendOffset: 36
    }}
    axisRight={null}
    axisBottom={null}
    axisLeft={{
      orient: "left",
      tickSize: 5,
      tickPadding: 5,
      tickRotation: 0,
      legend: "country",
      legendPosition: "middle",
      legendOffset: -40
    }}
    cellOpacity={1}
    cellBorderColor={{ from: "color", modifiers: [["darker", 0.4]] }}
    labelTextColor={{ from: "color", modifiers: [["darker", 1.8]] }}
    defs={[
      {
        id: "lines",
        type: "patternLines",
        background: "inherit",
        color: "rgba(0, 0, 0, 0.1)",
        rotation: -45,
        lineWidth: 4,
        spacing: 7
      }
    ]}
    fill={[{ id: "lines" }]}
    animate={true}
    motionConfig="wobbly"
    motionStiffness={80}
    motionDamping={9}
    hoverTarget="cell"
    cellHoverOthersOpacity={0.25}
  />
);

export default function App() {
  return (
    <div style={{ width: 400, height: 300 }}>
      <MyResponsiveHeatMap data={data} />
    </div>
  );
}

We have the data array with the number properties as the values to show.

And the properties with Color at the end has the background color for each square.

In the ResponsiveHeatMap component, we have the keys prop with the item names.

margin have the margins.

forceSquare makes the heatmap items square.

axisTop has the top axis settings.

orient has the text orientation. tickSize has the tick size.

tickPadding has the tick padding.

tickRotation has the text rotation angle.

legendOffset has the offset for the legend text in pixels.

axisRight and axisBottom are set to null since we don’t want to add them.

cellOpacity has the opacity of the heat map items.

cellBorderColor and labelTextColor have the cell border and label text colors respectively.

defs have the color scheme for the lines. We set it to patternLines to add the lines between the item.

fill have the fill color.

animate , motionConfig , motionStiffness , motionDamping have the settings for the animation when we hover over a cell.

cellHoverOthersOpacity have the opacity of the cells that we didn’t hover over when we hover a cell.

Then in App , we set the width and height so that we can show the chart.

Conclusion

We can add heat maps into our React app with Nivo.

Categories
React

Add Charts into Our React App with Nivo — Funnel Chart

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 Nivo.

Funnel Chart

We can render the chord chart with the ResponsiveChord component.

To add it, we first have to install the @nivo/funnel package by running:

npm i @nivo/funnel @nivo/tooltip prop-types @nivo/annotations @nivo/colors

Then we can add the chart by writing:

import React from "react";
import { ResponsiveFunnel } from "@nivo/funnel";

const data = [
  {
    id: "step_sent",
    value: 91922,
    label: "Sent"
  },
  {
    id: "step_viewed",
    value: 77979,
    label: "Viewed"
  },
  {
    id: "step_clicked",
    value: 43633,
    label: "Clicked"
  }
];

const MyResponsiveFunnel = ({ data }) => (
  <ResponsiveFunnel
    data={data}
    margin={{ top: 20, right: 20, bottom: 20, left: 20 }}
    valueFormat=">-.4s"
    colors={{ scheme: "spectral" }}
    borderWidth={20}
    labelColor={{ from: "color", modifiers: [["darker", 3]] }}
    beforeSeparatorLength={100}
    beforeSeparatorOffset={20}
    afterSeparatorLength={100}
    afterSeparatorOffset={20}
    currentPartSizeExtension={10}
    currentBorderWidth={40}
    motionConfig="wobbly"
  />
);

export default function App() {
  return (
    <div style={{ width: 400, height: 300 }}>
      <MyResponsiveFunnel data={data} />
    </div>
  );
}

We add the data for our chart with the data array.

value has the value to display.

The label will be shown when we hover over the funnel item.

In the ResponsiveFunnel component, we set the data prop to data to render the data.

margin has the margins.

valueFormat has the format the values on the chart.

colors has the color scheme for the funnel items.

labelColor has the color scheme for the label.

beforeSeparatorLength has the length before the separator lines.

beforeSeparatorOffset has the offset for the parts before the separator lines.

afterSeparatorLength has the length after the separator lines.

afterSeparatorOffset has the offset for the parts after the separator lines.

currentPartSizeExtension has the size to expand on each side when a funnel is active.

currentBorderWidth has the border width of the item that’s selected.

motionConfig has the animation effect when we hover over an item.

In App , we’ve to set the width and height of the wrapper div to render the chart.

Conclusion

We can add funnel chart into our React app with Nivo.

Categories
React

Add Charts into Our React App with Nivo — Chord Chart

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 Nivo.

Chord Chart

We can render the map chart with the Choropleth component.

To add it, we first have to install the @nivo/chord package by running:

npm i @nivo/chord

Then we can add the chart by writing:

import React from "react";
import { ResponsiveChord } from "@nivo/chord";

const data = [
  [201, 103, 90, 59, 56],
  [867, 1927, 346, 457, 410],
  [256, 441, 204, 318, 90],
  [215, 420, 491, 481, 1769],
  [917, 834, 396, 312, 385]
];

const MyResponsiveChord = ({ matrix }) => (
  <ResponsiveChord
    matrix={matrix}
    keys={["John", "Raoul", "Jane", "Marcel", "Ibrahim"]}
    margin={{ top: 60, right: 60, bottom: 90, left: 60 }}
    valueFormat=".2f"
    padAngle={0.02}
    innerRadiusRatio={0.96}
    innerRadiusOffset={0.02}
    arcOpacity={1}
    arcBorderWidth={1}
    arcBorderColor={{ from: "color", modifiers: [["darker", 0.4]] }}
    ribbonOpacity={0.5}
    ribbonBorderWidth={1}
    ribbonBorderColor={{ from: "color", modifiers: [["darker", 0.4]] }}
    enableLabel={true}
    label="id"
    labelOffset={12}
    labelRotation={-90}
    labelTextColor={{ from: "color", modifiers: [["darker", 1]] }}
    colors={{ scheme: "nivo" }}
    isInteractive={true}
    arcHoverOpacity={1}
    arcHoverOthersOpacity={0.25}
    ribbonHoverOpacity={0.75}
    ribbonHoverOthersOpacity={0.25}
    animate={true}
    motionStiffness={90}
    motionDamping={7}
    legends={[
      {
        anchor: "bottom",
        direction: "row",
        justify: false,
        translateX: 0,
        translateY: 70,
        itemWidth: 80,
        itemHeight: 14,
        itemsSpacing: 0,
        itemTextColor: "#999",
        itemDirection: "left-to-right",
        symbolSize: 12,
        symbolShape: "circle",
        effects: [
          {
            on: "hover",
            style: {
              itemTextColor: "#000"
            }
          }
        ]
      }
    ]}
  />
);

export default function App() {
  return (
    <div style={{ width: 400, height: 300 }}>
      <MyResponsiveChord matrix={data} />
    </div>
  );
}

We have a nested array of numbers for the data.

Then we create the MyResponsiveChord component to render the ResponsiveChord component to render the chord chart.

We pass in the data to the matrix prop to render the nested array of numbers.

The keys prop has an array of names for the legend. They correspond to the numbers in each nested array.

margin has the margins.

valueFormat has the format for the numbers on the chart.

innerRadiusRatio has the length of the inner radius.

arcOpacity has the arc opacity. arcBorderWidth has each arc’s border width in pisels.

arcBorderColor has the arcs’ border colors.

We also set the label options with the label props.

label has the string to specify how to get the label text.

labelOffset has the label position offset in pixels.

labelRotation has the number of degrees to rotate the labels.

labelTextColor has the source of the colors for the chart.

colors have the color scheme to use to color the chart.

isInteractive lets us set whether the chart can be hovered over.

arcHoverOpacity has the opacity of the arc that we hovered over the chart.

arcHoverOthersOpacity has the opacity of the arc that we didn’t hover over the chart when our mouse is over the chart.

animate , motionStiffness , and motionDamping has the animation options.

legends has the legends options.

itemWidth , itemHeight , itemSpacing has the legend items dimensions and spacing.

itemTextColor has the item text color.

We translate the legend labels with the translate properties.

In App , we set the width and height so we can render the ResponsiveChord component.

Conclusion

We can add a chord chart easily into our React app with Nivo.