简体   繁体   中英

how to set different colors on each pie chart with data from ajax

how to set different colors on each pie chart with data from ajax with if condition?

this is my piechart js

var data_kendala = [];

$.each(data.data8, function(key, val){
  data_kendala.push({
    "country": val.CONTRAINTS_NAME,
    "visits": val.TOTAL_KENDALA,
    "color": "#fd0000"
  });
});

I want to set each color on different chart, for example if CONTRAINTS_NAME = 1 then green color, if CONTRAINTS_NAME = 2 then blue and etc.

You just have to set your color inside the each according to your specific conditions. Or I missed a point.

$.each(data.data8, function(key, val){
  var color;

  // set your color according to conditions
  // Like: color = CONTRAINTS_NAME === 1 ? 'green' : 'blue';

  data_kendala.push({
    "country": val.CONTRAINTS_NAME,
    "visits": val.TOTAL_KENDALA,
    "color": color
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM