score:1
Accepted answer
i
can mean different things. it's just an iterator. when you draw the lines, you selectall(".dot")
, which gives you one group per line. when you run d3.select(this)
, you only select one thing, and that means that i
is always zero.
to fix this, add the colour as a property to the dots when you draw them.
or, you can use materialname
or materialtypeid
as an identifier instead of the number. then, you add the materialname
as a propery to all circles and you can colour them from there:
var data = [{
depotid: 123,
depotname: "all depots",
materials: [{
materialname: "m7824 (msb0011359c) 600 mg",
materialtypeid: 1234,
materialstock: [{
date: "2020-10-01",
stock: 100
},
{
date: "2020-11-01",
stock: 200
},
{
date: "2020-12-01",
stock: 300
},
{
date: "2021-01-01",
stock: 400
},
{
date: "2021-02-01",
stock: 500
},
{
date: "2021-03-01",
stock: 600
},
{
date: "2021-04-01",
stock: 700
},
{
date: "2021-05-01",
stock: 800
},
{
date: "2021-06-01",
stock: 900
},
{
date: "2021-07-01",
stock: 1000
},
{
date: "2021-08-01",
stock: 1100
},
{
date: "2021-09-01",
stock: 1200
},
]
},
{
materialname: "m7824 (msb0011359c) 500 mg",
materialid: 1232,
materialstock: [{
date: "2020-10-01",
stock: 200
},
{
date: "2020-11-01",
stock: 300
},
{
date: "2020-12-01",
stock: 400
},
{
date: "2021-01-01",
stock: 500
},
{
date: "2021-02-01",
stock: 560
},
{
date: "2021-03-01",
stock: 870
},
{
date: "2021-04-01",
stock: 800
},
{
date: "2021-05-01",
stock: 900
},
{
date: "2021-06-01",
stock: 1000
},
{
date: "2021-07-01",
stock: 1100
},
{
date: "2021-08-01",
stock: 1200
},
{
date: "2021-09-01",
stock: 1300
},
]
}
]
}]
let width = 900,
height = 400,
margin = 100;
var dates = [];
for (let obj of data[0].materials[0].materialstock) {
dates.push(obj.date);
}
var domain = d3.extent(dates);
var newstartdate = new date(domain[0]).setdate(new date(domain[0]).getdate() - 15);
var newendtdate = new date(domain[1]).setdate(new date(domain[1]).getdate() + 15);
var xscaletest = d3.scaletime()
.domain([new date(newstartdate), new date(newendtdate)])
.range([0, width - margin * 2]);
var yscaletest = d3.scalelinear()
.domain([0, d3.max(data[0].materials, function(d) {
return d3.max(d.materialstock, function(d) {
return d.stock;
})
})])
.range([height - margin, 0]);
/* add svg */
var svg = d3.select("#xyaxes").append("svg")
.attr("width", (width + margin) + "px")
.attr("height", (height + margin) + "px")
.attr("style", "outline: thin solid black;")
.append('g')
.attr("transform", `translate(${margin}, ${margin})`);
//add line
var line = d3.line()
.x(function(d) {
return xscaletest(new date(d.date))
})
.y(function(d) {
return yscaletest(d.stock)
});
let lines = svg.append('g')
.attr('class', 'lines');
var groups = lines.selectall('.line-group')
.data(data[0].materials).enter()
.append('g');
var colors = d3.scaleordinal((d3.schemecategory10))
.domain(data[0].materials.map(function(d) {
return d.materialname;
}));
//line
groups.append("path")
.attr("d", function(d) {
return line(d.materialstock)
})
.attr("fill", "none")
.attr("stroke", (d, i) => colors(i))
.style("stroke-dasharray", "5,5") //dashed array for line;
//dot on line
svg.selectall("dot")
.data(data[0].materials)
.enter()
.append("g")
.attr("fill", "none")
.style("stroke", (d) => colors(d.materialname))
.attr("class", "dot")
.selectall("dot")
.data(function(d) {
d.materialstock.foreach(function(v) {
v.materialname = d.materialname;
});
return d.materialstock;
})
.enter().append("circle")
.attr("class", "circle")
.attr("r", 4)
.attr("cx", function(d) {
return xscaletest((new date(d.date)));
})
.attr("cy", function(d) {
return yscaletest(d.stock);
})
.on("mouseover", function(d) {
d3.select(this).style("fill", colors(d.materialname));
})
.on("mouseout", function() {
d3.select(this).style("fill", "none");
});
/* add axis into svg */
var xaxis = d3.axisbottom(xscaletest)
.ticks(d3.timemonth.every(1))
.ticksizeouter(0)
.ticksizeinner(-height + margin)
.tickformat(d3.timeformat("%b -%y"));
var yaxis = d3.axisleft(yscaletest)
.ticks(12)
.ticksize(-width + margin + 100)
svg.append("g")
.attr("class", "x axis")
.attr("transform", `translate(0, ${height-margin})`)
.call(xaxis)
.call(g => g.selectall(".tick:first-of-type line")
.attr("class", "axis_bar")
.attr("stroke", "#bebebe"))
.call(g => g.selectall(".tick:not(:first-of-type) line")
.attr("class", "axis_y_tick")
.attr("stroke", "#e8e8e8"));
svg.append("g")
.attr("class", "y axis")
.call(yaxis)
.call(g => g.selectall(".tick:first-of-type line")
.attr("class", "axis_bar")
.attr("stroke", "black"))
.call(g => g.selectall(".tick:not(:first-of-type) line")
.attr("class", "axis_y_tick")
.attr("stroke", "#e8e8e8"));
svg {
font-family: sans-serif, arial;
}
.line {
stroke-width: 2;
fill: none;
}
.text {
font-size: 12px;
}
text.title {
font-family: arial, helvetica, sans-serif;
font-weight: bold;
font-size: 14px;
color: #353535;
;
}
<script src="https://d3js.org/d3.v5.min.js"></script>
<div id="xyaxes"></div>
Source: stackoverflow.com
Related Query
- Fill same line color in to circle on Multi Line chart when mouse over and remove color from circle when mouse out
- nvd3.js-Line Chart with View Finder: rotate axis labels and show line values when mouse over
- Using D3, I am trying to add and remove lines from a multi line chart when the legend is clicked
- d3: tooltips on multi series line chart at each line when mouse hover event
- d3 multi line with mouse over cursor for both y AND x value
- D3 js line chart shows dot on mouse over and mouse out
- How to show and hides nodes when you move the mouse over a node in D3 Javascript
- D3 - Single and Multi Line chart tooltips
- D3 Line chart doesn't return correct value on ticks mouse over
- Connected link line animation when mouse over on the node in force layout d3
- Pie chart avoiding same adjacent color when rotating colors
- changing color of d3.js pie chart label when hovering over slice?
- Multi line chart using d3 and json in a nvd3 structure
- Change color of arrow as same as line in D3 when I switch lines
- Why doesn't the same group render a correct line chart and corresponding range chart?
- How to add a circle in the middle of donut chart and fill it in d3 js?
- Color overlapped portions of multi line chart
- D3 v4 Multi line chart Brush Issue NaN when moving brush
- why d3 line chart and bar chart display in same morment?
- Remove line from chart and draw new one
- How to put data on line chart when x axis and data has a time range
- D3 circle element remains when mouse over on data update
- I am using d3 to visualize some nodes and links. I want to be able to fadeout other circles (nodes) when I hover over one circle
- D3 V4 Multi series line chart: Adding data points to lines with same color as lines
- fill points on the multi-line chart with the same color as lines
- Is it possible to add zoom and tooltip on the same line chart in d3js?
- Ghost line path when zooming and panning a D3 chart
- How to keep mouse over object when using drag and snap to with d3.js
- show label when mouse over a circle
- Color the area formed between Line Chart and Threshold line
More Query from same tag
- Angular2 D3 - CSS styling is not applied
- D3 update values to a new dataset on mouse click
- How to flexibly select a region with nodes with d3
- Positioning multiple SVG elements in div HTML
- Unable to stack rects horizontally
- How to export neo4j data to csv/json
- d3.js Faux-3D Arcs - Uncaught typeerror: Cannot read property 'objects' of undefined
- d3.geoPath() vs d3.geo.path()
- Displaying circles using d3 from data
- Dojo js Select and onchange function
- I am trying to build a d3js graph but on the x-axis i am getting the timestamps instead of date and time even after i used formatting
- Arc Chart - with various segment indentions/heights
- Passing in new Date to d3 popups
- Using D3 chart in Pentaho CDE
- how to avoid overlapping circles on a map using d3js
- Unexpected value translate parsing transform attribute
- links in d3.js forced directed graph
- d3js: click event in reusable chart
- Getting array length with d3
- Is it possible to loop in SVG?
- How to draw tick in angular js gauge?
- d3.js v5 stratify returning missing: 0
- Linear/Incremental rotation animation with transition
- How to make D3 display xAxis correctly during daylight saving time change
- d3 - mock the labelling of the x axis
- Adding image/icon instead of svg shape in d3
- d3 fisheye undefined
- How to structure .csv file to allow D3.js to associate a line with a specified color?
- dc scatter plot binding onClick event
- select data based on multiple attributes d3