-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Responsive Chart #56
Comments
Set the width of the canvas using CSS. For retina devices Chart.js will apply a width inline, so your CSS might need an important. |
@nnnick I tried to set width and height with !important. But it does not work. The graph becomes stretched. |
I can confirm the stretching effect. @nnnick: if you could point me how to redraw the diagram, I could implement the handler. |
Ok, I have a temporary solution now, and a better solution in the pipeline. For now, in your CSS apply the following:
The max width (800px) is the width attribute on your canvas. This will at least scale properly as an image should, and you won't get into issues with stretching. For the future, I'm looking at adding redraw events and a set of utility methods you'll be able to call once you've created your chart. For example:
Then, on your re-size events, you'll be able to reduce the size of your actual drawing canvas, and then call reDraw on your chart elements. |
@nnnick Thanks, it works now. |
@nnnick the temporary solution doesn't work with Bootstrap fluid layout. BTW thanks for this awesome library! 👍 |
@decabyte For me, It works with twitter bootstrap fluid layout. Here is the HTML and CSS HTML
CSS
|
@fizerkhan yes, but this hack scales and stretches the canvas, which is rendered by the library at 550x300, making the text of the chart difficult to read if you are scaling to smaller viewport or pixelated if you are scaling up. :) |
@decabyte Yeah It is little bit stretched. Could not noticed previously. |
With this, little bit better for my graph.
|
+1 Any updates on fix for retina? Ability to redraw, or to use % instead of pixels? |
Got this solution to work without scaling and stretching the canvas. HTML<!--Place the canvas in a div and assign a class that has a width property defined.-->
<div class="span3">
<canvas id="mycanvas"></canvas>
</div> CoffeesetupCanvas = (canvas) ->
canvas = $(canvas)
# Set the size of the canvas to the parent div's size
newWidth = canvas.parent().width()
canvas.prop
width: newWidth
height: 200
# Draw the chart
ctx = canvas.get(0).getContext("2d")
new Chart(ctx).Line mydata
((canvas) ->
setupCanvas canvas
# Listen for resize events (fires on mobile when you change orientation)
$(window).resize ->
setupCanvas canvas
) "#mycanvas" +1 for a redraw method |
Hi all, Which gives us the following code: var pixelRatio = window.devicePixelRatio || 1;
var width = 404 / pixelRatio;
var height = 404 / pixelRatio; I populate my canvas elements in javascript so this is easy for me using this way, but I guess you can fix it using media queries as well. (Have a look at these here.) |
@nezo's solution worked for me. :) |
I am still running into this issue. I'm trying to get a fixed height, flexible width line chart. I have attached my fiddle. http://jsfiddle.net/andrewjmead/QNnZ6/ |
I'm having the best luck with this:
Although, the text is super-sized on Retina devices, at least the canvas fits the layout... easy to fix, just divide the desired font-size by the devicePixelRatio... |
i tried my best luck in modification here : https://github.com/arifLogic/respChartJS
|
@psyose function works like charm... |
@psyose's solution also worked for me. Thanks, pal! |
canvas element doesn't seem to scale the height correctly in IE with height auto - any ideas ? (charts land up stretched) |
Yet another solution. Uses style sheet to stretch/shrink the chart and a delayed redraw event to clear things up after the user has finished resizing. The redraw keeps the chart in focus. The style sheet keeps the chart responsive and cuts down on the flicker while the user is actively resizing the window. first add to CSS
next add your redraw function and bind it to resize event
Note that my example uses a minimum width and fixed height. Should be easy to change that to suit your needs. |
oh my god, a great issue |
@arifLogic That worked a treat, thanks. |
I've used a combo of media queries, using a larger canvas size (e.g., 800x400) and editing a local copy of Chart.js to use a font size of 16 instead of 12. It's a hack, but it's the simplest that works (only tested in Chrome on Mac so far, but I've used media queries similar to that of Bootstrap). |
Actually, here's a nice solution if you're using Bootstrap: http://stackoverflow.com/questions/17740901/bootstrap-grid-not-working-with-canvas. However, the height of the re-rendered chart was wonky. For my purposes, I used width()*0.5, in the JavaScript code. |
Responsive chart example http://jsfiddle.net/LHMRJ/ |
@DineshGitHub Im trying your solution, but it doesn't work with a radar chrat |
Here is what we came up with for responsive canvas. On entry we set the canvas to the width of the parent element which is a 100% width container, then draw the chart. We then repeat that when we resize the window.
|
@corysimmons mentioned on Jul 22, 2014 -- "Actually, the responsive: true thing has no concern for fixed-height charts." I'm using "responsive: true", but I'd like to have a fixed height on the chart. Using "maintainAspectRatio: false" in combination with "max-height" in a style seems to work OK. Is that approach "hacky" ?? |
@jtblin does it work correctly for fixed height charts? |
@jtblin I tried your library, and it's still squished for a fixed height chart. Am I doing something wrong or is fixed height not supported? |
Fixed height is supported, someone reported an issue but without repro case, see jtblin/angular-chart.js#30 with a screenshot of a fixed height chart. Can you create a jsbin and open an issue in my repo and I will look into that? |
Ahh, I was setting the height with css. Setting height in the html seems to work. |
@jtblin is there a way to do a line chart without labels? I just want to plot points and not have to put in an empty string for each label. Also, I am setting colours to an array of colours and it just turns all black and white. What am I doing wrong? |
For labels, I don't think Chart.js allow that, see #12.
For colours see jtblin/angular-chart.js#42, unfortunately I need to do some work to allow setting colours via string. I will probably land that this weekend. |
Try "showScale: false" |
@robrez that does allow you to hide the scale, but you still have to have a label for each point. I can just put a label for each one and hide it though, no big deal. |
Nice lib. I had a similar issue, and finagled a fix this way, using Bootstrap. If I didn't do it this way, either the charts would progressively shrink, or they'd get blurry, or on hover the previous charts would appear. <div class="canvas-container" style="height:160px;" data-chartjs="userSessionData"> var repaint_timeout;
function responsiveCharts() {
if( repaint_timeout )
clearTimeout( repaint_timeout );
repaint_timeout = setTimeout( function()
{
$("div[data-chartjs]").each(function (s, t)
{
var $div = $(t);
$div.empty();
var $canvas = $("<canvas>");
$canvas.attr('height', $div.height() );
$canvas.attr('width', $div.width() );
$div.append( $canvas );
var ctx = $canvas.get(0).getContext("2d");
new Chart(ctx).Line(chart_scope[$div.attr('data-chartjs')], {
reponsive: true,
maintainAspectRatio: false
});
});
}, 250 );
} Then in the body of the doc that drives my charts: var chart_scope = {};
chart_scope.userSessionData = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
},
{
label: "My Second dataset",
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: [28, 48, 40, 19, 86, 27, 90]
}
]
};
$(document).ready( function(){
$(window).resize(responsiveCharts);
responsiveCharts();
}); |
I just wanna share my experience with the responsiveness behaviour for this lib. I had to put 90 days (as short dates) in the (h) xAxis so because the 90 labels where all trying to fit into a one single axis it was impossible to be read and to know which data this axis points to. The only workaround for that was a css fix: Contain the canvas in two div parents; note: no width and height specific attributes for canvas tag!
result: I hope it helps. |
is there any way to adjust bars height? |
My line chart kept increasing in height on window resize. Solved by setting setting static height on the canvas parent element: e.g. 'style="height: 400px !important;' |
I was trying to create a fixed height responsive chart and was having various issues. Namely:
In the end, it was because I needed to both explicitly set the height of the canvas element, as well as setting the parent element to 100% width. .parent {
width: 100%;
canvas {
height:340px !important;
}
} I also use the |
Any actual fix to this? All the methods I have used fix the resizing but the chart is still affected, all the points and lines half in width same with the fonts and the hover states all change. Anyone managed to make it work as it should? Thanks for the awesome library though love the charts too much to change to another library but this is killing me, thanks! |
I could only make it work adapting @nezo´s solution: var pixelRatio = window.devicePixelRatio || 1;
var width = $('canvas').parent().width();
var height = $('canvas').parent().height();
ctx.canvas.width = width / pixelRatio;
ctx.canvas.height = (1.5 * height) / pixelRatio;
var myNewChart = new Chart(ctx).Line(data); The vars width and height inherit its size from a parent div. After that I set it as the graph´s width and height. I multiplied height by 1.5 to make it look better in my layout, you may have to change that value or remove it. |
I get good results with embedding the canvas into to div elements and changing the resize function little bit. HTML <div style="width:100%; height:400px">
<div>
<canvas id="myChart"></canvas>
</div>
</div> Changes in charts.js at extend(Chart.Type.prototype,{ newHeight = this.options.maintainAspectRatio ? newWidth / this.chart.aspectRatio : getMaximumHeight(this.chart.canvas); to newHeight = this.options.maintainAspectRatio ? newWidth / this.chart.aspectRatio : canvas.height; Does anybody know how I could override the resize() function within my javascript code? Currently I edited it directly in the chart.js . |
I can confirm that @immertroll's solution nicely fixed the issue for me. I did not, however, have to make any edits to my HTML. I only needed to edit the line Chart.js. I'm using Bootstrap 3, so my markup looks something like this: <div class="row">
<div class="col-md-3">
<canvas id="myChart"></canvas>
</div>
<div class="col-md-3">
<p>blah blah blah</p>
</div>
<div class="col-md-3">
<p>blah blah blah</p>
</div>
<div class="col-md-3">
<p>blah blah blah</p>
</div>
</div> My overrides to the Chart.js defaults look like this: Chart.defaults.global.responsive = true;
Chart.defaults.global.animation = false;
Chart.defaults.global.maintainAspectRatio = false; |
A mix of @dougfelton and @immertroll solution: <div class="row">
<div class="col-md-9">
<div>
<canvas id="my-cavas"></canvas>
</div>
</div>
</div> With: Chart.defaults.global.responsive = true;
Chart.defaults.global.maintainAspectRatio = false; And: #my-canvas {
height: 300px;
} |
Just put the canvas in a container. The chart will automatically adjust the size of the parent container. This worked for me: CSS responsive:true |
The problem for me is that my canvas container is |
^ I think I've got it working. Firstly, I had the charts in a table and they were making the table bigger when resized, fixed that by adding Then added these options when creating
Used @akz92 solution:
Used @immertroll solution:
to
And as the cake icing added So now all the charts are responsive with fixed height. |
setTimeout solved the problem.
|
chart js column width fixed issue please solved
|
triggering resize event after rendering chart works for me
|
May I know how to decrease a thickness of DC chart? |
Now, i have set width and height to the canvas element. But height and width of chart should be changed based on the Device.
I tried with bootstrap span element, it does not work. Any clues?
The text was updated successfully, but these errors were encountered: