Categories
React

Recharts — Change Opacity of Line When Hovering Over Legend and Responsive Container

We can add charts easily to a React app with Recharts.

In this article, we’ll look at how to use it.

Change Opacity of Line When Hovering Over Legend

We can change the opacity of the line when we hover over the legend.

For example, we can write:

import React from "react";
import {
  LineChart,
  Line,
  XAxis,
  YAxis,
  CartesianGrid,
  Tooltip,
  Legend
} from "recharts";

const data = [
  {
    name: "Page A",
    uv: 4000,
    pv: 2400,
    amt: 2400
  },
  {
    name: "Page B",
    uv: 3000,
    pv: 1398,
    amt: 2210
  },
  {
    name: "Page C",
    uv: 2000,
    pv: 9800,
    amt: 2290
  }
];

export default function App() {
  const [opacity, setOpacity] = React.useState({
    uv: 1,
    pv: 1
  });

  const handleMouseEnter = (o) => {
    const { dataKey } = o;
    setOpacity({ ...opacity, [dataKey]: 0.5 });
  };

  const handleMouseLeave = (o) => {
    const { dataKey } = o;
    setOpacity({ ...opacity, [dataKey]: 1 });
  };

  return (
    <div className="App">
      <LineChart
        width={500}
        height={300}
        data={data}
        margin={{
          top: 5,
          right: 30,
          left: 20,
          bottom: 5
        }}
      >
        <CartesianGrid strokeDasharray="3 3" />
        <XAxis dataKey="name" />
        <YAxis />
        <Tooltip />
        <Legend
          onMouseEnter={handleMouseEnter}
          onMouseLeave={handleMouseLeave}
        />
        <Line
          type="monotone"
          dataKey="pv"
          strokeOpacity={opacity.pv}
          stroke="#8884d8"
          activeDot={{ r: 8 }}
        />
        <Line
          type="monotone"
          dataKey="uv"
          strokeOpacity={opacity.uv}
          stroke="#82ca9d"
        />
      </LineChart>
    </div>
  );
}

We have the opacity state to set the opacity of the line.

The Legend has the onMouseEnter and onMouseLeave props to set the functions to set the opacity of the lines when we hover the lines.

The strokeOpacity prop of the Line component sets the opacity of the line.

Responsive Container

We can add a responsive container for our charts with the ResponsiveContainer component.

For example, we can write:

import React from "react";
import {
  AreaChart,
  Area,
  XAxis,
  YAxis,
  CartesianGrid,
  Tooltip,
  ResponsiveContainer
} from "recharts";

const data = [
  {
    name: "Page A",
    uv: 4000,
    pv: 2400,
    amt: 2400
  },
  {
    name: "Page B",
    uv: 3000,
    pv: 1398,
    amt: 2210
  },
  {
    name: "Page C",
    uv: 2000,
    pv: 9800,
    amt: 2290
  }
];
export default function App() {
  return (
    <div className="App" style={{ width: "100%", height: 300 }}>
      <ResponsiveContainer>
        <AreaChart
          data={data}
          margin={{
            top: 10,
            right: 30,
            left: 0,
            bottom: 0
          }}
        >
          <CartesianGrid strokeDasharray="3 3" />
          <XAxis dataKey="name" />
          <YAxis />
          <Tooltip />
          <Area type="monotone" dataKey="uv" stroke="#8884d8" fill="#8884d8" />
        </AreaChart>
      </ResponsiveContainer>
    </div>
  );
}

to add the charts.

We set the width and height of the div.

And the ResponsiveContainer will fill the width and height of the div.

Conclusion

We can change the line opacity when we hover over the legend and the responsive container with Recharts.

Categories
React

Recharts — Area, Bar, and Composed Charts

We can add charts easily to a React app with Recharts.

In this article, we’ll look at how to use it.

Area Chart

We can create an area chart with the AreaChart component:

import React from "react";
import {
  AreaChart,
  Area,
  XAxis,
  YAxis,
  CartesianGrid,
  Tooltip
} from "recharts";

