"D3.js" Focus + Context Line chart

Source: Internet
Author: User


Use D3.js Library to implement Focus+context line chart, read DATA.TSV file data

Index.html

<! DOCTYPE html><meta charset= "Utf-8" ><style>svg {font:10px sans-serif;}.  Axis Path,.axis line {fill:none;  Stroke: #000; Shape-rendering:crispedges;}.  line {fill:none;                            Clip-path:url (#clip);  Cutting off the stroke:steelblue in excess of the line segments beyond the coordinate system range; stroke-width:1.5px;}.  Brush. Extent {stroke: #fff;  Fill-opacity:. 125; Shape-rendering:crispedges;} </style><body><script src= "Https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" ></ Script><script>var margin = {top:10, right:10, bottom:100, left:40}, margin2 = {top:430, right:10, Botto M:20, left:40}, Width = 960-margin.left-margin.right, height = 500-margin.top-margin.bottom, height2 =    500-margin2.top-margin2.bottom; var color = D3.scale.category10 (), var parsedate = D3.time.format ("%y%m%d"). Parse;var x = D3.time.scale (). Range ([0, Width ]), x2 = D3.time.scale (). Range ([0, Width]), y = D3.scale.linear (). Range ([height, 0]), y2 = D3.scale.linear (). Range ([height2, 0]), var xaxis = D3.svg.axis (). scale (X). Orient ("Bottom"), XAxis2 = D3.svg.axis (). SC    Ale (x2). Orient ("Bottom"), YAxis = D3.svg.axis (). scale (Y). Orient ("left");    var line = D3.svg.line (). Interpolate ("basis"). x (function (d) {return x (d.date);})    . Y (function (d) {return y (d.temperature);});    var line2 = D3.svg.line (). Interpolate ("basis"). x (function (d) {return x2 (d.date);}) . Y (function (d) {return y2 (d.temperature);}); var brush = D3.svg.brush (). x (x2). On ("brush", brushed), var svg = d3.select ("Body"). Append ("SVG"). attr ("width", Width + margin.left + margin.right). attr ("height", height + margin.top + margin.bottom); Svg.append ("Defs"). Append ("CLI Ppath "). attr (" id "," clip "). Append (" Rect "). attr (" width ", width). attr (" height ", height); var focus = Svg.append ("G"). attr ("Class", "Focus"). attr ("transform", "translate (" + Margin.left + "," + Margin.top + ")"); var context = Svg.append ("G"). attr ("Class "," context "). attr (" transform "," translate ("+ Margin2.left +", "+ Margin2.top +") ");d 3.tsv (" DATA.TSV ", Functi  On (error, data) {Color.domain (D3.keys (data[0]). Filter (function (key) {return key!== "date";}));  Data.foreach (function (d) {d.date = Parsedate (d.date);  }); var cities = Color.domain (). Map (function (name) {return {name:name, values:data.map (function (d) {R      Eturn {date:d.date, temperature: +d[name]};  })    };  });  X.domain (d3.extent (data, function (d) {return d.date;})); Y.domain ([D3.min (Cities, function (c) {return d3.min (c.values, function (v) {return v.temperature;});}), D3.max (c  Ities, function (c) {return D3.max (c.values, function (v) {return v.temperature;});})  ]);  X2.domain (X.domain ());     Y2.domain (Y.domain ());  var city = Focus.selectall ('. City '). Data (cities). Enter (). Append ("G"). attr ("Class", "City"); City.append ("path"). attr ("Class", "line"). attr ("D", function (d) {return line (d.values);  }). Style ("Stroke", function (d) {return color (d.name);});  Focus.append ("G"). attr ("Class", "X axis"). attr ("transform", "translate (0," + height + ")"). Call (Xaxis);              Focus.append ("G"). attr ("Class", "Y axis"). Call (YAxis);  var city2 = Context.selectall (". City"). Data (cities). Enter (). Append ("G"). attr ("Class", "City");      City2.append ("path"). attr ("Class", "line"). attr ("D", function (d) {return line2 (d.values);})  . Style ("Stroke", function (d) {return color (d.name);}); Context.append ("G"). attr ("Class", "X axis"). attr ("transform", "translate (0," + height2 + ")"). Call (Xaxis  2); Context.append ("G"). attr ("Class", "X Brush"). Call (Brush). SelectAll ("rect"). attr ("Y",-6). attr (  "Height", height2 + 7);  }); function brushed () {X.domain (Brush.empty ()? X2.domain (): Brush.extent ());  Focus.selectall (". Line"). attr ("D", function (d) {return line (d.values);}); Focus.select (". X.axis").Call (Xaxis);} </script>

As follows:



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"D3.js" Focus + Context Line chart

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.