2014-01-07 2 views
0

막대/선 차트에서 데이터를 새로 고칠 때 왼쪽 Y 축의 눈금 표시가 왼쪽에서 오른쪽으로 변경됩니다. 나는 내가 D3에 4 일 동안 단순한 것을 혼란스럽게 만들었다 고 확신하지만, 나는 그 문제를 볼 수 없다.D3JS 데이터 새로 고침시 축 방향 변경

My code is in JFIDDLE here. 나는 또한이 게시물에 추가했습니다. 어떤 도움을 주셔서 감사합니다! (줄 번호 492-493)

svg.select("g.y").call(yAxisL); 
svg.select("g.y").call(yAxisR); 

귀하의 솔루션은 y는 두 개의 서로 다른 클래스를 축, 당신은 선 위에 변경하여 그것을 달성 할 두 가지를 가지고있다 :

var data; 

var margin = { 
    top: 20, 
    right: 80, 
    bottom: 30, 
    left: 50 
}, 
width = 838 - margin.left - margin.right, 
    height = 300 - margin.top - margin.bottom; 

var x = d3.scale.ordinal() 
    .rangeRoundBands([0, width], 0.05); 

var yL = d3.scale.linear() 
    .range([height, 0]); 

var yR = d3.scale.linear() 
    .range([height, 0]); 

var xAxis = d3.svg.axis() 
    .scale(x) 
    .orient("bottom"); 

var yAxisL = d3.svg.axis() 
    .scale(yL) 
    .orient("left") 
    .ticks(10); 

var yAxisR = d3.svg.axis() 
    .scale(yR) 
    .orient("right") 
    .ticks(10); 

var EfficiencyLine = d3.svg.line() 
    .interpolate("basis") 
    .x(function (d) { 
    return x(d.xaxis); 
}) 
    .y(function (d) { 
    return yR(d.max_efficiency); 
}); 

var svg = d3.select("#daychart") 
    .attr("width", width + margin.left + margin.right) 
    .attr("height", height + margin.top + margin.bottom) 
    .append("g") 
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); 

var which_data = Math.floor(Math.random() * 3) + 1 
switch (which_data) { 
    case 1: 
     data = data1; 
     break; 
    case 2: 
     data = data2; 
     break; 
    case 3: 
     data = data3; 
     break; 
    default: 
}; 

//d3.json("http://10.0.0.13/sma/sma-php/inverterdata.php?var=CDAY&id=C1200031", function (error, data) { 
data.forEach(function (d) { 
    d.max_energy = +d.max_energy; 
    d.max_efficiency = +d.max_efficiency;  
}); 

x.domain(data.map(function (d) { 
    return d.xaxis; 
})); 
yL.domain([0, d3.max(data, function (d) { 
    return d.max_energy; 
})]); 

yR.domain([0, d3.max(data, function (d) { 
    return d.max_efficiency; 
})]); 

svg.append("g") 
    .attr("class", "x axis") 
    .attr("transform", "translate(0," + height + ")") 
    .call(xAxis) 
    .append("text") 
    .attr("transform", "rotate(0)") 
    .attr("y", 23) 
    .attr("x", 340) 
    .attr("dy", ".71em") 
    .style("text-anchor", "bottom") 
    .text("Timeline"); 

svg.append("g") 
    .attr("class", "y axis") 
    .call(yAxisL) 
    .append("text") 
    .attr("transform", "rotate(-90)") 
    .attr("y", -50) 
    .attr("x", -145) 
    .attr("dy", ".71em") 
    .style("text-anchor", "top") 
    .text("Energy - KWh"); 

svg.append("g") 
    .attr("class", "y axis") 
    .attr("transform", "translate(" + width + " ,0)") 
    .call(yAxisR) 
    .append("text") 
    .attr("y", 50) 
    .attr("x", -160) 
    .attr("transform", "translate(" + width + " ,0)") 
    .attr("transform", "rotate(-90)") 
    .attr("dy", ".71em") 
    .style("text-anchor", "top") 
    .text("Efficiency - KWh/KW"); 

svg.selectAll(".bar") 
    .data(data) 
    .enter().append("rect") 
    .attr("class", "bar") 
    .attr("x", function (d) { 
    return x(d.xaxis); 
}) 
    .attr("width", x.rangeBand()) 
    .attr("y", function (d) { 
    return yL(d.max_energy); 
}) 
    .transition().delay(function (d, i) { 
    return i * 10; 
}).duration(10) 
    .attr("height", function (d) { 
    return height - yL(d.max_energy); 
}); 