const data = [
  {
    name: "Page A",
    uv: 4000,
    pv: 2400,
    amt: 2400
  },
  {
    name: "Page B",
    uv: 3000,
    pv: 1398,
    amt: 2210
  },
  {
    name: "Page C",
    uv: 2000,
    pv: 9800,
    amt: 2290
  }
];

export default function App() {
  return (
    <div className="App">
      <AreaChart
        width={500}
        height={400}
        data={data}
        margin={{
          top: 10,
          right: 30,
          left: 0,
          bottom: 0
        }}
      >
        <CartesianGrid strokeDasharray="3 3" />
        <XAxis dataKey="name" />
        <YAxis />
        <Tooltip />
        <Area type="monotone" dataKey="uv" stroke="#8884d8" fill="#8884d8" />
      </AreaChart>
    </div>
  );
}

We pass in the width and height props to set the dimensions.

The margin has the margin.

CartesianGrid has the grid to display.

XAxis has the x-axis.

YAxis has the y-axis.

Tooltip shows the values when we hover over the point.

Area fills the rea with the given color between the line and the x-axis.

The dataKey has the property name that has the value we want to display.

Bar Chart

To create a bar chart, we can use the BarChart component,

For example, we can write:

import React from "react";
import {
  BarChart,
  Bar,
  XAxis,
  YAxis,
  CartesianGrid,
  Tooltip,
  Legend
} from "recharts";

const data = [
  {
    name: "Page A",
    uv: 4000,
    pv: 2400,
    amt: 2400
  },
  {
    name: "Page B",
    uv: 3000,
    pv: 1398,
    amt: 2210
  },
  {
    name: "Page C",
    uv: 2000,
    pv: 9800,
    amt: 2290
  }
];

export default function App() {
  return (
    <div className="App">
      <BarChart
        width={500}
        height={300}
        data={data}
        margin={{
          top: 5,
          right: 30,
          left: 20,
          bottom: 5
        }}
      >
        <CartesianGrid strokeDasharray="3 3" />
        <XAxis dataKey="name" />
        <YAxis />
        <Tooltip />
        <Legend />
        <Bar dataKey="pv" fill="#8884d8" />
        <Bar dataKey="uv" fill="#82ca9d" />
      </BarChart>
    </div>
  );
}

to add our bar chart.

BarChart can hold multiple bars.

The dataKey has the property name that has the value we want to display.

fill has the fill color of the bars.

Composing Charts

We can have multiple graphs in one chart.

For example, we can write:

import React from "react";
import {
  ComposedChart,
  Line,
  Area,
  Bar,
  XAxis,
  YAxis,
  CartesianGrid,
  Tooltip,
  Legend,
  Scatter
} from "recharts";

const data = [
  {
    name: "Page A",
    uv: 4000,
    pv: 2400,
    amt: 2400
  },
  {
    name: "Page B",
    uv: 3000,
    pv: 1398,
    amt: 2210
  },
  {
    name: "Page C",
    uv: 2000,
    pv: 9800,
    amt: 2290
  }
];

export default function App() {
  return (
    <div className="App">
      <ComposedChart
        width={500}
        height={400}
        data={data}
        margin={{
          top: 20,
          right: 20,
          bottom: 20,
          left: 20
        }}
      >
        <CartesianGrid stroke="#f5f5f5" />
        <XAxis dataKey="name" />
        <YAxis />
        <Tooltip />
        <Legend />
        <Area type="monotone" dataKey="amt" fill="#8884d8" stroke="#8884d8" />
        <Bar dataKey="pv" barSize={20} fill="#413ea0" />
        <Line type="monotone" dataKey="uv" stroke="#ff7300" />
      </ComposedChart>
    </div>
  );
}

to add multiple charts with Recharts.

We just all the components into the ComposedChart component.

Legend has the legend for the graphs.

The type prop sets the type of the area chart.

Conclusion

We can create various kinds of charts with Recharts.

Categories
React

Create Charts in a React App with Recharts

We can add charts easily to a React app with Recharts.

In this article, we’ll look at how to use it.

