Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                $breakpoints: (
    sm: 520px,
    md: 760px,
);

@mixin media($bp) {
  @media screen and (max-width: map_get($breakpoints, $bp)) {
    @content;
  }
}

body {
  width: 100vw;
  height: 100vh;
  font-family: 'Montserrat', sans-serif;
  background-color: #e6e8ec;
  display: flex;
  align-items: center;
  justify-content: center;
}

.graph {
  position: relative;
  background-color: white;
  margin: 2rem;
  width: 1000px;
  height: 400px;
  border-radius: 10px;
  box-shadow: 
    0 2px 10px rgba(0,0,0,0.01),
    0 4px 20px rgba(0,0,0,0.01),
    0 6px 40px rgba(0,0,0,0.01),
    0 8px 60px rgba(0,0,0,0.01)
}

.labels {
  display: flex;
  width: 100%;
  position: absolute;
  justify-content: space-between;
  left: 0;
  bottom: 0;
  box-sizing: border-box;
  
  div {
    margin: 1rem;
    color: #b7bbc8;
    
    @include media('sm') {
      font-size: 0.8rem;
      margin: 0.5rem;
    }
  }
}

.heading {
  position: absolute;
  top: 3rem;
  left: 3rem;
  display: flex;
  flex-direction: column;
  gap: 1rem 0;
  align-items: flex-start;
  
  @include media('md') {
    top: 2rem;
    left: 2rem;
  }
  
  @include media('sm') {
    top: 1rem;
    left: 1rem;
  }
  
  .title {
    font-weight: bold;
    font-size: 3rem;
    color: #293155;
    
    @include media('sm') {
      font-size: 1.5rem;
    }
  }
  
  .subtitle {
    font-size: 1.2rem;
    color: #898fa3;
    
    @include media('sm') {
      font-size: 0.9rem;
    }
  }
}

.points {
  position: absolute;
  top: 0;
  
  div {
    --size: 3rem;
    position: absolute;
    width: var(--size);
    height: var(--size);
    top: var(--y);
    left: var(--x);
    transform: translate(calc(-0.5 * var(--size)), calc(-0.5 * var(--size)));
  }
}

.marker {
  position: absolute;
  top: var(--y);
  left: var(--x);
  bottom: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: none;
  transform: translateX(-50%);
  
  .tooltip {
    position: absolute;
    transform: translateY(calc(-100% - 2rem));
    background-color: white;
    padding: 1rem;
    border-radius: 0.4rem;
    box-shadow: 
      0 2px 2px rgb(0 0 0 / 10%), 
      0 4px 10px rgb(0 0 0 / 10%);
    
    &::before, &::after {
      --size: 0.8rem;
      content: '';
      position: absolute;
      border: calc(var(--size) * 0.5) solid transparent;
      left: calc(50% - calc(var(--size) * 0.5));
    }
    
    &::before { // Arrow shadow
      bottom: calc(-1 * var(--size) - 2px);
      border-top-color: rgb(0 0 0 / 6%);
    }
    
    &::after { // Arrow
      bottom: calc(-1 * var(--size));
      border-top-color: white;
    }
    
    span:first-child {
      color: #b7bbc8;
      padding-right: 1rem;
    }
    
    span:last-child {
      border-left: 1px solid #b7bbc8;
      padding-left: 1rem;
    }
  }
  
  .circle {
    position: absolute;
    top: 0;
    flex-shrink: 0;
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 1rem;
    background-color: var(--color);
    border: 0.7rem solid white;
    transform: translateY(-50%);
    box-shadow: 
      0 1px 5px rgb(0 0 0 / 10%),
      0 2px 15px rgb(0 0 0 / 10%);
  }
  
  .line {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 1px;
    background-color: #ccd3e7;
  }
}

.legend {
  position: absolute;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  gap: 0 1rem;
  margin: 3rem;
  
  @include media('md') {
    justify-content: flex-end;
    margin: 2rem;
  }
  
  @include media('sm') {
    margin: 1rem;
    font-size: 0.8rem;
  }
  
  > div {
    display: flex;
    color: #898fa3;
  }

  span {
    display: block;
    background-color: var(--color);
    width: 1rem;
    height: 1rem;
    border-radius: 1rem;
    margin-right: 0.5rem;
  }
}

svg {
  width: 100%;
  height: 100%;
  transform: scaleY(-1); // Change to cartesian coordinate system
  
  path {
    vector-effect: non-scaling-stroke;
  }
  
  .stroke {
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-width: 0.15rem;
    pointer-events: none;
  }
  
  .gradient {
    pointer-events: none;
  }
}
              
            
!

