I tem uma lista de dicionários para representar dados em uma forma de gráfico utilizando D3, mas mostra o gráfico doesnt correctamente como estou incapaz de analisar tempo num formato H: S: M.
Aqui é as poucas linhas:
var data= [{ val: "0", timestamp : "10:20:45"},
{ 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"},
]
var parseDate = d3.time.format("%H:%M:%S").parse;
// Set the ranges
var x = d3.time.scale().range([0, graphWidth]);
var y = d3.scale.linear().range([graphHeight, 0]);
// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
Eu acho, eu cobri praticamente todas as coisas, mas de alguma forma não funciona. Eu sou novo usando D3, precisa de alguma ajuda. completa code- https://jsfiddle.net/gaurav10022/p8oxdcey/
Primeiro você precisa para normalizar seus dados para que você tenha chaves únicas e você pode ver um único ponto por um tempo particular. Para isso você precisa usar d3.nest
.
Ele vai criar uma newData
onde key
será o tempo e values
será o valor que você deseja mostrar.
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>
Espero que ele vai resolver o problema.
Eu sugiro que você olhar para este exemplo, pode ajudá-lo a reformular a sua ideia gráfico. https://bl.ocks.org/EfratVil/92f894ac0ba265192411e73f633a3e2f
Código atualizado conforme perguntar - https://jsfiddle.net/wkb8vzjp/