Getting Started

We can get started by install the Recharts package:

npm i recharts

Then we can use it create a simple chart by writing:

import React from "react";
import { LineChart, Line } from "recharts";
const data = [
  { name: "Page A", uv: 400 },
  { name: "Page B", uv: 500 },
  { name: "Page C", uv: 450 }
];

export default function App() {
  return (
    <div className="App">
      <LineChart width={400} height={400} data={data}>
        <Line type="monotone" dataKey="uv" stroke="#8884d8" />
      </LineChart>
    </div>
  );
}

The dataKey property specifies the property with the most data.

stroke has the stroke color.

width and height are the dimensions.

LineChart creates a line chart.

We can also add the components to a line chart individually:

import React from "react";
import { LineChart, Line, CartesianGrid, XAxis, YAxis } from "recharts";
const data = [
  { name: "Page A", uv: 400 },
  { name: "Page B", uv: 500 },
  { name: "Page C", uv: 450 }
];

export default function App() {
  return (
    <div className="App">
      <LineChart width={400} height={400} data={data}>
        <Line type="monotone" dataKey="uv" stroke="#8884d8" />
        <CartesianGrid stroke="#ccc" />
        <XAxis dataKey="name" />
        <YAxis />
      </LineChart>
    </div>
  );
}

We add the CartesianGrid to add a grid in the background.

stroke sets the color of the grid lines.

XAxis lets us add the x-axis. The dataKey has the property name for the label.

YAxis renders the y-axis.

Also, we can adjust the props of some components:

import React from "react";
import { LineChart, Line, CartesianGrid, XAxis, YAxis } from "recharts";
const data = [
  { name: "Page A", uv: 400 },
  { name: "Page B", uv: 500 },
  { name: "Page C", uv: 450 }
];

export default function App() {
  return (
    <div className="App">
      <LineChart
        width={600}
        height={300}
        data={data}
        margin={{ top: 5, right: 20, bottom: 5, left: 0 }}
      >
        <Line type="monotone" dataKey="uv" stroke="#8884d8" />
        <CartesianGrid stroke="#ccc" strokeDasharray="5 5" />
        <XAxis dataKey="name" />
        <YAxis />
      </LineChart>
    </div>
  );
}

The margin sets the margins of the graph.

The ticks can also be customized with a function:

import React from "react";
import { LineChart, Line, CartesianGrid, XAxis, YAxis } from "recharts";
const data = [
  { name: "Page A", uv: 400 },
  { name: "Page B", uv: 500 },
  { name: "Page C", uv: 450 }
];