JS

              
                import React, {useState, useRef} from 'https://cdn.skypack.dev/react';
import ReactDOM from 'https://cdn.skypack.dev/react-dom';
import {useDimensions} from 'https://cdn.skypack.dev/webrix@1.5.9/hooks';

const DATA = [
  [1131, 1604, 1240, 1731, 1304, 2101, 1701],
  [708, 1401, 1140, 1503, 1020, 1751, 902],
];

const COLORS = ['#00baf0', '#5637f4'];
const LABELS = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL'];

const Line = ({path, color}) => {
  const dx = 100 / (path.length - 1);
  const d = `M0,${path[0]} ${path.slice(1).map((p, i) => (
    `C${dx*i + dx/2},${path[i]} `+
    `${dx*(i+1) - dx/2},${path[i+1]} `+
    `${dx*(i+1)},${path[i+1]} `
  )).join(' ')}`;
  return (
    <>
      <path stroke={color} d={d} fill='none' className='stroke'/>
      <path d={d+` V0 H0 Z`} fill={`url(#gradient-${color})`} className='gradient'/>
      <defs>
        <linearGradient id={`gradient-${color}`} x1='0' x2='0' y1='0' y2='1'>
          <stop offset='0%' stopColor={color} stopOpacity={0}/>
          <stop offset='100%' stopColor={color} stopOpacity={0.15}/>
        </linearGradient>
       </defs>
    </>
  );
}

const Points = ({data, width, height, setActive, range}) => {
  const timeout = useRef();
  const dr = Math.abs(range[1] - range[0]);
  const activate = (path, point) => {
    clearTimeout(timeout.current);
    setActive({path, point});
  }
  const deactivate = (path, point) => {
    clearTimeout(timeout.current);
    timeout.current = setTimeout(() => {
      setActive(cur => {
        if (cur.path === path && cur.point === point) {
          return null;
        }
        return cur;
      });
    }, 200);
  }
  return (
    <div className='points'>
      {data.map((row, r) => row.map((y, i) => (
        <div style={{'--x': `${i * width / (row.length - 1)}px`, '--y': `${height - y * (height / dr)}px`}} onMouseEnter={() => activate(r, i)} onMouseLeave={() => deactivate(r, i)}/>
      )))}
    </div>
  );
}

const Legend = ({labels, colors}) => (
  <div className='legend'>
    {labels.map((l, i) => (
      <div key={l}><span style={{'--color': colors[i]}}/>{l}</div>
    ))}
  </div>
);

const Marker = ({colors, labels, data, active, width, height, range}) => {
  const {path, point} = active || {};
  const value = data[path]?.[point];
  const dr = Math.abs(range[1] - range[0]);
  return (
    <div className='marker' style={{
        opacity: active ? 1 : 0,
        '--color': colors[path], 
        '--x': `${point * width / (data[path]?.length - 1)}px`,
        '--y': `${height - value * (height / dr)}px`
      }}>
      <div className='tooltip'>
        <span>{labels[point]}</span>
        <span>${value?.toLocaleString?.()}</span>
      </div>
      <div className='line'/>
      <div className='circle'/>
    </div>
  );
}

const Graph = ({data, colors, range, labels, title, subtitle, legend}) => {
  const [active, setActive] = useState({path: 1, point: 2});
  const graph = useRef();
  const {width, height} = useDimensions(graph);
  return (
    <div className='graph' ref={graph}>
      <div className='heading'>
        <div className='title'>{title}</div>
        <div className='subtitle'>{subtitle}</div>
      </div>
      <Marker colors={colors} data={data} active={active} labels={labels} width={width} height={height} range={range}/>
      <Legend colors={colors} labels={legend}/>
      <svg viewBox={`0 ${range[0]} 100 ${range[1]}`} preserveAspectRatio='none'>
        {data.map((path, i) => (
          <Line key={i} path={path} color={colors[i]}/>
        ))}
      </svg>
      <div className='labels'>
        {labels.map(label => (
          <div key={label}>{label}</div>
        ))}
      </div>
      <Points data={data} width={width} height={height} setActive={setActive} range={range}/>
    </div>
  );
};

const App = () => (
  <Graph data={DATA} colors={COLORS} range={[0, 3000]} labels={LABELS} title='$11,3K' subtitle='YTD Revenue' legend={['Stream A', 'Stream B']}/>
);

ReactDOM.render(
  <App/>,
  document.body
);
              
            
!
999px

Console