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

Composed charts #139

Closed
dortamiguel opened this issue Jan 31, 2018 · 28 comments
Closed

Composed charts #139

dortamiguel opened this issue Jan 31, 2018 · 28 comments

Comments

@dortamiguel
Copy link

Can I compose two different charts into one?

Like for example drawing a line in a bar chart.

image

@stahlmanDesign
Copy link

I recently asked the same thing and then answer is 'not right now' https://twitter.com/jstahlman/status/939260265618706435

@plouc
Copy link
Owner

plouc commented Feb 1, 2018

I've started something on #116, but it involves far more work than I expected :/, I'm planning to re-introduce this in a more progressive way.

@wulucxy
Copy link

wulucxy commented Sep 12, 2018

@plouc what is the progress now?

@wulucxy
Copy link

wulucxy commented Sep 13, 2018

@stahlmanDesign @ellipticaldoor do you guys solve this problem?

@plouc
Copy link
Owner

plouc commented Sep 13, 2018

Unfortunately, there is no proper guides on how to do it, but if you have a look at the code for each component, you could be able to build composed charts.
Main chart components are build on smaller components, axes, containers, nodes…
I'm also adding layout components, which will also help.

@wulucxy
Copy link

wulucxy commented Sep 13, 2018

@plouc so where I can find the layout Component?

@plouc
Copy link
Owner

plouc commented Sep 13, 2018

@plouc
Copy link
Owner

plouc commented Sep 13, 2018

For now, the best you can do is having a look at the components and extract the relevant pieces, not really handy I admit :/

@plouc
Copy link
Owner

plouc commented Nov 27, 2018

I've started to introduce a layers property which allows to pass extra layers and also to change the order of elements.

For now, it's supported on Bar and ScatterPlot components. You can find a working example of it here using the ScatterPlot component.

If you're willing to contribute on other components, please do not hesitate :)

@plouc plouc added the feature label Nov 27, 2018
@ncknuna
Copy link

ncknuna commented Nov 27, 2018

Oh, great! I'll try to take a look at adding layers to Line today, following this commit that added it to Bar (8a817ec#diff-5799e61b0b5bfcb7468aee7a5406e61a). If there's a better thing for me to look at, let me know!

@plouc
Copy link
Owner

plouc commented Nov 28, 2018

And now, thanks to @ncknuna, we have layers support for Line component too!

@pfreedmanJG
Copy link

For now, it's supported on Bar and ScatterPlot components. You can find a working example of it here using the ScatterPlot component.

Has anyone got a link similar to the ScatterPlot one to some code showing a custom layer in use on the Bar component?

@pfreedmanJG
Copy link

Ok, here is an example I've written that is vaguely similar to what OP wanted.

@plouc
Copy link
Owner

plouc commented Dec 6, 2018

Thank you @pfreedmanJG! I've used your example as a base to reproduce what @ellipticaldoor wanted to achieve => https://codesandbox.io/s/m4ro13jjn8

@pfreedmanJG
Copy link

@plouc That's great, I was wondering how to get circles rendering on top of the points!

@plouc plouc closed this as completed Dec 11, 2018
@yojeek
Copy link

yojeek commented Feb 12, 2019

@plouc sorry for the slight off-topic, but why your codepen example only works if x value contains series of integers "0", "1", etc. If I change it to anything but integers it breaks inside xScale(index).

@plouc
Copy link
Owner

plouc commented Feb 12, 2019

@yojeek, because the bar chart only supports ordinal values for now, other charts (line, scatterplot) support other types like linear/time/log scales.

@plouc
Copy link
Owner

plouc commented Feb 12, 2019

@yojeek, sorry I don't respond to emails, please use GitHub instead. I had a look at the example, and fixed it => https://codesandbox.io/s/zrk1o3j29p, the problem was the use of bar.data.index instead of bar.data.indexValue.

@EduardoAC
Copy link
Contributor

@plouc are you giving supporting other @nivo components as Layers instead of D3?

