Tengo dos conjuntos de datos d1 y d2 de la siguiente manera:
const d1 = [ {group: "H", value: 6}, {group: "I", value: 2}, {group: "M", value: 3} ]; const d2 = [ {group: "H", value: 1}, {group: "I", value: 12}, {group: "M", value: 2} ];y una función llamada makethisgraph donde los "datos" que se envían son d1 y d2 según un botón en el que hago clic. En esta función, según los datos que selecciono, las barras cambian para coincidir con los valores. Quiero hacerlo de tal manera que si selecciono d1, el color de la barra cambie a rojo y si selecciono d2, se vuelva verde
function makethisgraph(data) { var u = svg.selectAll("rect") .data(data) u .enter() .append("rect") .merge(u) .transition() .duration(1000) .attr("x", function(d) { return x(d.group); }) .attr("y", function(d) { return y(d.value); }) .attr("width", x.bandwidth()) .attr("height", function(d) { return height - y(d.value); }) .attr("fill", function(d, i) { // if ( data = d1) // { // return "red" // } // else // { // return "green" // } return "blue" });Intenté escribir si los datos que obtuve son d1, entonces será rojo pero no funciona. ¿Hay alguna otra manera de resolver esto?
Agregue un parámetro de color para hacer makethisgraph :
function makethisgraph(data, color) { ... u.enter() ... .attr("fill", color); } ... makethisgraph(d1, 'red'); makethisgraph(d2, 'green');