Giter Site home page Giter Site logo

Color ramps? about d3-axis HOT 12 CLOSED

d3 avatar d3 commented on April 28, 2024 1
Color ramps?

from d3-axis.

Comments (12)

curran avatar curran commented on April 28, 2024 2

It's a nice implementation of color-legend, but it is possible to use outside of Observable?

If so, what license does it have? Thanks!

from d3-axis.

aendra-rininsland avatar aendra-rininsland commented on April 28, 2024 1

Is there any way this can be done with SVG gradients perhaps in addition to the Canvas-based approach? AFAIK, pushing an image element into SVG will break any workflow that necessarily requires using SVG Crowbar and importing the output into Illustrator (plus it admittedly just kinda feels yucky having rasters amongst a bunch of vector code)?

from d3-axis.

mbostock avatar mbostock commented on April 28, 2024 1

I use this often: https://observablehq.com/@mbostock/color-ramp

You can embed in SVG using canvas.toDataURL, e.g.: https://observablehq.com/@d3/hexbin-map

from d3-axis.

mbostock avatar mbostock commented on April 28, 2024 1

https://observablehq.com/@d3/color-legend

from d3-axis.

mbostock avatar mbostock commented on April 28, 2024

Example usage:

var color = d3.scaleSequential(d3.interpolateRainbow);

var x = d3.scaleLinear().range([margin.left, width - margin.right]);

svg.append("g")
    .attr("transform", `translate(0,${height - margin.bottom})`)
    .call(d3.rampHorizontal(x, color))
    .call(d3.axisBottom(x));

Example output:

screen shot 2017-05-16 at 3 20 33 pm

from d3-axis.

mbostock avatar mbostock commented on April 28, 2024

Questions:

  1. Is “ramp” the right name? A swatch? A gradient? An image? A continuous color key? A sequential color key?

  2. Do we need separate bottom- and top-orientations? What if you want to use d3.axisTop and have the ramp hang below the axis? (Likewise for vertical orientation.)

  3. Is it helpful to have this primitive separate from the axis, or should we just combine the two? If we combined them, we could provide a default position scale with range [0, 320]… though unsure how useful that default would be.

  4. Or should we go the other way, and just have a helper for creating the canvas data URL?

from d3-axis.

mbostock avatar mbostock commented on April 28, 2024

If we combined them:

var color = d3.scaleSequential(d3.interpolateRainbow);

svg.append("g")
    .attr("transform", `translate(0,${height - margin.bottom})`)
    .call(d3.rampHorizontal(color).range([margin.left, width - margin.right]));

from d3-axis.

tomshanley avatar tomshanley commented on April 28, 2024

My thoughts..

  1. Out of those options, "Ramp" makes most sense to me in this situation, and is used in other applications I use
  2. Yes, I would think so. I would make sense to me to have similar options to axis (top/bottom/left/right)
  3. Separate
  4. Don't know

from d3-axis.

mbostock avatar mbostock commented on April 28, 2024

We can’t internalize the position scale inside the ramp component if we wish to allow non-linear scales, which coincidentally is the motivation for this request (a key for a continuous, diverging log color scale). Also if we internalized the scale, we’d have to re-expose all the standard axis properties, such as for setting the tick values and tick format. That’s not too onerous I suppose but it would be nice to avoid.

I think it’s still worth considering whether we want to pare down the abstraction a little further so that it just generates the data URL; that way it can be used either to create an SVG image element or an HTML img element. On the other hand there are still quite a few tedious steps required to create the SVG image element even given this data URL, and it feels reasonable to make it SVG-specific since it’s designed to work in conjunction with an axis.

So… I think my answers are: (2) yes, four orientations; (3) separate, as originally proposed; (4) no.

I’m not sure the name “ramp” is right because normally I think of that as referring to the color scale, rather than the key (or “guide” or “legend”) for said scale.

from d3-axis.

tomshanley avatar tomshanley commented on April 28, 2024

I think key/legend/guide might imply that I could use ordinal colour scales, or sequential scales with bands. But I take your point re Ramp.

from d3-axis.

syntagmatic avatar syntagmatic commented on April 28, 2024

The visual legend is the color scale. #teamramp

from d3-axis.

syntagmatic avatar syntagmatic commented on April 28, 2024

Here's a tricky nit. Can we tween the canvas so transitions magically work?

var ramp = function(scale) {
  d3.rampHorizontal(scale).range([left, right])
}

svg.append("g")
    .call(ramp(colorscale['RdBu']))
    .transition()
    .call(ramp(colorscale['PiYG']))

from d3-axis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.