@simoami
Copy link

simoami commented May 28, 2019

@plouc Would it be best to support an entirely new Composite chart container instead of making Bar chart as the primary container? So it controls the layers and knows what layers to strip off from the inner charts. There might be other benefits.

@plouc
Copy link
Owner

plouc commented May 29, 2019

@simoami, some charts supports the same layer ids, but contains different things, but not every chart share the same layers as others. Also nivo provides specific packages for each chart type, and this approach assumes that all chart types are installed.

@simoami
Copy link

simoami commented Jun 2, 2019

Thanks @plouc. I was just throwing an idea in case it offers a predictable composition. To not be aware of charts, you could have an explicit declaration. <CompositeChart><Chart1 /><Chart2 /></CompositeChart>. Then the composite chart can share a layer service that the inner charts can choose to support. Something react contexts can help with.

djpowers added a commit to djpowers/movie-club that referenced this issue May 25, 2020
- add layer to bar chart rendering D3 line
- generating line using portion of bars (to avoid issue caused by
  grouped data)
- rendering breaks if first user is missing rating values
- Reference: plouc/nivo#139
djpowers added a commit to djpowers/movie-club that referenced this issue May 25, 2020
- add layer to bar chart rendering D3 line
- generating line using portion of bars (to avoid issue caused by
  grouped data)
- rendering breaks if first user is missing rating values
- Reference: plouc/nivo#139
@VolodymyrKornetskyi
Copy link

@plouc is it possible to add tooltip to Line chart in such approach?

@andreas-ateles
Copy link

@plouc same question as above, is there a way to make the tooltips show up for both the bar and line chart?
In my case preferably in a single tooltip box, with all graphed Y values for the current X value.

@VolodymyrKornetskyi did you have any luck solving that?

@plouc
Copy link
Owner

plouc commented Dec 2, 2020

@andreas-ateles you could probably achieve this in your custom layer, you could for example use the useTooltip() hook from @nivo/tooltip to display some extra tooltips.

@sarang12
Copy link

sarang12 commented Dec 7, 2021

@plouc Is it possible to add a right axis corresponding to the line layer in this type of chart?https://codesandbox.io/s/zrk1o3j29p

@rosshoyt
Copy link

rosshoyt commented Dec 13, 2021

@yojeek, because the bar chart only supports ordinal values for now, other charts (line, scatterplot) support other types like linear/time/log scales.

@plouc Scatterplot with xScale={{ type: 'point', min: 0, max: 'auto' }} works with custom D3 Line layer, implemented following your description here. Heres how that looks:
working example
However, when changing the Scatterplot type to time, the xScale function fails in the same LineGenerator function used above.

The changed ResponsiveScatterPlot settings:

xScale={{ 
    type: 'time',  
    useUTC: false,
    format: '%m-%d',
    precision: 'day'
}}
xFormat="time:%m-%d"`
axisBottom={{
  format: '%b %d',
  tickValues: 'every 4 days',
  // etc
}}

Custom Line layer excerpt:

function Line({ data, xScale, yScale}) { 
        const lineGenerator = line().curve(curveNatural)
            .x(d => xScale(d.x))
            .y(d => yScale(d.y));
       // etc

Error produced:

<path> attribute d: Expected number, "MNaN,289CNaN,282.…"

The x values are, in both cases, simply M-D date formats, e.g. 4-23

I'm working with Scatterplot version 0.72, and can provide a CodePen if needed.

@alissaVrk
Copy link

Hi, I made an example of a combined bar with a line chart.
the bars and the line values have different scales - so there is an axis on the left for the bars and an axis on the right for the line.

I also added a tooltip - I made a combined tooltip for both values, but there are many different possibilities here...
Also if you already have some lib for tooltips in your project (like react-tooltip or floating-ui) you can use the tooltip lib for the tooltips.

@plouc, thank you for making the code easy enough to navigate :)
I do think that this would have been easier if useLine, for example, would have been exposed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests