cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

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.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

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.

            
              @import url('https://fonts.googleapis.com/css?family=Lato:300,900');

#emmys {
  font-family: "Lato";
}

#emmys .subject-ring {
  display: none;
}

div.title {
  font-size: 2em;
  width: 200px;
  position: absolute;
  left: 70px;
  top: 100px;
  line-height: 1.5em;
  color: "black";
  font-family: "Lato";
}

#emmys .annotation.badge text {
  font-weight: normal;
  font-size: 10px;
}

#emmys .annotation text {
  font-size: 0.7em;
  text-transform: uppercase;
  font-weight: bold;
}

.annotation.axis .annotation-note-bg {
  fill: white;
  stroke: white;
  stroke-width: 10;
}

.annotation.axis path {
  stroke: lightgrey;
}

.annotation.axis text {
  fill: lightgrey;
}

            
          
!
            
              d3.json(
  "https://gist.githubusercontent.com/susielu/625aa4814098671290a8c6bb88a6301e/raw/871576bf6cd336aee813ba97078b97fc91da3062/yearNetwork.json",
  function(error, { networkLines }) {
    const { AnnotationLabel, AnnotationBadge, AnnotationXYThreshold } = ReactAnnotation
    
    function LineChart() {
      const width = 960,
        height = 500,
        margin = { top: 30, right: 130, bottom: 50, left: 340 }

      const x = d3.scaleLinear()
        .range([margin.left, width - margin.right])
        .domain([2013, 2017])
      const y = d3.scaleLinear().range([height - margin.bottom, margin.top])

      y.domain([0, Math.max(...networkLines.map(d => d.max))])
      let lineGen = d3.line()
        .x(function(d) {
          return x(d.year)
        })
        .y(function(d) {
          return y(d.value)
        })

      const colors = {
        HBO: "black",
        Netflix: "#D32F2F",
        NBC: "#ffc107",
        "FX Networks": "#0097a7",
        ABC: "#00BFA5",
        CBS: "#00BCD4",
        FOX: "#3f51b5",
        Showtime: "#C5CAE9",
        AMC: "#D32F2F",
        PBS: "#B39DDB",
        Amazon: "#ffc107",
        "Nat Geo": "#ff9800",
        Hulu: "#00BFA5"
      }
      const highlight = ["HBO", "Netflix"]

      const lines = networkLines
        .sort((a, b) => a.total - b.total)
        .map(d => (
          <path
            key={d.network}
            className="segment"
            d={lineGen(d.line)}
            fill="none"
            stroke={colors[d.network] || "grey"}
            strokeDasharray={
              highlight.indexOf(d.network) !== -1 ? "none" : "2, 4"
            }
          />
        ))

      /* Code below relevant for annotations */
      let previousNY = 0
      const labelAnnotations = networkLines
        .sort(
          //sort annotations by last data point for ordering
          (a, b) =>
            b.line[b.line.length - 1].value - a.line[a.line.length - 1].value
        )
        .reduce((p, c) => {
          //push annotation down if it will overlap
          const ypx = y(c.line[c.line.length - 1].value)
          let ny

          if (ypx - previousNY < 10) {
            ny = previousNY + 15
          }

          p.push(
            <AnnotationLabel
              key={c.network}
              note={{
                label: c.network,
                orientation: "leftRight",
                align: "middle"
              }}
              y={ypx}
              x={width - margin.right}
              dx={highlight.indexOf(c.network) !== -1 ? 20 : 5}
              id={c.network}
              color={colors[c.network]}
              disable={["connector"]}
              ny={ny} //use ny to directly place the note in xy space if needed
            />
          )
          previousNY = ny || ypx

          return p
        }, [])

      const axisAnnotations = networkLines
        .filter(d => d.network === "HBO")[0]
        .line.map(d => (
          <AnnotationXYThreshold
            key={d.year}
            note={{ label: d.year, align: "middle", lineType: null }}
            ny={190}
            className={"axis"}
            y={190}
            x={x(d.year)}
            subject={{
              y1: y(0),
              y2: y(d.value)
            }}
          />
        ))

      const labels = networkLines
        .filter(d => highlight.indexOf(d.network) !== -1)
        .reduce((p, c) => {
          p = p.concat(
            c.line.map(d => {
              return {
                network: c.network,
                year: d.year,
                value: d.value
              }
            })
          )
          return p
        }, [])

      const badgeAnnotations = labels.map(d => {
        return (
          <AnnotationBadge
            key={d.year + d.network}
            subject={{
              text: d.value,
              radius: 12
            }}
            color={colors[d.network]}
            x={x(d.year)}
            y={y(d.value)}
          />
        )
      })

      return (
        <div style={{ position: "relative" }}>
          <svg width={960} height={500} id="emmys">
            <g className="lineChart">
              {lines}
              {labelAnnotations}
              {axisAnnotations}
              {badgeAnnotations}
              <line
                className="basline axis"
                stroke="lightgrey"
                x1={x(2013)}
                x2={x(2017)}
                y1={y(0)}
                y2={y(0)}
              />
            </g>
          </svg>
          <div className="title">
            <b style={{ color: "#d32f2f" }}>Netflix</b> Challenges <b>HBO</b> at
            the 2017 Emmys
          </div>
        </div>
      )
    }

    ReactDOM.render(<LineChart />, document.getElementById("main"))
  }
)

            
          
!
999px
Loading ..................

Console