Pen Settings

HTML

CSS

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.

+ 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

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.

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

              
                <div class="js-report-sparkline" data-range-low-color="orange" data-range-high-color="green">[0.0297,0.0267,0.0312,0.0330,0.0386,0.0347]</div>

<div class="js-report-sparkline" data-range-low-color="red" data-range-high-color="blue" style="width: 250px">[0.3767,0.8800,2.1001,1.1000,1.300,0.8800,2.1476]</div>
              
            
!

CSS

              
                .js-report-sparkline {
  width: 208px;
  height: 70px;
  margin: 50px auto;
  font-family: sans-serif;
  position: relative;
  
  * {
    box-sizing: border-box;
  }

  g {
  }

  path {
    stroke-width: 3;
    fill: none;
  }

  .point {
    stroke-width: 2;
    transition: all .3s;
  }

  .bar-rect {
    fill: transparent;
    stroke: none;
  }

  .point.hover {
    stroke-width: 4;
  }

  .chart-container {
    position: relative;
  }

  .chart-tooltip {
    position: absolute;
    background: rgba(0, 0, 0, .8);
    color: white;
    left: 0;
    display: none;
    padding: 2px 5px;
    font-size: 12px;
    border-radius: 5px;
    border: 1px solid rgba(255, 255, 255, 1);
    pointer-events: none;

    .title {
      z-index: 3;
      text-transform: uppercase;
      font-size: 13px;
    }
  }

  .bullet {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    display: block;
    position: absolute;
    right: 5px;
    top: 13px;
  }
}
              
            
!

JS

              
                // Metric
$('.js-report-sparkline').each(function(sparklineId) {
    var th = $(this)

        // Instead of splitting with "," we are passing the data in JSON format
        // Because splitting may cause getting datas as string
        // And that breaks scale calculators
        // Also this chain clears the HTML content
        data = $.parseJSON(
            th.data("sparkline-data", th.text())
               .text('')
               .data("sparkline-data")
        ),

        // Get width and height of the container
        w = th.width(),
        h = th.height(),

        // Setting the margins
        // You may set different margins for X/Y
        xMargin = 30,
        yMargin = 15,

        // Scale functions
        // Setting the range with the margin
        y = d3.scale.linear()
                    .domain([d3.min(data), d3.max(data)])
                    .range([yMargin, h - yMargin]),
        x = d3.scale.linear()
                    .domain([0, data.length - 1])
                    .range([xMargin, w - xMargin]),

        // Scale functions for creating the gradient fill/stroke
        // Calculating the color according to data in the range of colors
        // That user has passed with the data-range-[high-low]-color attributes
        gradientY = d3.scale.linear()
                            .domain([d3.min(data), d3.max(data)])
                            .range([th.data("range-low-color"), th.data("range-high-color")]),

        // This is a different margin than the one for the chart
        // Setting the gradient stops from 0% to 100% will cause wrong color ranges
        // Because data points are positioned in the center of containing rect
        percentageMargin = 100 / data.length,
        percentageX = d3.scale.linear()
                              .domain([0, data.length - 1])
                              .range([percentageMargin, 100 - percentageMargin]),

        // Create S
        container = d3.select(this).append("div"),

        // Create tooltip
        tooltip = container
            .append("div")
            .attr("class", "chart-tooltip"),

        // Create SVG object and set dimensions
        vis = container
            .append("svg:svg")
            .attr("width", w)
            .attr("height", h)

        // Create the group object and set styles for gradient definition
        // Which is about to add in a few lines
        g = vis.append("svg:g")
                .attr("stroke", "url(#sparkline-gradient-" + sparklineId + ")")
                .attr("fill", "url(#sparkline-gradient-" + sparklineId + ")"),

        // Create the line
        // Using cardinal interpolation because we need
        // The line to pass on every point
        line = d3.svg.line()
            .interpolate("cardinal")
            .x(function(d, i) { return x(i); })
            .y(function(d) { return h - y(d); }),

        // Create points
        // We are only creating points for first and last data
          // Because that looks cooler :)
        points = g.selectAll(".point")
            .data(data)
            .enter().append("svg:circle")
            .attr("class", "point")
            .attr("cx", function(d, i) { return x(i) })
            .attr("cy", function(d, i) { return h - y(d) })
            .attr("r", function(d, i) { return (i === (data.length - 1) || i === 0) ? 5 : 0; });

    // Append the line to the group
    g.append("svg:path").attr("d", line(data));

    // Bind calculator functions to tooltip
    th.find(".chart-tooltip").data({
        calcY: y,
        calcX: x
    });

    // Create the gradient effect
    // This is where the magic happens
    // We get datas and create gradient stops with calculated colors
    vis.append("svg:defs")
        .append("svg:linearGradient")
        .attr("id", "sparkline-gradient-" + sparklineId)
        .attr("x1", "0%").attr("y1", "0%").attr("x2", "100%").attr("y2", "0%")
        .attr("gradientUnits", "userSpaceOnUse")
        .selectAll(".gradient-stop")
        .data(data)
        .enter()
        .append("svg:stop").attr('offset', function(d, i) {
            return ((percentageX(i))) + "%";
        }).attr("style", function(d) {
            return "stop-color:" + gradientY(d) + ";stop-opacity:1";
        });

    // Creating invisible rectangles for a better hover interaction
    // Because otherwise user would need to hover to the line or point
    // Which is a terrible experience
    // Creating full height invisible bars and binding mouse events
    // To do some special stuff like showing data or adding classes to
    // The point in the targeted area
    var rect = g.selectAll(".bar-rect")
        .data(data)
        .enter().append("svg:rect")
        .attr("class", "bar-rect")
        .attr("x", function(d, i) { return x(i) - (w / data.length / 2) })
        .attr("y", 0)
        .attr("width", w / data.length)
        .attr("height", h)
        .on("mouseenter", function(d, i) {
            // Calculate left position
            var $tooltip = $(this).closest(".js-report-sparkline").find(".chart-tooltip")
                                  .html(formatTooltip(d, i)),
                tooltipLeft = $tooltip.data("calcX")(i) - ($tooltip.width() / 2),
                tooltipTop = h - $tooltip.data("calcY")(d) - 40;

            // Position it again
            $tooltip.css({
                        left: tooltipLeft + "px",
                        top: tooltipTop + "px"
                    }).show();

            // Add hover class to the targeted point
            $(this).parent().parent().find('.point:eq(' + i + ')').attr('class', 'point hover');
        }).on("mouseleave", function(d, i) {
            var $tooltip = $(this).closest(".js-report-sparkline").find(".chart-tooltip");

            // Hide the tooltip
            $tooltip.hide();

            // Remove hover class from the targeted point
            $(this).parent().parent().find('.point:eq(' + i + ')').attr('class', 'point');
        });

    // Helper function to calculate the HTML content of the tooltip
    // Tooltip may contain any HTML
    function formatTooltip(d, i) {
        return '<div class="title">' + d + '</div>'
    }
});
              
            
!
999px

Console