Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow labels functions to return Promise (resolving with label text) #85

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

christophe-g
Copy link
Contributor

@christophe-g christophe-g commented Jul 22, 2018

Motivation: fetch label text async (e.g. based on locale)

No breaking changes.

const asyncHelper = (  i,
  genLength,
  generatedLabels,
  labelDelimiter) => {
    // get a text value stored in firebase
   return firebase.database().ref(pathToLocaleText).once('value').then(snap =>{
    return snap.val();
  })

}

example:
image

var thresholdScale = d3.scaleThreshold()
  .domain([0, 1000, 2500, 5000, 10000])
  .range(d3.range(6)
    .map(function(i) { return "q" + i + "-9" }));

var linear = d3.scaleLinear()
  .domain([0, 10])
  .range(["rgb(46, 73, 123)", "rgb(71, 187, 94)"]);

// ----legendHelpers.thresholdLabels----
const promiseHelper = function({
  i,
  genLength,
  generatedLabels,
  labelDelimiter
}) {

  //
  const promise =  Promise.resolve()
    .then(function() {
      return new Promise(function(resolve, reject) {
        setTimeout(function() {
          let res;
          if (i === 0) {
            const values = generatedLabels[i].split(` ${labelDelimiter} `);
            return resolve(`Less than ${values[1]}`);
          } else if (i === genLength - 1) {
            const values = generatedLabels[i].split(` ${labelDelimiter} `);
            return resolve(`${values[0]} or more`);
          }
          return resolve(generatedLabels[i]);
        }, 100);
      });
    });

  return promise;  
};

// ----legendHelpers.thresholdLabels----
const helper = function({
  i,
  genLength,
  generatedLabels,
  labelDelimiter
}) {

  if (i === 0) {
    const values = generatedLabels[i].split(` ${labelDelimiter} `);
    return `Less than ${values[1]}`;
  } else if (i === genLength - 1) {
    const values = generatedLabels[i].split(` ${labelDelimiter} `);
    return `${values[0]} or more`;
  }
  return generatedLabels[i];
};

const legend = d3.legendColor()
  .labelFormat(d3.format('.2f'))
  .title('Sync labels')
  .labels(helper)
  .useClass(true)
  .scale(thresholdScale);

const promiseLegend = d3.legendColor()
  .labelFormat(d3.format('.2f'))
  .title('Async labels (Promise)')
  .labels(promiseHelper)
  .useClass(true)
  .scale(thresholdScale);



d3.select('#legend .legend')
  .call(legend);

d3.select('#promiseLegend .legend')
  .call(promiseLegend);

@susielu
Copy link
Owner

susielu commented Aug 4, 2018

Thanks for putting this together. Do you know of package to polyfill Promises? I want this package to still be able to work in IE. I tried using the babel-plugin-es6-promise but ran into a grunt error that was difficult to decipher.

@tonym97
Copy link

tonym97 commented Feb 20, 2019

Randomly looking since I submitted a pull request. We used es6-shim and/or core.js to poly promises...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants