score:1
I'm posting the solution that I used. https://jsfiddle.net/yasirunilan/rvoft1h8/7/
var data = [{
name: "USA",
values: [{
date: "2000",
price: "100"
},
{
date: "2001",
price: "110"
},
{
date: "2002",
price: "145"
},
{
date: "2003",
price: "241"
},
{
date: "2004",
price: "101"
},
{
date: "2005",
price: "90"
},
{
date: "2006",
price: "10"
},
{
date: "2007",
price: "35"
},
{
date: "2008",
price: "21"
},
{
date: "2009",
price: "201"
}
]
},
{
name: "Canada",
values: [{
date: "2000",
price: "100"
},
{
date: "2001",
price: "110"
},
{
date: "2002",
price: "145"
},
{
date: "2003",
price: "241"
},
{
date: "2004",
price: "101"
},
{
date: "2005",
price: "90"
},
{
date: "2006",
price: "10"
},
{
date: "2007",
price: "35"
},
{
date: "2008",
price: "21"
},
{
date: "2009",
price: "201"
}
]
},
{
name: "Maxico",
values: [{
date: "2000",
price: "100"
},
{
date: "2001",
price: "110"
},
{
date: "2002",
price: "145"
},
{
date: "2003",
price: "241"
},
{
date: "2004",
price: "101"
},
{
date: "2005",
price: "90"
},
{
date: "2006",
price: "10"
},
{
date: "2007",
price: "35"
},
{
date: "2008",
price: "21"
},
{
date: "2009",
price: "201"
}
]
}
];
var width = 500;
var height = 300;
var margin = 50;
var duration = 250;
var lineOpacity = "0.25";
var lineOpacityHover = "0.85";
var otherLinesOpacityHover = "0.1";
var lineStroke = "1.5px";
var lineStrokeHover = "2.5px";
var circleOpacity = '0.85';
var circleOpacityOnLineHover = "0.25"
var circleRadius = 3;
var circleRadiusHover = 6;
/* Format Data */
var parseDate = d3.timeParse("%Y");
data.forEach(function(d) {
d.values.forEach(function(d) {
d.date = parseDate(d.date);
d.price = +d.price;
});
});
/* Scale */
var xScale = d3.scaleTime()
.domain(d3.extent(data[0].values, d => d.date))
.range([0, width - margin]);
var yScale = d3.scaleLinear()
.domain([0, d3.max(data[0].values, d => d.price)])
.range([height - margin, 0]);
var color = d3.scaleOrdinal(d3.schemeCategory10);
/* Add SVG */
var svg = d3.select("#chart").append("svg")
.attr("width", (width + margin) + "px")
.attr("height", (height + margin) + "px")
.append('g')
.attr("transform", `translate(${margin}, ${margin})`);
/* Add line into SVG */
var line = d3.line()
.x(d => xScale(d.date))
.y(d => yScale(d.price));
let lines = svg.append('g')
.attr('class', 'lines');
lines.selectAll('.line-group')
.data(data).enter()
.append('g')
.attr('id',function(d){ return d.name.replace(/\s+/g, '')+"-line"; })
.attr('class', 'line-group')
.on("mouseover", function(d, i) {
svg.append("text")
.attr("class", "title-text")
.style("fill", color(i))
.text(d.name)
.attr("text-anchor", "middle")
.attr("x", (width - margin) / 2)
.attr("y", 5);
})
.on("mouseout", function(d) {
svg.select(".title-text").remove();
})
.append('path')
.attr('class', 'line')
.attr('d', d => line(d.values))
.style('stroke', (d, i) => color(i))
.style('opacity', lineOpacity)
.on("mouseover", function(d) {
d3.selectAll('.line')
.style('opacity', otherLinesOpacityHover);
d3.selectAll('.circle')
.style('opacity', circleOpacityOnLineHover);
d3.select(this)
.style('opacity', lineOpacityHover)
.style("stroke-width", lineStrokeHover)
.style("cursor", "pointer");
})
.on("mouseout", function(d) {
d3.selectAll(".line")
.style('opacity', lineOpacity);
d3.selectAll('.circle')
.style('opacity', circleOpacity);
d3.select(this)
.style("stroke-width", lineStroke)
.style("cursor", "none");
});
/* Add circles in the line */
lines.selectAll("circle-group")
.data(data).enter()
.append("g")
.attr('id',function(d){ return d.name.replace(/\s+/g, '')+"-circle"; })
.style("fill", (d, i) => color(i))
.selectAll("circle")
.data(d => d.values).enter()
.append("g")
.attr("class", "circle")
.on("mouseover", function(d) {
d3.select(this)
.style("cursor", "pointer")
.append("text")
.attr("class", "text")
.text(`${d.price}`)
.attr("x", d => xScale(d.date) + 5)
.attr("y", d => yScale(d.price) - 10);
})
.on("mouseout", function(d) {
d3.select(this)
.style("cursor", "none")
.transition()
.duration(duration)
.selectAll(".text").remove();
})
.append("circle")
.attr("cx", d => xScale(d.date))
.attr("cy", d => yScale(d.price))
.attr("r", circleRadius)
.style('opacity', circleOpacity)
.on("mouseover", function(d) {
d3.select(this)
.transition()
.duration(duration)
.attr("r", circleRadiusHover);
})
.on("mouseout", function(d) {
d3.select(this)
.transition()
.duration(duration)
.attr("r", circleRadius);
});
/* Add Axis into SVG */
var xAxis = d3.axisBottom(xScale).ticks(5);
var yAxis = d3.axisLeft(yScale).ticks(5);
svg.append("g")
.attr("class", "x axis")
.attr("transform", `translate(0, ${height-margin})`)
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append('text')
.attr("y", 15)
.attr("transform", "rotate(-90)")
.attr("fill", "#000")
.text("Total values");
var dataNest = d3.nest()
.key(function(d) {
return d.name;
})
.entries(data);
var legendSpace = width / dataNest.length;
// Loop through each symbol / key
dataNest.forEach(function(d, i) {
// Add the Legend
svg.append("text")
.attr("x", (legendSpace / 2) + i * legendSpace) // space legend
.attr("y", height)
.attr("class", "legend") // style the legend
.style("fill", color(i))
.on("click", function() {
// Determine if current line is visible
var active = d.active ? false : true,
newOpacity = active ? 0 : 1;
// Hide or show the elements based on the ID
d3.select("#" + d.key.replace(/\s+/g, '') + "-line")
.transition().duration(100)
.style("opacity", newOpacity);
d3.select("#" + d.key.replace(/\s+/g, '') + "-circle")
.transition().duration(100)
.style("opacity", newOpacity);
// Update whether or not the elements are active
d.active = active;
})
.text(d.key);
});
Source: stackoverflow.com
Related Query
- D3 Multi Series Line Chart with Clickable Legend
- D3 Multi Series Line chart not working with correct xAxis values
- D3 Multi Series Line chart with non time x axis
- dc.js Series Chart multi line
- Multi series chart (D3) with missing values
- Error in A Simple D3 Line chart with Legend and Tooltips for D3v 3.5.13
- Dimple.js line chart with composite axis, no links between points on series
- d3: tooltips on multi series line chart at each line when mouse hover event
- simple multi line chart with D3
- Legend in Multi line chart - d3
- Time series line chart is not sync with axis
- How to create a multi-series line chart with series for each unique item in 1st column?
- SVG Legend for multi line chart d3 v6
- D3 Multi Line Chart Not Displaying Properly With X Axis Value
- Error displaying dots on a multi series line - dots chart
- D3 line chart legend not working with v3 but working with v2
- dc.js multi line series chart filtering
- How to draw a multi line series graph using d3.js with json data using angular directives
- Using D3, I am trying to add and remove lines from a multi line chart when the legend is clicked
- D3 V4 Multi series line chart: Adding data points to lines with same color as lines
- d3.js - Multi series line chart tool tip issue
- How to add tooltip with eclipse in legend in line chart in D3.js
- Animated Path multi series line chart in D3.Js
- d3.js multi line chart with brush
- D3 Interactive Legend on Multi Line Chart Issue
- How to render multi line chart with DC.js with the following type of data - JAVASCRIPT (DC.js)
- Show labels conditionally on multi series line chart
- d3 Multi Series Chart with Incorrect Values
- D3 Multi Series Line Chart
- Issue with d3 Multi Line Chart
More Query from same tag
- d3 sunburst chart selection rotation not working
- How to create a % difference arrow line with value in a bar chart using D3.js
- X axis label not displayed in bar chart with scroll and zoom feature - d3js, Brushing
- Set text width or put text in a div
- Reorder group elements to a specific order
- D3 Dynamic Bar Chart Issue
- Overlap in d3 wordcloud
- How do I display a d3.js circle pack graph with RoR?
- Hightlight the respective layer in stacked Bar chart when mouseover on legend in d3
- How to do a timeline graph similar to GitHub using D3?
- D3.geo : Spherical arcs rather than straight lines for parallels?
- d3 scale.linear(): pass data as parameter
- D3.js Straight links in tree after transformation
- Unneeded white space before the 1st bar in D3 Stack chart
- D3.js change time format and export data into csv
- How to change the scale on the stackedAreaChart of NVD3
- D3: Populate Table from Dict when Line Selected
- Major and minor ticks with V3 of D3?
- D3 - Using JSON data to create multiple bars
- D3 path transition direction
- D3 - Event binding inside another event
- Upload a csv file in apps script from google drive blocked by CORS policy
- Filter/select specific type of data
- Why does d3 date axis fail in Firefox for some particular date domains?
- How can I dynamically update text labels in d3?
- D3.js animation for donut labels
- Leaflet Markercluster showCoverageOnHover triggered wrong
- Redrawing SVG on resize
- How to draw circles at different times with D3js?
- Adding Multiple graphs to one page with D3