const renderCustomAxisTick = ({ x, y, payload }) => {
  let path = "";

switch (payload.value) {
    case "Page A":
      path =
        "M899.072 99.328q9.216 13.312 17.92 48.128t16.384 81.92 13.824 100.352 11.264 102.912 9.216 90.112 6.144 60.928q4.096 30.72 7.168 70.656t5.632 79.872 4.096 75.264 2.56 56.832q-13.312 16.384-30.208 25.6t-34.304 11.264-34.304-2.56-30.208-16.896q-1.024-10.24-3.584-33.28t-6.144-53.76-8.192-66.56-8.704-71.68q-11.264-83.968-23.552-184.32-7.168 37.888-11.264 74.752-4.096 31.744-6.656 66.56t-0.512 62.464q1.024 18.432 3.072 29.184t4.608 19.968 5.12 21.504 5.12 34.304 5.12 56.832 4.608 90.112q-11.264 24.576-50.688 42.496t-88.576 29.696-97.28 16.896-74.752 5.12q-18.432 0-46.08-2.56t-60.416-7.168-66.048-12.288-61.952-17.92-49.664-24.064-28.16-30.208q2.048-55.296 5.12-90.112t5.632-56.832 5.12-34.304 5.12-21.504 4.096-19.968 3.584-29.184q2.048-27.648-0.512-62.464t-6.656-66.56q-4.096-36.864-11.264-74.752-13.312 100.352-24.576 184.32-5.12 35.84-9.216 71.68t-8.192 66.56-6.656 53.76-2.56 33.28q-13.312 12.288-30.208 16.896t-34.304 2.56-33.792-11.264-29.696-25.6q0-21.504 2.048-56.832t4.096-75.264 5.632-79.872 6.656-70.656q2.048-20.48 6.144-60.928t9.728-90.112 11.776-102.912 13.824-100.352 16.384-81.92 17.92-48.128q20.48-12.288 56.32-25.6t73.216-26.624 71.168-25.088 50.176-22.016q10.24 13.312 16.896 61.44t13.312 115.712 15.36 146.432 23.04 153.6l38.912-334.848-29.696-25.6 43.008-54.272 15.36 2.048 15.36-2.048 43.008 54.272-29.696 25.6 38.912 334.848q14.336-74.752 23.04-153.6t15.36-146.432 13.312-115.712 16.896-61.44q16.384 10.24 50.176 22.016t71.168 25.088 73.216 26.624 56.32 25.6";
      break;
    case "Page B":
      path =
        "M662.528 451.584q10.24 5.12 30.208 16.384t46.08 31.744 57.856 52.736 65.024 80.896 67.072 115.2 64.512 154.624q-15.36 9.216-31.232 21.504t-31.232 22.016-31.744 15.36-32.768 2.56q-44.032-9.216-78.336-8.192t-62.976 7.68-53.248 16.896-47.616 19.968-46.08 16.384-49.664 6.656q-57.344-1.024-110.592-16.896t-101.376-32.256-89.6-25.088-75.264 4.608q-20.48 8.192-41.984 1.024t-38.912-18.432q-20.48-13.312-39.936-33.792 37.888-116.736 86.016-199.68t92.672-136.704 78.848-81.408 43.52-33.792q9.216-5.12 10.24-25.088t-1.024-40.448q-3.072-24.576-9.216-54.272l-150.528-302.08 180.224-29.696q27.648 52.224 53.76 79.36t50.176 36.864 45.568 5.12 39.936-17.92q43.008-30.72 80.896-103.424l181.248 29.696q-20.48 48.128-45.056 99.328-20.48 44.032-47.616 97.28t-57.856 105.472q-12.288 34.816-13.824 57.344t1.536 36.864q4.096 16.384 12.288 25.6z";
      break;
    default:
      path =
        "M662.528 451.584q10.24 5.12 30.208 16.384t46.08 31.744 57.856 52.736 65.024 80.896 67.072 115.2 64.512 154.624q-15.36 9.216-31.232 21.504t-31.232 22.016-31.744 15.36-32.768 2.56q-44.032-9.216-78.336-8.192t-62.976 7.68-53.248 16.896-47.616 19.968-46.08 16.384-49.664 6.656q-57.344-1.024-110.592-16.896t-101.376-32.256-89.6-25.088-75.264 4.608q-20.48 8.192-41.984 1.024t-38.912-18.432q-20.48-13.312-39.936-33.792 37.888-116.736 86.016-199.68t92.672-136.704 78.848-81.408 43.52-33.792q9.216-5.12 10.24-25.088t-1.024-40.448q-3.072-24.576-9.216-54.272l-150.528-302.08 180.224-29.696q27.648 52.224 53.76 79.36t50.176 36.864 45.568 5.12 39.936-17.92q43.008-30.72 80.896-103.424l181.248 29.696q-20.48 48.128-45.056 99.328-20.48 44.032-47.616 97.28t-57.856 105.472q-12.288 34.816-13.824 57.344t1.536 36.864q4.096 16.384 12.288 25.6z";
  }

return (
    <svg
      x={x - 12}
      y={y + 4}
      width={24}
      height={24}
      viewBox="0 0 1024 1024"
      fill="#666"
    >
      <path d={path} />
    </svg>
  );
};

export default function App() {
  return (
    <div className="App">
      <LineChart
        width={400}
        height={300}
        data={data}
        margin={{ top: 5, right: 20, bottom: 5, left: 0 }}
      >
        <Line type="monotone" dataKey="uv" stroke="#8884d8" />
        <CartesianGrid stroke="#ccc" strokeDasharray="5 5" />
        <XAxis dataKey="name" tick={renderCustomAxisTick} />
        <YAxis />
      </LineChart>
    </div>
  );
}

