UNPKG

dc

Version:

A multi-dimensional charting library built to work natively with crossfilter and rendered using d3.js

119 lines (108 loc) 5.03 kB
<!DOCTYPE html> <html lang="en"> <head> <title>dc.js - Scatter Plot Brushing Example - Large Dataset, Canvas Backend</title> <meta charset="UTF-8"> <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="../css/dc.css"/> <script type="text/javascript" src="../js/d3.js"></script> <script type="text/javascript" src="../js/crossfilter.js"></script> <script type="text/javascript" src="../js/dc.js"></script> </head> <body> <div class="container"> <script type="text/javascript" src="header.js"></script> <h3>dc.js - Scatter Plot Brushing Example - Large Dataset, SVG Backend</h3> <p>Plotting 20,000 data points in 2 plots. Plots may take a few seconds to load. Brush on one chart to see the points filtered on the other. Compare against performance using Canvas backend in the <a href="scatter-canvas-large.html">Canvas scatter plot example</a>.</p> <hr /> <div id="test1"></div> <div id="test2"></div> </div> </body> <footer> <script type="text/javascript"> d3.csv('sampleData20000.csv').then(function(rowData) { rowData.forEach(function(d) { d.feature1 = +d.feature1; d.feature2 = +d.feature2; d.feature3 = +d.feature3; d.class = +d.class; }) // Calculate x and y binning with each bin being PIXEL_BIN_SIZE wide. // Binning coordinates into bins such that 1-2 bins per pixel makes // crossfilter operations more efficient, especially with large // datasets const nXBins = 300; const nYBins = 300; const binWidth = 20 / nXBins; var ndx = crossfilter(rowData), dim1 = ndx.dimension(function (d) { return [Math.floor(d.feature1 / binWidth) * binWidth, Math.floor(d.feature2 / binWidth) * binWidth, d.class]; }), dim2 = ndx.dimension(function (d) { return [Math.floor(d.feature2 / binWidth) * binWidth, Math.floor(d.feature3 / binWidth) * binWidth, d.class]; }), group1 = dim1.group(), group2 = dim2.group(); // var ndx = crossfilter(rowData), // dim1 = ndx.dimension(function (d) { // return [d.feature1, d.feature2, d.class]; // }), // dim2 = ndx.dimension(function (d) { // return [d.feature2, d.feature3, d.class]; // }), // group1 = dim1.group(), // group2 = dim2.group(); var plotColorMap = {0: '#ff7f0e', 1: '#2ca02c'}; var chart1 = dc.scatterPlot("#test1").width(300) .height(300) .width(350) .useCanvas(false) .x(d3.scaleLinear().domain([-8, 8])) .yAxisLabel("Feature 2") .xAxisLabel("Feature 1") .keyAccessor(function (d) { return d.key[0]; }) .valueAccessor(function (d) { return d.key[1]; }) .clipPadding(10) .dimension(dim1) .highlightedSize(4) .symbolSize(3) .excludedSize(2) .excludedOpacity(0.5) .excludedColor('#ddd') .group(group1) .colorAccessor(function (d) { return d.key[2]; }) .colors(function(colorKey) { return plotColorMap[colorKey]; }); chart1.legendables = function () { return [{name: 'Class 0', chart: chart1, color: plotColorMap[0]}, {name: 'Class 1', chart: chart1, color: plotColorMap[1]}]; } chart1.legend(dc.legend().x(250).y(10).itemHeight(13).gap(5).legendWidth(90).itemWidth(70)); var chart2 = dc.scatterPlot("#test2").width(300) .height(300) .width(350) .useCanvas(false) .x(d3.scaleLinear().domain([-8, 8])) .yAxisLabel("Feature 2") .xAxisLabel("Feature 3") .clipPadding(10) .dimension(dim2) .highlightedSize(4) .symbolSize(3) .excludedSize(2) .excludedOpacity(0.5) .excludedColor('#ddd') .group(group2) .colorAccessor(function (d) { return d.key[2]; }) .colors(function(colorKey) { return plotColorMap[colorKey]; }); chart2.legendables = function () { return [{name: 'Class 0', chart: chart2, color: plotColorMap[0]}, {name: 'Class 1', chart: chart2, color: plotColorMap[1]}]; } chart2.legend(dc.legend().x(250).y(10).itemHeight(13).gap(5).legendWidth(90).itemWidth(70)); dc.renderAll(); }); </script> </footer> </html>