最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - chart.js responsive bar chart label sizing - Stack Overflow

programmeradmin0浏览0评论

So I have a bar chart using chart.js and I have the responsive enabled. It seems to work well with some charts and not so well with others. For example my bar chart labels seem to not re-size with the window, but everything else does making the entire chart look awfully strange at smaller window sizes. How can I re-size the labels so that it scales all together? Any ideas? Thanks!

My fiddle:/

Html:

<canvas id="canvas" width="940" height="540"></canvas>

JS:

var barChartData = {
    labels: ["Adjust claims fairly", "Pays promptly", "Resolves issues quickly", "Communicates clearly, honestly to agents", "Underwriter knowledge/expirience", "Listens, responds to agents", "Consistant underwriting", "Easy, intuitive functionality-technology", "Stable underwriting market", "Flexible underwriting when warrented"],
    datasets: [
        {
        label: "My First dataset",
        //new option, barline will default to bar as that what is used to create the scale
        type: "line",
        fillColor: "rgba(225,225,225,0.01)",
        strokeColor: "rgba(0,0,0,1)",
        pointColor: "rgba(0,0,0,1)",
        pointStrokeColor: "#000000",
        pointHighlightFill: "#fff",
        pointHighlightStroke: "rgba(0,0,0,1)",
        data: [9.5, 9.4, 9.3, 9.2, 9.2, 9.1, 9, 9, 9, 9]
    }, {
        label: "My First dataset",
        //new option, barline will default to bar as that what is used to create the scale
        type: "bar",
        fillColor: "rgba(225,50,50,0.8)",
        strokeColor: "rgba(225,50,50,1)",
        pointColor: "rgba(220,20,220,1)",
        pointStrokeColor: "#fff",
        pointHighlightFill: "#fff",
        pointHighlightStroke: "rgba(220,220,220,1)",
        data: [7.4, 7.6, 6.2, 6.3, 6.8, 5.8, 7.1, 6.1, 7.6, 5.2]
    }]
};
Chart.defaults.global.responsive = true;
//canvases
var lineBar = document.getElementById("canvas").getContext("2d");

//charts
var myLineBarChart = new Chart(lineBar).Overlay(barChartData);

So I have a bar chart using chart.js and I have the responsive enabled. It seems to work well with some charts and not so well with others. For example my bar chart labels seem to not re-size with the window, but everything else does making the entire chart look awfully strange at smaller window sizes. How can I re-size the labels so that it scales all together? Any ideas? Thanks!

My fiddle:http://fiddle.jshell/SteveSerrano/e3o9z2wg/

Html:

<canvas id="canvas" width="940" height="540"></canvas>

JS:

var barChartData = {
    labels: ["Adjust claims fairly", "Pays promptly", "Resolves issues quickly", "Communicates clearly, honestly to agents", "Underwriter knowledge/expirience", "Listens, responds to agents", "Consistant underwriting", "Easy, intuitive functionality-technology", "Stable underwriting market", "Flexible underwriting when warrented"],
    datasets: [
        {
        label: "My First dataset",
        //new option, barline will default to bar as that what is used to create the scale
        type: "line",
        fillColor: "rgba(225,225,225,0.01)",
        strokeColor: "rgba(0,0,0,1)",
        pointColor: "rgba(0,0,0,1)",
        pointStrokeColor: "#000000",
        pointHighlightFill: "#fff",
        pointHighlightStroke: "rgba(0,0,0,1)",
        data: [9.5, 9.4, 9.3, 9.2, 9.2, 9.1, 9, 9, 9, 9]
    }, {
        label: "My First dataset",
        //new option, barline will default to bar as that what is used to create the scale
        type: "bar",
        fillColor: "rgba(225,50,50,0.8)",
        strokeColor: "rgba(225,50,50,1)",
        pointColor: "rgba(220,20,220,1)",
        pointStrokeColor: "#fff",
        pointHighlightFill: "#fff",
        pointHighlightStroke: "rgba(220,220,220,1)",
        data: [7.4, 7.6, 6.2, 6.3, 6.8, 5.8, 7.1, 6.1, 7.6, 5.2]
    }]
};
Chart.defaults.global.responsive = true;
//canvases
var lineBar = document.getElementById("canvas").getContext("2d");

//charts
var myLineBarChart = new Chart(lineBar).Overlay(barChartData);
Share Improve this question asked Oct 14, 2015 at 18:34 Steven SerranoSteven Serrano 3033 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

The problem has more to do with the really long x axis labels than the responsiveness. That said, setting maintainAspectRatio will solve your problem.

Chart.defaults.global.maintainAspectRatio = false;

Note that you can do this per chart too.


If you want to maintain the aspect ratio too, you can extend the chart to trim the labels like in https://stackoverflow./a/31699438/360067

发布评论

评论列表(0)

  1. 暂无评论