We pass in the renderCustomAxisTick component which renders the SVG we want to display as the x-axis label.

Conclusion

Recharts lets us add line charts easily.

Categories
React

Create Charts in React Easily with the react-chartist Library

We can add charts to our React app easily with the react-chartist library.

In this article, we’ll look at how to add charts easily with the react-chartist library.

Installation

We can install the library by running:

npm install react-chartist --save

We also need the chartist package which it depends on.

This can be installed by running:

npm install chartist --save

Creating Charts

Once we installed the libraries, we can create our own charts with it.

We have to add the CSS by adding:

<link
      rel="stylesheet"
      href="//cdn.jsdelivr.net/chartist.js/latest/chartist.min.css"
    />

to the head tag in public/index.html .

Then in our component, we can add our graph:

import React from "react";
import ChartistGraph from "react-chartist";

const data = {
  labels: [2016, 2017, 2018, 2019, 2020],
  series: [[1, 2, 4, 8, 6]]
};

const options = {
  high: 10,
  low: -10,
  axisX: {
    labelInterpolationFnc(value, index) {
      return index % 2 === 0 ? value : null;
    }
  }
};

const type = "Bar";

export default function App() {
  return (
    <div className="App">
      <ChartistGraph data={data} options={options} type={type} />
    </div>
  );
}

data has the chart data.

labels has the x-axis labels.

series has the y-axis values.

options has various options.

high sets the max value to display for the y-axis.

low sets the min value to display for the x-axis.

axisX has the labelInterpolationFnc to set how the x-axis labels are displayed.

value has the x-axis value.

index has the index of the x-axis value in labels .

Whatever returns true when this function is called with is displayed.

Line charts can be created by changing 'Bar' to 'Line' .

Pie Chart

We can add the pie chart by writing:

import React from "react";
import ChartistGraph from "react-chartist";

const data = {
  series: [20, 10, 30, 40]
};

const options = {};

const type = "Pie";

export default function App() {
  return (
    <div className="App">
      <ChartistGraph data={data} options={options} type={type} />
    </div>
  );
}

The series property now has a 1-d array instead of a 2-d one.

And the type is 'Pie' .

We can change a few options to configure our chart.

For example, we can write:

import React from "react";
import ChartistGraph from "react-chartist";

const data = {
  series: [20, 10, 30, 40]
};

const options = {
  donut: true,
  donutWidth: 20,
  startAngle: 270,
  total: 100
};

const type = "Pie";

export default function App() {
  return (
    <div className="App">
      <ChartistGraph data={data} options={options} type={type} />
    </div>
  );
}

donut set to true makes the pie chart display as a donut.

donutWidth sets the width of the ring in pixels.

startAngle sets the starting angle for the chart in degrees.

total has the total amount that the series numbers add up to.

Conclusion

We can use react-chartist to create simple charts with lots of flexibility.

Categories
React

Create Charts in React Easily with the react-jsx-highcharts Library

The react-jsx-highcharts library provides us with a flexible way to create charts in our React apps.

In this article, we’ll look at how to add charts with this library.

Installation

We can install the library by running:

npm i react-jsx-highcharts

We also need to install the Highcharts package by running:

npm i highcharts@^8.0.0

Then we can use it by writing:

import React from "react";
import {
  HighchartsChart,
  Chart,
  Title,
  XAxis,
  YAxis,
  LineSeries,
  withHighcharts,
  Subtitle,
  Legend
} from "react-jsx-highcharts";
import Highcharts from "highcharts";

