Only set canvas height when window width is over 768px, otherwise let it default

时间:2016-02-12 20:19:44

标签: javascript jquery canvas html5-canvas angular-chart

I am using angular charts to create a bar chart on my page. I only want the height to be set to 80 so it does not take up as much of my page. The issue is that when the size of the page is decreased, on mobile for instance, the chart squishes itself so it is not legible. If I do not set a canvas height, then it is too big on a desktop, but looks great on a screen size smaller than 768px.

How can I set the canvas height to 80 when the screen size is above 768px and let it default on any lesser size. Or set the canvas height to 80 above 768px and to 150 on anything smaller.

I have been pulling my hair out trying to figure this out.

HTML:

in.data[, w := match(vendor, unique(vendor)), by=fruits]

# or in base R ...

in.data$w = with(in.data, ave(vendor, fruits, FUN = function(x) match(x, unique(x))))

I have tried a few different things, but the canvas size does not change at all.

Here is my last attempt:

<div class="chart-wrapper">
  <div class="chart-title">
    <b>Annual Sales</b>
  </div>
  <div class="chart-stage" ng-controller="BarCtrl">
    <div>
      <canvas id="bar" class="chart chart-bar"
                    chart-data="data" chart-labels="labels" chart-series="series">
      </canvas>
    </div>
  </div>
</div>

I should note that I am trying to keep the canvas width to cover the full width of the screen. Which is why I get the squishing effect on a smaller screen.

Here is the codepen I've been using to play around with it: Click here

This is the squishing effect on a smaller screen:

enter image description here

And here is what happens when I try to use media queries and set the height with CSS (I believe it is necessary to set with javascript):

enter image description here

2 个答案:

答案 0 :(得分:0)

You could use CSS:

stringsplit("[\\r\\n]+");

It's a little bit cleaner than using the js approach. And, I believe, faster :)

答案 1 :(得分:0)

A vanilla js option, ive set 2 height properties but you may only need to set 1

SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 101), '/', '') AS [MMDDYYYY]