Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make node radius respect the package size #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/scripts/viewer/2d/graphUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ var eventify = require('ngraph.events');
var arrow = require('./arrow');
var defaultNodeColor = '#CFCCDF';
var defaultTextColor = '#484A5C';
var defaultRadius = 5;
var minRadius = 0.25;
var maxRadius = 30;

module.exports = function(svgRoot) {
module.exports = function(svgRoot, showSize) {
var nodeUIModels = Object.create(null);
var linkUIModels = Object.create(null);

Expand Down Expand Up @@ -45,7 +48,7 @@ module.exports = function(svgRoot) {
node: function(node) {
var ui = svg.compile([
"<g>",
"<circle fill='{{nodeColor}}' r='5px'></circle>",
"<circle fill='{{nodeColor}}' r='{{nodeRadius}}px'></circle>",
"<text fill='{{textColor}}' y='-10' x='-5'>{{text}}</text>",
"</g>"
].join('\n'));
Expand All @@ -55,7 +58,8 @@ module.exports = function(svgRoot) {
originalColor: defaultNodeColor,
textColor: defaultTextColor,
originalTextColor: defaultTextColor,
text: node.id
text: node.id,
nodeRadius: getNodeRadius(node)
};

nodeUIModels[node.id] = {
Expand Down Expand Up @@ -111,4 +115,12 @@ module.exports = function(svgRoot) {
info.model.textColor = textColor;
info.ui.dataSource(info.model);
}

function getNodeRadius(node) {
var radius = defaultRadius;
if (showSize && node.data && node.data.dist && node.data.dist.unpackedSize) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious - where does unpackedSize come from? I tried to explore a random package from the registry.npmjs.cf and I don't see it there

Copy link
Author

@styfle styfle Jul 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anvaka I think this was introduced in the last 4 months or so, so only packages published since then will have this information populated. I asked the npm team if there are plans to backfill.

I originally planned to make a HTTP request to my tool, Package Phobia, which will work for any arbitrary package, but I realized that I would need to submit a PR to the other repo (which would take more time and I'm lazy 😄)

I fallback to the 5px radius if the size was not found (this was the previous size used) .

radius = (node.data.dist.unpackedSize / 5000).toFixed(2);
}
return Math.min(Math.max(minRadius, radius), maxRadius);
}
};
5 changes: 3 additions & 2 deletions src/scripts/viewer/2d/graphViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function graphViewer() {
'source': '=',
'nodeSelected': '=',
'root': '=',
'mode': '='
'mode': '=',
'showSize': '='
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that showSize was going to show up from the parent $scope but this didn't work. Any ideas?

},

compile: function(tElement, tAttrs, transclude) {
Expand Down Expand Up @@ -48,7 +49,7 @@ function graphViewer() {
highlightNodesFromRequest(request);
});

var graphUI = require('./graphUI')(renderer.svgRoot);
var graphUI = require('./graphUI')(renderer.svgRoot, scope.showSize);

renderer.node(graphUI.node).placeNode(graphUI.placeNode);
renderer.link(graphUI.link).placeLink(graphUI.placeLink);
Expand Down
2 changes: 2 additions & 0 deletions src/scripts/viewer/2d/package2dView.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ <h4>Remaining packages: <span>{{progress}}</span></h4>

<div ng-show='showSwitchMode' class='switchMode d2d'>
<button class="btn btn-xs btn-danger" ng-click="switchMode()" type="button">Show 3D</button>
<button class="btn btn-xs btn-danger" ng-hide="showSize" ng-click="toggleSize()" type="button">Toggle Size</button>
<button class="btn btn-xs btn-success" ng-show="showSize" ng-click="toggleSize()" type="button">Toggle Size</button>
</div>
26 changes: 19 additions & 7 deletions src/scripts/viewer/2d/package2dViewController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@ var getLocation = require('../getLocation.js');
var createGraphBuilder = require('../graphBuilder');

module.exports = function($scope, $routeParams, $http, $location) {
var applyToScope = require('../applyToScope')($scope);

// TODO: Remove root, it's no longer valid
$scope.root = $routeParams.pkgId;
$scope.showProgress = true;
$scope.switchMode = switchMode;
$scope.showSize = false;
$scope.toggleSize = toggleSize;

var graphBuilder = createGraphBuilder($routeParams.pkgId, $routeParams.version, $http, applyToScope(progressChanged));
$scope.graph = graphBuilder.graph;

graphBuilder.start
.then(applyToScope(graphLoaded))
.catch(applyToScope(errorOccured));
refreshGraph();

function progressChanged(queueLength) {
$scope.progress = queueLength;
Expand Down Expand Up @@ -54,4 +50,20 @@ module.exports = function($scope, $routeParams, $http, $location) {
var path = getLocation($routeParams, /* is2d = */ false);
$location.path(path);
}

function toggleSize() {
$scope.showSize = !$scope.showSize;
refreshGraph();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anvaka I have no idea how to use Angular and it seems like there is some wrapper (an) around Angular that makes this even more mysterious.

How can I make this toggleSize() function refresh/re-render the graph?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember most of it as well :). I think you could use approach similar to node highlighting. Check the graphViewer.js and graphUI.js files.

Copy link
Author

@styfle styfle Jul 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I think about it, I think I want to make this part of the url, because I would like to link directly to this page from https://packagephobia.com

Actually, I already have a link but I would like the link to enable this feature which is why I need it in the URL.

Thoughts?

}

function refreshGraph() {
var applyToScope = require('../applyToScope')($scope);
var graphBuilder = createGraphBuilder($routeParams.pkgId, $routeParams.version, $http, applyToScope(progressChanged));

$scope.graph = graphBuilder.graph;

graphBuilder.start
.then(applyToScope(graphLoaded))
.catch(applyToScope(errorOccured));
}
};