let Charts = () => {
  return (
    <HighchartsChart>
      <Chart />
      <Title>fruit sales, 2010-2014</Title>
      <Subtitle>Source</Subtitle>
      <Legend layout="vertical" align="right" verticalAlign="middle" />
      <XAxis>
        <XAxis.Title>Time</XAxis.Title>
      </XAxis>
      <YAxis>
        <YAxis.Title>sales</YAxis.Title>
        <LineSeries name="apple" data={[100, 200, 500, 300, 200]} />
        <LineSeries name="orange" data={[200, 300, 349, 193, 483]} />
        <LineSeries name="grape" data={[158, 582, 482, 486, 192]} />
      </YAxis>
    </HighchartsChart>
  );
};

Charts = withHighcharts(Charts, Highcharts);

export default function App() {
  return (
    <div className="App">
      <Charts />
    </div>
  );
}

We use the withHighcharts higher-order component so that we get access to the Highcharts object for rendering the chart.

In the Charts component, we create the chart by using the HighchartsChart as the wrapper.

Chart creates the chart.

Title has the title.

Subtitle has the subtitle.

Legend has the chart legend.

XAxis has the x-axis label.

YAxis has the y-axis values.

LineSeries has the lines we want to add to the chart.

name has the name of the line.

data has the y-axis values.

We can add categories to the x-axis by adding the categories prop:

import React from "react";
import {
  HighchartsChart,
  Chart,
  Title,
  XAxis,
  YAxis,
  LineSeries,
  withHighcharts,
  Subtitle,
  Legend
} from "react-jsx-highcharts";
import Highcharts from "highcharts";

let Charts = () => {
  return (
    <HighchartsChart>
      <Chart />
      <Title>friut sales, 2010-2014</Title>
      <Subtitle>Source</Subtitle>
      <Legend layout="vertical" align="right" verticalAlign="middle" />
      <XAxis categories={[2010, 2011, 2012, 2013, 2014]}>
        <XAxis.Title>Time</XAxis.Title>
      </XAxis>
      <YAxis>
        <YAxis.Title>sales</YAxis.Title>
        <LineSeries name="apple" data={[100, 200, 500, 300, 200]} />
        <LineSeries name="orange" data={[200, 300, 349, 193, 483]} />
        <LineSeries name="grape" data={[158, 582, 482, 486, 192]} />
      </YAxis>
    </HighchartsChart>
  );
};

Charts = withHighcharts(Charts, Highcharts);

export default function App() {
  return (
    <div className="App">
      <Charts />
    </div>
  );
}

Now we see the x-axis ticks with the labels listed in catergories .

We can have multiple kinds of graphs in one chart.

For example, we can write:

import React from "react";
import {
  HighchartsChart,
  Chart,
  Title,
  XAxis,
  YAxis,
  ColumnSeries,
  withHighcharts,
  PieSeries,
  SplineSeries,
  Legend
} from "react-jsx-highcharts";
import Highcharts from "highcharts";

const pieData = [
  {
    name: "Jane",
    y: 13
  },
  {
    name: "John",
    y: 23
  },
  {
    name: "Joe",
    y: 19
  }
];

let Charts = () => {
  return (
    <HighchartsChart>
      <Chart />
      <Title>Combination chart</Title>
      <Legend />
      <XAxis categories={["Apples", "Oranges", "Pears", "Bananas", "Plums"]} />
      <YAxis>
        <ColumnSeries name="Jane" data={[3, 2, 1, 3, 4]} />
        <ColumnSeries name="John" data={[2, 3, 5, 7, 6]} />
        <ColumnSeries name="Joe" data={[4, 3, 3, 9, 0]} />
        <SplineSeries name="Average" data={[3, 2.67, 3, 6.33, 3.33]} />
        <PieSeries
          name="Total consumption"
          data={pieData}
          center={[100, 80]}
          size={100}
          showInLegend={false}
        />
      </YAxis>
    </HighchartsChart>
  );
};

Charts = withHighcharts(Charts, Highcharts);

export default function App() {
  return (
    <div className="App">
      <Charts />
    </div>
  );
}

We add the ColumnSeries component to add a bar chart.

PieSeries adds a pie chart.

SplineSeries adds a line chart.

The charts are very dynamic because we define the x and y axes separately.

Conclusion

The react-jsx-highcharts library is one of the most flexible charting libraries for React.