javascript - Not able to plot line in d3 chart -


i trying make d3 chart interactive, before have chart , div chart id.

on click of id filtering data , trying plot no luck

i new d3 due not able understand issue also

data4.json

{ "data_high":[     {         "line_id": "i6_line",         "line_name": "6 line",         "mean": 73.400000000000006     },     {         "line_id": "i5_line",         "line_name": "5 line",         "mean": 73.400000000000006     },     {         "line_id": "i4_line",         "line_name": "4 line",         "mean": 73.400000000000006     },     {         "line_id": "i3_line",         "line_name": "3 line",         "mean": 73.400000000000006     }   ] } 

data5.json

{ "data_low":[     {         "line_id": "i6_line",         "line_name": "6 line",         "late_percent": 73.1,         "month": 1     },     {         "line_id": "i6_line",         "line_name": "6 line",         "late_percent": 63.1,         "month": 1     },     {         "line_id": "i5_line",         "line_name": "5 line",         "late_percent": 73.1,         "month": 1     },     {         "line_id": "i4_line",         "line_name": "4 line",         "late_percent": 73.1,         "month": 1     },     {         "line_id": "i3_line",         "line_name": "3 line",         "late_percent": 73.1,         "month": 1     } ] 

}

html , script

<div id="timeseries"></div>     <div id="key"></div>     <script>          d3.json("data4.json", function(data){              console.log(data);              var container_dimensions = {width: 900, height: 500},                 margins = {top: 10, right: 20, bottom: 50, left: 60},                 chart_dimensions = {                 width: container_dimensions.width - margins.left - margins.right,                 height: container_dimensions.height - margins.top - margins.bottom                 };              var chart = d3.select("#timeseries")                 .append("svg")                 .attr("width", container_dimensions.width)                 .attr("height", container_dimensions.height)                 .append("g")                 .attr("transform", "translate(" + margins.left + "," + margins.top + ")")                 .attr("id","chart");              var time_scale = d3.time.scale()                 .range([0,chart_dimensions.width])                 .domain([new date(2008, 0, 1), new date(2011, 3, 1)]);              var percent_scale = d3.scale.linear()                 .range([chart_dimensions.height, 0])                 .domain([65,90]);              var time_axis = d3.svg.axis()                 .scale(time_scale);              var count_axis = d3.svg.axis()                 .scale(percent_scale)                 .orient("left");              chart.append("g")                 .attr("class", "x axis")                 .attr("transform", "translate(0," + chart_dimensions.height + ")")                 .call(time_axis);              chart.append("g")                 .attr("class", "y axis")                 .call(count_axis);              d3.select(".y.axis")                 .append("text")                 .attr("text-anchor","middle")                 .text("percent on time")                 //.attr("transform", "rotate (90)")                 .attr("transform","rotate (-90, -88, 0) translate(-280)")                 //.attr("x", chart_dimensions.height/2)                 .attr("y", 50);              d3.select(".x.axis")                 .append("text")                 .attr("text-anchor","middle")                 .text("time")                 //                 .attr("transform","rotate (-90, 50,-90) translate(-490)")                 //.attr("transform", "rotate (90,0,0)")                 .attr("x", chart_dimensions.width/2)                 .attr("y", 50);              var key_items = d3.select("#key")                 .selectall("div")                 .data(data.data_high)                 .enter()                 .append("div")                 .attr("class","key_line")                 .attr("id",function(d){return d.line_id});              key_items.append("div")                 .attr("id", function(d){return "key_square_" + d.line_id})                 .attr("class", "key_square");              key_items.append("div")                 .attr("class","key_label")                 .text(function(d){return d.line_name});              d3.selectall(".key_line").on("click", get_timeseries_data);              function get_timeseries_data(){                 // id of current element                 var id = d3.select(this).attr("id");                 // see if have associated time series                 var ts = d3.select("#"+id+"_path");                 console.log(ts);                 if (ts.empty()){                     d3.json("data5.json", function(data){                         console.log("======= " +data);                         filtered_data = data.data_low.filter(function(d){return d.line_id === id});                         console.log(filtered_data);                         draw_timeseries(filtered_data, id);                     })                 } else {                     ts.remove();                 }             }              function draw_timeseries(data, id){                  var line = d3.svg.line()                     .x(function(d){return time_scale(d.time)})                     .y(function(d){return percent_scale(d.late_percent)})                     .interpolate("linear");                  var g = d3.select("#chart")                     .append("g")                     .attr("id", id + "_path")                     .attr("class", id.split("_")[1]);                  g.append("path")                     .attr("d", line(75));             }          }); 

i have did coding following reason when click on div here (check box or text beside textbox) should plot chart not behaving expected.

fiddle

there several issues:

  • data not added path

            g.append("path")             .datum(data) //<--this             .attr("class", "line")             .attr("d", line); 
  • there no property called 'time' in data

           var line = d3.svg.line()                 .x(function(d){return time_scale(d.time)}) // <--                 .y(function(d){return percent_scale(d.late_percent)})                 .interpolate("linear"); 

    time_scale expects date object:

              x(function(d) {                 var date = (new date());                 date.setmonth(d.month);                 date.setyear(2008);                 return time_scale(date)             }) 
  • class should assigned path

check https://jsfiddle.net/vcuo9x4e/, have added more data 'i6_line' , 'i5_line', had 1 data points each.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -