不使用d3绘制多线图

时间:2018-06-11 08:38:14

标签: javascript angular d3.js

我希望绘制一个多线图,其中包含一些聚合操作,例如求和。 我也使用了嵌套和汇总,并且可以看到值正在正确聚合,但浏览器上没有打印任何内容。 如果有人能够知道这可能会出现什么问题,那将会很有帮助

示例数据

{date: "2018-06-01T08:30:00+05:30", value: "81", storeId: 5722646637445120}
{date: "2018-06-01T08:30:00+05:30", value: "51", storeId: 5722646637445120}
{date: "2018-06-02T08:30:00+05:30", value: "72", storeId: 5722646637445120}
{date: "2018-06-02T08:30:00+05:30", value: "45", storeId: 5722646637445120}
{date: "2018-06-03T08:30:00+05:30", value: "51", storeId: 5722646637445120}
{date: "2018-06-03T08:30:00+05:30", value: "36", storeId: 5722646637445120}
{date: "2018-06-04T08:30:00+05:30", value: "48", storeId: 5722646637445120}
{date: "2018-06-04T08:30:00+05:30", value: "42", storeId: 5722646637445120}
{date: "2018-06-05T08:30:00+05:30", value: "36", storeId: 5722646637445120}

d3_mycomponent.ts

 ngAfterViewChecked() {
    if (this.avg_across_store !== undefined && typeof this.avg_across_store === 'string') {
      this.data = JSON.parse(this.avg_across_store);
      console.log('d3 this.peopleInSumArr', this.data);
      this.dashboard_date = this.dateForD3;            

      this.storeIntraffic_plot();


    }
  }

 public storeIntraffic_plot() 
 {
// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 70, left: 50},
    width = 1300 -margin.left - margin.right,
    height = 800 - margin.top - margin.bottom;

// Parse the date / time
var parseDate =timeParse("%b %Y");


// Set the ranges
var x = d3Scale.scaleTime().range([0, width]);  
var y = d3Scale.scaleLinear().range([height, 0]);

// Define the line
var priceline = d3Shape.line()  
    .x(function(d) { return x(new Date(d.date)); })
    .y(function(d) { return y(d.value); });
console.log("priceline.date",priceline.value) 
// Adds the svg canvass
var svg = d3.select("body")
    .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 + ")")
     .attr("stroke-width", 2)

    var mindate = new Date(this.dashboard_date['startTime']),
            maxdate = new Date(this.dashboard_date['endTime']);
    x.domain(d3.extent([mindate, maxdate]));
    // Scale the range of the data

    y.domain([0, d3.max(this.data, function(d) { return d.value; })]);
    console.log("this.data",this.data)
    // Nest the entries by symbol
     var dataNest=d3.nest().key(function(d) {return d.storeId;})
     .key(function(d) {return d.date;})

      .rollup(function(leaves) {
                    return {
                        "computed_sum": d3.sum(leaves, function(d) {return d.value;})} })
.entries(this.data)



   console.log("datanest",dataNest)
    var test=[]
  for (var key1 in dataNest ) {  
      var val = dataNest[key1]['key'];
      for (var i = 0; i < dataNest[key1].values.length; i++) {
          test.push({
    key:   val,
    values: [{"date":dataNest[key1].values[i]['key'],"value":dataNest[key1].values[i]['value']['computed_sum']+''}]
});

    //Do something
} 
  }



    console.log("dataNest",dataNest)
    console.log("test",test)
    // set the colour scale
    var color = d3.scaleOrdinal(d3.schemeCategory10);
    console.log("width",width)
    //console.log("width",dataNest.length)
    var legendSpace = width/test.length; // spacing for the legend
    console.log("this.legendSpace",legendSpace)
    // Loop through each symbol / key
    test.forEach(function(d,i) { 

        svg.append("path")
            .attr("class", "line")
            .style("stroke", function() { // Add the colours dynamically
                return d.color = color(d.key); })
            .attr("d", priceline(d.values))
            .attr("stroke-width", 3)
            .style("fill","none")
            .data(test)
         // Add the Legend
        svg.append("text")
            .attr("x", (legendSpace/2)+i*legendSpace)  // space legend
            .attr("y", height + (margin.bottom/2)+ 5)
            .attr("class", "legend")    // style the legend
            .style("fill", function() { // Add the colours dynamically
                return d.color = color(d.key); })
            .text(d.key)
            .attr("stroke-width", 3)


    });
  console.log("after",dataNest)
  // Add the X Axis
  svg.append("g")
      .attr("class", "axis")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));

  // Add the Y Axis
  svg.append("g")
      .attr("class", "axis")
      .call(d3.axisLeft(y));

}}

enter image description here

0 个答案:

没有答案