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

4.0 attr not support object as parameter #2793

Closed
EcutDavid opened this issue Apr 13, 2016 · 9 comments
Closed

4.0 attr not support object as parameter #2793

EcutDavid opened this issue Apr 13, 2016 · 9 comments

Comments

@EcutDavid
Copy link

In 3.X, we can use syntax like:

  .attr({
    cx: d => d.x,
    cy: d => d.y,
    r: d => colorScale(d.value),
    fill: '#D0021B',
    opacity: 0.5,
  })

But now, it's not work and no error prompt.
Need change it to

.attr('cx', d=> d.x)
.......

What's the reason the syntax changed?

Thanks in advance.

@EcutDavid EcutDavid changed the title 4.0 attr support object 4.0 attr not support object as parameter Apr 13, 2016
@mbostock
Copy link
Member

In D3 4.0, multi-value map support has moved to the d3-selection-multi repository as an optional plugin, using distinct names (e.g., selection.attrs instead of selection.attr) to avoid ambiguity.

@skipjack
Copy link

skipjack commented Jun 28, 2016

What's the proper way to require this plugin (or any plugin) if we're not exposing d3 globally?

I upgraded to v4 (d3@4.0.0-rc.2) and npm installed d3-selection-multi however I'm totally lost as to how I'm supposed to require or import the plugin in a module.

import * as d3 from 'd3'

works fine for the base functionality, but I use the attr({...}) syntax frequently and I haven't been able to get it to work again. I would expect something like this:

import * as d3 from 'd3'
import 'd3-selection-multi'

// ...

d3.select(...).attrs({...})

however this fails. I know I'm probably missing something silly here but from reading the docs and using other libs like leaflet, this was my best guess as to how I could get it working (although leaflet does expose itself globally). Using leaflet plugins through npm and import or require:

import L from 'leaflet'
import 'leaflet.heat'
import 'leaflet.markercluster'

My only other guess would be that maybe I need to use rollup and make a custom build?

@mbostock
Copy link
Member

It might depend on how your bundler is configured. If your bundler isn’t observing the jsnext:main defined in the d3 package.json, then it’s possible that you are importing the generated UMD bundle (see build/d3.js). If that’s the case, then the imported d3 is a copy of all the default D3 modules, rather than a reference, and so when you import d3-selection-multi, you end up with two copies of d3-selection, and the one that was imported and modified by d3-selection-multi isn’t exposed on the d3 symbol.

One way to avoid this problem is to use the modules directly and avoid the default bundle:

import * as d3_selection from "d3-selection";
import "d3-selection-multi"; // modifies d3_selection.selection.prototype

If you’re using require in Node, you can also use the default bundle without pulling in duplicate code:

var d3 = require("d3");
require("d3-selection-multi"); // modifies d3.selection.prototype

That’s because there’s a special d3.node.js bundle that uses require for the dependencies rather than inlining the code. This approach is not feasible for the UMD bundle, since inlining the code is a requirement for vanilla script tags.

As you guessed, another solution is to create your own custom bundle and then require that as d3 rather than loading them separately. You could use Rollup for that, but other tools could be made to work too.

@skipjack
Copy link

Thanks for the quick response! I'll keep playing around with it (I'm using Webpack btw)...

@ValentinH
Copy link

I'm migrating several components to v4 and I just got hit by this breaking change. Too bad that it's not listed in https://github.com/d3/d3/blob/master/CHANGES.md

@curran
Copy link
Contributor

curran commented Nov 16, 2016

@ValentinH It is listed there, in the last paragraph of the Selections section, quoted here:

For the sake of parsimony, the multi-value methods—where you pass an object to set multiple attributes, styles or properties simultaneously—have been extracted to d3-selection-multi and are no longer part of the default bundle. The multi-value map methods have also been renamed to plural form to reduce overload: selection.attrs, selection.styles and selection.properties.

@ValentinH
Copy link

OK, sorry for that. I didn't read until the end of the section and was searching for the attr( string in the documentation.

Just to understand, is extracting those multi-value methods in a separate repository showing that the "recommended" way is to chain single methods?

@mbostock
Copy link
Member

Yes, d3-selection-multi is available but not recommended by default.

@saun4app
Copy link

saun4app commented Feb 5, 2017

@mbostock, would you please help us understand why d3-selection-multi is not recommended. Thank you.

wchargin added a commit to tensorflow/tensorboard that referenced this issue Aug 16, 2017
Summary:
In the graph dashboard, we attempt to set the size of the download
canvas according to the scene size. However, the syntax that we use to
do this is no longer supported by d3v4 ([source]), and so this ends up
doing nothing. Simply changing the syntax fixes the bug.

Fixes #88.

[source]: d3/d3#2793

Test Plan:
Launch the graph explorer, and click "Download PNG" at left. Before this
patch, this always resulted in a 300×150 px image. Now, it results in an
image whose size is properly determined by the contents of the graph.

wchargin-branch: fix-graph-png-size
wchargin added a commit to tensorflow/tensorboard that referenced this issue Aug 16, 2017
Summary:
In the graph dashboard, we attempt to set the size of the download
canvas according to the scene size. However, the syntax that we use to
do this is no longer supported by d3v4 ([source]), and so this ends up
doing nothing. Simply changing the syntax fixes the bug.

Fixes #88.

[source]: d3/d3#2793

Test Plan:
Launch the graph explorer, and click "Download PNG" at left. Before this
patch, this always resulted in a 300×150 px image. Now, it results in an
image whose size is properly determined by the contents of the graph.

wchargin-branch: fix-graph-png-size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

6 participants