svg.append("path") 
    .attr("d", EfficiencyLine(data)) 
    .attr("class", "EfficiencyLine"); 

//Create labels 
svg.selectAll("text.label") 
    .data(data) 
    .enter() 
    .append("text") 
    .attr("class", "label") 
    .text(function (d) { 
    if (d.max_energy == 0) { 
     return ""; 
    } else { 
     return parseFloat(Math.round(d.max_energy * 100)/100).toFixed(1); 
    }; 
}) 
    .attr("x", function (d) { 
    return x(d.xaxis) + x.rangeBand()/2; 
}) 
    .attr("y", function (d) { 
    return yL(d.max_energy) - 2; 
}) 
    .attr("text-anchor", "middle") 
    .attr("font-family", "sans-serif") 
    .attr("font-size", "11px") 
    .attr("fill", "black"); 

//}); 

//On click, update with new data    
d3.select("p").on("click", function() { 

    var which_data = Math.floor(Math.random() * 3) + 1 
    switch (which_data) { 
     case 1: 
      data = data1; 
      break; 
     case 2: 
      data = data2; 
      break; 
     case 3: 
      data = data3; 
      break; 
     default: 
    }; 

    // Get the data again 
    //  d3.json("http://10.0.0.13/sma/sma-php/inverterdata.php?var=PDAY&id=P100023", function (error, data) { 
    data.forEach(function (d) { 
     d.max_energy = +d.max_energy; 
     d.max_efficiency = +d.max_efficiency; 
    }); 

    // Scale the range of the data again 
    x.domain(data.map(function (d) { 
     return d.xaxis; 
    })); 
    yL.domain([0, d3.max(data, function (d) { 
     return d.max_energy; 
    })]); 
    yR.domain([0, d3.max(data, function (d) { 
     return d.max_efficiency; 
    })]); 

    svg.select("g.x").call(xAxis); 
    svg.select("g.y").call(yAxisL); <---- PROBLEM HERE IS SUSPECT?! 
    svg.select("g.y").call(yAxisR); 

    // Make the changes 
    svg.selectAll(".bar") // change the bar 
    .data(data) // Update the data within. 
    .transition().delay(function (d, i) { 
     return i/data.length * 1000; 
    }) 
     .duration(500) 
     .attr("x", function (d) { 
     return x(d.xaxis); 
    }) 
     .attr("y", function (d) { 
     return yL(d.max_energy); 
    }) 
     .attr("width", x.rangeBand()) 
     .attr("height", function (d) { 
     return height - yL(d.max_energy); 
    }); 

    svg.selectAll("path.EfficiencyLine") // change the EfficiencyLine 
    .data(data) // Update the data within. 
    .transition().delay(function (d, i) { 
     return i/data.length * 1000; 
    }) 
     .duration(500) 
     .attr("d", EfficiencyLine(data)); 

    svg.selectAll("text.label") 
     .data(data) 
     .transition().delay(function (d, i) { 
     return i/data.length * 1000; 
    }) 
     .duration(500) 
     .text(function (d) { 
     if (d.max_energy == 0) { 
      return ""; 
     } else { 
      return parseFloat(Math.round(d.max_energy * 100)/100).toFixed(1); 
     }; 
    }) 
     .attr("x", function (d) { 
     return x(d.xaxis) + x.rangeBand()/2; 
    }) 
     .attr("y", function (d) { 
     return yL(d.max_energy) - 2; 
    }) 

    //}); 

}); 

답변

0

귀하의 문제는 두 줄을 다음에 하려면 다음과 같은

svg.select("g.y-l").call(yAxisL); 
svg.select("g.y-r").call(yAxisR); 

하고

라인 (386)을 변경

,617을
.attr("class", "y-l axis") 

라인 397 :

.attr("class", "y-r axis") 

당신은 일치하는 클래스 이름이 필요합니다.

업데이트 작업 바이올린 is here

, 당신이 어떤 추가 설명이 필요한 경우 알려줘 딜레마 등이

+0

안녕 생생한, 감사합니다! 굉장하고 아름답게 작동합니다! – TheRealPapa

+0

대단히 환영합니다! :) – VividD