deep sodhi deep sodhi - 6 months ago 24
HTML Question

how to replace comma with dot

I am having comma values inside my json and i want when i get those values i get them in dot so basically I want to convert my comma values into dot values..my json looks like and its always fixed that i will get comma values at val003.
I know something to do like var new = new.replace(/,/g, '.') . but how can i specify my val003 here for the conversion. Thank you in advance

My html

<!doctype html>

<html>

<head>

<meta charset="UTF-8">
<meta content="utf-8" http-equiv="encoding">
<div id="below">
<div id="chart"></div>
</div>
<script>
var jsonURL = 'avb.json';
var myData = [];
var fliterdata = [];
var tempdata = [];
var selectop = "";
var selectDate = false;
var chartType = chartType || 'bar';

function filterJSON(json, key, value) {
var result = [];
for (var foo in json) {
var extractstr = json[foo][key] ;
extractstr=String(extractstr);

if (extractstr.slice(3)== value) {

result.push(json[foo]);

}
}
return result;
}
function selectValue(d) {
switch (selectop) { //d object select particular value for Y axis
case "01":
return d.val001;
break;
case "02":
return d.val002;
break;
case "03":
return d.val003;
break;
case "04":
return d.val004;
break;
default:
//console.log("default");
return d.val001;
}


}

var line = d3.svg.line()
.x(function(d) {
return xScale(d.date);
})
.y(function(d) {
return yScale(selectValue(d));
})
.interpolate("monotone")
.tension(0.9);




yScale.domain([0, d3.max(tempData, function(d) {
return +selectValue(d);
})]);

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


if (chartType == 'bar') {
svg
.selectAll(".bar") //makes bar
.data(tempData)
.enter().append("rect")
.attr("class", "bar")
.style("fill", "teal")
.attr("x", function(d) {
return xScale(d.date);
}).attr("width", xScale.rangeBand())
.attr("y", function(d) {


return yScale(selectValue(d));
}).attr("height", function(d) {

console.log("as", d.value);
return height - yScale(selectValue(d));
})
}

if (chartType == 'line') {
svg.append("path") // Add the line path.
.data(tempData)
.attr("class", "line")
.attr("d", line(tempData));
}

}
d3.json(jsonURL, function(data) {

myData = data; //data from json in mydata
d.val003.replace(",",".")
myData.forEach(function(d) {

d.date = new Date(d.date);
d.date = new Date(d.date + " UTC");

});

$("#listbox").on("click", function() {

var key = $(this).val();
console.log("key:", key);
var value = $('#listbox option:selected').text();
console.log("vaue:", value);

selectop = String(key);

selectop = selectop.slice(-2);
console.log("mydata: ", myData);
console.log("selectops:", selectop);

fliterdata = filterJSON(myData, key, value); //selected value from user and picks the whole element that contains that attribute

console.log("fliterdata: ", fliterdata);
tempData = fliterdata; //graph made by temp data
if (selectDate)
render(true);
});
});

function selectChartType(type) {
chartType = type;
render(true);
}
</script>
</body>
</div>
</body>
</html>

Answer

Try this,

return d.val003.toString().replace(",",".");