score:1

Accepted answer

First you need to normalize your data so that you have unique keys and you can see a single point for a particular time. For that you need to use d3.nest.

It will create a newData where key will be the time and values will be the value you want to show.

var dataset = [{
    val: "0",
    timestamp: "10:20:45"
  },
  {
    val: "34",
    timestamp: "10:20:46"
  },
  {
    val: "45",
    timestamp: "10:20:47"
  },
  {
    val: "54",
    timestamp: "10:20:48"
  },
  {
    val: "0",
    timestamp: "10:21:45"
  },
  {
    val: "0",
    timestamp: "10:22.45"
  },
  {
    val: "0",
    timestamp: "10:20:45"
  },
  {
    val: "267",
    timestamp: "10:21:45"
  },
  {
    val: "0",
    timestamp: "10:22.45"
  },
  {
    val: "0",
    timestamp: "10:24:45"
  },
  {
    val: "128",
    timestamp: "10:25:45"
  },
  {
    val: "0",
    timestamp: "10:30.45"
  },
]

data = d3.nest()
  .key(function(d) {
    return d.timestamp
  })
  .rollup(function(leaves) {
    return d3.sum(leaves, function(d) {
      return +d.val
    })
  })
  .entries(dataset)

const firstValue = 0,
  lastValue = data.length,
  middleValue = Math.round((firstValue + lastValue) / 2),
  secondVaue = Math.round((firstValue + middleValue / 2)),
  thirdValue = Math.round((middleValue + lastValue) / 2)


var width = 500,
  height = 200;

var svg = d3.selectAll("body")
  .append("svg")
  .attr("width", width)
  .attr("height", height);

var xScale = d3.scalePoint()
  .domain(data.map(function(d) {
    return d.key
  }))
  .range([50, width - 50])
  .padding(0.5);

var yScale = d3.scaleLinear()
  .domain([0, d3.max(data, function(d) {
    return d.value
  }) * 1.1])
  .range([height - 50, 10]);

var line = d3.line()
  .x(function(d, i) {
    return xScale(d.key)
  })
  .y(function(d, i) {
    return yScale(d.value)
  });

svg.append("path")
  .attr("d", line(data))
  .attr("stroke", "teal")
  .attr("stroke-width", "2")
  .attr("fill", "none");

var xAxis = d3.axisBottom(xScale)

if (data.length > 5) {
  const firstValue = 0,
    lastValue = data.length - 1,
    middleValue = Math.round((firstValue + lastValue) / 2),
    secondValue = Math.round((firstValue + middleValue / 2)),
    thirdValue = Math.round((middleValue + lastValue) / 2),
    tickArr = [data[firstValue].key, data[secondValue].key, data[middleValue].key, data[thirdValue].key, data[lastValue].key];

  xAxis
    .tickValues(tickArr);
}

var yAxis = d3.axisLeft(yScale).ticks(5);

svg.append("g").attr("transform", "translate(0,150)")
  .attr("class", "xAxis")
  .call(xAxis);

svg.append("g")
  .attr("transform", "translate(50,0)")
  .attr("class", "yAxis")
  .call(yAxis);
<script src="https://d3js.org/d3.v4.min.js"></script>

I hope it will resolve your issue.

I would suggest you to look into this example, it might help you to reshape your chart idea. https://bl.ocks.org/EfratVil/92f894ac0ba265192411e73f633a3e2f

Updated code as per ask - https://jsfiddle.net/wkb8vzjp/


Related Query