score:0

Accepted answer

the easiest way is to just save the generated color to a variable you can reference for each object (for eample when you map over it).

but if for some reason you don't want to do that then you could create an object factory function:

const buildobj = obj => {
    const color = generatecolor();
    const o = { ...obj, backgroundcolor: color, bordercolor: color };
    return o;
}

and then call it to generate your new objects:

const mappeddata = data.map(o => buildobj(o))

now every object will have a unique color, but the backgroundcolor and bordercolor of each object will be the same.

score:0

you just have to declare a global variable and use it multiple of times.

const globalcolor = this.generatecolor();
var data = [{
      label: "records",
      data: [2, 4 ,20,10],
      fill: false,
      backgroundcolor: globalcolor ,
      bordercolor: globalcolor ,
      borderwidth: 1
    },{
      label: "amount",
      data: [100, 200, 500,50],
      fill: false,
      backgroundcolor: globalcolor ,
      bordercolor: globalcolor ,
      borderwidth: 1
    }

}]
  generatecolor = () => {
    var letters = '0123456789abcdef';
    var color = '#';
    for (var i = 0; i < 6; i++) {
      color += letters[math.floor(math.random() * 16)];
    }
    return color;
  }

hope it helps

score:0

so the problem is that you're getting a random color each time you're calling the function. try generating a color once per element in your array. a simple idea is:

  generatecolor = () => {
    var letters = '0123456789abcdef';
    var color = '#';
    for (var i = 0; i < 6; i++) {
      color += letters[math.floor(math.random() * 16)];
    }
    return color;
  }

var data = [{
      label: "records",
      data: [2, 4 ,20,10],
      fill: false,
      borderwidth: 1
    },{
      label: "amount",
      data: [100, 200, 500,50],
      fill: false,
      borderwidth: 1
    }].map(v => {
                var color = generatecolor(); 
                return {...v, backgroundcolor: color, bordercolor: color}
           })

score:0

so the reason the border color and the background color are not matching up is because you are calling the function for each property where you need a color. functions alone do not have any kind of state, so there is no way for a function itself to remember what the function returned previously.

the best way i could think of doing this, that also retains the nice declarative way you layed-out your data was by the use of a class.

class generatecolorpairs {
    constructor(numofcolorpairs) {
        this.tickcycle = 2
        this.currenttick = 0
        this.colorsetindex = 0

        this.colorset = []

        const letters = '0123456789abcdef';
        for (let i = 0; i < numofcolorpairs; i++) {
            let color = '#';
            for (let j = 0; j < 6; j++) {
                color += letters[math.floor(math.random() * 16)];
            }
            debugger;
            this.colorset.push(color)
        }
    }

    tick() {
        if (this.currenttick < this.tickcycle) {

            this.currenttick += 1

        } else if (this.currenttick === this.tickcycle) {

            if (this.colorsetindex === this.numofcolorpairs) {
                throw new error(`
            function has been called more times than 
            the 'numofcolorpairs' x2\n to be able to use this 
            function more than ${this.numofcolorpairs} times, 
            pass a higher value when instanciating to the class 
            constructor
          `)
            }

            this.colorsetindex += 1
            this.currenttick = 0

        }
    }

    generatecolor() {
        this.tick()
        return this.colorset[this.colorsetindex];
    }

}

var colors = new generatecolorpairs(2)

var data = [{
    label: "records",
    data: [2, 4, 20, 10],
    fill: false,
    backgroundcolor: colors.generatecolor(),
    bordercolor: colors.generatecolor(),
    borderwidth: 1
}, {
    label: "amount",
    data: [100, 200, 500, 50],
    fill: false,
    backgroundcolor: colors.generatecolor(),
    bordercolor: colors.generatecolor(),
    borderwidth: 1
}];

console.log(data)

its a lot verbose than the other options mentioned here, but allows you to declare all your data in one statement and not have to pass it through a factory.


Related Query

More Query from same tag