Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Same width scales in two independent charts (Fixed scale step width)

Open mdopieralski opened this issue 10 years ago • 6 comments

Hi guys,

I'd like to make two different type charts, with same y-axis scale width.

What I've got already. zrzut ekranu 2016-06-03 o 15 54 41

What I'm trying to achieve:

zrzut ekranu 2016-06-03 o 15 58 41

Is there a way to have same width scales on two different charts ?

mdopieralski avatar Jun 03 '16 14:06 mdopieralski

@mdopieralski which version are you using?

etimberg avatar Jun 04 '16 14:06 etimberg

Hi @etimberg, I use v 2.1.4.

mdopieralski avatar Jun 06 '16 06:06 mdopieralski

Is it possible to post a fiddle of what you have so far?

etimberg avatar Jun 06 '16 11:06 etimberg

I'm also trying to do this (in v2). I've tried setting the paddingLeft etc. properties on the scales and fudging setting the minSize structure.. but they don't seem to have any effect.

I have 2 y-axis scales on 2 charts one above the other but with different labels and I'd just like to be able to align the actual data section of the charts to each other somehow.

Charts seems to always do a best-fit and I appreciate that for it to work with 2 charts they'd need to agree on how wide the scale should be - so I'm just looking to either fix the width of the scale, or fix the draw-area for the chart itself.

I found a fiddle here which creates a new chart type and overrides the draw function (v1 charts) but wondering if this is still the right way to achieve this? http://jsfiddle.net/2kmf10hg/

Thanks a lot

olliholliday avatar Jun 06 '16 15:06 olliholliday

Got it working now. The "afterFit" callback passes a scale which has a calculated "width" property on it. You can override it at that point to force it to a certain width:

options: {
    scales: {
        xAxes: [{
        }],
        yAxes: [
            {
                id: 'yAxis1',
                position: 'left',
                afterFit: function (scale) {
                    scale.width = scaleWidth;
                },
            }
        ]
    }
};

olliholliday avatar Jun 06 '16 17:06 olliholliday

Closing since there was a good answer from @olliholliday

etimberg avatar Jun 28 '16 02:06 etimberg