Chartjs-Chart js graph legend colors

1👍

you can generate color codes as follows:

  angular.forEach($scope.data , function (dataItem, index) {
      var colorCode = getColorHex(index);
      $scope.colours.push(colorCode);
  });

  var getColorHex = function (i) {
      //skip black & white
      i+=2;

      var colorDecimal = getRGB(i);
      var colorHex = decimalColorToHTMLcolor(colorDecimal);
      return colorHex;
  }

  function decimalColorToHTMLcolor(colorDecimal) {
      var intnumber = colorDecimal - 0;
      var red, green, blue;
      var template = "#000000";

      red = (intnumber & 0x0000ff) << 16;
      green = intnumber & 0x00ff00;
      blue = (intnumber & 0xff0000) >>> 16;

      intnumber = red | green | blue;
      var HTMLcolor = intnumber.toString(16);
      HTMLcolor = template.substring(0, 7 - HTMLcolor.length) + HTMLcolor;

      return HTMLcolor;
  }

  function getRGB(index) {
      var p = getPattern(index);
      return getElement(p[0]) << 16 | getElement(p[1]) << 8 | getElement(p[2]);
  }

  function getElement(index) {
      var value = index - 1;
      var v = 0;

      for (var i = 0; i < 8; i++) {
          v = v | (value & 1);
          v <<= 1;
          value >>= 1;
      }

      v >>= 1;
      return v & 0xff;
  }

  function getPattern(index) {
      var n = parseInt(Math.cbrt(index));
      index -= (n*n*n);
      var p = [n, n, n];
      if (index == 0) {
          return p;
      }

      index--;
      var v = index % 3;
      index = parseInt(index / 3);
      if (index < n) {
          p[v] = index % n;
          return p;
      }

      index -= n;
      p[v] = parseInt(index / n);
      p[++v % 3] = index % n;
      return p;
  }

Leave a comment