DEV Community

Cover image for Color 2 Areas underlines in Recharts without mixing colors caused by the Opacity
Hadi Houssainy
Hadi Houssainy

Posted on

3 1

Color 2 Areas underlines in Recharts without mixing colors caused by the Opacity

This is how to separate the charts areas color when dealing with recharts,

I'm using react with material-ui theme ( the useTheme line 🙄 )

U need to create 3 areas for the 2 lines,
this method work only when u know that 1 line is always on top

function RevenueChart({ data }: RevenueChartProps) {
  const theme = useTheme();
  return (
    <div style={{ height: 300, width: '100%' }}>
      <ResponsiveContainer>
        <AreaChart
          width={730}
          height={250}
          data={data}
          margin={{ top: 10, right: 30, left: 0, bottom: 0 }}
        >
          <XAxis dataKey="name" tick={{ fill: theme.palette.text.hint }} />
          <YAxis tick={{ fill: theme.palette.text.hint }} />
          <CartesianGrid strokeDasharray="3 3" />
          <Area
            activeDot={false}
            dot={false}
            strokeWidth={0}
            type="monotone"
            dataKey="diff"
            stroke={theme.palette.primary.main}
            fillOpacity={0.6}
            fill={lighten(theme.palette.primary.main, 0.2)}
          />
          <Area
            activeDot={{ r: 8, fill: theme.palette.primary.main }}
            strokeWidth={2}
            type="monotone"
            dataKey="incomes"
            stroke={theme.palette.primary.main}
            fillOpacity={0}
            dot
          />
          <Area
            activeDot={{ r: 8, fill: '#ea6b78' }}
            strokeWidth={2}
            type="monotone"
            dataKey="outcomes"
            stroke="#ea6b78"
            fillOpacity={0.6}
            fill={lighten('#ea6b78', 0.2)}
            dot
          />
          <Tooltip content={<CustomContent />} />
        </AreaChart>
      </ResponsiveContainer>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

the data prop has the form of:

type data = {
  incomes: Array<Numbers>,
  outcomes: Array<Numbers>,
  diff: Array<Array<Number, Number>>,
}
Enter fullscreen mode Exit fullscreen mode

the incomes & outcomes are clearly just your data,
the diff is an array of arrays, the ith inner array is formed by [ incomes[i], outcomes[i] ] as u can see incomes is on top so it's the start point & outcomes is in the bottom, the end.

the components are created in this form:

<Area
    ...
    strokeWidth={2}
    dataKey="diff"
    fillOpacity={0.6}
    fill={lighten(theme.palette.primary.main, 0.2)}
    ...
    />
Enter fullscreen mode Exit fullscreen mode

the colored area between the lines has no stroke (strokeWidth = 0) but has a fill color (opacity != 0),

<Area
    ...
    strokeWidth={2}
    dataKey="diff"
    fillOpacity={0}
    ...
    />
Enter fullscreen mode Exit fullscreen mode

the top line have no fill color (opacity = 0) but have stroke (strokeWidth != 0),

and the bottom line can have both

The main idea is that Area component can take data as an array of start & end limits

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay