How to change line interpolation dynamically - javascript
How do I change the line interpolation dynamically with D3.js?
I.e. why does the 'Toggle' button on my D3.js example below not update the interpolation, and how do I fix it?
jsfiddle
<!DOCTYPE html>
<head>
<script type="text/javascript" src="http://d3js.org/d3.v2.js"></script>
<style>
svg {width: 200px; height: 200px; border: 1px solid gray;}
.line { fill: none; stroke: steelblue; stroke-width: 2;}
</style>
</head>
<body>
<button id="toggle">Toggle</button><br/>
<svg></svg>
</body>
<script type="text/javascript">
document.getElementById('toggle').onclick=function(){
if (chart.interpolate === 'basis') {
chart.interpolate = 'linear';
} else {
chart.interpolate = 'basis';
}
chart.render();
};
function Chart() {
this.svg = d3.select('svg');
this.points = [{x:0, y:60}, {x:50, y:110}, {x:90, y:70}, {x:140, y:100}];
this.interpolate = 'basis';
this.line = d3.svg.line()
.x(function(d){return d.x;})
.y(function(d){return d.y;})
.interpolate(this.interpolate);
this.render = function (container) {
this.svg.append('path')
.attr('d', this.line(this.points))
.attr('class', 'line');
};
}
chart = new Chart();
chart.render("#chart");
</script>
</html>
The problem is that the line is not recreated or updated inside the render function and so it keeps the old interpolate value.
function Chart() {
this.svg = d3.select('svg');
this.points = [{x:0, y:60}, {x:50, y:110}, {x:90, y:70}, {x:140, y:100}];
this.interpolate = 'basis';
this.line = d3.svg.line()
.x(function(d){return d.x;})
.y(function(d){return d.y;})
.interpolate(this.interpolate);
this.render = function (container) {
this.svg.selectAll('path.line').remove()
// update the line with the new interpolate value
this.line.interpolate(this.interpolate);
this.svg.append('path')
.attr('d', this.line(this.points))
.attr('class', 'line');
};
}
Here it is in action on jsfiddle
Related
d3.js chart rendering at bottom of wordpress page
Why is my js chart rendering at the bottom of the page and not in the body? I've used tons of these within my site with no problems but this one doesn't seem to want to go where desired. Thoughts? Thank you in advance! www.wcsddata.net/data-topics/jttest/ <center><!DOCTYPE html> <meta charset="utf-8"> <style> text{ font-size:12px; } .mainBars rect{ shape-rendering: auto; fill-opacity: 0; stroke-width: 0.5px; stroke: rgb(0, 0, 0); stroke-opacity: 0; } .subBars{ shape-rendering:crispEdges; } .edges{ stroke:none; fill-opacity:0.5; } .header{ text-anchor:middle; font-size:16px; } </style> <script src="https://d3js.org/d3.v4.min.js"></script> <script src="http://vizjs.org/viz.v1.1.0.min.js"></script> <script> var data=[['Lite','CA',16,0], ['Small','CA',1278,4], ['Medium','CA',27,0], ['Plus','CA',58,0], ['Grand','CA',1551,15], ['Elite','CA',141,0], ['Lite','AZ',5453,35], ['Small','AZ',683,1], ['Medium','AZ',862,0], ['Grand','AZ',6228,30], ['Lite','AL',15001,449], ['Small','AL',527,3], ['Medium','AL',836,0], ['Plus','AL',28648,1419], ['Grand','AL',3,0], ['Lite','CO',13,0], ['Small','CO',396,0], ['Medium','CO',362,0], ['Plus','CO',78,10], ['Grand','CO',2473,32], ['Elite','CO',2063,64], ['Medium','DE',203,0], ['Grand','DE',686,2], ['Elite','DE',826,0], ['Lite','KS',1738,110], ['Small','KS',12925,13], ['Medium','KS',15413,0], ['Small','GA',2166,2], ['Medium','GA',86,0], ['Plus','GA',348,3], ['Grand','GA',4244,18], ['Elite','GA',1536,1], ['Small','IA',351,0], ['Grand','IA',405,1], ['Small','IL',914,1], ['Medium','IL',127,0], ['Grand','IL',1470,7], ['Elite','IL',516,1], ['Lite','IN',43,0], ['Small','IN',667,1], ['Medium','IN',172,0], ['Plus','IN',149,1], ['Grand','IN',1380,5], ['Elite','IN',791,23], ['Small','FL',1,0], ['Grand','FL',1,0], ['Small','MD',1070,1], ['Grand','MD',1171,2], ['Elite','MD',33,0], ['Plus','TX',1,0], ['Small','MS',407,0], ['Medium','MS',3,0], ['Grand','MS',457,2], ['Elite','MS',20,0], ['Small','NC',557,0], ['Medium','NC',167,0], ['Plus','NC',95,1], ['Grand','NC',1090,5], ['Elite','NC',676,6], ['Lite','NM',1195,99], ['Small','NM',350,3], ['Medium','NM',212,0], ['Grand','NM',1509,8], ['Lite','NV',3899,389], ['Small','NV',147,0], ['Medium','NV',455,0], ['Plus','NV',1,1], ['Grand','NV',4100,16], ['Lite','OH',12,0], ['Small','OH',634,2], ['Medium','OH',749,0], ['Plus','OH',119,1], ['Grand','OH',3705,19], ['Elite','OH',3456,25], ['Small','PA',828,2], ['Medium','PA',288,0], ['Plus','PA',141,0], ['Grand','PA',2625,7], ['Elite','PA',1920,10], ['Small','SC',1146,2], ['Medium','SC',212,0], ['Plus','SC',223,4], ['Grand','SC',1803,6], ['Elite','SC',761,8], ['Small','TN',527,0], ['Medium','TN',90,0], ['Grand','TN',930,4], ['Elite','TN',395,1], ['Lite','ME',7232,58], ['Small','ME',1272,0], ['Medium','ME',1896,0], ['Plus','ME',1,0], ['Grand','ME',10782,33], ['Elite','ME',1911,3], ['Small','VA',495,0], ['Medium','VA',32,0], ['Plus','VA',7,0], ['Grand','VA',1557,12], ['Elite','VA',24,0], ['Small','WA',460,1], ['Plus','WA',88,3], ['Grand','WA',956,3], ['Small','WV',232,0], ['Medium','WV',71,0], ['Grand','WV',575,2], ['Elite','WV',368,3] ]; var color ={Elite:"#3366CC", Grand:"#DC3912", Lite:"#FF9900", Medium:"#109618", Plus:"#990099", Small:"#0099C6"}; var svg = d3.select("body").append("svg").attr("width", 960).attr("height", 800); svg.append("text").attr("x",250).attr("y",70) .attr("class","header").text("Sales Attempt"); svg.append("text").attr("x",750).attr("y",70) .attr("class","header").text("Sales"); var g =[svg.append("g").attr("transform","translate(150,100)") ,svg.append("g").attr("transform","translate(650,100)")]; var bp=[ viz.bP() .data(data) .min(12) .pad(1) .height(600) .width(200) .barSize(35) .fill(d=>color[d.primary]) ,viz.bP() .data(data) .value(d=>d[3]) .min(12) .pad(1) .height(600) .width(200) .barSize(35) .fill(d=>color[d.primary]) ]; [0,1].forEach(function(i){ g[i].call(bp[i]) g[i].append("text").attr("x",-50).attr("y",-8).style("text-anchor","middle").text("Channel"); g[i].append("text").attr("x", 250).attr("y",-8).style("text-anchor","middle").text("State"); g[i].append("line").attr("x1",-100).attr("x2",0); g[i].append("line").attr("x1",200).attr("x2",300); g[i].append("line").attr("y1",610).attr("y2",610).attr("x1",-100).attr("x2",0); g[i].append("line").attr("y1",610).attr("y2",610).attr("x1",200).attr("x2",300); g[i].selectAll(".mainBars") .on("mouseover",mouseover) .on("mouseout",mouseout); g[i].selectAll(".mainBars").append("text").attr("class","label") .attr("x",d=>(d.part=="primary"? -30: 30)) .attr("y",d=>+6) .text(d=>d.key) .attr("text-anchor",d=>(d.part=="primary"? "end": "start")); g[i].selectAll(".mainBars").append("text").attr("class","perc") .attr("x",d=>(d.part=="primary"? -100: 80)) .attr("y",d=>+6) .text(function(d){ return d3.format("0.0%")(d.percent)}) .attr("text-anchor",d=>(d.part=="primary"? "end": "start")); }); function mouseover(d){ [0,1].forEach(function(i){ bp[i].mouseover(d); g[i].selectAll(".mainBars").select(".perc") .text(function(d){ return d3.format("0.0%")(d.percent)}); }); } function mouseout(d){ [0,1].forEach(function(i){ bp[i].mouseout(d); g[i].selectAll(".mainBars").select(".perc") .text(function(d){ return d3.format("0.0%")(d.percent)}); }); } d3.select(self.frameElement).style("height", "800px"); </script>
problem is your JS selector, you need to select #content. change var svg = d3.select("body").append("svg").attr("width", 960).attr("height", 800); to: var svg = d3.select("#content").append("svg").attr("width", 960).attr("height", 800); this will show your charts in desired area. in order to center the charts you need to change the width and height: var svg = d3.select("#content").append("svg").attr("width", 600).attr("height", auto); then create a <div id="mycenterdiv" style="text-align:center; width: 100%;"> </div> in your wordpress page and then target that div (change the select) var svg = d3.select("#mycenterdiv").append("svg").attr("width", 600).attr("height", auto);
d3 Bar Chart Add Margins
I would like to make this d3 bar chart (ignore the different colors, that's for grouping which is not what I'm talking about): Look like this javascript bar chart (specifically, how it has each instance broken up into "links" or "blocks" i.e. there is spacing between each item): I can do this in javascript fairly easily but I am not sure how to do this in d3. Can anyone provide any examples of how to break down each item as shown in the graph above? I have tried this so far: .append("rect") .style("margin", "20") This is the source for the d3 chart: https://jsfiddle.net/mq2jcfz0/ <!DOCTYPE html> <html> <head> <script src="http://mbostock.github.com/d3/d3.v2.js"></script> <title>barStack</title> <style> .axis text { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } </style> </head> <body> <script type="text/javascript" > function barStack(d) { var l = d[0].length while (l--) { var posBase = 0, negBase = 0; d.forEach(function(d) { d=d[l] d.size = Math.abs(d.y) if (d.y<0) { d.y0 = negBase negBase-=d.size } else { d.y0 = posBase = posBase + d.size } }) } d.extent= d3.extent(d3.merge(d3.merge(d.map(function(e) { return e.map(function(f) { return [f.y0,f.y0-f.size]})})))) return d } /* Here is an example */ var data = [[{y:3},{y:6},{y:-3}], [{y:4},{y:-2},{y:-9}], [{y:10},{y:-3},{y:4}]] var h=500 ,w=500 ,margin=10 ,color = d3.scale.category10() ,x = d3.scale.ordinal() .domain(['abc','abc2','abc3']) .rangeRoundBands([margin,w-margin], .1) ,y = d3.scale.linear() .range([h-margin,0+margin]) ,xAxis = d3.svg.axis().scale(x).orient("bottom").tickSize(6, 0) ,yAxis = d3.svg.axis().scale(y).orient("left") barStack(data) y.domain(data.extent) svg = d3.select("body") .append("svg") .attr("height",h) .attr("width",w) svg.selectAll(".series").data(data) .enter().append("g").classed("series",true).style("fill", function(d,i) { return color(i)}) .selectAll("rect").data(Object) .enter().append("rect") .attr("x",function(d,i) { return x(x.domain()[i])}) .attr("y",function(d) { return y(d.y0)}) .attr("height",function(d) { return y(0)-y(d.size)}) .attr("width",x.rangeBand()) svg.append("g").attr("class","axis x").attr("transform","translate (0 "+y(0)+")").call(xAxis) svg.append("g").attr("class","axis y").attr("transform","translate ("+x(margin)+" 0)").call(yAxis) </script> </body> </html>
You will need to work with the barStack function that deals with arranging the data to stack the bars. You just have to move rects position like this: var barPad = 0.5; function barStack(d) { var l = d[0].length while (l--) { var posBase = 0, negBase = 0; d.forEach(function(d) { d=d[l] d.size = Math.abs(d.y) if (d.y<0) { d.y0 = negBase - barPad negBase-=d.size + barPad } else { d.y0 = posBase = posBase + d.size + barPad } }) } d.extent= d3.extent(d3.merge(d3.merge(d.map(function(e) { return e.map(function(f) { return [f.y0,f.y0-f.size]})})))) return d }
relocate d3 map to center of screen no matter the device
I've got the following d3 map: but as you can see it's all bunched up there in the left corner of the screen- this is sub opimal- ideally I would like it to be centered. How can I achieve this? I guess it should be easy but every time I google something like "center align d3 map" I get things about zooming :/ maybe I need to create a div or something? the code is here on my GitHub also below- it's pretty well commented. <!DOCTYPE html> <html lang='en'> <head> <meta charset='utf-8'> <style> .border { stroke: #000; fill: none; } .graticule { fill: none; stroke: #777; stroke-width: .5px; stroke-opacity: .5; } div.tooltip { position: absolute; text-align: center; width: 84px; height: 64px; padding: 2px; font: 12px sans-serif; background: lightgrey; border: 0px; border-radius: 8px; pointer-events: none; } </style> </head> <body> <h1>Administrative Sub-Regions of Europe</h1> <!-- this is the form at the bottom to change the NUTS --> <form> <select id="json_sources" name="json_sources" > <option value ="nuts0" selected >Source 0</option> <option value ="nuts1" >Source 1</option> <option value ="nuts2" >Source 2</option> <option value ="nuts3" >Source 3</option> </select> <form> <!-- spinner --> <script src='https://cdnjs.cloudflare.com/ajax/libs/spin.js/2.0.1/spin.min.js'></script> <script src="http://d3js.org/d3.v3.min.js"></script> <script src="http://d3js.org/topojson.v1.min.js"></script> <script src="http://d3js.org/colorbrewer.v1.min.js"></script> <script src="https://cdn.rawgit.com/rveciana/d3-composite-projections/v0.2.0/composite-projections.min.js"></script> <!-- why do we need this? --> <section id='chart'> </section> <script> var div = d3.select("body").append("div") .attr("class", "tooltip") .style("opacity", 0); var width = 600, height = 500; var projection = d3.geo.conicConformalEurope(); var graticule = d3.geo.graticule(); var path = d3.geo.path() .projection(projection); // Find new colours here: http://colorbrewer2.org/ var scale = d3.scale .quantize() .domain([10,60]) .range(colorbrewer.PuRd[3]); var svg = d3.select("body") .append("svg") .attr("width", width, "100%") .attr("height", height, "100%") .call( d3. behavior. zoom(). on("zoom", function () { svg.attr("transform", "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")") } ) ) .on("dblclick.zoom", null) .append("g") //what the hell does this do? svg.append("path") .datum(graticule) .attr("class", "graticule") .attr("d", path); // pretty self spoken var dropdown = d3.select("#json_sources") // config references SPINNER RELATED var chartConfig = { target : 'chart', data_url : './nuts0.json', width: 600, height: 500, val: 90 }; // loader settings SPINNER RELATED var opts = { lines: 9, // The number of lines to draw length: 9, // The length of each line width: 5, // The line thickness radius: 14, // The radius of the inner circle color: '#EE3124', // #rgb or #rrggbb or array of colors speed: 1.9, // Rounds per second trail: 40, // Afterglow percentage className: 'spinner', // The CSS class to assign to the spinner }; // SPINNER RELATED var target = document.getElementById(chartConfig.target); // KICK OFF callback function wrapped for loader in 'init' function function init() { // trigger loader initial spinner var spinner = new Spinner(opts).spin(target); // load json data and trigger callback d3.json(chartConfig.data_url, function(data) { // stop spin.js loader spinner.stop(); // instantiate chart within callback chart(data); }); } //call that init function we define above init(); //here where all the real stuff happens //in fact all that init stuff is just legacy //from the spinner example function chart(data) { //start of map making function var change = function() { // trigger loader of the spinner var spinner = new Spinner(opts).spin(target); // did they change the NUTS? var source = dropdown.node().options[dropdown.node().selectedIndex].value; //necessary data processing var str1 = source; var str2 = ".json"; var file = str1.concat(str2); console.log(file); d3.json(file, function(error, europe) { d3.csv("povertry_rate.csv", function(error, povrate) { //change the map to apadpt to the nuts file if (source == "nuts1") { var land = topojson.feature(europe, europe.objects.nuts1); } else if (source == "nuts2") { var land = topojson.feature(europe, europe.objects.nuts2); } else if (source == "nuts3") { var land = topojson.feature(europe, europe.objects.nuts3); } else if (source == "nuts0") { var land = topojson.feature(europe, europe.objects.nuts0); } data = {}; povrate.forEach(function(d) { data[d.GEO] = d['2013']; }); //clear way for the regeneration d3.selectAll("path").remove(); //recreate those map lines svg.append("path") .datum(graticule) .attr("class", "graticule") .attr("d", path); // stop spin.js loader spinner.stop(); console.info(data); svg .selectAll("path") .data(land.features) .enter() .append("path") .attr("d", path) .style("stroke","#000") .style("stroke-width",".5px") .style("fill",function(d){ var value = data[d.id]; if (isNaN(value)){ value = data[d.id.substring(0,2)]; } if (isNaN(value)){ return "#fff"; } return scale(value); }) .on("mouseover", function(d,i) { var value = data[d.id]; if (isNaN(value)){ value = data[d.id.substring(0,2)]; } div.transition() .duration(200) .style("opacity", 0.9); div.html("<b>"+d.properties.name+"</b><br/>" + value + "%") .style("left", (d3.event.pageX) + "px") .style("top", (d3.event.pageY - 28) + "px"); }) .on("mouseout", function(d,i) { div.transition() .duration(500) .style("opacity", 0); }); svg .append("path") .style("fill","none") .style("stroke","#000") .attr("d", projection.getCompositionBorders()); }); }) } dropdown.on("change", change) change(); //call that change function once } </script> </body> </html>
Not really a d3 question just a little CSS: svg { display: block; margin: auto; border: 1px solid gray; }
how to put bird counts data in d3.js to draw bar-chart on google map
I am new to d3.js ,I made reference this example: http://bl.ocks.org/emeeks/4531633 . I changed the example's map to Google map ,and want to use SVG to draw bar chart on the Google map. <!DOCTYPE html> <title>test</title> <meta charset="utf-8"> <style type="text/css"> .gmap{ display: block; width: 1000px; height: 800px; } .stations, .stations svg { position: absolute; } .stations svg { width: 120px; height: 30px; padding-right: 100px; font: 12px sans-serif; } .stations circle { fill: yellow; stroke: black; stroke-width: 1.5px; } </style> <body> <div class="gmap" id="map-canvas"></div> <script src="http://d3js.org/d3.v3.min.js"></script> <script src="http://maps.googleapis.com/maps/api/js"></script> <script></script> <script> var map; function initialize() { var mapOptions = { zoom: 8, center: new google.maps.LatLng(23.7147979,120.7105502) }; map = new google.maps.Map( document.getElementById('map-canvas') , mapOptions); } initialize(); var width = 960, height = 960; var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height); d3.csv("223-loc.csv", function(data) { var parse = d3.time.format("%Y/%m/%d").parse; aaa = d3.nest() .key(function(d){ return d.birdName;}) .entries(stocks = data); aaa_copy = d3.nest() .key(function(d){ return d.birdName;}) .entries(stocks = data); var position2keep= new Array(); var i=0; aaa.forEach(function(s) { for (nn=0; nn<selectAryy.length; nn++) { if (s.key == selectAryy[nn]) { position2keep.push(i); break; } } i++; }); position2keep.sort(); for (j=aaa_copy.length-1; j>=0; j--) { if ( position2keep.indexOf(j) == -1) aaa_copy.splice(j,1); } aaa_copy.forEach(function(s) { s.values.forEach(function(d) { for (nn=0; nn<selectAryy.length; nn++){ if (d.birdName== selectAryy[nn]){ d.date = parse(d.date); d.count = +d.count; d.lat = +d.lat; d.lng = +d.lng; } } bars = svg.selectAll("g") .data(s) .enter() .append("g") .attr("class", "bars") .attr("transform", function(d) {return "translate("+ d.lat +","+ d.lng+")";}); bars.append("rect") .attr('height', function(d) {return d.count*1000}) .attr('width', 10) .attr('y', function(d) {return -(d.count)}) .attr("class", "bars") .style("fill", "green"); bars.append("text") .text(function(d) {return d.location}) .attr("x", -10) .attr("y", 18); bars.setMap(map); }); }); }); </script> </body> My CSV data: https://drive.google.com/open?id=0B6SUWnrBmDwSWkI4bVNtOTNSOTA I use d3.csv load data,it works. But When I want to put the data into SVG to draw bar chart,it didn't work. Can anyone help me to fix it?
In the provided example there are some issues: selectAryy is not defined and most importantly bars.setMap(map); does not seem valid. Do you mean setMap function of google.maps.OverlayView object? In order to create a bar chart on Google Maps i would recommend to implement it as Custom Overlay. Having said that the below example demonstrates how to add svg objects (bar chart) into Google Maps using overlay technique: Example function BarChartOverlay(chartData, map){ this.map_ = map; this.chartData_ = chartData; this.div_=null; this.setMap(map); } BarChartOverlay.prototype = new google.maps.OverlayView(); BarChartOverlay.prototype.onAdd = function(){ var overlayProjection = this.getProjection(); var div = document.createElement('div'); div.setAttribute('id','chartDiv'); var chartArea = d3.select(div).append("svg"); this.chartData_.forEach(function(item){ var pos = overlayProjection.fromLatLngToDivPixel(new google.maps.LatLng(item[0], item[1])); var bar = chartArea .append("rect") .attr("x", pos.x) .attr("y", pos.y) .attr("width", 40) .attr("height", item[2]) .attr("fill-opacity", '0.5') .attr("fill", 'purple'); }); this.div_ = div; this.chartArea_ = chartArea; var panes = this.getPanes(); panes.overlayLayer.appendChild(div); }; BarChartOverlay.prototype.draw = function(){ var overlayProjection = this.getProjection(); var sw = overlayProjection.fromLatLngToDivPixel(this.map_.getBounds().getSouthWest()); var ne = overlayProjection.fromLatLngToDivPixel(this.map_.getBounds().getNorthEast()); var chartAreaSize = sw.x + ' ' + ne.y + ' ' + (ne.x - sw.x) + ' ' + (sw.y - ne.y); this.chartArea_.attr('viewBox',chartAreaSize); }; BarChartOverlay.prototype.onRemove = function(){ this.div_.parentNode.removeChild(this.div_); this.div_ = null; }; function initialize() { var mapOptions = { zoom: 8, center: new google.maps.LatLng(23.7147979, 120.7105502) }; var chartData = [ [25.204757,121.6896172,100], [22.7972447,121.0713702,130], [24.254972,120.6011066,80] ]; var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); var overlay = new BarChartOverlay(chartData, map); } initialize(); .gmap { display: block; width: 1000px; height: 800px; } <script src="http://d3js.org/d3.v3.min.js"></script> <script src="http://maps.googleapis.com/maps/api/js"></script> <div class="gmap" id="map-canvas"></div>
D3 line graph appearing as area graph
I have drawn a line graph and area under the line is appears to be colored to make it appear like a area graph. The code is shown below <!DOCTYPE html> <html> <head> <style type="text/css"> body { font: 12px Arial; } path { stroke-width: 1; stroke : 1; } .axis path,.axis line { fill: none; stroke: grey; stroke-width: 1; shape-rendering: crispEdges; } </style> <script type="text/javascript" src="d3.min.js"></script> <script type="text/javascript" src="jquery-1.8.0.js"></script> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <script type="text/javascript"> var baseSvg = d3.select("body") .append("svg") .attr("height",800) .attr("width",800) .append("g") .attr("transform","translate(50,50)"); $.ajax({ method : 'GET', url:"URL", //Called my URL here success:function(data){ var res = data.d.results; /* res.forEach(function(d){ console.log(new Date(parseInt(d.DATE_SQL.substring(6)))); }) */ buildTrend(res); } }) ; function buildTrend(res) { var x = d3.time.scale().range([ 50, 700 ]); var y = d3.scale.linear().range([ 500, 0 ]); res.forEach(function(d){ d.DATE_SQL = new Date(parseInt(d.DATE_SQL.substring(6))); }); var line = d3.svg.line().interpolate("basis").x(function(d) { return x(d.DATE_SQL) }).y(function(d) { return y(d.M_COUNT) }); x.domain(d3.extent(res, function(d) { return d.DATE_SQL; })); y.domain([0,d3.max(res, function(d) { return d.M_COUNT; })]); var xAxis = d3.svg.axis().scale(x).orient("bottom").ticks(15); baseSvg.append("g") .attr("class", "x axis").attr("transform", "translate(0," + 500 + ")").call(xAxis) .selectAll("text").attr("transform",function(d){ return "rotate(-90)"}) .attr("dx", "-.8em").attr("dy", ".15em").style("text-anchor", "end"); var yAxis = d3.svg.axis().scale(y).orient("left").ticks(8); baseSvg.append("g") .attr("transform","translate(50,0)")// Add the Y Axis .attr("class", "y axis").call(yAxis); baseSvg.append("g") .attr("transform","translate(0,10)") .append("path") .attr("d",line(res)) .attr("stroke","blue"); } </script> </body> </html> However the result looks like this I have checked all my code to search "Black" to identify possible cause of the color and i dont find any. Maybe it is default color. Cannot figure out the reason for this. Thanks, Veera
Try adding this css to your line: path line { fill: none; stroke: #000; } I've had this happen to me in the past, if I remember correctly what is happening is that d3 thinks that the first point and the last point on the line are joined and therefore making it an area and filling it by default with the color black. If you set the fill to none and add a stroke it should fix this. Hope this helps.